Public: anyone can read this; the crew can change it.
/templates lists the templates and links each to making a forge from it #13282
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13266-templates-lists-the-templates-and-links → main 10 commits fork 3 threads · 3 unresolved
docs/design/audit.md +1
| @@ -52,6 +52,7 @@ Machine routes are not pages and have no entry: `/audit.csv`, | ||
| 52 | 52 | - `/pricing` — `pages/pricing.ts` · the plans and what each brings, and the sale: pick a plan and a name, then pay; the page the no-credit line points to · `/pricing/done`, `/settings/billing`, that line, `/` · none seen. |
| 53 | 53 | - `/pricing/done` — `pages/pricing.ts` (`pricingDone`) · after payment, the claim link, shown once · `/pricing`, `/pricing/resend` · none seen. |
| 54 | 54 | - `/pricing/resend` — `pages/pricing.ts` (`resendPage`) · a lost claim link sent again to the buyer's email · `/pricing` · none seen. |
| 55 | - `/templates` — `pages/templates.ts` · the templates a new forge starts from, one button each to New forge with it chosen, public · `/forges/new` · none seen. | |
| 55 | 56 | - `/terms` — `pages/terms.ts` · what a paid forge is, in plain words, from the billing constants · `/pricing`, `/signup` · none seen. |
| 56 | 57 | - `/explore` — `studio/explore.ts` · browse what the forge holds · `/search`, `/apps` · colours 2. |
| 57 | 58 | - `/board` — `pages/board.ts` (1,231 lines) · every app the viewer can see, with a wall mode · `/boards` (saved queries as panels), `/flow`, `/` · colour 1, sizes 6. |
docs/features/new-forge-template.md +6
| @@ -5,3 +5,9 @@ A new forge can start from a template repository: one an admin marked at Setting | ||
| 5 | 5 | The template's code becomes the child's own `forge` repository. It keeps the template's visibility: a private template never becomes a public repository in the child. |
| 6 | 6 | |
| 7 | 7 | Code: `src/spawn/templates.ts` (ids and words), `src/spawn/forge_code.ts` (the fork), `src/actions/spawn.ts` (the checks). |
| 8 | ||
| 9 | ## The list: /templates | |
| 10 | ||
| 11 | `/templates` lists what a new forge can start from: the built-in templates, then the template repositories you can read. Each row has one sentence and one secondary button, "Start a forge from <name>", which opens `/forges/new?template=<id>` with that template chosen. Signed out works too: a stranger lands on the same form with the template kept. `?template=` is accepted only when spawn accepts it (`parseTemplate`); anything else leaves Blank chosen, and it is never echoed. The plan (`do=plan`) shows "It starts from the <b>…</b> template". | |
| 12 | ||
| 13 | The terminal has the same list: the "Templates" command (`src/tui/screens/templates.ts`) shows the same rows; Enter on a row opens New forge with that template chosen, for people signed in, and a guest is told to sign in. Both read `templateList` (`src/spawn/template_list.ts`), so a private template repository is listed only to people who can read it, on the page and in the terminal alike. |
src/core/reserved.ts +1
| @@ -39,6 +39,7 @@ export const SYSTEM_NAMES: readonly string[] = [ | ||
| 39 | 39 | "terms", |
| 40 | 40 | "security", |
| 41 | 41 | "pricing", |
| 42 | "templates", | |
| 42 | 43 | "bot", |
| 43 | 44 | "steward", |
| 44 | 45 | "anonymous", |
src/http/route_marks.ts +1
| @@ -165,6 +165,7 @@ export const HEADS: Readonly<Record<string, Mark | "mixed">> = { | ||
| 165 | 165 | "/privacy": "open", |
| 166 | 166 | "/terms": "open", |
| 167 | 167 | "/security": "open", |
| 168 | "/templates": "open", | |
| 168 | 169 | "/recover": "open", |
| 169 | 170 | "/releases": "admin", |
| 170 | 171 | "/releases.json": "open", |
src/http/server.ts +2
| @@ -320,6 +320,7 @@ import { pricingDone, pricingOrder, pricingPage, resendPage, resendSubmit } from | ||
| 320 | 320 | import { startForgeSubmit } from "../web/pages/start-forge.ts"; |
| 321 | 321 | import { redirect } from "../web/layout.ts"; |
| 322 | 322 | import { newForgePage, newForgeSubmit } from "../web/pages/new-forge.ts"; |
| 323 | import { templatesPage } from "../web/pages/templates.ts"; | |
| 323 | 324 | import { askedPage } from "../web/studio/asked_page.ts"; |
| 324 | 325 | import { notesOverrule, notesSave, notesView } from "../web/pages/notes.ts"; |
| 325 | 326 | import { reviewPage } from "../web/studio/review_page.ts"; |
| @@ -960,6 +961,7 @@ export function buildRouter(): Router<Handler> { | ||
| 960 | 961 | router.add("GET", "/pricing/done", pricingDone); |
| 961 | 962 | router.add("GET", "/pricing/resend", resendPage); |
| 962 | 963 | router.add("POST", "/pricing/resend", resendSubmit); |
| 964 | router.add("GET", "/templates", templatesPage); | |
| 963 | 965 | router.add("GET", "/forges/new", newForgePage); |
| 964 | 966 | router.add("POST", "/forges/new", newForgeSubmit); |
| 965 | 967 | router.add("POST", "/forges/start", startForgeSubmit); |
src/spawn/template_list.ts added +25
| @@ -0,0 +1,25 @@ | ||
| 1 | /** | |
| 2 | * The templates a person may start a forge from, as rows: the built-ins in order, | |
| 3 | * then the template repositories the viewer can read. The page (`/templates`) and | |
| 4 | * the terminal screen read this one list, so they say the same words. | |
| 5 | */ | |
| 6 | ||
| 7 | import type { Database } from "bun:sqlite"; | |
| 8 | import { listReposVisibleTo } from "../core/repos.ts"; | |
| 9 | import type { User } from "../core/users.ts"; | |
| 10 | import { TEMPLATES, templateOf } from "./templates.ts"; | |
| 11 | ||
| 12 | export interface TemplateRow { | |
| 13 | /** What `template` takes in a spawn: a built-in id or `owner/name`. */ | |
| 14 | readonly id: string; | |
| 15 | readonly title: string; | |
| 16 | readonly says: string; | |
| 17 | } | |
| 18 | ||
| 19 | export function templateList(db: Database, viewer: User | undefined): TemplateRow[] { | |
Both doors: the terminal has no screen reading this list. Add src/tui/screens/templates.ts over templateList, with a test. |
||
| 20 | const repos = listReposVisibleTo(db, viewer, { templatesOnly: true, limit: 50 }).map((r) => `${r.ownerName}/${r.name}`); | |
| 21 | return [...TEMPLATES.map((t) => t.id), ...repos].map((id) => { | |
| 22 | const t = templateOf(id); | |
| 23 | return { id, title: t.title, says: t.says }; | |
| 24 | }); | |
| 25 | } |
src/tui/forge.ts +2
| @@ -18,6 +18,7 @@ import { sourceFor } from "../view/source.ts"; | ||
| 18 | 18 | import { fleetCommand } from "./screens/fleet.ts"; |
| 19 | 19 | import { dataCommand } from "./screens/data.ts"; |
| 20 | 20 | import { packsCommand } from "./screens/packs.ts"; |
| 21 | import { templatesCommand } from "./screens/templates.ts"; | |
| 21 | 22 | import { needsCommands } from "./screens/needs.ts"; |
| 22 | 23 | import { asksAdminCommands } from "./screens/asks_admin.ts"; |
| 23 | 24 | import { shareTellCommand } from "./screens/share_tell.ts"; |
| @@ -266,6 +267,7 @@ export function forgeCommands(session: Session): (ctx: Ctx) => Command[] { | ||
| 266 | 267 | ...needsCommands(session, ctx), |
| 267 | 268 | ...asksAdminCommands(session, ctx), |
| 268 | 269 | packsCommand(session, ctx), |
| 270 | templatesCommand(session, ctx), | |
| 269 | 271 | shareTellCommand(session, ctx), |
| 270 | 272 | sendChangeCommand(ctx), |
| 271 | 273 | interruptedOutCommand(session, ctx), |
src/tui/screens/templates.ts added +54
| @@ -0,0 +1,54 @@ | ||
| 1 | /** | |
| 2 | * `/templates` in the terminal: what a new forge starts from, from the page's own list | |
| 3 | * (`templateList`). Enter on a row opens New forge with that template chosen; a guest reads | |
| 4 | * the list and is told the form is for people signed in. | |
| 5 | */ | |
| 6 | ||
| 7 | import { templateList } from "../../spawn/template_list.ts"; | |
| 8 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 9 | import { binding } from "../app/keys.ts"; | |
| 10 | import { style } from "../term/style.ts"; | |
| 11 | import { truncate, wrap } from "../term/width.ts"; | |
| 12 | import type { Session } from "../session.ts"; | |
| 13 | import { newForgeScreen } from "./newforge.ts"; | |
| 14 | ||
| 15 | export function templatesScreen(session: Session, ctx: Ctx): Screen { | |
| 16 | let at = 0; | |
| 17 | const rows = () => templateList(session.app.db, session.user); | |
| 18 | return { | |
| 19 | title: "Templates", | |
| 20 | keys: [binding(["j", "k"], "move"), ...(session.user === undefined ? [] : [binding(["enter"], "start a forge from it")]), binding(["esc"], "back")], | |
| 21 | render(area, s) { | |
| 22 | const c = ctx.theme.c; | |
| 23 | const list = rows(); | |
| 24 | at = Math.min(at, Math.max(0, list.length - 1)); | |
| 25 | let y = area.y; | |
| 26 | list.forEach((r, i) => { | |
| 27 | const on = i === at; | |
| 28 | if (y < area.y + area.h) s.text(area.x + 1, y, truncate(`${on ? "> " : " "}${r.title} (${r.id})`, area.w - 2), style(on ? c.accent : c.fg, c.bg), area.x + area.w); | |
| 29 | y += 1; | |
| 30 | if (on) for (const line of wrap(r.says, area.w - 6)) if (y < area.y + area.h) s.text(area.x + 5, y++, line, style(c.mutedFg, c.bg), area.x + area.w); | |
| 31 | }); | |
| 32 | if (session.user === undefined && y < area.y + area.h) | |
| 33 | s.text(area.x + 1, y + 1, truncate("Sign in to start a forge from one, or use /templates on the web.", area.w - 2), style(c.mutedFg, c.bg), area.x + area.w); | |
| 34 | }, | |
| 35 | key(e) { | |
| 36 | if (e.ctrl || e.alt) return false; | |
| 37 | if (e.key === "esc" || e.key === "q") ctx.pop(); | |
| 38 | else if (e.key === "j" || e.key === "down") at = Math.min(Math.max(0, rows().length - 1), at + 1); | |
| 39 | else if (e.key === "k" || e.key === "up") at = Math.max(0, at - 1); | |
| 40 | else if (e.key === "enter" && session.user !== undefined) { | |
| 41 | const row = rows()[at]; | |
| 42 | if (row !== undefined) ctx.push(newForgeScreen(session, ctx, { template: row.id })); | |
| 43 | } else return false; | |
| 44 | return true; | |
| 45 | }, | |
| 46 | }; | |
| 47 | } | |
| 48 | ||
| 49 | export const templatesCommand = (session: Session, ctx: Ctx) => ({ | |
| 50 | id: "templates", | |
| 51 | title: "Templates", | |
| 52 | hint: "what a new forge can start from", | |
| 53 | run: () => ctx.push(templatesScreen(session, ctx)), | |
| 54 | }); |
src/web/pages/new-forge.ts +9 −2
| @@ -28,7 +28,7 @@ import { | ||
| 28 | 28 | MIN_BUDGET_USD, |
| 29 | 29 | } from "../../spawn/plan.ts"; |
| 30 | 30 | import { DEFAULT_SIZE, SIZES, sizesView } from "../../spawn/sizes.ts"; |
| 31 | import { DEFAULT_TEMPLATE, TEMPLATES, templateOf } from "../../spawn/templates.ts"; | |
| 31 | import { DEFAULT_TEMPLATE, parseTemplate, TEMPLATES, templateOf } from "../../spawn/templates.ts"; | |
| 32 | 32 | import { FOLLOW_LABELS, FOLLOW_WORDS } from "../../spawn/follow_words.ts"; |
| 33 | 33 | import { dropGift, keepGift, keptGift } from "../gift_cookie.ts"; |
| 34 | 34 | import { notFoundPage, pageHead } from "../chrome.ts"; |
| @@ -378,6 +378,7 @@ function formView( | ||
| 378 | 378 | options: templateOptions(values.template, templates), |
| 379 | 379 | ...(errors["template"] ? { error: errors["template"] } : {}), |
| 380 | 380 | })} |
| 381 | <p class="muted small"><a href="/templates">Templates</a></p> | |
outdated
Principle 1: the link explains the select a second time; /templates already says it. Keep it only if the page needs the entry, and word it plainly. |
||
| 381 | 382 | ${selectField({ |
| 382 | 383 | label: "Size (optional)", |
| 383 | 384 | name: "size", |
| @@ -499,6 +500,12 @@ function fromPack(ctx: Ctx, user: User | undefined): Partial<Values> { | ||
| 499 | 500 | return { apps, direction: pack.brief.slice(0, MAX_DIRECTION_CHARS) }; |
| 500 | 501 | } |
| 501 | 502 | |
| 503 | /** `?template=` from /templates: kept only when spawn accepts it, else Blank; never echoed. */ | |
| 504 | function askedTemplate(ctx: Ctx): string { | |
| 505 | const asked = parseTemplate(ctx.url.searchParams.get("template") ?? ""); | |
| 506 | return asked.ok ? asked.value : DEFAULT_TEMPLATE; | |
| 507 | } | |
| 508 | ||
| 502 | 509 | // GET /forges/new — any signed-in person, or a stranger (agents are refused). |
| 503 | 510 | export function newForgePage(ctx: Ctx): Response { |
| 504 | 511 | const user = ctx.auth.user; |
| @@ -519,7 +526,7 @@ export function newForgePage(ctx: Ctx): Response { | ||
| 519 | 526 | ctx, |
| 520 | 527 | title: "New forge", |
| 521 | 528 | keyboardResizes: true, |
| 522 | body: formView(ctx, user, { ...EMPTY, code, name: (ctx.url.searchParams.get("name") ?? "").slice(0, 38), ...fromPack(ctx, user) }, errors, gift, reads), | |
| 529 | body: formView(ctx, user, { ...EMPTY, code, name: (ctx.url.searchParams.get("name") ?? "").slice(0, 38), template: askedTemplate(ctx), ...fromPack(ctx, user) }, errors, gift, reads), | |
| 523 | 530 | }); |
| 524 | 531 | notePhase(ctx.request, "render", performance.now() - rendering); |
| 525 | 532 | return response; |
src/web/pages/templates.ts added +43
| @@ -0,0 +1,43 @@ | ||
| 1 | /** | |
| 2 | * GET /templates: what a new forge can start from, one row each with a plain | |
| 3 | * sentence and one button that opens New forge with that template chosen. Public: | |
| 4 | * a stranger lands on the same form (`/forges/new` works signed out) with the | |
| 5 | * template kept. The button is a GET form, so it works without JavaScript. | |
| 6 | */ | |
| 7 | ||
| 8 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 9 | import type { Ctx } from "../../http/context.ts"; | |
| 10 | import { templateList } from "../../spawn/template_list.ts"; | |
| 11 | import { pageHead } from "../chrome.ts"; | |
| 12 | import { html } from "../html.ts"; | |
| 13 | import { page } from "../layout.ts"; | |
| 14 | import { btn } from "../ui.ts"; | |
| 15 | ||
| 16 | export const TEMPLATES_WORDS = "Pick where your forge starts. You can change everything after."; | |
| 17 | ||
| 18 | export function templatesPage(ctx: Ctx): Response { | |
| 19 | const user = ctx.auth.user; | |
| 20 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 21 | const rows = timePhase(ctx.request, "reads", () => templateList(ctx.app.db, user?.kind === "user" ? user : undefined)); | |
| 22 | return timePhase(ctx.request, "render", () => | |
| 23 | page({ | |
| 24 | ctx, | |
| 25 | title: "Templates", | |
| 26 | description: TEMPLATES_WORDS, | |
| 27 | body: html`<div class="narrow-wide"> | |
| 28 | ${pageHead("Templates", { sub: TEMPLATES_WORDS })} | |
| 29 | <ul class="plain" id="templates"> | |
| 30 | ${rows.map( | |
| 31 | (r) => html`<li data-template="${r.id}"> | |
| 32 | <form method="get" action="/forges/new" class="row"> | |
| 33 | <input type="hidden" name="template" value="${r.id}"> | |
| 34 | <span><b>${r.title}</b> ${r.says}</span> | |
| 35 | ${btn(`Start a forge from ${r.title}`, { variant: "secondary" })} | |
outdated
Principle 5: a primary button on every row gives the page many accents. Use a non-primary variant. Principles 6/7: li.settings-block with h2+p+form per row is a box; make each a plain row. |
||
| 36 | </form> | |
| 37 | </li>`, | |
| 38 | )} | |
| 39 | </ul> | |
| 40 | </div>`, | |
| 41 | }), | |
| 42 | ); | |
| 43 | } |
test/forge-looks.test.ts +1
| @@ -26,6 +26,7 @@ const NOT_A_PAGE: Record<string, string> = { | ||
| 26 | 26 | "/feed": "no picture: its slot went to /docs; the page is covered by its own tests", |
| 27 | 27 | "/front": "no picture: its slot went to /settings/security/controls, the security controls (#13001); covered by its own tests", |
| 28 | 28 | "/pricing": "no picture: not in app.looks; the page is covered by test/pricing-order.test.ts", |
| 29 | "/templates": "no picture: not in app.looks; the page is covered by test/templates-page.test.ts", | |
| 29 | 30 | "/terms": "no picture: not in app.looks; the page is covered by test/terms-page.test.ts", |
| 30 | 31 | "/audit": "no picture: its slot went to /pinned; /audit.csv is its download, covered by test/audit.test.ts", |
| 31 | 32 | "/deploy-to": "a form for a forge admin that fills from its link; covered by test/pinned-page.test.ts", |
test/palette-roster.test.ts +4
| @@ -53,6 +53,7 @@ test("a site-admin person is offered the whole roster", () => { | ||
| 53 | 53 | ["packs", "Forge packs", "premade forges to deploy, and how to help build their apps"], |
| 54 | 54 | ["mirror-packs", "Mirror the packs' upstreams", "bring each pack's upstream projects in as read-only repositories"], |
| 55 | 55 | ["pack-repos", "Pack repositories", "which repositories /packs lists"], |
| 56 | ["templates", "Templates", "what a new forge can start from"], | |
| 56 | 57 | ["share-tell", "Share a link or text with the crew", "a draft, then where; nothing is sent until you confirm"], |
| 57 | 58 | ["send-change", "Send a change", "fork, patch, pull: no forge of your own"], |
| 58 | 59 | ["interrupted-out", "Stopped runs", "runs the host stopped five times, with Try again"], |
| @@ -228,6 +229,7 @@ test("a site-admin agent is offered the roster without the person-only entries", | ||
| 228 | 229 | ["needs", "How is my forge?", "what needs you, with a recommendation each; a takes a safe one"], |
| 229 | 230 | ["asks-admin", "What stands in the way", "what waits on you and what stands in your way; a asks whoever can, o opens the control"], |
| 230 | 231 | ["packs", "Forge packs", "premade forges to deploy, and how to help build their apps"], |
| 232 | ["templates", "Templates", "what a new forge can start from"], | |
| 231 | 233 | ["share-tell", "Share a link or text with the crew", "a draft, then where; nothing is sent until you confirm"], |
| 232 | 234 | ["send-change", "Send a change", "fork, patch, pull: no forge of your own"], |
| 233 | 235 | ["interrupted-out", "Stopped runs", "runs the host stopped five times, with Try again"], |
| @@ -325,6 +327,7 @@ test("a member person is offered the signed-in roster without the admin entries" | ||
| 325 | 327 | ["needs", "How is my forge?", "what needs you, with a recommendation each; a takes a safe one"], |
| 326 | 328 | ["asks-admin", "What stands in the way", "what waits on you and what stands in your way; a asks whoever can, o opens the control"], |
| 327 | 329 | ["packs", "Forge packs", "premade forges to deploy, and how to help build their apps"], |
| 330 | ["templates", "Templates", "what a new forge can start from"], | |
| 328 | 331 | ["share-tell", "Share a link or text with the crew", "a draft, then where; nothing is sent until you confirm"], |
| 329 | 332 | ["send-change", "Send a change", "fork, patch, pull: no forge of your own"], |
| 330 | 333 | ["interrupted-out", "Stopped runs", "runs the host stopped five times, with Try again"], |
| @@ -417,6 +420,7 @@ test("signed out, the palette offers sign-in and the public entries", () => { | ||
| 417 | 420 | ["explore", "Explore", "on the web"], |
| 418 | 421 | ["explore-feed", "Explore feed", "Atom address"], |
| 419 | 422 | ["packs", "Forge packs", "premade forges to deploy, and how to help build their apps"], |
| 423 | ["templates", "Templates", "what a new forge can start from"], | |
| 420 | 424 | ["share-tell", "Share a link or text with the crew", "a draft, then where; nothing is sent until you confirm"], |
| 421 | 425 | ["send-change", "Send a change", "fork, patch, pull: no forge of your own"], |
| 422 | 426 | ["interrupted-out", "Stopped runs", "runs the host stopped five times, with Try again"], |
test/templates-page.test.ts added +99
| @@ -0,0 +1,99 @@ | ||
| 1 | /** | |
| 2 | * /templates lists the templates; its button reaches New forge with the template, and the plan | |
| 3 | * reads it (#13266). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { createRepo } from "../src/core/repos.ts"; | |
| 11 | import { createSession } from "../src/core/sessions.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 14 | import { templateList } from "../src/spawn/template_list.ts"; | |
| 15 | ||
| 16 | let dir: string; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | ||
| 20 | beforeAll(async () => { | |
| 21 | dir = await mkdtemp(join(tmpdir(), "forge-templates-page-")); | |
| 22 | app = createApp({ dataDir: dir, url: "https://forge.test", port: 0, forgeRepo: "alice/forge-code" }); | |
| 23 | servers = await serve(app); | |
| 24 | }, 30_000); | |
| 25 | ||
| 26 | afterAll(async () => { | |
| 27 | servers.stop(); | |
| 28 | app.close(); | |
| 29 | await rm(dir, { recursive: true, force: true }); | |
| 30 | }); | |
| 31 | ||
| 32 | test("signed out: one row and one button per template", async () => { | |
| 33 | const res = await fetch(`${servers.url}/templates`); | |
| 34 | const text = await res.text(); | |
| 35 | expect(res.status).toBe(200); | |
| 36 | expect(text).toContain('data-template="blank"'); | |
| 37 | expect(text).toContain('data-template="starter"'); | |
| 38 | expect(text).toContain('<form method="get" action="/forges/new"'); | |
| 39 | expect(text).toContain("Start a forge from Starter app"); | |
| 40 | }); | |
| 41 | ||
| 42 | test("the button lands on the form with the template chosen; junk is not echoed", async () => { | |
| 43 | const ok = await (await fetch(`${servers.url}/forges/new?template=starter`)).text(); | |
| 44 | expect(ok).toMatch(/<option\s+value="starter"\s+selected/); | |
| 45 | const junk = await (await fetch(`${servers.url}/forges/new?template=%3Cscript%3Ex`)).text(); | |
| 46 | expect(junk).not.toContain("<script>x"); | |
| 47 | expect(junk).toMatch(/<option\s+value="blank"\s+selected/); | |
| 48 | }); | |
| 49 | ||
| 50 | test("posting the chosen template reads it in the plan (dry run)", async () => { | |
| 51 | const res = await fetch(`${servers.url}/forges/new`, { | |
| 52 | method: "POST", | |
| 53 | headers: { origin: "https://forge.test", "content-type": "application/x-www-form-urlencoded" }, | |
| 54 | body: new URLSearchParams({ name: "from-starter", template: "starter", do: "plan" }), | |
| 55 | }); | |
| 56 | const text = await res.text(); | |
| 57 | expect(res.status).toBe(200); | |
| 58 | expect(text).toContain("It starts from the <b>Starter app</b> template."); | |
| 59 | }); | |
| 60 | ||
| 61 | test("a private template repository is hidden from a stranger and shown to its reader", async () => { | |
| 62 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "tess", email: "[email protected]", password: "a-good-passphrase" }); | |
| 63 | if (!made.ok) throw new Error(made.error); | |
| 64 | const tess = made.value; | |
| 65 | const other = await createUser(app.db, { origin: "seed", trusted: true, username: "otto", email: "[email protected]", password: "a-good-passphrase" }); | |
| 66 | if (!other.ok) throw new Error(other.error); | |
| 67 | for (const [name, visibility] of [["open-kit", "public"], ["hidden-kit", "private"]] as const) { | |
| 68 | const r = await createRepo(app.db, app.reposRoot, { ownerId: tess.id, name, visibility, description: name, defaultBranch: "main" }); | |
| 69 | if (!r.ok) throw new Error(r.error); | |
| 70 | app.db.query("UPDATE repo SET is_template = 1 WHERE name = ?1").run(name); | |
| 71 | } | |
| 72 | const ids = (u: Parameters<typeof templateList>[1]) => templateList(app.db, u).map((r) => r.id); | |
| 73 | expect(ids(undefined)).toContain("tess/open-kit"); | |
| 74 | expect(ids(undefined)).not.toContain("tess/hidden-kit"); | |
| 75 | expect(ids(other.value)).not.toContain("tess/hidden-kit"); | |
| 76 | expect(ids(tess)).toContain("tess/hidden-kit"); | |
| 77 | const stranger = await (await fetch(`${servers.url}/templates`)).text(); | |
| 78 | expect(stranger).toContain('data-template="tess/open-kit"'); | |
| 79 | expect(stranger).not.toContain("hidden-kit"); | |
| 80 | const session = createSession(app.db, tess.id); | |
| 81 | const mine = await (await fetch(`${servers.url}/templates`, { headers: { cookie: `forge_session=${session.token}` } })).text(); | |
| 82 | expect(mine).toContain('data-template="tess/hidden-kit"'); | |
| 83 | }); | |
| 84 | ||
| 85 | test("the row buttons are secondary: the page has no primary accent", async () => { | |
| 86 | const text = await (await fetch(`${servers.url}/templates`)).text(); | |
| 87 | expect(text).toContain('data-variant="secondary"'); | |
| 88 | expect(text).not.toContain('data-variant="primary"'); | |
| 89 | }); | |
| 90 | ||
| 91 | test("a stranger who stumbles on the form keeps the template; the refusal keeps it too", async () => { | |
| 92 | const res = await fetch(`${servers.url}/forges/new`, { | |
| 93 | method: "POST", | |
| 94 | headers: { origin: "https://forge.test", "content-type": "application/x-www-form-urlencoded" }, | |
| 95 | body: new URLSearchParams({ name: "Bad Name!", template: "starter" }), | |
| 96 | }); | |
| 97 | expect(res.status).toBeGreaterThanOrEqual(400); | |
| 98 | expect(await res.text()).toMatch(/<option\s+value="starter"\s+selected/); | |
| 99 | }); |
test/templates-tui.test.ts added +87
| @@ -0,0 +1,87 @@ | ||
| 1 | /** | |
| 2 | * /templates in the terminal: the page's list, a private template only for its reader, Enter opens | |
| 3 | * New forge with it chosen. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { createRepo } from "../src/core/repos.ts"; | |
| 11 | import { createUser, type User } from "../src/core/users.ts"; | |
| 12 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 13 | import { templatesScreen } from "../src/tui/screens/templates.ts"; | |
| 14 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 15 | import { settleApp } from "./helpers/tui.ts"; | |
| 16 | ||
| 17 | let dir: string; | |
| 18 | let app: App; | |
| 19 | let tess: User; | |
| 20 | let otto: User; | |
| 21 | ||
| 22 | beforeAll(async () => { | |
| 23 | dir = await mkdtemp(join(tmpdir(), "forge-templates-tui-")); | |
| 24 | app = createApp({ dataDir: dir, port: 0 }); | |
| 25 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "tess", email: "[email protected]", password: "a-good-passphrase" }); | |
| 26 | const b = await createUser(app.db, { origin: "seed", trusted: true, username: "otto", email: "[email protected]", password: "a-good-passphrase" }); | |
| 27 | if (!a.ok || !b.ok) throw new Error("no users"); | |
| 28 | tess = a.value; | |
| 29 | otto = b.value; | |
| 30 | const r = await createRepo(app.db, app.reposRoot, { ownerId: tess.id, name: "hidden-kit", visibility: "private", description: "x", defaultBranch: "main" }); | |
| 31 | if (!r.ok) throw new Error(r.error); | |
| 32 | app.db.query("UPDATE repo SET is_template = 1 WHERE name = 'hidden-kit'").run(); | |
| 33 | }); | |
| 34 | afterAll(async () => { | |
| 35 | app.close(); | |
| 36 | await rm(dir, { recursive: true, force: true }); | |
| 37 | }); | |
| 38 | ||
| 39 | const key = (k: string) => ({ type: "key" as const, key: k, ctrl: false, alt: false, shift: false }); | |
| 40 | function open(user: User | undefined) { | |
| 41 | const { app: tui, session } = createForgeApp( | |
| 42 | app, | |
| 43 | { sshUsername: user?.username ?? "guest", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", ...(user ? { user } : {}) }, | |
| 44 | { terminal: headlessTerminal(120, 40), exitHooks: false }, | |
| 45 | ); | |
| 46 | tui.start(); | |
| 47 | const screen = templatesScreen(session, tui.ctx); | |
| 48 | tui.ctx.push(screen); | |
| 49 | return { tui, screen }; | |
| 50 | } | |
| 51 | ||
| 52 | test("lists the built-ins; a private template shows to its reader only", async () => { | |
| 53 | const reader = open(tess); | |
| 54 | await settleApp(reader.tui); | |
| 55 | expect(reader.tui.screen.toText()).toContain("Starter app"); | |
| 56 | expect(reader.tui.screen.toText()).toContain("tess/hidden-kit"); | |
| 57 | const stranger = open(otto); | |
| 58 | await settleApp(stranger.tui); | |
| 59 | expect(stranger.tui.screen.toText()).toContain("Starter app"); | |
| 60 | expect(stranger.tui.screen.toText()).not.toContain("hidden-kit"); | |
| 61 | reader.tui.stop?.(); | |
| 62 | stranger.tui.stop?.(); | |
| 63 | }); | |
| 64 | ||
| 65 | test("Enter opens New forge with the row's template chosen", async () => { | |
| 66 | const t = open(otto); | |
| 67 | await settleApp(t.tui); | |
| 68 | t.screen.key(key("j")); // blank, then starter | |
| 69 | t.screen.key(key("enter")); | |
| 70 | await settleApp(t.tui); | |
| 71 | const text = t.tui.screen.toText(); | |
| 72 | expect(text).toContain("Template"); | |
| 73 | expect(text).toContain("starter"); | |
| 74 | t.tui.stop?.(); | |
| 75 | }); | |
| 76 | ||
| 77 | test("a guest reads the public rows only and is told to sign in", async () => { | |
| 78 | const g = open(undefined); | |
| 79 | await settleApp(g.tui); | |
| 80 | expect(g.tui.screen.toText()).toContain("Starter app"); | |
| 81 | expect(g.tui.screen.toText()).not.toContain("hidden-kit"); | |
| 82 | expect(g.tui.screen.toText()).toContain("Sign in to start a forge"); | |
| 83 | expect(g.screen.key(key("enter"))).toBe(false); // no New forge for a guest | |
| 84 | await settleApp(g.tui); | |
| 85 | expect(g.tui.screen.toText()).toContain("Sign in to start a forge"); | |
| 86 | g.tui.stop?.(); | |
| 87 | }); |
Visibility filter is the leak boundary and untested; add a test that a private template repo is absent for a stranger and present for its reader. Also the terminal screen that should share this list is missing (both doors).