import { chromium } from "playwright"; import { mkdir, readFile, writeFile } from "node:fs/promises"; import { existsSync } from "node:fs"; import path from "node:path"; const baseURL = process.env.BASE_URL || "http://localhost:3000"; const root = process.cwd(); const outDir = path.join(root, "test-results", "registration-e2e"); await mkdir(outDir, { recursive: true }); async function record(result, exitCode = 0) { await writeFile(path.join(outDir, "results.json"), JSON.stringify({ generatedAt: new Date().toISOString(), baseURL, ...result, }, null, 2)); if (exitCode) process.exitCode = exitCode; console.log(JSON.stringify(result, null, 2)); } async function health() { try { const response = await fetch(`${baseURL}/api/health`); return { ok: response.ok, status: response.status, body: await response.json() }; } catch (error) { return { ok: false, status: 0, body: { error: error instanceof Error ? error.message : "unknown" } }; } } const healthCheck = await health(); if (!healthCheck.ok) { await record({ blocked: true, reason: "Database-backed E2E requires a running LinkedIn app with initialized SQLite/D1 storage.", health: healthCheck, }, 2); } else { const stamp = Date.now(); const email = process.env.TEST_REGISTRATION_EMAIL || `linkedin.e2e.${stamp}@example.test`; const username = `e2e-${stamp}`; const password = process.env.TEST_REGISTRATION_PASSWORD || "LinkedInE2E123"; const outbox = path.join(root, ".linkedin-mail", "outbox.jsonl"); const steps = []; const browser = await chromium.launch({ headless: true }); try { const context = await browser.newContext({ viewport: { width: 1366, height: 900 }, colorScheme: "light" }); const page = await context.newPage(); await page.goto(`${baseURL}/register`, { waitUntil: "networkidle" }); await page.getByLabel("Full name").fill("LinkedIn E2E Tester"); await page.getByLabel("Username").fill(username); await page.getByLabel("Email").fill(email); await page.getByLabel("Password").fill(password); await page.getByRole("button", { name: "Agree & join" }).click(); await page.getByRole("heading", { name: "Verify your email" }).waitFor({ state: "visible", timeout: 10000 }); steps.push({ step: "register", email, username, result: "verification_required" }); if (!existsSync(outbox)) { throw new Error("No local development outbox was created. Use EMAIL_DELIVERY_MODE=console for automated verification, or verify manually through QQ Mail."); } const lines = (await readFile(outbox, "utf8")).trim().split("\n").filter(Boolean); const message = lines.map((line) => JSON.parse(line)).reverse().find((item) => item.to === email); if (!message?.previewUrl) { throw new Error("Verification email did not expose a development preview URL."); } await page.goto(message.previewUrl, { waitUntil: "networkidle" }); await page.getByRole("heading", { name: "Email verified" }).waitFor({ state: "visible", timeout: 10000 }); await page.getByRole("link", { name: /Continue to LinkedIn/i }).click(); await page.waitForURL(/\/feed/); steps.push({ step: "verify-email", result: "session_established" }); await page.getByRole("button", { name: /Start a post/i }).click(); await page.getByPlaceholder(/Share an idea/i).fill(`Registration E2E post ${stamp}`); await page.getByRole("button", { name: "Post", exact: true }).click(); await page.getByText(`Registration E2E post ${stamp}`).waitFor({ state: "visible", timeout: 10000 }); steps.push({ step: "feed-post", result: "posted" }); await page.goto(`${baseURL}/jobs`, { waitUntil: "networkidle" }); const appliedButton = page.getByRole("button", { name: /Applied/i }); if (!(await appliedButton.isVisible().catch(() => false))) { await page.getByRole("button", { name: "Easy Apply" }).click(); await page.getByRole("dialog", { name: /Apply to/i }).waitFor({ state: "visible", timeout: 10000 }); await page.getByRole("button", { name: "Submit application" }).click(); } await appliedButton.waitFor({ state: "visible", timeout: 10000 }); steps.push({ step: "job-application", result: "submitted_or_already_submitted" }); await page.goto(`${baseURL}/premium`, { waitUntil: "networkidle" }); await page.getByText(/Stripe Test Mode/i).waitFor({ state: "visible", timeout: 10000 }); steps.push({ step: "premium-guardrail", result: "stripe_test_mode_notice_visible" }); await page.screenshot({ path: path.join(outDir, "registration-e2e-final.png"), fullPage: false, animations: "disabled" }); await context.close(); await record({ blocked: false, steps }); } catch (error) { await record({ blocked: true, steps, reason: error instanceof Error ? error.message : "unknown" }, 2); } finally { await browser.close(); } }