// Tests for plan2code-ui-prototype's scripts/ui-prototype.mjs: assemble turns a // round folder into one page around the shipped shell, unpackPage reads it // back, and check reports every broken contract rule by rule and variant. See // skill-script-helpers.mjs for how these run (from the built skills/ tree). 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, SKILLS, runScript, tmp, copyFixture, fixtureCleanup } from "./skill-script-helpers.mjs"; const madeDirs = []; after(() => { fixtureCleanup(); for (const dir of madeDirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true }); }); /** A temp folder removed when the suite ends. */ function scratch(name) { const dir = tmp(name); madeDirs.push(dir); return dir; } const SCRIPTS = path.join(SKILLS, "plan2code-ui-prototype", "scripts"); const { unpackPage, placeholderUrl } = await import(pathToFileURL(path.join(SCRIPTS, "ui-prototype-page.mjs")).href); const { projectKey } = await import(pathToFileURL(path.join(SCRIPTS, "ui-prototype-drafts.mjs")).href); const { notepadKey } = await import(pathToFileURL(path.join(ROOT, "src", "web-console", "notepads.mjs")).href); const FIXTURES = path.join(ROOT, "scripts", "fixtures", "ui-prototype"); const proto = (args) => runScript("plan2code-ui-prototype", "ui-prototype.mjs", args); /** A fresh copy of a fixture round as /round-1; returns the round folder. */ function roundFrom(name) { const dir = copyFixture("ui-prototype", name, (into) => fs.cpSync(path.join(FIXTURES, name), path.join(into, "round-1"), { recursive: true })); return path.join(dir, "round-1"); } /** Assemble a fixture and return { r, html, round }. */ function assembled(name) { const round = roundFrom(name); const r = proto(["assemble", "--round", round]); assert.equal(r.code, 0, `${name}: ${r.stdout} ${r.stderr}`); return { r, round, html: fs.readFileSync(r.json.file, "utf8") }; } const variantText = (name, file) => fs.readFileSync(path.join(FIXTURES, name, file), "utf8"); const roundJson = (name) => JSON.parse(fs.readFileSync(path.join(FIXTURES, name, "round.json"), "utf8")); const GOOD = ["good-screen", "good-flow", "good-terminal"]; /* ------------------------------------------------------------- help, usage */ test("ui-prototype --help names every command and the exit codes", () => { const r = proto(["--help"]); assert.equal(r.code, 0, r.stderr); for (const command of ["sweep", "new-round", "assemble", "check", "open", "read-block", "save"]) { assert.match(r.stdout, new RegExp(`ui-prototype\\.mjs ${command}\\b`), command); } for (const exit of ["0 ok", "1 crash", "2 bad arguments", "3 not found", "4 invalid", "5 refused"]) assert.ok(r.stdout.includes(exit), exit); }); test("ui-prototype: an unknown command and a missing one exit 2", () => { for (const args of [["frobnicate"], []]) { const r = proto(args); assert.equal(r.code, 2, args.join(" ")); assert.equal(r.json.error, "unknown-command"); } }); test("ui-prototype: assemble and check without their flag exit 2", () => { assert.equal(proto(["assemble"]).code, 2); assert.equal(proto(["check"]).code, 2); }); /* ---------------------------------------------------------------- assemble */ for (const name of GOOD) { test(`assemble ${name}: one page beside the round folder, with its parts`, () => { const { r, round, html } = assembled(name); const facts = roundJson(name); assert.equal(r.json.ok, true); assert.equal(r.json.file, path.join(path.dirname(round), `${facts.name}-round-${facts.round}.html`)); assert.equal(r.json.bytes, Buffer.byteLength(html, "utf8")); assert.deepEqual(r.json.warnings, []); const whats = r.json.parts.map((p) => p.what); assert.deepEqual([...whats].sort(), ["shell", "base stylesheet", ...facts.variants.map((v) => `variant ${v.letter}`), "round facts"].sort()); // Largest first. for (let i = 1; i < r.json.parts.length; i++) assert.ok(r.json.parts[i - 1].bytes >= r.json.parts[i].bytes, whats.join(", ")); for (const p of r.json.parts) assert.ok(Number.isInteger(p.bytes) && p.bytes > 0, p.what); // Only the page was added: the round folder holds what it held. assert.deepEqual(fs.readdirSync(path.dirname(round)).sort(), [`${facts.name}-round-${facts.round}.html`, "round-1"].sort()); }); test(`assemble ${name}: one sandboxed frame per variant, the policy line, nothing outside data:`, () => { const { html } = assembled(name); const facts = roundJson(name); const frames = [...html.matchAll(/']) assert.ok(hostile.includes(s), `the fixture holds ${s}`); // Counted outside the srcdoc values, where a quoted attribute may hold any // text but a raw " (the hostile text's own "> sits there legally). const tags = html.replace(/srcdoc="[^"]*"/g, 'srcdoc=""'); assert.equal((tags.match(//g) || []).length, 3); assert.equal((tags.match(/<\/iframe>/); assert.ok(a, "frame A's attribute runs to its own closing tag"); assert.equal(unpackPage(html).frames.A.content, hostile); }); test("unpackPage returns null for a page with no round facts", () => { assert.equal(unpackPage("

not a prototype

