// Page tests for the UI prototype shell: an assembled fixture page opened from // file:// in a real Chrome or Edge, through scripts/browser.mjs. One browser // for the file, a fresh context per test. Run with: // node scripts/node-test.mjs scripts/test-ui-prototype-page.mjs // // The page is built by the built skill's assemble (skill-script-helpers.mjs // rebuilds skills/ first), so these cover the three shell files as shipped. // Nothing else parses shell.js: the "no console error" test is its parse check. import { test, after } from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; import { pathToFileURL } from "node:url"; import { ROOT, runScript, tmp } from "./skill-script-helpers.mjs"; import { launchBrowser } from "./browser.mjs"; const FIXTURES = path.join(ROOT, "scripts", "fixtures", "ui-prototype"); const POLICY = "default-src 'none'; img-src data:; font-src data:; style-src 'unsafe-inline'; script-src 'unsafe-inline'"; const madeDirs = []; let browser = null; after(async () => { if (browser) await browser.close(); for (const dir of madeDirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); }); const roundJson = (name) => JSON.parse(fs.readFileSync(path.join(FIXTURES, name, "round.json"), "utf8")); /** * Assemble a fixture round into a temp folder and open the page from file://. * Waits for window.p2c and for every frame's own document to be complete with * the dials written on its root. Returns { page, url, requests, errors }. */ async function openFixture(t, name, { width = 1400, height = 900, edit, permissions } = {}) { const dir = tmp(`ui-page-${name}`); madeDirs.push(dir); const round = path.join(dir, "round-1"); fs.cpSync(path.join(FIXTURES, name), round, { recursive: true }); if (edit) edit(round); const r = runScript("plan2code-ui-prototype", "ui-prototype.mjs", ["assemble", "--round", round]); assert.equal(r.code, 0, `assemble ${name}: ${r.stdout} ${r.stderr}`); browser ||= await launchBrowser(); const context = await browser.newContext({ viewport: { width, height }, ...(permissions ? { permissions } : {}) }); t.after(() => context.close()); const page = await context.newPage(); const requests = []; const errors = []; page.on("request", (q) => requests.push(q.url())); page.on("console", (m) => { if (m.type() === "error") errors.push(m.text()); }); page.on("pageerror", (e) => errors.push(String(e))); const url = pathToFileURL(r.json.file).href; await page.goto(url); await page.waitForFunction(() => { if (!window.p2c) return false; return [...document.querySelectorAll("iframe.p2c-frame")].every((f) => { const d = f.contentDocument; if (!d || d.readyState !== "complete" || !d.body || !d.body.children.length) return false; const root = d.documentElement; if (!root.hasAttribute("data-p2c-kind")) return false; return root.getAttribute("data-p2c-kind") === "terminal" || root.style.getPropertyValue("--p2c-screen-width") !== ""; }); }); return { page, url, requests, errors, file: r.json.file }; } /** Run fn(doc, letter) inside every frame's document; returns { : result }. */ function inFrames(page, fn, arg) { return page.evaluate( ([source, a]) => { const run = new Function("doc", "letter", "arg", `return (${source})(doc, letter, arg);`); const out = {}; for (const f of document.querySelectorAll("iframe.p2c-frame")) out[f.dataset.variant] = run(f.contentDocument, f.dataset.variant, a); return out; }, [fn.toString(), arg] ); } /** A computed style value on every frame's root (custom properties trimmed). */ const rootValue = (page, prop) => inFrames(page, (doc, _l, p) => getComputedStyle(doc.documentElement).getPropertyValue(p).trim(), prop); const rootAttr = (page, attr) => inFrames(page, (doc, _l, a) => doc.documentElement.getAttribute(a), attr); /** Which frames are on show: { : true|false }. */ const shown = (page) => page.evaluate(() => { const out = {}; for (const f of document.querySelectorAll("iframe.p2c-frame")) out[f.dataset.variant] = getComputedStyle(f).visibility === "visible"; return out; }); const every = (map, value) => Object.values(map).every((v) => v === value); const pressed = (page, dial) => page.locator(`[data-dial="${dial}"] button[aria-pressed="true"]`).textContent(); const pickChoice = (page, dial, label) => page.locator(`[data-dial="${dial}"] button`, { hasText: label }).click(); /* ----------------------------------------------------- (a) policy and reach */ test("the frames render under the policy line, and the page's script reaches each frame's root", async (t) => { const { page } = await openFixture(t, "good-screen"); assert.equal(await page.locator('meta[http-equiv="Content-Security-Policy"]').getAttribute("content"), POLICY); const facts = roundJson("good-screen"); assert.equal(await page.locator("iframe.p2c-frame").count(), facts.variants.length); for (const sandbox of await page.locator("iframe.p2c-frame").evaluateAll((els) => els.map((e) => e.getAttribute("sandbox")))) { assert.equal(sandbox, "allow-same-origin"); } // The variant's own rules render: the frame's body takes base.css and the dial values. const accents = await rootValue(page, "--p2c-accent"); assert.ok(every(accents, facts.starts.accent), JSON.stringify(accents)); const fonts = await inFrames(page, (doc) => getComputedStyle(doc.body).fontSize); assert.ok(every(fonts, `${facts.starts.textSize}px`), JSON.stringify(fonts)); // window.p2c is the page's one handle: state and apply(). const handle = await page.evaluate(() => ({ keys: Object.keys(window.p2c).sort(), variant: window.p2c.state.variant, apply: typeof window.p2c.apply })); assert.deepEqual(handle, { keys: ["apply", "state"], variant: "A", apply: "function" }); assert.equal(await page.title(), facts.name); assert.equal(await page.locator("#p2c-name").textContent(), facts.name); }); /* --------------------------------------------- (b) no network, no console error */ for (const name of ["good-screen", "good-flow", "good-terminal", "hostile-text"]) { test(`${name}: no request but the page itself, and no console error`, async (t) => { const { page, url, requests, errors } = await openFixture(t, name); // Move through the page a little so the handlers run too. await page.click("#p2c-compare"); await page.click("#p2c-compare"); await page.locator('[role="tab"]').last().click(); assert.deepEqual(errors, []); assert.deepEqual( requests.filter((u) => u !== url), [], "the page loaded nothing else" ); }); } /* ----------------------------------------------------------------- (c) tabs */ test("one tab per variant; a click or an arrow key shows only that frame, kept in step with the panel's pick", async (t) => { const { page } = await openFixture(t, "good-screen"); const facts = roundJson("good-screen"); const tabs = page.locator('#p2c-tabs [role="tab"]'); assert.equal(await tabs.count(), facts.variants.length); for (const [i, v] of facts.variants.entries()) assert.equal((await tabs.nth(i).textContent()).replace(/\s+/g, " ").trim(), `${v.letter}${v.name}`); assert.deepEqual(await shown(page), { A: true, B: false, C: false }); await tabs.nth(1).click(); assert.deepEqual(await shown(page), { A: false, B: true, C: false }); assert.equal(await tabs.nth(1).getAttribute("aria-selected"), "true"); assert.equal(await tabs.nth(0).getAttribute("aria-selected"), "false"); assert.equal(await pressed(page, "variant"), "B"); assert.equal(await page.evaluate(() => window.p2c.state.variant), "B"); await page.keyboard.press("ArrowRight"); assert.deepEqual(await shown(page), { A: false, B: false, C: true }); assert.equal(await page.evaluate(() => document.activeElement.id), "p2c-tab-C"); await page.keyboard.press("ArrowRight"); assert.deepEqual(await shown(page), { A: true, B: false, C: false }, "the arrow wraps around"); await page.keyboard.press("ArrowLeft"); assert.deepEqual(await shown(page), { A: false, B: false, C: true }); // The panel's pick moves the tabs too. await pickChoice(page, "variant", "B"); assert.equal(await tabs.nth(1).getAttribute("aria-selected"), "true"); assert.deepEqual(await shown(page), { A: false, B: true, C: false }); }); /* ------------------------------------------------------------ (d) every dial */ test("each core dial writes its value on every frame's root, and the tool's own look stays put", async (t) => { const { page } = await openFixture(t, "good-screen"); const toolLook = () => page.evaluate(() => [document.body, document.getElementById("p2c-bar"), document.getElementById("p2c-panel")].map((e) => `${getComputedStyle(e).backgroundColor} ${getComputedStyle(e).fontFamily} ${getComputedStyle(e).fontSize}`)); const before = await toolLook(); await page.locator("#p2c-dial-theme").check(); assert.ok(every(await rootAttr(page, "data-p2c-theme"), "dark")); assert.ok(every(await inFrames(page, (doc) => getComputedStyle(doc.documentElement).colorScheme), "dark")); await page.locator("#p2c-dial-accent").fill("#ff0000"); assert.ok(every(await rootValue(page, "--p2c-accent"), "#ff0000")); const button = await inFrames(page, (doc) => (doc.querySelector(".inv button") ? getComputedStyle(doc.querySelector(".inv button")).backgroundColor : null)); assert.equal(button.A, "rgb(255, 0, 0)", "a variant's own rule follows the accent"); await page.locator("#p2c-dial-neutral").fill("#00aa00"); assert.ok(every(await rootValue(page, "--p2c-neutral"), "#00aa00")); await page.locator("#p2c-dial-fontSet").selectOption("Slab Serif"); const families = await inFrames(page, (doc) => getComputedStyle(doc.body).fontFamily); assert.ok(Object.values(families).every((f) => f.startsWith("Rockwell")), JSON.stringify(families)); assert.equal(await page.locator('#p2c-dial-fontSet option[value="App font"]').count(), 0, "no App font without appFont in the round facts"); await page.locator("#p2c-dial-textSize").fill("20"); assert.ok(every(await inFrames(page, (doc) => getComputedStyle(doc.body).fontSize), "20px")); await page.locator("#p2c-dial-space").fill("1.5"); assert.ok(every(await rootValue(page, "--p2c-space"), "1.5")); const pad = await inFrames(page, (doc) => (doc.querySelector(".inv") ? getComputedStyle(doc.querySelector(".inv")).paddingTop : null)); assert.equal(pad.A, "36px", "--p2c-space-5 follows the spacing dial"); await page.locator("#p2c-dial-radius").fill("20"); assert.ok(every(await rootValue(page, "--p2c-radius"), "20px")); await pickChoice(page, "state", "Loading"); assert.ok(every(await rootAttr(page, "data-p2c-state"), "loading")); await pickChoice(page, "length", "Very long"); assert.ok(every(await rootAttr(page, "data-p2c-length"), "long")); await pickChoice(page, "screen", "Tablet 820"); assert.ok(every(await rootValue(page, "--p2c-screen-width"), "820px")); const dials = await page.evaluate(() => Object.fromEntries(Object.entries(window.p2c.state.dials).map(([k, d]) => [k, [d.value, d.moved]]))); assert.deepEqual(dials, { variant: ["A", false], screen: ["Tablet 820", true], theme: ["dark", true], accent: ["#ff0000", true], neutral: ["#00aa00", true], fontSet: ["Slab Serif", true], textSize: [20, true], space: [1.5, true], radius: [20, true], state: ["loading", true], length: ["long", true], "x-shadows": ["soft", false], "x-columns": [2, false], "x-dividers": [true, false], }); assert.deepEqual(await toolLook(), before, "no dial touches the tool's own look"); // The panel holds the dials in the planned order, the extras after them. const order = await page.locator("#p2c-panel-body [data-dial]").evaluateAll((rows) => rows.map((r) => r.dataset.dial)); assert.deepEqual(order, ["variant", "screen", "theme", "accent", "neutral", "fontSet", "textSize", "space", "radius", "state", "length", "x-shadows", "x-columns", "x-dividers"]); }); test("App font is offered only when the round facts say appFont, and writes the app font value", async (t) => { const dir = tmp("ui-page-appfont"); madeDirs.push(dir); const src = path.join(dir, "good-app"); fs.cpSync(path.join(FIXTURES, "good-screen"), src, { recursive: true }); // The round facts are the page's own JSON: set appFont there after assembling. const round = path.join(dir, "round-1"); fs.cpSync(src, round, { recursive: true }); const r = runScript("plan2code-ui-prototype", "ui-prototype.mjs", ["assemble", "--round", round]); assert.equal(r.code, 0, r.stdout + r.stderr); // assemble writes "appFont":false itself when no font file was embedded, so // flip that key rather than adding a second one (the later key would win). const before = fs.readFileSync(r.json.file, "utf8"); assert.match(before, /"appFont":false/, "the round facts carry appFont"); const html = before.replace('"appFont":false', '"appFont":true'); fs.writeFileSync(r.json.file, html); browser ||= await launchBrowser(); const context = await browser.newContext({ viewport: { width: 1400, height: 900 } }); t.after(() => context.close()); const page = await context.newPage(); await page.goto(pathToFileURL(r.json.file).href); await page.waitForFunction(() => window.p2c); await page.locator("#p2c-dial-fontSet").selectOption("App font"); const value = await page.evaluate(() => document.getElementById("p2c-frame-A").contentDocument.documentElement.style.getPropertyValue("--p2c-font")); assert.equal(value, "var(--p2c-font-set-app)"); }); /* ------------------------------------------------------------ (e) screen size */ test("Screen size: a variant's @media rule sees the chosen width, not the window's", async (t) => { const { page } = await openFixture(t, "good-screen"); await page.locator('#p2c-tabs [role="tab"]').nth(1).click(); const tracks = async () => (await inFrames(page, (doc) => getComputedStyle(doc.querySelector(".split") || doc.body).gridTemplateColumns.split(" ").length)).B; const innerWidth = async () => page.evaluate(() => document.getElementById("p2c-frame-B").contentWindow.innerWidth); assert.equal(await innerWidth(), 1280); assert.equal(await tracks(), 2, "Desktop: the two-column rule"); await pickChoice(page, "screen", "Phone 390"); assert.equal(await innerWidth(), 390); assert.equal(await tracks(), 1, "Phone: the @media (max-width: 500px) rule matches"); await pickChoice(page, "screen", "Tablet 820"); assert.equal(await innerWidth(), 820); assert.equal(await tracks(), 2); assert.equal(await page.locator('[data-dial="screen"] .tool-scale').textContent(), "", "no scale shown while the frame fits"); }); test("Screen size: a frame wider than the stage is scaled down to fit, and the scale is shown", async (t) => { const { page } = await openFixture(t, "good-screen", { width: 1000, height: 800 }); const fit = () => page.evaluate(() => { const stage = document.getElementById("p2c-stage").getBoundingClientRect(); const f = document.getElementById("p2c-frame-A").getBoundingClientRect(); return { inside: f.left >= stage.left && f.right <= stage.right, inner: document.getElementById("p2c-frame-A").contentWindow.innerWidth }; }); assert.deepEqual(await fit(), { inside: true, inner: 1280 }); assert.match(await page.locator('[data-dial="screen"] .tool-scale').textContent(), /^\d+%$/); await pickChoice(page, "screen", "Phone 390"); assert.deepEqual(await fit(), { inside: true, inner: 390 }); assert.equal(await page.locator('[data-dial="screen"] .tool-scale').textContent(), ""); }); /* ------------------------------------------------------- (f) states and length */ test("the States and Content length dials show exactly one state's parts and one length's text", async (t) => { const { page } = await openFixture(t, "good-screen"); const visibleMarks = (attr) => inFrames( page, (doc, _l, a) => { const seen = []; for (const el of doc.querySelectorAll(`[${a}]`)) if (getComputedStyle(el).display !== "none") seen.push(el.getAttribute(a)); return seen; }, attr ); for (const [label, value] of [["Ready", "ready"], ["Empty", "empty"], ["Loading", "loading"], ["Error", "error"]]) { await pickChoice(page, "state", label); for (const [letter, marks] of Object.entries(await visibleMarks("data-p2c-when"))) { assert.ok(marks.length > 0, `${letter} shows a ${value} part`); assert.ok(marks.every((m) => m.split(" ").includes(value)), `${letter} at ${value}: ${marks.join(" | ")}`); } } for (const [label, value] of [["Short", "short"], ["Typical", "typical"], ["Very long", "long"]]) { await pickChoice(page, "length", label); for (const [letter, marks] of Object.entries(await visibleMarks("data-p2c-text"))) { assert.ok(marks.length > 0, `${letter} shows ${value} text`); assert.ok(marks.every((m) => m.split(" ").includes(value)), `${letter} at ${value}: ${marks.join(" | ")}`); } } }); /* ----------------------------------------------------------- (g) Compare all */ test("Compare all shows every frame in a row, a click picks one, and a dial change reaches all", async (t) => { const { page } = await openFixture(t, "good-screen"); const ids = await page.locator("iframe.p2c-frame").evaluateAll((els) => els.map((e) => e.id)); await page.click("#p2c-compare"); assert.equal(await page.locator("#p2c-compare").getAttribute("aria-pressed"), "true"); assert.deepEqual(await shown(page), { A: true, B: true, C: true }); const row = await page.evaluate(() => { const stage = document.getElementById("p2c-stage").getBoundingClientRect(); const boxes = [...document.querySelectorAll("iframe.p2c-frame")].map((f) => f.getBoundingClientRect()); const labels = [...document.querySelectorAll(".tool-label")].map((l) => ({ text: l.textContent, shown: !l.hidden, bottom: l.getBoundingClientRect().bottom })); return { stage: [stage.left, stage.right], boxes: boxes.map((b) => [b.left, b.right, b.top]), labels }; }); for (let i = 0; i < row.boxes.length; i++) { const [left, right, top] = row.boxes[i]; assert.ok(left >= row.stage[0] && right <= row.stage[1], `frame ${i} fits the stage width`); if (i) assert.ok(left > row.boxes[i - 1][1], `frame ${i} sits right of the one before`); assert.ok(row.labels[i].shown && row.labels[i].bottom <= top + 1, `frame ${i} has its label above it`); } assert.deepEqual( row.labels.map((l) => l.text), roundJson("good-screen").variants.map((v) => `${v.letter}${v.name}`) ); // A click on the third frame makes it current without leaving the row. const box = await page.locator("#p2c-frame-C").boundingBox(); await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2); assert.equal(await page.locator("#p2c-tab-C").getAttribute("aria-selected"), "true"); assert.equal(await page.locator("#p2c-compare").getAttribute("aria-pressed"), "true"); assert.deepEqual(await shown(page), { A: true, B: true, C: true }); await page.locator("#p2c-dial-radius").fill("3"); assert.ok(every(await rootValue(page, "--p2c-radius"), "3px")); await page.click("#p2c-compare"); assert.deepEqual(await shown(page), { A: false, B: false, C: true }, "back to the single view of the current variant"); assert.deepEqual(await page.locator("iframe.p2c-frame").evaluateAll((els) => els.map((e) => e.id)), ids, "the same frames, never re-created"); // A frame moved or re-created would reload and lose its written values. assert.ok(every(await rootValue(page, "--p2c-radius"), "3px")); }); /* ----------------------------------------------------------------- (h) panel */ test("the panel drags, never covers the top bar, hides to the Dials tab where it was and returns", async (t) => { const { page } = await openFixture(t, "good-screen"); const panelBox = () => page.locator("#p2c-panel").boundingBox(); const grip = async () => { const b = await page.locator("#p2c-panel-title").boundingBox(); return { x: b.x + 10, y: b.y + b.height / 2 }; }; const dragBy = async (dx, dy) => { const g = await grip(); await page.mouse.move(g.x, g.y); await page.mouse.down(); await page.mouse.move(g.x + dx / 2, g.y + dy / 2); await page.mouse.move(g.x + dx, g.y + dy); await page.mouse.up(); }; const start = await panelBox(); assert.ok(start.width <= 320, "the panel is at most 320px wide"); await dragBy(300, 200); const moved = await panelBox(); assert.ok(Math.abs(moved.x - start.x - 300) < 2 && Math.abs(moved.y - start.y - 200) < 2, JSON.stringify({ start, moved })); const barBottom = (await page.locator("#p2c-bar").boundingBox()).height; await dragBy(0, -2000); assert.ok((await panelBox()).y >= barBottom - 0.5, "the panel stops under the top bar"); await dragBy(5000, 5000); const corner = await panelBox(); const view = page.viewportSize(); const handle = await page.locator("#p2c-panel-handle").boundingBox(); assert.ok(corner.x + corner.width <= view.width + 0.5 && handle.y + handle.height <= view.height + 0.5, "the handle stays on screen"); await dragBy(-400, -300); const before = await panelBox(); await page.click("#p2c-hide"); assert.equal(await page.locator("#p2c-panel").isVisible(), false); const tab = await page.locator("#p2c-dials-tab").boundingBox(); assert.ok(tab && Math.abs(tab.x - before.x) < 1 && Math.abs(tab.y - before.y) < 1, "the Dials tab sits where the panel was"); await page.click("#p2c-dials-tab"); assert.equal(await page.locator("#p2c-dials-tab").isVisible(), false); const back = await panelBox(); assert.ok(Math.abs(back.x - before.x) < 1 && Math.abs(back.y - before.y) < 1, "the panel returns where it was"); }); /* ------------------------------------------------------------------ (i) flow */ test("a flow lays each variant's screens in one row with their step labels, and the stage scrolls sideways", async (t) => { const { page } = await openFixture(t, "good-flow", { width: 1000, height: 800 }); const facts = roundJson("good-flow"); const steps = () => inFrames(page, (doc) => [...doc.querySelectorAll("[data-p2c-step]")].map((s) => { const r = s.getBoundingClientRect(); return { label: getComputedStyle(s, "::before").content, top: r.top, left: r.left, right: r.right, width: r.width, viewport: doc.defaultView.innerWidth }; }) ); const check = (all, width) => { for (const [letter, row] of Object.entries(all)) { assert.deepEqual( row.map((s) => s.label), ['"1. Account"', '"2. Team"', '"3. Done"'], letter ); for (let i = 0; i < row.length; i++) { assert.equal(Math.round(row[i].width), width, `${letter} step ${i} is one screen wide`); assert.equal(row[i].top, row[0].top, `${letter} step ${i} is in the row`); if (i) assert.ok(row[i].left >= row[i - 1].right, `${letter} step ${i} follows the one before`); } assert.ok(row[row.length - 1].right <= row[0].viewport, `${letter}: the frame holds the whole row`); } }; assert.equal(facts.starts.screen, "Phone 390"); check(await steps(), 390); const scrolls = await page.evaluate(() => { const s = document.getElementById("p2c-stage"); return s.scrollWidth > s.clientWidth; }); assert.ok(scrolls, "the stage scrolls sideways"); await pickChoice(page, "screen", "Tablet 820"); check(await steps(), 820); await page.locator("#p2c-dial-space").fill("0.75"); check(await steps(), 820); }); test("a flow whose variants differ in steps scrolls only as far as the current row, and a variant's own row gap is measured", async (t) => { // Variant B gets a fourth step; variant A sets its own, wider gap between screens. const edit = (round) => { const b = path.join(round, "variant-b.html"); fs.writeFileSync(b, fs.readFileSync(b, "utf8") + '\n
\n

Take the tour

\n
\n'); const a = path.join(round, "variant-a.html"); fs.writeFileSync(a, fs.readFileSync(a, "utf8").replace("