Public: anyone can read this; the crew can change it.
The New forge form and the terminal's new-forge screen offer the size, and an unknown size is shown beside the field #5676
scout/forge:scout/2996-the-new-forge-form-and-the-terminal-s-ne → main 2 commits fork 3 threads · 3 unresolved
3 files
changed +146
−1
src/tui/screens/newforge.ts +2
| @@ -34,6 +34,7 @@ const SPECS: readonly Spec[] = [ | ||
| 34 | 34 | { key: "modelEndpoint", label: "API base URL", hint: "A gateway for the key; empty for Anthropic. Optional." }, |
| 35 | 35 | { key: "budgetUsd", label: "Budget in dollars", hint: `${MIN_BUDGET_USD}–${MAX_BUDGET_USD}, for the key's whole life. Needed with a key.` }, |
| 36 | 36 | { key: "followMode", label: "Follows this forge's code", hint: "tell-me (default) or take-updates. Recorded for now: nothing tells you or takes updates yet." }, |
| 37 | { key: "size", label: "Size", hint: "small (default), medium or large." }, | |
| 37 | 38 | ]; |
| 38 | 39 | |
| 39 | 40 | /** The door's "field: reason" refusal, as the field it names and the reason. */ |
| @@ -59,6 +60,7 @@ export function newForgeScreen(f: Session, ctx: Ctx): Screen { | ||
| 59 | 60 | name: val("name"), |
| 60 | 61 | apps, |
| 61 | 62 | ...(val("fresh") === "" ? {} : { fresh: val("fresh") }), |
| 63 | ...(val("size") === "" ? {} : { size: val("size") }), | |
| 62 | 64 | followMode: val("followMode") === "" ? "tell-me" : val("followMode"), |
| 63 | 65 | ...(val("for") === "" ? {} : { for: val("for") }), |
| 64 | 66 | ...(val("direction") === "" ? {} : { direction: val("direction") }), |
src/web/pages/new-forge.ts +25 −1
| @@ -26,7 +26,8 @@ import { notFoundPage, pageHead } from "../chrome.ts"; | ||
| 26 | 26 | import { field, readForm, type Form } from "../form.ts"; |
| 27 | 27 | import { html, type Html } from "../html.ts"; |
| 28 | 28 | import { page } from "../layout.ts"; |
| 29 | import { btn, card, field as formField, row } from "../ui.ts"; | |
| 29 | import { btn, card, field as formField, row, selectField } from "../ui.ts"; | |
| 30 | import { DEFAULT_SIZE, SIZES, isSize, type Size } from "../../spawn/sizes.ts"; | |
| 30 | 31 | |
| 31 | 32 | interface Values { |
| 32 | 33 | readonly name: string; |
| @@ -39,6 +40,7 @@ interface Values { | ||
| 39 | 40 | readonly keepAccess: boolean; |
| 40 | 41 | readonly budgetUsd: string; |
| 41 | 42 | readonly modelEndpoint: string; |
| 43 | readonly size: string; | |
| 42 | 44 | /** The last post carried a key: it is never sent back, so it is asked for again. */ |
| 43 | 45 | readonly hadKey: boolean; |
| 44 | 46 | } |
| @@ -54,6 +56,7 @@ const EMPTY: Values = { | ||
| 54 | 56 | keepAccess: false, |
| 55 | 57 | budgetUsd: "", |
| 56 | 58 | modelEndpoint: "", |
| 59 | size: DEFAULT_SIZE, | |
| 57 | 60 | hadKey: false, |
| 58 | 61 | }; |
| 59 | 62 | |
| @@ -117,6 +120,24 @@ function readsFor(ctx: Ctx, user: User | undefined): Reads { | ||
| 117 | 120 | return { full, repos, crew }; |
| 118 | 121 | } |
| 119 | 122 | |
| 123 | /** The size choice: `selectField` as it is, its error a sibling span beside it. */ | |
| 124 | function sizeField(chosen: string, error: string | undefined): Html { | |
| 125 | const at: Size = isSize(chosen) ? chosen : DEFAULT_SIZE; | |
| 126 | return html`<div ${error ? html`data-invalid` : null}> | |
| 127 | ${selectField({ | |
| 128 | label: "Size (optional)", | |
| 129 | name: "size", | |
| 130 | hint: "How much room it gets. Small is enough to start.", | |
| 131 | options: (Object.keys(SIZES) as Size[]).map((size) => ({ | |
| 132 | value: size, | |
| 133 | label: size[0]!.toUpperCase() + size.slice(1), | |
| 134 | selected: size === at, | |
| 135 | })), | |
| 136 | })} | |
| 137 | ${error ? html`<span class="error" id="f-size-err">${error}</span>` : null} | |
| 138 | </div>`; | |
| 139 | } | |
| 140 | ||
| 120 | 141 | function formView( |
| 121 | 142 | ctx: Ctx, |
| 122 | 143 | user: User | undefined, |
| @@ -290,6 +311,7 @@ function formView( | ||
| 290 | 311 | hint: "For the key's whole life; the child cannot raise it. Needed with a key.", |
| 291 | 312 | ...(errors["budgetUsd"] ? { error: errors["budgetUsd"] } : {}), |
| 292 | 313 | })} |
| 314 | ${sizeField(values.size, errors["size"])} | |
| 293 | 315 | <p class="muted small"> |
| 294 | 316 | Never copied: ${LEFT_BEHIND.join(", ")}. The child mints its own. |
| 295 | 317 | </p> |
| @@ -421,6 +443,7 @@ export async function newForgeSubmit(ctx: Ctx): Promise<Response> { | ||
| 421 | 443 | keepAccess: asBool(form, "keepAccess"), |
| 422 | 444 | budgetUsd: field(form, "budgetUsd").trim(), |
| 423 | 445 | modelEndpoint: field(form, "modelEndpoint").trim(), |
| 446 | size: field(form, "size").trim() || DEFAULT_SIZE, | |
| 424 | 447 | hadKey: field(form, "modelKey").trim() !== "", |
| 425 | 448 | }; |
| 426 | 449 | const key = field(form, "modelKey").trim(); |
| @@ -437,6 +460,7 @@ export async function newForgeSubmit(ctx: Ctx): Promise<Response> { | ||
| 437 | 460 | ...(key === "" ? {} : { modelKey: key }), |
| 438 | 461 | ...(values.modelEndpoint === "" ? {} : { modelEndpoint: values.modelEndpoint }), |
| 439 | 462 | ...(values.budgetUsd === "" ? {} : { budgetUsd: Number(values.budgetUsd) }), |
| 463 | size: values.size, | |
This sends size: "small" on every post, so every plan the page makes now carries a size. Omit it when it is the default, to match the 'recorded only when given' rule. |
||
| 440 | 464 | dryRun: planOnly, |
| 441 | 465 | }); |
| 442 | 466 | if (door.status >= 400) |
test/new-forge-size.test.ts added +119
| @@ -0,0 +1,119 @@ | ||
| 1 | /** | |
| 2 | * The size on /forges/new (#2996): offered, small preselected; an unknown one | |
| 3 | * is refused beside its field with the rest kept; a chosen one reaches | |
| 4 | * `parent.json`. | |
| 5 | */ | |
| 6 | ||
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, readdir, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { childDirOf } from "../src/actions/spawn.ts"; | |
| 13 | import { createApp, type App } from "../src/app.ts"; | |
| 14 | import { createUser } from "../src/core/users.ts"; | |
| 15 | import { commitFiles } from "../src/git/write.ts"; | |
| 16 | import { repoDir } from "../src/git/repos.ts"; | |
| 17 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 18 | import { quietLogs } from "./helpers/globals.ts"; | |
| 19 | ||
| 20 | quietLogs(); | |
| 21 | ||
| 22 | const PASSWORD = "a-long-new-forge-size-passphrase"; | |
| 23 | ||
| 24 | describe("the size on the New forge form", () => { | |
| 25 | let dir = ""; | |
| 26 | let app: App; | |
| 27 | let servers: Servers; | |
| 28 | let cookie = ""; | |
| 29 | ||
| 30 | const post = (form: [string, string][]) => | |
| 31 | fetch(`${servers.url}/forges/new`, { | |
| 32 | method: "POST", | |
| 33 | headers: { cookie, origin: "https://forge.test" }, | |
| 34 | body: new URLSearchParams(form), | |
| 35 | redirect: "manual", | |
| 36 | }); | |
| 37 | ||
| 38 | beforeAll(async () => { | |
| 39 | dir = await mkdtemp(join(tmpdir(), "forge-new-forge-size-")); | |
| 40 | app = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); | |
| 41 | const a = await createUser(app.db, { | |
| 42 | origin: "seed", | |
| 43 | trusted: true, | |
| 44 | username: "alice", | |
| 45 | email: "[email protected]", | |
| 46 | password: PASSWORD, | |
| 47 | isAdmin: true, | |
| 48 | }); | |
| 49 | if (!a.ok) throw new Error("user"); | |
| 50 | const r = await createRepo(app, a.value, { name: "bakery" }); | |
| 51 | if (!r.ok) throw new Error("repo"); | |
| 52 | const c = await commitFiles(repoDir(app.reposRoot, "alice", "bakery"), { | |
| 53 | branch: "main", | |
| 54 | message: "bakery", | |
| 55 | files: [{ path: "README.md", content: "# bakery\n" }], | |
| 56 | author: { name: "a", email: "[email protected]" }, | |
| 57 | }); | |
| 58 | if (!c.ok) throw new Error("commit"); | |
| 59 | servers = await serve(app); | |
| 60 | const res = await fetch(`${servers.url}/signin`, { | |
| 61 | method: "POST", | |
| 62 | body: new URLSearchParams({ username: "alice", password: PASSWORD }), | |
| 63 | redirect: "manual", | |
| 64 | }); | |
| 65 | cookie = (res.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 66 | }, 30_000); | |
| 67 | ||
| 68 | afterAll(async () => { | |
| 69 | servers.stop(); | |
| 70 | app.close(); | |
| 71 | await rm(dir, { recursive: true, force: true }); | |
| 72 | }); | |
| 73 | ||
| 74 | test("the form offers small, medium and large, small preselected", async () => { | |
| 75 | const html = (await (await fetch(`${servers.url}/forges/new`, { headers: { cookie } })).text()).replace(/\s+/g, " "); | |
| 76 | expect(html).toMatch(/<select[^>]*name="size"/); | |
| 77 | expect(html).toMatch(/<option value="small" selected\s*>/); | |
| 78 | expect(html).toMatch(/<option value="medium"\s*>/); | |
| 79 | expect(html).toMatch(/<option value="large"\s*>/); | |
| 80 | }); | |
| 81 | ||
| 82 | test("an unknown size is a 422 with the message beside the field and the rest kept", async () => { | |
| 83 | const res = await post([ | |
| 84 | ["name", "kai"], | |
| 85 | ["apps", "alice/bakery"], | |
| 86 | ["direction", "Run Kai's shop."], | |
| 87 | ["size", "huge"], | |
| 88 | ["do", "spawn"], | |
| 89 | ]); | |
| 90 | expect(res.status).toBe(422); | |
| 91 | const html = await res.text(); | |
| 92 | expect(html).toMatch(/id="f-size-err">[^<]*size must be small, medium or large/); | |
| 93 | expect(html).toContain('value="kai"'); | |
| 94 | expect(html).toContain("Run Kai's shop."); | |
| 95 | expect(html).toMatch(/value="alice\/bakery"[^>]*checked|checked[^>]*value="alice\/bakery"/); | |
| 96 | expect(await readdir(join(dir, "spawn")).catch(() => [])).toEqual([]); | |
| 97 | }); | |
| 98 | ||
| 99 | test("a chosen size reaches parent.json", async () => { | |
| 100 | const res = await post([ | |
| 101 | ["name", "kai"], | |
| 102 | ["apps", "alice/bakery"], | |
| 103 | ["size", "large"], | |
| 104 | ["do", "spawn"], | |
| 105 | ]); | |
| 106 | expect(res.status).toBe(201); | |
| 107 | const ledger = await Bun.file(join(dir, "spawn", "kai.json")).json(); | |
| 108 | const meta = await Bun.file(join(childDirOf(app, ledger.bodyId), "parent.json")).json(); | |
| 109 | expect(meta.size).toBe("large"); | |
| 110 | }); | |
| 111 | }); | |
| 112 | ||
| 113 | describe("the size on the terminal's new-forge screen", () => { | |
| 114 | test("it is a field, and the three sizes are named in its hint", async () => { | |
| 115 | const src = await Bun.file(join(import.meta.dir, "../src/tui/screens/newforge.ts")).text(); | |
Grepping the source is not a behaviour test; drive the terminal screen and assert the size reaches the plan. |
||
| 116 | expect(src).toContain('key: "size"'); | |
| 117 | expect(src).toMatch(/small[^"]*medium[^"]*large/); | |
| 118 | }); | |
| 119 | }); |
new above
offline
The error span is not associated with the select (no aria-describedby or aria-invalid), so a screen reader never hears it. Link them.