Add a Prototypes tab: list a spec's saved prototypes, read an md in place, open an html in a browser

AI Assisted
This commit is contained in:
jparkerweb committed 2026-11-12 07:56:52 -08:00
1 parent b38f2b987b
commit ba19f79f1c
16 files changed
+511 -28

No files matched your search

+1
View File
@@ -34,6 +34,7 @@
"test-us-english.mjs",
"test-web-console-agents-doc.mjs",
"test-web-console-git-board.mjs",
"test-web-console-prototypes.mjs",
"test-web-console-send-state.mjs",
"test-web-console-shape.mjs",
"test-web-console-skills.mjs",
+111
View File
@@ -0,0 +1,111 @@
// The Prototypes tab: which files the server lists, reads and opens from a
// spec's prototypes/ folder.
// Run with: node --test scripts/test-web-console-prototypes.mjs
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { listPrototypes, readPrototype, prototypeHtmlPath } from "../src/web-console/sessions.mjs";
const SPEC = "specs/picker";
function project(files) {
const base = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-prototypes-"));
const root = path.join(base, "project");
fs.mkdirSync(root, { recursive: true });
for (const [rel, text] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true });
fs.writeFileSync(path.join(root, rel), text);
}
fs.writeFileSync(path.join(base, "secret.md"), "outside\n");
fs.writeFileSync(path.join(base, "secret.html"), "<p>outside</p>\n");
return { base, root };
}
const clean = (base) => fs.rmSync(base, { recursive: true, force: true });
test("listPrototypes lists the .md and .html files directly in prototypes/, by name", async () => {
const { base, root } = project({
[`${SPEC}/prototypes/picker.md`]: "# Picker\n",
[`${SPEC}/prototypes/picker.html`]: "<p>picker</p>\n",
[`${SPEC}/prototypes/Another One.htm`]: "<p>two</p>\n",
[`${SPEC}/prototypes/notes.txt`]: "not shown\n",
[`${SPEC}/prototypes/shots/deep.md`]: "not shown\n",
[`${SPEC}/overview.md`]: "not shown\n",
});
try {
assert.deepEqual(await listPrototypes(root, SPEC), [
{ name: "Another One.htm", kind: "html" },
{ name: "picker.html", kind: "html" },
{ name: "picker.md", kind: "md" },
]);
} finally {
clean(base);
}
});
test("listPrototypes is empty with no prototypes folder, and for a spec outside the root", async () => {
const { base, root } = project({ [`${SPEC}/overview.md`]: "# O\n" });
try {
assert.deepEqual(await listPrototypes(root, SPEC), []);
assert.deepEqual(await listPrototypes(root, "../"), []);
assert.deepEqual(await listPrototypes(root, base), []);
assert.deepEqual(await listPrototypes(root, ""), []);
assert.deepEqual(await listPrototypes("", SPEC), []);
} finally {
clean(base);
}
});
test("readPrototype reads one .md in prototypes/ and refuses everything else", async () => {
const { base, root } = project({
[`${SPEC}/prototypes/picker.md`]: "# Picker\n",
[`${SPEC}/prototypes/picker.html`]: "<p>picker</p>\n",
[`${SPEC}/overview.md`]: "# O\n",
});
try {
assert.deepEqual(await readPrototype(root, SPEC, "picker.md"), { file: "picker.md", text: "# Picker\n" });
const refused = ["picker.html", "../overview.md", "..\\overview.md", "../../../secret.md", "missing.md", path.join(base, "secret.md"), "", null];
for (const name of refused) assert.equal(await readPrototype(root, SPEC, name), null, String(name));
assert.equal(await readPrototype(root, "../", "secret.md"), null);
} finally {
clean(base);
}
});
test("prototypeHtmlPath names one .html in prototypes/ and refuses everything else", async () => {
const { base, root } = project({
[`${SPEC}/prototypes/picker.md`]: "# Picker\n",
[`${SPEC}/prototypes/picker.html`]: "<p>picker</p>\n",
});
try {
const found = await prototypeHtmlPath(root, SPEC, "picker.html");
assert.equal(found, fs.realpathSync(path.join(root, SPEC, "prototypes", "picker.html")));
const refused = ["picker.md", "../../../secret.html", "missing.html", path.join(base, "secret.html"), "picker.html.exe", "", undefined];
for (const name of refused) assert.equal(await prototypeHtmlPath(root, SPEC, name), null, String(name));
} finally {
clean(base);
}
});
test("prototype files do not follow a link out of the project", async (t) => {
const { base, root } = project({ [`${SPEC}/prototypes/picker.md`]: "# Picker\n" });
try {
try {
fs.symlinkSync(path.join(base, "secret.md"), path.join(root, SPEC, "prototypes", "leak.md"));
fs.symlinkSync(path.join(base, "secret.html"), path.join(root, SPEC, "prototypes", "leak.html"));
} catch {
return t.skip("symlinks are not allowed here");
}
assert.equal(await readPrototype(root, SPEC, "leak.md"), null);
assert.equal(await prototypeHtmlPath(root, SPEC, "leak.html"), null);
assert.deepEqual(
(await listPrototypes(root, SPEC)).map((f) => f.name),
["picker.md"]
);
} finally {
clean(base);
}
});
+65
View File
@@ -765,6 +765,71 @@ test("GET /overview serves the session's spec, and on the dashboard only a scann
}
});
// The Prototypes tab's routes: the list, one .md, and opening one .html in the
// default browser (PLAN2CODE_NO_BROWSER keeps the suite from starting one).
// The spec is chosen as /overview chooses it, and nothing outside the spec's
// prototypes/ folder is read or opened.
test("the Prototypes routes list, read and open only a scanned spec's prototypes", async () => {
const { base, root } = overviewProject();
const protos = path.join(root, "specs", "p", "prototypes");
fs.mkdirSync(protos, { recursive: true });
fs.writeFileSync(path.join(protos, "pick.md"), "# Pick\n");
fs.writeFileSync(path.join(protos, "pick.html"), "<p>pick</p>\n");
const sids = [];
try {
const dash = cliIn(root, ["open", "--workflow", "dashboard", "--no-open"]);
sids.push(dash.json.sid);
const d = sessionUrl(dash.json.url);
const get = (q, cookie = d.cookie) =>
fetch(`${d.base}${q}`, { headers: cookie ? { cookie, connection: "close" } : { connection: "close" } });
const open = (body, origin = d.base) =>
fetch(`${d.base}/open-prototype`, {
method: "POST",
headers: { "content-type": "application/json", cookie: d.cookie, origin, connection: "close" },
body: JSON.stringify(body),
});
const list = await get("/prototypes?spec=specs/p");
assert.equal(list.status, 200);
assert.deepEqual(await list.json(), { files: [{ name: "pick.html", kind: "html" }, { name: "pick.md", kind: "md" }] });
assert.equal((await get("/prototypes?spec=specs/p", null)).status, 403);
assert.equal((await get("/prototypes?spec=specs/a")).status, 204, "no prototypes folder: no tab");
assert.equal((await get("/prototypes?spec=specs/not-scanned")).status, 204);
const doc = await get("/prototype?spec=specs/p&name=pick.md");
assert.equal(doc.status, 200);
assert.equal(await doc.text(), "# Pick\n");
for (const name of ["pick.html", "../../a/overview.md", "missing.md"]) {
assert.equal((await get("/prototype?spec=specs/p&name=" + encodeURIComponent(name))).status, 204, name);
}
const ok = await open({ spec: "specs/p", name: "pick.html" });
assert.equal(ok.status, 200);
assert.deepEqual(await ok.json(), { ok: true });
for (const body of [
{ spec: "specs/p", name: "pick.md" },
{ spec: "specs/p", name: "../pick.html" },
{ spec: "specs/p", name: path.join(protos, "pick.html") },
{ spec: "specs/not-scanned", name: "pick.html" },
{ spec: "specs/p" },
]) {
assert.equal((await open(body)).status, 404, JSON.stringify(body));
}
assert.notEqual((await open({ spec: "specs/p", name: "pick.html" }, "https://evil.example")).status, 200, "another site's page cannot open files");
// Away from the dashboard, the session's own spec is used and ?spec= is ignored.
const plan = cliIn(root, ["open", "--workflow", "plan", "--spec", "specs/p", "--no-open"]);
sids.push(plan.json.sid);
const p = sessionUrl(plan.json.url);
const own = await fetch(`${p.base}/prototypes?spec=specs/a`, { headers: { cookie: p.cookie, connection: "close" } });
assert.equal(own.status, 200);
assert.equal((await own.json()).files.length, 2);
} finally {
for (const s of sids) cliIn(root, ["stop", "--session", s, "--force"]);
fs.rmSync(base, { recursive: true, force: true });
}
});
test("open records worktree and branch, and wakes only a freshly opened dashboard", () => {
const dash = cli(["open", "--workflow", "dashboard", "--no-open"]);
try {
+2 -2
View File
@@ -188,11 +188,11 @@ test("shape: no page module sets a style attribute from script", () => {
// and WORKSPACE_EDITS for /workspace/add, /edit and /remove. The git board has
// no routes of its own: its buttons are sends (`__git`), through /submit.
const ROUTE_ORDER = [
"/health", "/s/", "/handover", "/", "SERVABLE", "/overview", "/agents-doc", "/handoff", "/version", "/state", "/events", "/draft",
"/health", "/s/", "/handover", "/", "SERVABLE", "/overview", "/agents-doc", "/prototypes", "/prototype", "/handoff", "/version", "/state", "/events", "/draft",
"/looks", "/looks", "/subagents", "/pr-host", "/models", "/models", "/agents", "/agents", "/agents/test", "/agents/use-path", "/cleanup", "/upload",
"/notepad", "/notepad/text", "/notepad/image", "/notepad/thumb", "/notepad/image/delete", "/notepad/tab",
"/notepad/tab/rename", "/notepad/tab/delete", "/notepad/tab/order", "/notepad/tab/last", "/notepad/tab/sent", "/notepad/images/",
"/attachments/", "/shots/", "/uploads/", "/submit", "/open-link", "/chat", "/chat/decision", "/workspace",
"/attachments/", "/shots/", "/uploads/", "/submit", "/open-link", "/open-prototype", "/chat", "/chat/decision", "/workspace",
"WORKSPACE_EDITS", "/workspace/scope", "/workspace/browse", "/loop-setup", "/loop-setup", "/spec-carry", "/cancel", "/ping", "/bye",
];