// tests/e2e/system-verification.js // Automated E2E verification script for C# .NET API Backend & BackOffice const API_BASE = 'http://localhost:5000'; async function runE2EVerification() { console.log('=================================================='); console.log(' D3RO Voice — Closed Loop E2E Automated Test'); console.log('=================================================='); try { // 1. Health & Server Stats console.log('\n[1/5] Testing Server Stats & Health...'); const statsRes = await fetch(`${API_BASE}/api/admin/stats`); if (!statsRes.ok) throw new Error(`Stats endpoint failed with status ${statsRes.status}`); const stats = await statsRes.json(); console.log(' ✓ Server Stats:', JSON.stringify(stats)); // 2. User Registration & Login console.log('\n[2/5] Testing User Registration & Mandatory Login Guard...'); const testEmail = `e2e_user_${Date.now()}@d3ro.ai`; const testPassword = 'Password123!'; const regRes = await fetch(`${API_BASE}/api/auth/register`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: testEmail, password: testPassword }) }); if (!regRes.ok) throw new Error(`Registration failed: ${await regRes.text()}`); const regData = await regRes.json(); console.log(` ✓ Registration successful for: ${regData.email} (Role: ${regData.role})`); const loginRes = await fetch(`${API_BASE}/api/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: testEmail, password: testPassword }) }); if (!loginRes.ok) throw new Error(`Login failed: ${await loginRes.text()}`); const loginData = await loginRes.json(); const token = loginData.token; console.log(' ✓ Login successful, JWT token issued.'); // Verify unauthenticated request fails const unauthRes = await fetch(`${API_BASE}/api/llm/generate`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ prompt: 'Test' }) }); if (unauthRes.status !== 401) throw new Error(`Expected 401 Unauthenticated, got ${unauthRes.status}`); console.log(' ✓ Mandatory Login Guard verified (Unauthenticated request rejected with 401).'); // 3. Dynamic Model Endpoint Creation (Admin) console.log('\n[3/5] Testing Dynamic Service Model & Endpoint Addition...'); const newModelId = `e2e-model-${Date.now()}`; const createEpRes = await fetch(`${API_BASE}/api/admin/endpoints`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ modelId: newModelId, modelName: 'E2E Dynamic Custom Model', provider: 'OpenAI', endpointUrl: 'https://api.openai.com/v1/chat/completions', apiKey: 'sk-e2e-dummy-key', costPer1kPromptTokens: 0.00020, costPer1kCompletionTokens: 0.00080 }) }); if (!createEpRes.ok) throw new Error(`Endpoint creation failed: ${await createEpRes.text()}`); const epData = await createEpRes.json(); console.log(` ✓ Dynamic Model Endpoint added: ${epData.modelId}`); // 4. AI Generation & Usage Cost Calculation console.log('\n[4/5] Testing AI Generation & Token Cost Calculation...'); const genRes = await fetch(`${API_BASE}/api/llm/generate`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, body: JSON.stringify({ prompt: 'Self-verification prompt for D3RO Voice E2E test', model: newModelId }) }); if (!genRes.ok) throw new Error(`AI generation failed: ${await genRes.text()}`); const genData = await genRes.json(); console.log(' ✓ AI Generation result:', JSON.stringify(genData)); if (typeof genData.cost !== 'number') throw new Error('Cost calculation missing in response'); // 5. BackOffice Usage & Cost Analytics Report console.log('\n[5/5] Testing BackOffice Usage & Cost Analytics Report...'); const usageRes = await fetch(`${API_BASE}/api/admin/usage`); if (!usageRes.ok) throw new Error(`Usage report failed: ${await usageRes.text()}`); const usageData = await usageRes.json(); console.log(' ✓ Usage Report received:', JSON.stringify(usageData)); console.log('\n=================================================='); console.log(' SUCCESS: ALL E2E VERIFICATION CHECKS PASSED!'); console.log('=================================================='); } catch (err) { console.error('\n❌ E2E VERIFICATION FAILED:', err); process.exit(1); } } runE2EVerification();