Public: anyone can read this; the crew can change it.
The New forge page offers the built-in templates as radio choices, defaulting to today's behaviour, server-rendered with no script #14147
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14122-the-new-forge-page-offers-the-built-in-t → main 6 commits fork 2 threads · 2 unresolved
7 files
changed +107
−20
docs/features/new-forge-template.md +4
| @@ -31,3 +31,7 @@ The terminal has the same list: the "Templates" command (`src/tui/screens/templa | ||
| 31 | 31 | ## One template's page: /templates/<name> |
| 32 | 32 | |
| 33 | 33 | `/templates/<id>` (`/templates/starter`, `/templates/owner/name`) is a page to share: Open Graph and Twitter meta (title, the one-line sentence, and the card `/og/template/<id>.png`, `owner~name` for a repository), and one plain link, "Start a forge from <name>", to `/forges/new?template=<id>`. The card is the existing renderer and its cached PNG path, drawn only from what a signed-out reader is offered. A template not listed to the reader, or unknown, answers 404 alike. The terminal's Templates screen prints the same path under the row as "Share:". Code: `templatePage` in `src/web/pages/template.ts`; the id and card-ref pair in `src/spawn/template_list.ts`. Test: `test/template-share.test.ts`. |
| 34 | ||
| 35 | ## Choosing on the page: radios | |
| 36 | ||
| 37 | The New forge page lists the built-in templates as radio buttons, Blank checked, and needs no script; `/forges/new?template=starter` opens the form with Starter app checked. Pick one and press Make. In the terminal the New forge screen cycles the same choices with left and right, so both doors offer the same list. Code: `src/web/pages/new-forge.ts`. Test: `test/new-forge-template.test.ts`. |
src/web/pages/new-forge.ts +13 −10
| @@ -90,7 +90,7 @@ const EMPTY: Values = { | ||
| 90 | 90 | }; |
| 91 | 91 | |
| 92 | 92 | /** The fields behind "More options": an error on one opens it. */ |
| 93 | const MORE_FIELDS = ["followMode", "crew", "keepAccess", "migration", "modelKey", "modelEndpoint", "budgetUsd", "template", "size"]; | |
| 93 | const MORE_FIELDS = ["followMode", "crew", "keepAccess", "migration", "modelKey", "modelEndpoint", "budgetUsd", "size"]; | |
| 94 | 94 | |
| 95 | 95 | type Errors = Partial<Record<string, string>>; |
| 96 | 96 | |
| @@ -166,7 +166,7 @@ function templateOptions(chosen: string, repos: readonly string[]): { value: str | ||
| 166 | 166 | return [ |
| 167 | 167 | ...TEMPLATES.map((t) => ({ |
| 168 | 168 | value: t.id, |
| 169 | label: t.id === DEFAULT_TEMPLATE ? `The forge's own code: ${lower(t.says)}` : `${t.title}: ${lower(t.says)}`, | |
| 169 | label: `${t.title}: ${lower(t.says)}`, | |
| 170 | 170 | selected: t.id === at, |
| 171 | 171 | })), |
| 172 | 172 | ...repos.map((ref) => ({ value: ref, label: `${ref}: ${lower(templateOf(ref).says)}`, selected: ref === at })), |
| @@ -303,6 +303,17 @@ function formView( | ||
| 303 | 303 | ...(freshErr ? { error: freshErr } : {}), |
| 304 | 304 | })} |
| 305 | 305 | ${modeRows(values, errors["modes"])} |
| 306 | <fieldset class="field" id="f-template" aria-describedby="f-template-hint${errors["template"] ? " f-template-err" : ""}" ${errors["template"] ? html`data-invalid` : null}> | |
| 307 | <legend>Template</legend> | |
| 308 | ${templateOptions(values.template, templates).map( | |
| 309 | (o) => html`<label class="check"> | |
| 310 | <input type="radio" name="template" value="${o.value}" ${o.selected ? html`checked` : null} /> | |
| 311 | <span>${o.label}</span> | |
| 312 | </label>`, | |
| 313 | )} | |
| 314 | <span class="muted small" id="f-template-hint">What the new forge starts from besides the apps you fork. <a href="/templates">Templates</a></span> | |
outdated
Principle 1: the hint explains the default a second time and the Templates link is crammed into it. Say it once: "What the new forge starts from besides the apps you fork." and drop "Blank is the default" once the label says Blank. Also give the span an id and set aria-describedby on the fieldset (plus the f-template-err id on error) so a screen reader hears it with the group. |
||
| 315 | ${errors["template"] ? html`<span class="error" id="f-template-err">${errors["template"]}</span>` : null} | |
| 316 | </fieldset> | |
| 306 | 317 | <details class="disclosure" ${Object.keys(errors).some((k) => MORE_FIELDS.includes(k)) ? html`open` : null}> |
| 307 | 318 | <summary>More options</summary> |
| 308 | 319 | <fieldset class="field" ${errors["followMode"] ? html`data-invalid` : null}> |
| @@ -397,14 +408,6 @@ function formView( | ||
| 397 | 408 | hint: "For the key's whole life; the new forge cannot raise it. Needed with a key.", |
| 398 | 409 | ...(errors["budgetUsd"] ? { error: errors["budgetUsd"] } : {}), |
| 399 | 410 | })} |
| 400 | ${selectField({ | |
| 401 | label: "Template", | |
| 402 | name: "template", | |
| 403 | hint: "What the new forge starts from besides the apps you fork. Blank is the default.", | |
| 404 | options: templateOptions(values.template, templates), | |
| 405 | ...(errors["template"] ? { error: errors["template"] } : {}), | |
| 406 | })} | |
| 407 | <p class="muted small"><a href="/templates">Templates</a></p> | |
| 408 | 411 | ${selectField({ |
| 409 | 412 | label: "Size (optional)", |
| 410 | 413 | name: "size", |
test/new-forge-template.test.ts added +78
| @@ -0,0 +1,78 @@ | ||
| 1 | /** | |
| 2 | * The New forge page offers each built-in template as a radio, server-rendered: | |
| 3 | * Blank is checked, `?template=` preselects a valid id and is never echoed | |
| 4 | * raw, and a choice posted through the form reaches the boundary. | |
| 5 | */ | |
| 6 | ||
| 7 | import { afterAll, beforeAll, describe, expect, spyOn, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 14 | import * as door from "../src/spawn/door.ts"; | |
| 15 | import { TEMPLATES } from "../src/spawn/templates.ts"; | |
| 16 | import { quietLogs } from "./helpers/globals.ts"; | |
| 17 | ||
| 18 | quietLogs(); | |
| 19 | ||
| 20 | const PASSWORD = "a-long-new-forge-passphrase"; | |
| 21 | ||
| 22 | describe("New forge template radios", () => { | |
| 23 | let dir = ""; | |
| 24 | let app: App; | |
| 25 | let servers: Servers; | |
| 26 | let cookie = ""; | |
| 27 | ||
| 28 | const page = async (q = "") => | |
| 29 | (await (await fetch(`${servers.url}/forges/new${q}`, { headers: { cookie } })).text()).replace(/\s+/g, " "); | |
| 30 | const radios = (html: string) => [...html.matchAll(/<input type="radio" name="template" value="([^"]+)" (checked)?/g)]; | |
| 31 | ||
| 32 | beforeAll(async () => { | |
| 33 | dir = await mkdtemp(join(tmpdir(), "forge-new-forge-template-")); | |
| 34 | app = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); | |
| 35 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: PASSWORD, isAdmin: true }); | |
| 36 | if (!a.ok) throw new Error("user"); | |
| 37 | servers = await serve(app); | |
| 38 | const res = await fetch(`${servers.url}/signin`, { method: "POST", body: new URLSearchParams({ username: "alice", password: PASSWORD }), redirect: "manual" }); | |
| 39 | cookie = (res.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 40 | }, 30_000); | |
| 41 | ||
| 42 | afterAll(async () => { | |
| 43 | servers.stop(); | |
| 44 | app.close(); | |
| 45 | await rm(dir, { recursive: true, force: true }); | |
| 46 | }); | |
| 47 | ||
| 48 | test("one radio per built-in template with its sentence, Blank checked", async () => { | |
| 49 | const html = await page(); | |
| 50 | const found = radios(html); | |
| 51 | expect(found.map((m) => m[1])).toEqual(TEMPLATES.map((t) => t.id)); | |
| 52 | expect(found.filter((m) => m[2]).map((m) => m[1])).toEqual(["blank"]); | |
| 53 | for (const t of TEMPLATES) expect(html).toContain(t.says.slice(1).replace(/'/g, "'")); | |
| 54 | expect(html).not.toMatch(/<select[^>]*name="template"/); | |
| 55 | }); | |
| 56 | ||
| 57 | test("?template= preselects a valid id; an unknown one is ignored and never echoed", async () => { | |
| 58 | expect(radios(await page("?template=starter")).filter((m) => m[2]).map((m) => m[1])).toEqual(["starter"]); | |
| 59 | const evil = "<script>alert(1)</script>"; | |
| 60 | const html = await page(`?template=${encodeURIComponent(evil)}`); | |
| 61 | expect(radios(html).filter((m) => m[2]).map((m) => m[1])).toEqual(["blank"]); | |
| 62 | expect(html).not.toContain(evil); | |
| 63 | expect(html).not.toContain("alert(1)"); | |
| 64 | }); | |
| 65 | ||
| 66 | test("the choice posted through the form reaches the boundary", async () => { | |
| 67 | const spy = spyOn(door, "spawnDoor"); | |
| 68 | try { | |
| 69 | const form = new URLSearchParams({ name: "tpl", do: "plan", template: "starter" }); | |
| 70 | const res = await fetch(`${servers.url}/forges/new`, { method: "POST", headers: { cookie, origin: "https://forge.test" }, body: form }); | |
| 71 | expect(res.status).toBe(200); | |
| 72 | expect(spy.mock.calls.at(-1)![2]["template"]).toBe("starter"); | |
| 73 | expect(radios((await res.text()).replace(/\s+/g, " ")).filter((m) => m[2]).map((m) => m[1])).toEqual(["starter"]); | |
| 74 | } finally { | |
| 75 | spy.mockRestore(); | |
| 76 | } | |
| 77 | }); | |
| 78 | }); |
test/new-forge.test.ts +7 −5
| @@ -135,10 +135,12 @@ describe("+ New forge", () => { | ||
| 135 | 135 | |
| 136 | 136 | test("the Template field lists Blank first and selected; starter reaches the plan from the door's own body; an unknown id is refused on the field", async () => { |
| 137 | 137 | const page = (await (await get(admin, "/forges/new")).text()).replace(/\s+/g, " "); |
| 138 | const at = page.indexOf('name="template"'); | |
| 138 | const at = page.indexOf('<fieldset class="field" id="f-template"'); | |
| 139 | 139 | expect(at).toBeGreaterThan(0); |
| 140 | const field = page.slice(at, page.indexOf("</select>", at)); | |
| 141 | expect(field).toMatch(/<option value="blank" selected >\s*The forge's own code/); | |
| 140 | const field = page.slice(at, page.indexOf("</fieldset>", at)); | |
| 141 | expect(field).toMatch(/value="blank" checked/); | |
| 142 | expect(field).toContain("Blank: only the apps"); | |
| 143 | expect(field).toContain(`aria-describedby="f-template-hint"`); | |
| 142 | 144 | expect(field.indexOf('value="blank"')).toBeLessThan(field.indexOf('value="starter"')); |
| 143 | 145 | expect(field).toContain("Starter app"); |
| 144 | 146 | |
| @@ -146,7 +148,7 @@ describe("+ New forge", () => { | ||
| 146 | 148 | expect(plan.status).toBe(200); |
| 147 | 149 | const planned = (await plan.text()).replace(/\s+/g, " "); |
| 148 | 150 | expect(planned).toContain("starts from the <b>Starter app</b> template"); |
| 149 | expect(planned).toMatch(/<option value="starter" selected >/); | |
| 151 | expect(planned).toMatch(/value="starter" checked/); | |
| 150 | 152 | |
| 151 | 153 | const blank = await post(admin, [["name", "tpl"], ["do", "plan"], ["template", "blank"]]); |
| 152 | 154 | expect(await blank.text()).not.toContain("template.</li>"); |
| @@ -413,7 +415,7 @@ describe("/forges/new: More options (#9647)", () => { | ||
| 413 | 415 | const form = html.slice(html.indexOf('id="new-forge"')); |
| 414 | 416 | const more = form.slice(form.indexOf("<summary>More options</summary>"), form.indexOf("</details>")); |
| 415 | 417 | expect(more.length).toBeGreaterThan(0); |
| 416 | for (const name of ["followMode", "keepAccess", "modelKey", "modelEndpoint", "budgetUsd", "template", "size"]) | |
| 418 | for (const name of ["followMode", "keepAccess", "modelKey", "modelEndpoint", "budgetUsd", "size"]) | |
| 417 | 419 | expect(more).toContain(`name="${name}"`); |
| 418 | 420 | expect(form.indexOf("</details>")).toBeLessThan(form.indexOf("</form>")); |
| 419 | 421 | // The visible fields stay out of it. |
test/spawn-template.test.ts +1 −1
| @@ -139,7 +139,7 @@ describe("a new forge from a template repository", () => { | ||
| 139 | 139 | { |
| 140 | 140 | const cookie = `forge_session=${createSession(app.db, bob.id).token}`; |
| 141 | 141 | const html = await (await fetch(`${servers.url}/forges/new`, { headers: { cookie } })).text(); |
| 142 | expect(html.match(/<select[^>]*name="template"/g)?.length).toBe(1); | |
| 142 | expect(html.match(/<input type="radio" name="template" value="alice\/kit"/g)?.length).toBe(1); | |
| 143 | 143 | expect(html).toContain('value="alice/kit"'); |
| 144 | 144 | expect(html).not.toContain('value="alice/plain"'); |
| 145 | 145 | expect(html).not.toContain('value="alice/secret"'); |
test/template-unknown.test.ts +1 −1
| @@ -90,6 +90,6 @@ describe("unknown template", () => { | ||
| 90 | 90 | |
| 91 | 91 | test("a known id in the link preselects it", async () => { |
| 92 | 92 | const html = await (await fetch(`${base}/forges/new?template=starter`)).text(); |
| 93 | expect(html).toMatch(/value="starter"\s+selected/); | |
| 93 | expect(html.replace(/\s+/g, " ")).toMatch(/value="starter" checked/); | |
| 94 | 94 | }); |
| 95 | 95 | }); |
test/templates-page.test.ts +3 −3
| @@ -41,10 +41,10 @@ test("signed out: one row and one button per template", async () => { | ||
| 41 | 41 | |
| 42 | 42 | test("the button lands on the form with the template chosen; junk is not echoed", async () => { |
| 43 | 43 | const ok = await (await fetch(`${servers.url}/forges/new?template=starter`)).text(); |
| 44 | expect(ok).toMatch(/<option\s+value="starter"\s+selected/); | |
| 44 | expect(ok).toMatch(/value="starter"\s+checked/); | |
| 45 | 45 | const junk = await (await fetch(`${servers.url}/forges/new?template=%3Cscript%3Ex`)).text(); |
| 46 | 46 | expect(junk).not.toContain("<script>x"); |
| 47 | expect(junk).toMatch(/<option\s+value="blank"\s+selected/); | |
| 47 | expect(junk).toMatch(/value="blank"\s+checked/); | |
| 48 | 48 | }); |
| 49 | 49 | |
| 50 | 50 | test("posting the chosen template reads it in the plan (dry run)", async () => { |
| @@ -95,7 +95,7 @@ test("a stranger who stumbles on the form keeps the template; the refusal keeps | ||
| 95 | 95 | body: new URLSearchParams({ name: "Bad Name!", template: "starter" }), |
| 96 | 96 | }); |
| 97 | 97 | expect(res.status).toBeGreaterThanOrEqual(400); |
| 98 | expect(await res.text()).toMatch(/<option\s+value="starter"\s+selected/); | |
| 98 | expect(await res.text()).toMatch(/value="starter"\s+checked/); | |
| 99 | 99 | }); |
| 100 | 100 | |
| 101 | 101 | test("the starter's files and run lines are dl rows; blank says it holds none", async () => { |
new above
offline
Principle 8 (plain words): the label for the default reads "The forge's own code: ..." and never says Blank, while the hint below says Blank is the default. Lead each label with the template name (Blank: the forge's own code ...) so the radio and the hint use the same word. Fix in templateOptions or here.