"), null); }); /* ------------------------------------------------------------------- check */ for (const name of [...GOOD, "hostile-text"]) { test(`check passes ${name} with exit 0`, () => { const { r } = assembled(name); const c = proto(["check", "--file", r.json.file]); assert.equal(c.code, 0, c.stdout); assert.deepEqual(c.json, { ok: true, findings: [], bytes: r.json.bytes, overCeiling: false }); }); } test("the contract reference's example variant is the contract-example fixture's variant A, and that round passes check", () => { const contract = fs.readFileSync(path.join(SKILLS, "plan2code-ui-prototype", "references", "ui-prototype-contract.md"), "utf8"); const section = contract.split(/^## 10\. Writing a round$/m)[1]; assert.ok(section, "the contract has a '## 10. Writing a round' section"); const shown = section.match(/```html\n([\s\S]*?)```/); assert.ok(shown, "the section shows an html example"); assert.equal(shown[1], variantText("contract-example", "variant-a.html")); const { r } = assembled("contract-example"); const c = proto(["check", "--file", r.json.file]); assert.equal(c.code, 0, c.stdout); assert.deepEqual(c.json.findings, []); }); const BROKEN = [ ["bad-outside-link", "outside-link", "B"], ["bad-script", "script", "B"], ["bad-state-missing", "state-missing", "A"], ["bad-length-missing", "length-missing", "C"], ["bad-value-unused", "value-unused", "B"], ["bad-step-missing", "step-missing", "B"], ]; for (const [name, rule, variant] of BROKEN) { test(`check reports ${name} as ${rule} in variant ${variant}, and nothing else`, () => { const { r } = assembled(name); const c = proto(["check", "--file", r.json.file]); assert.equal(c.code, 4, c.stdout); assert.equal(c.json.ok, false); assert.equal(c.json.findings.length, 1, JSON.stringify(c.json.findings)); const [f] = c.json.findings; assert.equal(f.rule, rule); assert.equal(f.variant, variant); assert.equal(typeof f.message, "string"); assert.ok(f.message.length > 10); }); } test("check: a terminal variant is held to the palette and density values, not fonts and corners", () => { const round = roundFrom("good-terminal"); const file = path.join(round, "variant-b.html"); fs.writeFileSync(file, fs.readFileSync(file, "utf8").replace("line-height: var(--p2c-density); ", "")); const r = proto(["assemble", "--round", round]); const c = proto(["check", "--file", r.json.file]); assert.equal(c.code, 4); assert.deepEqual(c.json.findings.map((f) => [f.rule, f.variant]), [["value-unused", "B"]]); assert.match(c.json.findings[0].message, /density/); }); test("check: inline SVG with xmlns and url(#id) is not an outside link; a // src is", () => { const round = roundFrom("good-screen"); const file = path.join(round, "variant-a.html"); const svg = ''; fs.writeFileSync(file, fs.readFileSync(file, "utf8") + svg); let r = proto(["assemble", "--round", round]); assert.equal(proto(["check", "--file", r.json.file]).code, 0); fs.writeFileSync(file, fs.readFileSync(file, "utf8") + ''); r = proto(["assemble", "--round", round]); const c = proto(["check", "--file", r.json.file]); assert.equal(c.code, 4); assert.deepEqual(c.json.findings.map((f) => [f.rule, f.variant]), [["outside-link", "A"]]); }); /** Check good-screen with `extra` appended to variant A (and `edit` applied first); returns the findings as [rule, variant]. */ function checkWith(extra, edit = (t) => t) { const round = roundFrom("good-screen"); const file = path.join(round, "variant-a.html"); fs.writeFileSync(file, edit(fs.readFileSync(file, "utf8")) + extra); const r = proto(["assemble", "--round", round]); assert.equal(r.code, 0, r.stdout); const c = proto(["check", "--file", r.json.file]); assert.equal(c.code, c.json.findings.length ? 4 : 0); return c.json.findings.map((f) => [f.rule, f.variant]); } test("check: every kind of script is reported, wherever the handler sits in the tag", () => { for (const extra of [ "", 'Pay', '', '

Pay

', "", ]) { assert.deepEqual(checkWith(extra), [["script", "A"]], extra); } // Text that only reads like a handler is not one. assert.deepEqual(checkWith("

Turn on = off

"), []); }); test("check: a // URL is reported wherever one can begin", () => { for (const extra of [ '', '', `

x

`, ]) { assert.deepEqual(checkWith(extra), [["outside-link", "A"]], extra); } }); test("check: file:// URLs and // URLs to a dotless host are reported; plain text that only looks like one is not", () => { for (const extra of [ '', '', '', "

x

", ]) { assert.deepEqual(checkWith(extra), [["outside-link", "A"]], extra); } assert.deepEqual(checkWith("

Attach file: invoice.pdf. //TODO later

"), []); }); test("check: a handler on a tag written inside an svg style block is reported", () => { assert.deepEqual(checkWith(''), [["script", "A"]]); // In an ordinary style block the same text is CSS, not markup. assert.deepEqual(checkWith(""), []); }); test("check: an outside link in the page around the frames is reported with no variant", () => { const { html, r } = assembled("good-screen"); const at = html.lastIndexOf(""); fs.writeFileSync(r.json.file, html.slice(0, at) + '' + html.slice(at)); const c = proto(["check", "--file", r.json.file]); assert.equal(c.code, 4, c.stdout); assert.deepEqual(c.json.findings.map((f) => [f.rule, f.variant]), [["outside-link", null]]); }); test("check: a variant whose frame is gone from the page is reported as frame-missing", () => { const { html, r } = assembled("good-screen"); const cut = html.replace(/