import { test, expect, type Page } from "@playwright/test"; import { readFileSync } from "fs"; type Fix = { fixtures: Record }; const data = JSON.parse(readFileSync("test/solver-fixtures.json", "utf8")) as Fix; const s1win = data.fixtures["s1-heat"].win; const s1loss = data.fixtures["s1-heat"].loss; const s1collapse = [ { ring: "inner", dir: "cw" }, { ring: "middle", dir: "ccw" }, { ring: "outer", dir: "cw" }, { ring: "outer", dir: "cw" }, { ring: "inner", dir: "cw" }, ]; const BASE = "http://localhost:4173"; async function playRoute(page: Page, route: { ring: string; dir: string }[]): Promise { for (const action of route) { await page.getByRole("button", { name: new RegExp(`Rotate ${action.ring} ${action.dir === "cw" ? "clockwise" : "counter-clockwise"}`) }).click(); await page.locator("#btnCommit").click(); } } test("loads, renders title, no console errors", async ({ page }) => { const errors: string[] = []; page.on("pageerror", (e) => errors.push(String(e))); page.on("console", (m) => { if (m.type() === "error") errors.push(m.text()); }); await page.goto(BASE); await expect(page.locator("h1")).toContainText("Adaptive Loop"); await expect(page.getByRole("button", { name: "New Campaign" })).toBeVisible(); expect(errors).toEqual([]); }); test("Continue without a save enters the next campaign scenario", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "Continue" }).click(); await expect(page.locator(".obj h2")).toContainText("Cultivation Quota"); await expect(page.locator(".turn")).toContainText("Turn 0"); }); test("start campaign, board renders, controls present", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await expect(page.locator(".board-svg")).toBeVisible(); await expect(page.getByRole("button", { name: /Rotate inner clockwise/ })).toBeVisible(); await expect(page.locator(".tile").first()).toBeVisible(); // 36 tiles total await expect(page.locator(".tile")).toHaveCount(36); await expect(page.locator(".tile").first()).toHaveAttribute("aria-label", /nutrient \d, heat \d, toxin \d/); }); test("exact preview shows relocation, flow, deltas, and equals commit", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await page.getByRole("button", { name: /Rotate outer clockwise/ }).click(); await expect(page.locator("#btnCommit")).toBeEnabled(); await expect(page.locator(".relocation")).toContainText("O0→O1"); await expect(page.locator(".delta-list")).toContainText(/[NHTPVF][+-]\d/); await expect(page.locator(".tile-delta").first()).toBeVisible(); await expect(page.locator(".board-svg title").filter({ hasText: "radial-inward flow" }).first()).toBeAttached(); const previewValues = await page.locator(".tile").evaluateAll((tiles) => tiles.map((tile) => tile.getAttribute("data-values"))); await page.locator("#btnCancel").focus(); await expect(page.locator("#btnCommit")).toBeEnabled(); await page.locator("#btnCommit").click(); await expect(page.locator(".turn")).toContainText("Turn 1"); const committedValues = await page.locator(".tile").evaluateAll((tiles) => tiles.map((tile) => tile.getAttribute("data-values"))); expect(committedValues).toEqual(previewValues); }); test("keyboard operation (1 selects, Enter commits, Esc cancels)", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await page.keyboard.press("3"); // middle cw await expect(page.locator(".rot.selected")).toHaveCount(1); await page.keyboard.press("Enter"); await expect(page.locator(".turn")).toContainText("Turn 1"); await page.keyboard.press("1"); // inner cw await page.keyboard.press("Escape"); await expect(page.locator(".rot.selected")).toHaveCount(0); }); test("cancel and commit restore keyboard focus; Help exposes disclosure state", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); const focusPreview = page.getByRole("button", { name: /Rotate outer clockwise/ }); await focusPreview.focus(); await expect(page.locator("#btnCommit")).toBeEnabled(); await page.keyboard.press("Escape"); await expect(focusPreview).toBeFocused(); const rotation = page.getByRole("button", { name: /Rotate inner clockwise/ }); await rotation.click(); await page.locator("#btnCancel").click(); await expect(rotation).toBeFocused(); await rotation.click(); await page.locator("#btnCommit").click(); await expect(rotation).toBeFocused(); const help = page.locator("#btnHelp"); await expect(help).toHaveAttribute("aria-controls", "helpPanel"); await expect(help).toHaveAttribute("aria-expanded", "false"); await help.click(); await expect(help).toHaveAttribute("aria-expanded", "true"); await expect(page.locator("#helpPanel")).toHaveAttribute("aria-hidden", "false"); await rotation.click(); await help.focus(); await page.keyboard.press("Enter"); await expect(page.locator(".turn")).toContainText("Turn 1"); }); test("full scenario 1 win via verified route -> outcome + lineage choice (adaptation)", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await playRoute(page, s1win); // won -> lineage choice or next scenario await expect(page.locator("h1, h2").first()).toContainText(/Lineage choice|Endurance|Adaptive Loop/i, { timeout: 10000 }); }); test("adaptation benefit, cost, spillover, and strengthened response are visible", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); for (const action of s1win.slice(0, 4)) await playRoute(page, [action]); const events = page.locator(".event-list"); await expect(events).toContainText("unlocked Dormancy"); await expect(events).toContainText("Benefit:"); await expect(events).toContainText("Cost:"); await expect(events).toContainText("Spillover:"); await expect(events).toContainText("Hunger response active"); await expect(events).toContainText("nutrient +1"); await expect(events).toContainText("Benefit active"); }); test("a provoked adaptation visibly triggers its benefit, cost, and spillover", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await playRoute(page, s1win); await page.getByRole("button", { name: "Decline" }).click(); await playRoute(page, data.fixtures["s2-hunger"].win.slice(0, 5)); const events = page.locator(".event-list"); await expect(events).toContainText("Benefit active"); await expect(events).toContainText("Cost active"); await expect(events).toContainText("Spillover active"); await expect(page.locator(".tile.adapted").first()).toBeVisible(); await expect(page.locator(".adapted-tag").first()).toContainText("✓"); }); test("collapse, retry, then win through public controls", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await playRoute(page, s1collapse); await expect(page.locator(".turn")).toContainText("Ecosystem collapsed"); await page.getByRole("button", { name: "Retry" }).click(); await expect(page.locator(".turn")).toContainText("Turn 0"); await playRoute(page, s1win); await expect(page.getByRole("heading", { name: "Lineage choice" })).toBeVisible(); }); test("lose then retry via verified loss route", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await playRoute(page, s1loss); await expect(page.locator(".turn").first()).toContainText(/collapsed|expired|Turn limit|Ecosystem|Turn 8/i); await expect(page.getByRole("button", { name: "Retry" })).toBeVisible(); await page.getByRole("button", { name: "Retry" }).click(); await expect(page.locator(".turn")).toContainText("Turn 0"); }); test("Save & Quit, close/reopen, Continue restores exact public state", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await page.getByRole("button", { name: /Rotate inner clockwise/ }).click(); await page.locator("#btnCommit").click(); const expectedValues = await page.locator(".tile").evaluateAll((tiles) => tiles.map((tile) => tile.getAttribute("data-values"))); const expectedTurn = await page.locator(".turn").textContent(); await page.locator("#btnSaveQuit").click(); const context = page.context(); await page.close(); const reopened = await context.newPage(); await reopened.goto(BASE); await reopened.locator("#btnContinue").click(); expect(await reopened.locator(".turn").textContent()).toBe(expectedTurn); const restoredValues = await reopened.locator(".tile").evaluateAll((tiles) => tiles.map((tile) => tile.getAttribute("data-values"))); expect(restoredValues).toEqual(expectedValues); }); test("narrow viewport (390px) has no horizontal scroll and controls reachable", async ({ page }) => { await page.setViewportSize({ width: 390, height: 800 }); await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await expect(page.locator(".board-svg")).toBeVisible(); const ledger = page.locator(".tile-ledger"); await expect(ledger).toBeVisible(); await expect(ledger).toContainText(/I\d.*N\d.*H\d.*T\d/); expect(await ledger.evaluate((element) => parseFloat(getComputedStyle(element).fontSize))).toBeGreaterThanOrEqual(13); const scrollWidth = await page.evaluate(() => document.documentElement.scrollWidth); const clientWidth = await page.evaluate(() => document.documentElement.clientWidth); expect(scrollWidth).toBeLessThanOrEqual(clientWidth); await expect(page.locator("#btnCommit")).toBeVisible(); }); test("200% browser zoom keeps every control reachable and operable", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); const cdp = await page.context().newCDPSession(page); await cdp.send("Emulation.setDeviceMetricsOverride", { width: 640, height: 450, deviceScaleFactor: 2, mobile: false }); await expect(page.locator(".board-svg")).toBeVisible(); await expect(page.locator(".rot")).toHaveCount(6); const controls = page.locator("button:visible"); expect(await controls.count()).toBeGreaterThanOrEqual(10); for (const control of await controls.all()) { await control.scrollIntoViewIfNeeded(); await expect(control).toBeVisible(); } expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBeLessThanOrEqual(await page.evaluate(() => document.documentElement.clientWidth)); const rotation = page.getByRole("button", { name: /Rotate inner clockwise/ }); await rotation.focus(); await expect(rotation).toBeFocused(); await page.keyboard.press("Enter"); await expect(page.locator(".turn")).toContainText("Turn 1"); }); test("touch input previews and commits on a mobile context", async ({ browser }) => { const context = await browser.newContext({ hasTouch: true, isMobile: true, viewport: { width: 390, height: 844 } }); const page = await context.newPage(); await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).tap(); await page.getByRole("button", { name: /Rotate inner clockwise/ }).tap(); await expect(page.locator("#btnCommit")).toBeEnabled(); await page.locator("#btnCommit").tap(); await expect(page.locator(".turn")).toContainText("Turn 1"); await context.close(); }); test("reduced-motion preference renders without error", async ({ browser }) => { const ctx = await browser.newContext({ reducedMotion: "reduce", viewport: { width: 1280, height: 900 } }); const page = await ctx.newPage(); const errors: string[] = []; page.on("pageerror", (e) => errors.push(String(e))); await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await expect(page.locator(".board-svg")).toBeVisible(); const motion = await page.locator(".tile").first().evaluate((element) => ({ animation: getComputedStyle(element).animationName, transition: getComputedStyle(element).transitionDuration })); expect(motion).toEqual({ animation: "none", transition: "0s" }); expect(errors).toEqual([]); await ctx.close(); }); test("primary text contrast meets WCAG AA", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); const ratios = await page.evaluate(() => { const rgb = (value: string) => value.match(/[\d.]+/g)!.slice(0, 3).map(Number); const luminance = (value: string) => { const channels = rgb(value).map((channel) => channel / 255).map((channel) => channel <= 0.03928 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4); return 0.2126 * channels[0] + 0.7152 * channels[1] + 0.0722 * channels[2]; }; const ratio = (foreground: string, background: string) => { const [lighter, darker] = [luminance(foreground), luminance(background)].sort((a, b) => b - a); return (lighter + 0.05) / (darker + 0.05); }; const body = getComputedStyle(document.body); const button = getComputedStyle(document.querySelector(".btn")!); return [ratio(body.color, body.backgroundColor), ratio(button.color, button.backgroundColor)]; }); expect(Math.min(...ratios)).toBeGreaterThanOrEqual(4.5); }); test("five scenarios unlock in order and campaign victory is playable", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); const names = ["Cultivation Quota", "Endurance", "Cleansing", "Escort", "Stable Equilibrium"]; for (let index = 0; index < names.length; index++) { await expect(page.locator(".obj h2")).toContainText(names[index]); await playRoute(page, data.fixtures[["s1-heat", "s2-hunger", "s3-toxin", "s4-immunity", "s5-convergence"][index]].win); if (index < names.length - 1) { await expect(page.getByRole("heading", { name: "Lineage choice" })).toBeVisible(); await page.getByRole("button", { name: "Decline" }).click(); } } await expect(page.getByRole("heading", { name: "Campaign Victory" })).toBeVisible(); await page.locator("#btnVictoryMenu").click(); await expect(page.getByRole("heading", { name: "Current Campaign" })).toBeVisible(); }); test("lineage choice carries its visible gift and cost", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await playRoute(page, s1win); const choice = page.locator(".choice-grid .trait-choice").first(); await expect(choice).toContainText(/Benefit:/); await expect(choice).toContainText(/Cost:/); const label = (await choice.locator(".ch-trait").textContent()) ?? ""; await choice.click(); await expect(page.locator(".inh")).toContainText(label); }); test("standalone save and retry preserve the retained campaign", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await playRoute(page, s1win); const choice = page.locator(".choice-grid .trait-choice").first(); const inheritedLabel = (await choice.locator(".ch-trait").textContent()) ?? ""; await choice.click(); await expect(page.locator(".obj h2")).toContainText("Endurance"); await page.locator("#btnSaveQuit").click(); await page.getByRole("button", { name: "Replay Standalone" }).click(); await expect(page.getByRole("button", { name: "Replay Cultivation Quota" })).toBeEnabled(); await expect(page.getByRole("button", { name: "Replay Endurance" })).toBeEnabled(); await expect(page.getByRole("button", { name: "Replay Cleansing" })).toBeDisabled(); await page.getByRole("button", { name: "Replay Cultivation Quota" }).click(); await page.getByRole("button", { name: /Rotate inner clockwise/ }).click(); await page.locator("#btnCommit").click(); await page.locator("#btnSaveQuit").click(); await page.locator("#btnContinue").click(); await expect(page.locator(".obj h2")).toContainText("Cultivation Quota"); await expect(page.locator(".turn")).toContainText("Turn 1"); await playRoute(page, s1loss.slice(1)); await page.getByRole("button", { name: "Retry" }).click(); await expect(page.locator(".turn")).toContainText("Turn 0"); await page.locator("#btnSaveQuit").click(); await page.locator("#btnResumeCampaign").click(); await expect(page.locator(".obj h2")).toContainText("Endurance"); await expect(page.locator(".inh")).toContainText(inheritedLabel); }); test("campaign menu resumes only the active run", async ({ page }) => { await page.goto(BASE); await page.getByRole("button", { name: "New Campaign" }).click(); await page.getByRole("button", { name: /Rotate inner clockwise/ }).click(); await page.locator("#btnCommit").click(); await page.locator("#btnMenu").click(); await expect(page.getByRole("heading", { name: "Current Campaign" })).toBeVisible(); await expect(page.getByRole("button", { name: "Resume Cultivation Quota" })).toBeEnabled(); await expect(page.getByRole("button", { name: "Resume Endurance" })).toBeDisabled(); await page.getByRole("button", { name: "Resume Cultivation Quota" }).click(); await expect(page.locator(".turn")).toContainText("Turn 1"); });