import * as OTPAuth from "otpauth"; import { expect, test, type Page } from "./utils/test"; import { expectNoCriticalA11yViolations } from "./utils/axe"; import { loginAsAdminUser } from "./utils/auth"; import { waitForHydration } from "./utils/navigation"; import { skipOrFailWhenE2eInputsMissing } from "./utils/readiness"; const credentials = process.env.E2E_ADMIN_EMAIL && process.env.E2E_ADMIN_PASSWORD ? { email: process.env.E2E_ADMIN_EMAIL, password: process.env.E2E_ADMIN_PASSWORD } : null; const platformLeadsPath = process.env.E2E_ADMIN_PLATFORM_LEADS_PATH ?? "/leads"; const platformLeadSourceName = process.env.E2E_ADMIN_PLATFORM_LEAD_SOURCE_NAME ?? "E2E Readiness Platform Leads"; async function establishTemporaryAdminMfaStepUp(page: Page): Promise<() => Promise> { if (new URL(page.url()).pathname !== "/settings/profile") return async () => undefined; const password = page.locator('[data-ui-id="ui.settings.profile.mfa.passwordCurrent"]'); const toggle = page.locator('[data-ui-id="ui.settings.profile.mfa.toggle.submit"]'); await expect(password).toBeVisible(); await password.fill(credentials!.password); await toggle.click(); const setupUri = page.getByLabel(/Authenticator setup URI|Authenticator-Einrichtungs-URI/iu); await expect(setupUri).toBeVisible(); const uri = (await setupUri.textContent())?.trim(); expect(uri, "Temporary E2E admin MFA enrollment returned no setup URI").toBeTruthy(); const code = OTPAuth.URI.parse(uri!).generate(); await page.locator('[data-ui-id="ui.settings.profile.mfa.confirm.code"]').fill(code); await page.locator('[data-ui-id="ui.settings.profile.mfa.confirm.submit"]').click(); await expect(page.getByText(/^(Enabled|Aktiviert)$/iu).first()).toBeVisible(); return async () => { await page.goto("/settings/profile", { waitUntil: "domcontentloaded" }); await expect(password).toBeVisible(); await password.fill(credentials!.password); await toggle.click(); await expect(page.getByText(/^(Not enabled|Nicht aktiviert)$/iu).first()).toBeVisible(); }; } test.describe("R-LEADS platform lead inbox", () => { test("creates a lead through the native inbox and shows the persisted result", async ({ page }, testInfo) => { skipOrFailWhenE2eInputsMissing(["E2E_ADMIN_EMAIL", "E2E_ADMIN_PASSWORD"], "Skipping authenticated platform-lead smoke"); await loginAsAdminUser(page, credentials!); const cleanupMfa = await establishTemporaryAdminMfaStepUp(page); try { const newLeadPath = `${platformLeadsPath.replace(/\/$/u, "")}/new`; const response = await page.goto(newLeadPath, { waitUntil: "domcontentloaded" }); expect(response, "Platform lead intake returned no document response").not.toBeNull(); expect(response!.ok(), `Platform lead intake returned ${response!.status()}`).toBe(true); expect(new URL(page.url()).pathname).toBe(newLeadPath); await waitForHydration(page); const createScreen = page.locator('[data-ui-id="ui.admin.platformLeads.new"]'); await expect(createScreen).toBeVisible(); await expect(page.getByRole("heading", { name: /^(Lead manuell anlegen|Create lead manually)$/iu })).toBeVisible(); const sourceSelect = page.getByRole("combobox", { name: /^(Quelle|Source)$/iu }).first(); await expect(sourceSelect).toBeEnabled(); await sourceSelect.click(); const sourceOption = page.getByRole("option", { name: platformLeadSourceName, exact: true }); await expect(sourceOption, `Seeded platform lead source ${platformLeadSourceName} must be available`).toBeVisible(); await sourceOption.click(); const suffix = `${Date.now()}-${testInfo.workerIndex}`; const firstName = "E2E"; const lastName = `Smoke ${suffix}`; const displayName = `${firstName} ${lastName}`; const company = `HiveGrid E2E ${suffix}`; const message = `Persisted platform lead smoke ${suffix}`; await createScreen.locator('input[name="firstName"]').fill(firstName); await createScreen.locator('input[name="lastName"]').fill(lastName); await createScreen.locator('input[name="email"]').fill(`platform-lead-${suffix}@example.test`); await createScreen.locator('input[name="company"]').fill(company); await createScreen.locator('textarea[name="message"]').fill(message); await createScreen.getByRole("button", { name: /^(Lead erstellen|Create lead)$/iu }).click(); await page.waitForURL((url) => /^\/leads\/[^/]+$/u.test(url.pathname) && url.searchParams.get("feedback") === "created"); const detailScreen = page.locator('[data-ui-id="ui.admin.platformLeads.detail"]'); await expect(detailScreen).toBeVisible(); await expect(page.getByRole("heading", { name: displayName, exact: true })).toBeVisible(); await expect(detailScreen.getByText(/^(Lead wurde angelegt\.|Lead created\.)$/iu)).toBeVisible(); await expect(detailScreen.getByText(company, { exact: true })).toBeVisible(); await expect(detailScreen.getByText(message, { exact: true })).toBeVisible(); await expect(detailScreen.getByText(platformLeadSourceName, { exact: true })).toBeVisible(); const note = `Verified in platform lead smoke ${suffix}`; await detailScreen.locator('textarea[name="note"]').fill(note); await detailScreen.getByRole("button", { name: /^(Notiz hinzufügen|Add note)$/iu }).click(); await page.waitForURL((url) => /^\/leads\/[^/]+$/u.test(url.pathname) && url.searchParams.get("feedback") === "updated"); await expect(detailScreen.getByText(/^(Lead wurde aktualisiert\.|Lead updated\.)$/iu)).toBeVisible(); await expect(detailScreen.getByText(note, { exact: true })).toBeVisible(); await expectNoCriticalA11yViolations(page, { include: '[data-ui-id="ui.admin.platformLeads.detail"]' }); } finally { // Preserve the primary assertion error if the page is already being // closed by Playwright's test timeout. A fresh readiness seed also // clears disposable MFA fixture state before the next live run. await cleanupMfa().catch(() => undefined); } }); });