import { expect, type APIRequestContext, type Page } from '@playwright/test'; /** Matches server/internal/api/integration_test.go testAuthUser / testAuthPass. */ export const E2E_USER = process.env.AETHERFORGE_E2E_USER || 'testuser'; export const E2E_PASS = process.env.AETHERFORGE_E2E_PASS || 'testpass'; /** Seed this into the server data dir as users.json before first start (see tests/README.md). */ export const E2E_USERS_JSON = JSON.stringify({ [E2E_USER]: E2E_PASS }); export function e2eAuthHeaders(): Record { const token = Buffer.from(`${E2E_USER}:${E2E_PASS}`).toString('base64'); return { Authorization: `Basic ${token}`, 'X-AetherForge-Client': 'dashboard', }; } /** * Reads fleet_secret for stub agent auth. * Prefer AETHERFORGE_FLEET_SECRET (test-suite.ps1 seeds from data/config.json). */ export async function fetchFleetSecret(request: APIRequestContext): Promise { const fromEnv = process.env.AETHERFORGE_FLEET_SECRET?.trim(); if (fromEnv) return fromEnv; const res = await request.get('/api/v1/config', { headers: e2eAuthHeaders(), timeout: 10_000, }); if (!res.ok()) { throw new Error(`config fetch failed: ${res.status()}`); } const body = (await res.json()) as { server?: { fleet_secret?: string } }; const secret = body.server?.fleet_secret?.trim() ?? ''; if (!secret) { throw new Error( 'fleet_secret missing — set AETHERFORGE_FLEET_SECRET from data/config.json in the E2E runner', ); } return secret; } /** Poll /api/v1/health until status ok or timeout (mirrors test-suite.ps1 phase 8). */ export async function waitForServerHealth( request: APIRequestContext, opts?: { timeoutMs?: number; intervalMs?: number }, ): Promise { const timeoutMs = opts?.timeoutMs ?? 30_000; const intervalMs = opts?.intervalMs ?? 1_000; const deadline = Date.now() + timeoutMs; while (Date.now() < deadline) { try { const res = await request.get('/api/v1/health', { timeout: 2_000 }); if (res.ok()) { const body = (await res.json()) as { status?: string }; if (body.status === 'ok') return true; } } catch { /* retry */ } await new Promise((r) => setTimeout(r, intervalMs)); } return false; } export async function loginToDashboard(page: Page): Promise { await page.goto('/'); await expect(page.getByRole('heading', { name: 'AetherForge' })).toBeVisible({ timeout: 15_000 }); await page.getByLabel('Username').fill(E2E_USER); await page.getByLabel('Password').fill(E2E_PASS); await page.getByRole('button', { name: /enter command deck/i }).click(); await expect(page.getByRole('heading', { name: 'Command Deck' })).toBeVisible({ timeout: 20_000 }); }