|
| 1 | +import { mkdirSync } from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | +import { chromium, expect } from '@playwright/test' |
| 4 | +import { |
| 5 | + readPersonaCredentials, |
| 6 | + readScenarioManifest, |
| 7 | + scenarioManifestSchema, |
| 8 | + writeJsonAtomic, |
| 9 | +} from '../fixtures/e2e-world' |
| 10 | + |
| 11 | +const baseUrl = requiredEnv('E2E_BASE_URL') |
| 12 | +const manifestPath = requiredEnv('E2E_MANIFEST_PATH') |
| 13 | +const credentialsPath = requiredEnv('E2E_CREDENTIALS_PATH') |
| 14 | +const storageStateDirectory = requiredEnv('E2E_STORAGE_STATE_DIR') |
| 15 | +const screenshotsDirectory = requiredEnv('E2E_AUTH_SCREENSHOT_DIR') |
| 16 | +const UI_RETRY_DELAYS_MS = [1_000, 2_000, 4_000, 8_000, 16_000, 30_000] as const |
| 17 | + |
| 18 | +async function main(): Promise<void> { |
| 19 | + const manifest = readScenarioManifest(manifestPath) |
| 20 | + const credentials = readPersonaCredentials(credentialsPath) |
| 21 | + if (manifest.runId !== credentials.runId || manifest.runId !== requiredEnv('E2E_RUN_ID')) { |
| 22 | + throw new Error('Persona auth inputs belong to different E2E runs') |
| 23 | + } |
| 24 | + mkdirSync(storageStateDirectory, { recursive: true }) |
| 25 | + mkdirSync(screenshotsDirectory, { recursive: true }) |
| 26 | + |
| 27 | + const browser = await chromium.launch({ |
| 28 | + args: ['--host-resolver-rules=MAP e2e.sim.ai 127.0.0.1'], |
| 29 | + }) |
| 30 | + try { |
| 31 | + for (const [personaKey, persona] of Object.entries(manifest.personas)) { |
| 32 | + const login = credentials.personas[personaKey] |
| 33 | + if (!login) throw new Error(`Missing private login for persona: ${personaKey}`) |
| 34 | + const context = await browser.newContext({ baseURL: baseUrl }) |
| 35 | + try { |
| 36 | + const page = await context.newPage() |
| 37 | + await page.goto('/login') |
| 38 | + await expect(page.getByRole('heading', { name: 'Sign in' })).toBeVisible() |
| 39 | + await page.getByLabel('Email').fill(login.email) |
| 40 | + await page.getByRole('textbox', { name: 'Password' }).fill(login.password) |
| 41 | + await signInThroughUi(page, personaKey) |
| 42 | + |
| 43 | + const sessionResponse = await context.request.get('/api/auth/get-session') |
| 44 | + if (!sessionResponse.ok()) { |
| 45 | + throw new Error(`Session probe failed for ${personaKey}: ${sessionResponse.status()}`) |
| 46 | + } |
| 47 | + const session = (await sessionResponse.json()) as { |
| 48 | + user?: { id?: string; email?: string } |
| 49 | + session?: { activeOrganizationId?: string | null } |
| 50 | + } |
| 51 | + if (session.user?.id !== persona.userId || session.user.email !== persona.email) { |
| 52 | + throw new Error(`Session identity mismatch for persona: ${personaKey}`) |
| 53 | + } |
| 54 | + if ( |
| 55 | + (session.session?.activeOrganizationId ?? null) !== persona.expectedActiveOrganizationId |
| 56 | + ) { |
| 57 | + throw new Error(`Active organization mismatch for persona: ${personaKey}`) |
| 58 | + } |
| 59 | + |
| 60 | + await page.goto(persona.canonicalRoute) |
| 61 | + await expect(page).toHaveURL(/\/settings\/general$/) |
| 62 | + await expect(page.getByRole('heading', { name: 'General', level: 1 })).toBeVisible() |
| 63 | + await page.screenshot({ |
| 64 | + path: path.join(screenshotsDirectory, `${personaKey}.png`), |
| 65 | + fullPage: false, |
| 66 | + }) |
| 67 | + const storageStatePath = path.join(storageStateDirectory, persona.storageStatePath) |
| 68 | + await context.storageState({ path: storageStatePath }) |
| 69 | + } finally { |
| 70 | + await context.close() |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + manifest.authCaptureComplete = true |
| 75 | + writeJsonAtomic(manifestPath, scenarioManifestSchema.parse(manifest)) |
| 76 | + } finally { |
| 77 | + await browser.close() |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function requiredEnv(key: string): string { |
| 82 | + const value = process.env[key] |
| 83 | + if (!value) throw new Error(`Missing auth capture environment value: ${key}`) |
| 84 | + return value |
| 85 | +} |
| 86 | + |
| 87 | +async function signInThroughUi( |
| 88 | + page: import('@playwright/test').Page, |
| 89 | + personaKey: string |
| 90 | +): Promise<void> { |
| 91 | + for (let attempt = 0; attempt <= UI_RETRY_DELAYS_MS.length; attempt += 1) { |
| 92 | + const responsePromise = page.waitForResponse((response) => { |
| 93 | + const url = new URL(response.url()) |
| 94 | + return url.pathname === '/api/auth/sign-in/email' |
| 95 | + }) |
| 96 | + await page.getByRole('button', { name: 'Sign in' }).click() |
| 97 | + const response = await responsePromise |
| 98 | + if (response.status() === 200) { |
| 99 | + try { |
| 100 | + await page.waitForURL(/\/workspace(?:\/|$)/, { timeout: 30_000 }) |
| 101 | + } catch { |
| 102 | + throw new Error(`UI sign-in did not redirect for persona: ${personaKey}`) |
| 103 | + } |
| 104 | + return |
| 105 | + } |
| 106 | + if (response.status() !== 429 || attempt === UI_RETRY_DELAYS_MS.length) { |
| 107 | + throw new Error( |
| 108 | + `UI sign-in failed for persona ${personaKey} with status ${response.status()}; response body redacted` |
| 109 | + ) |
| 110 | + } |
| 111 | + await new Promise((resolve) => setTimeout(resolve, UI_RETRY_DELAYS_MS[attempt])) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +main().catch((error) => { |
| 116 | + const raw = error instanceof Error ? (error.stack ?? error.message) : String(error) |
| 117 | + let redacted = raw |
| 118 | + try { |
| 119 | + const credentials = readPersonaCredentials(credentialsPath) |
| 120 | + for (const { password } of Object.values(credentials.personas)) { |
| 121 | + redacted = redacted.replaceAll(password, '[REDACTED]') |
| 122 | + } |
| 123 | + } catch { |
| 124 | + redacted = 'Auth capture failed; credentials unavailable for safe diagnostic formatting' |
| 125 | + } |
| 126 | + console.error(redacted) |
| 127 | + process.exitCode = 1 |
| 128 | +}) |
0 commit comments