// Focused modal-focus probe: serves web/ on a free port against the live // Pipelines server, then drives the real page in headless Chrome with trusted // key events. It proves the shared controller's contract on the attention // sheet, the retry confirm and the changes viewer, plus one replaced/stacked // overlay (the artifact viewer over a stage pane). // // cd web && node test/modal-probe.mjs --out ../captures // // It is read-only: no run is retried/cancelled/decided, no chat or template is // created or changed. The one PNG is an open state focused from the keyboard. import { spawn } from "node:child_process"; import { mkdir } from "node:fs/promises"; import net from "node:net"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { eventually, launchChrome } from "./browser.mjs"; const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), ".."); const pipelines = flag(process.argv, "--pipelines", process.env.PIPELINES_TARGET ?? "127.0.0.1:8080"); const out = resolve(flag(process.argv, "--out", process.env.WEB_MODAL_OUT ?? resolve(ROOT, "../captures"))); const port = flag(process.argv, "--port", process.env.WEB_MODAL_PORT); const settle = (ms = 120) => new Promise((resolvePromise) => setTimeout(resolvePromise, ms)); function flag(argv, name, fallback) { const index = argv.indexOf(name); return index !== -1 && argv[index + 1] ? argv[index + 1] : fallback; } function freePort() { return new Promise((resolvePort) => { const server = net.createServer(); server.listen(0, "127.0.0.1", () => { const { port: bound } = server.address(); server.close(() => resolvePort(bound)); }); }); } const FOCUSABLE = [ "a[href]", "button:not([disabled])", "input:not([disabled])", "select:not([disabled])", "textarea:not([disabled])", "[tabindex]:not([tabindex='-1'])", ].join(","); const focusableList = (selector) => `(() => { const panel = document.querySelector(${JSON.stringify(selector)}); if (!panel) return []; return [...panel.querySelectorAll(${JSON.stringify(FOCUSABLE)})].filter((node) => !node.hidden && node.getAttribute("aria-hidden") !== "true" && !node.closest("[inert]") && getComputedStyle(node).display !== "none" && getComputedStyle(node).visibility !== "hidden" ); })()`; const activeId = () => page.evaluate(`(() => { const active = document.activeElement; if (!active) return ""; return active.id || active.getAttribute("data-change-index") || active.className || active.tagName; })()`); const inertState = () => page.evaluate(`(() => ({ appRoot: document.querySelector(".app-root")?.hasAttribute("inert") ?? false, skipHidden: document.querySelector(".skip-link")?.getAttribute("aria-hidden") ?? null, skipTabindex: document.querySelector(".skip-link")?.getAttribute("tabindex") ?? null, skipInert: document.querySelector(".skip-link")?.hasAttribute("inert") ?? false, }))()`); const insideDialog = (selector) => page.evaluate(`(() => { const panel = document.querySelector(${JSON.stringify(selector)}); return Boolean(panel && panel.contains(document.activeElement)); })()`); async function wrapCheck(selector, label) { const count = await page.evaluate(`${focusableList(selector)}.length`); if (!count) throw new Error(`${label}: no focusable control in the dialog`); await page.evaluate(`(() => { const list = ${focusableList(selector)}; list[list.length - 1].focus(); })()`); await page.pressKey("Tab"); const afterTab = await page.evaluate(`(() => { const list = ${focusableList(selector)}; return list.indexOf(document.activeElement); })()`); if (afterTab !== 0) throw new Error(`${label}: Tab from the last control landed on index ${afterTab}`); await page.pressKey("Tab", { shift: true }); const afterShift = await page.evaluate(`(() => { const list = ${focusableList(selector)}; return list.indexOf(document.activeElement); })()`); const last = count - 1; if (afterShift !== last) { throw new Error(`${label}: Shift+Tab from the first control landed on index ${afterShift}, expected ${last}`); } console.log(` ${label}: ${count} controls; Tab last->first and Shift+Tab first->last`); } async function assertInert(label) { const state = await inertState(); if (!state.appRoot) throw new Error(`${label}: .app-root is not inert while the dialog is open`); return state; } async function assertRestored(label, prior) { const state = await inertState(); if (state.appRoot) throw new Error(`${label}: .app-root stayed inert after close`); if (state.skipHidden !== prior.skipHidden || state.skipTabindex !== prior.skipTabindex) { throw new Error(`${label}: skip-link state was not restored exactly: ${JSON.stringify({ before: prior, after: state })}`); } console.log(` ${label}: app root and skip link restored exactly`); } async function openRun(id) { const detail = `.run-detail .run-identity`; await eventually(() => page.evaluate(`document.querySelector(${JSON.stringify(detail)})?.textContent?.includes("run ${id} ") ?? false`), { timeout: 25000, what: `run ${id} detail`, }); } let serve = null; let browser = null; let page = null; try { await mkdir(out, { recursive: true }); const webPort = port ? Number(port) : await freePort(); console.log(`• serving web/ on 127.0.0.1:${webPort} (proxy /connect -> ${pipelines})`); serve = spawn(process.execPath, ["serve.mjs", "--port", String(webPort), "--pipelines", pipelines], { cwd: ROOT, stdio: ["ignore", "pipe", "pipe"], }); let serveLog = ""; serve.stdout.on("data", (chunk) => (serveLog += chunk.toString())); serve.stderr.on("data", (chunk) => (serveLog += chunk.toString())); await eventually( async () => { if (serve.exitCode != null) throw new Error(`serve.mjs exited: ${serveLog.trim().slice(-400)}`); const response = await fetch(`http://127.0.0.1:${webPort}/`).catch(() => null); return response?.ok; }, { what: "the page to be served" }, ); console.log("• launching headless Chrome"); browser = await launchChrome({ width: 1440, height: 900 }); page = await browser.openPage(); await page.addInitScript(`(() => { localStorage.setItem("pipelines.web.serverUrl.v2", "ws://127.0.0.1:${webPort}/connect"); localStorage.removeItem("pipelines.web.attention.v1"); localStorage.removeItem("pipelines.web.workspace.v1"); })()`); const pageUrl = `http://127.0.0.1:${webPort}/`; await page.goto(pageUrl); await page.waitForSelector('#shellStatus[data-state="connected"]', { timeout: 30000 }); const runsClicked = await page.evaluate(`(() => { const all = [...document.querySelectorAll('[data-destination="runs"]')]; const control = all.find((node) => node.offsetParent !== null) ?? all[0]; if (!control) return false; control.click(); return true; })()`); if (!runsClicked) throw new Error("no Runs destination control"); await page.waitForSelector(".list.runs .run-row", { timeout: 30000 }); const prior = await inertState(); console.log(`• attention sheet (prior app inert=${prior.appRoot}, skip aria-hidden=${prior.skipHidden})`); await page.waitForSelector("#attentionBell", { timeout: 15000 }); const bellCount = await page.evaluate( `Number(document.querySelector("#attentionBell .attention-badge")?.dataset.count ?? 0)`, ); if (!(bellCount > 0)) throw new Error("the live server has no attention entries to open"); await page.evaluate(`document.querySelector("#attentionBell").focus()`); await page.pressKey("Enter"); await page.waitForSelector(".attention-sheet", { timeout: 10000 }); await settle(); await assertInert("attention open"); if (!(await insideDialog(".attention-sheet"))) { throw new Error("attention sheet: focus is outside the panel after open"); } if (await page.evaluate(`!!document.querySelector(".attention-sheet [inert]")`)) { throw new Error("attention sheet: its own controls are inert"); } const capture = `${out}/86-modal-focus-attention.png`; await page.screenshot(capture); console.log(` capture ${capture}`); await wrapCheck(".attention-sheet", "attention sheet"); await page.pressKey("Escape"); await eventually(() => page.evaluate(`!document.querySelector(".attention-sheet")`), { timeout: 5000, what: "the attention sheet to close", }); const afterAttention = await activeId(); if (afterAttention !== "attentionBell") { throw new Error(`attention sheet: Escape returned focus to ${afterAttention || "body"}, not #attentionBell`); } await assertRestored("attention close", prior); console.log("• retry confirm (run 440)"); await page.goto(`${pageUrl}runs/440`); await page.waitForSelector(".run-detail .run-actions .retry-run", { timeout: 25000 }); await openRun(440); await page.evaluate(`document.querySelector(".run-detail .run-actions .retry-run").focus()`); await page.pressKey("Enter"); await page.waitForSelector(".confirm-dialog.confirm-retry", { timeout: 10000 }); await settle(); await assertInert("retry confirm open"); if (!(await insideDialog(".confirm-dialog.confirm-retry"))) { throw new Error("retry confirm: focus is outside the panel after open"); } await wrapCheck(".confirm-dialog.confirm-retry", "retry confirm"); await page.pressKey("Escape"); await eventually(() => page.evaluate(`!document.querySelector(".confirm-dialog.confirm-retry")`), { timeout: 5000, what: "the retry confirm to close", }); await settle(); const afterConfirm = await activeId(); if (!String(afterConfirm).includes("retry-run")) { throw new Error(`retry confirm: Escape returned focus to ${afterConfirm || "body"}, not .retry-run`); } await assertRestored("retry confirm close", prior); console.log("• changes viewer (run 438)"); await page.goto(`${pageUrl}runs/438`); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await openRun(438); const expanded = await page.evaluate(`(() => { const toggle = [...document.querySelectorAll(".run-detail .collapsible .section-toggle")] .find((node) => /\\d+ files? changed/.test(node.querySelector(".section-title")?.textContent ?? "")); if (!toggle) return false; toggle.click(); return true; })()`); if (!expanded) throw new Error("changes viewer: no Changes section on run 438"); await page.waitForSelector(".run-detail .changes-list .change-row", { timeout: 15000 }); await page.evaluate(`(() => { const row = document.querySelector(".run-detail .changes-list .change-row"); row.scrollIntoView({ block: "center" }); row.focus(); })()`); await page.pressKey("Enter"); await page.waitForSelector(".change-viewer", { timeout: 10000 }); await settle(); await assertInert("changes viewer open"); if (!(await insideDialog(".change-viewer"))) { throw new Error("changes viewer: focus is outside the panel after open"); } await wrapCheck(".change-viewer", "changes viewer"); await page.pressKey("Escape"); await eventually(() => page.evaluate(`!document.querySelector(".change-viewer")`), { timeout: 5000, what: "the changes viewer to close", }); const afterChanges = await activeId(); if (afterChanges !== "0") { throw new Error(`changes viewer: Escape returned focus to ${afterChanges || "body"}, not change row 0`); } await assertRestored("changes viewer close", prior); console.log("• stacking: artifact viewer over the stage pane (run 440)"); await page.goto(`${pageUrl}runs/440`); await page.waitForSelector("#detailTitle", { timeout: 25000 }); await openRun(440); const timelineClicked = await page.evaluate(`(() => { const tab = [...document.querySelectorAll(".rail-tab, .strip-tab")] .find((node) => node.offsetParent !== null && node.textContent.trim() === "Timeline"); if (!tab) return false; tab.click(); return true; })()`); if (!timelineClicked) throw new Error("stacking: no Timeline tab"); await page.waitForSelector(".run-detail .timeline-row", { timeout: 15000 }); await page.evaluate(`document.querySelector(".run-detail .timeline-row").focus()`); await page.pressKey("Enter"); await page.waitForSelector(".stage-pane", { timeout: 10000 }); const openedArtifacts = await page.evaluate(`(() => { const toggle = [...document.querySelectorAll(".stage-pane .section-toggle")] .find((node) => node.querySelector(".section-title")?.textContent?.trim().startsWith("Artifacts")); if (!toggle) return false; toggle.click(); return true; })()`); if (!openedArtifacts) throw new Error("stacking: no Artifacts section in the stage pane"); await page.waitForSelector(".stage-pane .artifact-grid button.artifact-tile", { timeout: 10000 }); await page.evaluate(`(() => { const tiles = [...document.querySelectorAll(".stage-pane .artifact-grid button.artifact-tile")]; const tile = tiles.find((node) => node.querySelector("img")) ?? tiles[0]; tile.scrollIntoView({ block: "center" }); tile.focus(); })()`); await page.pressKey("Enter"); await page.waitForSelector(".artifact-viewer", { timeout: 10000 }); await settle(); const stack = await page.evaluate(`(() => ({ lower: document.querySelector(".stage-pane")?.hasAttribute("inert") ?? false, upper: document.querySelector(".artifact-viewer")?.hasAttribute("inert") ?? false, activeInsideUpper: document.querySelector(".artifact-viewer")?.contains(document.activeElement) ?? false, appRoot: document.querySelector(".app-root")?.hasAttribute("inert") ?? false, }))()`); if (!stack.lower) throw new Error("stacking: the lower stage pane is still tabbable while the viewer is open"); if (stack.upper) throw new Error("stacking: the top artifact viewer is inert"); if (!stack.activeInsideUpper) throw new Error("stacking: focus is not inside the top artifact viewer"); if (!stack.appRoot) throw new Error("stacking: the app root is not inert under both dialogs"); await page.pressKey("Escape"); await eventually(() => page.evaluate(`!document.querySelector(".artifact-viewer")`), { timeout: 5000, what: "the artifact viewer to close", }); const lowerRestored = await page.evaluate(`(() => ({ lowerInert: document.querySelector(".stage-pane")?.hasAttribute("inert") ?? false, activeInsideLower: document.querySelector(".stage-pane")?.contains(document.activeElement) ?? false, appRoot: document.querySelector(".app-root")?.hasAttribute("inert") ?? false, }))()`); if (lowerRestored.lowerInert) throw new Error("stacking: the stage pane stayed inert after the viewer closed"); if (!lowerRestored.activeInsideLower) throw new Error("stacking: focus did not return into the stage pane"); if (!lowerRestored.appRoot) throw new Error("stacking: the app root lost inert while the stage pane remained open"); await page.pressKey("Escape"); await eventually(() => page.evaluate(`!document.querySelector(".stage-pane")`), { timeout: 5000, what: "the stage pane to close", }); const afterStack = await page.evaluate(`(() => ({ activeRow: document.activeElement?.getAttribute("data-stage-key") ?? "", appRoot: document.querySelector(".app-root")?.hasAttribute("inert") ?? false, }))()`); if (!afterStack.activeRow) throw new Error("stacking: focus was not returned to the timeline row"); if (afterStack.appRoot) throw new Error("stacking: the app root stayed inert after the last close"); console.log(` artifact viewer -> stage pane -> timeline row (${afterStack.activeRow}); app root restored`); console.log("\nMODAL PROBE PASS"); } finally { await page?.close().catch(() => {}); await browser?.close().catch(() => {}); if (serve) { serve.kill("SIGTERM"); await eventually(async () => serve.exitCode != null || serve.signalCode != null, { timeout: 5000, what: "serve.mjs exit", }).catch(() => {}); } }