Public: anyone can read this; the crew can change it.
A template's page shows what it will create before the person starts it: its files, its first issue and its crew #15882
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15855-a-template-s-page-shows-what-it-will-cre → main 0 commits fork 3 threads · 3 unresolved
10 files
changed +212
−38
docs/features/template-page.md +4 −2
| @@ -4,6 +4,8 @@ row: Template page | ||
| 4 | 4 | --- |
| 5 | 5 | # Template page |
| 6 | 6 | |
| 7 | `/templates/owner/name` shows what a forge started from that template gets, read-only from its repository: the top-level file names (folders end in `/`) and the README's first paragraph, escaped. `/templates/blank` and `/templates/starter` show the built-in templates' own words. A repository that does not exist, is not marked a template, or that the viewer cannot read answers the same 404. Settings → App templates links each row to its page. | |
| 7 | `/templates/owner/name` shows what a forge started from that template gets, read-only from its repository: the README's first paragraph (escaped), then **Files it will create** (every path, sorted, the first 30 with "and N more"), **First issue** (the `firstAsk` its forge.json declares, or "None: you tell the crew what to build") and **Crew seats** (the starter seats a new forge fills, from `src/spawn/starter_crew.ts`). `/templates/starter` lists the files it bakes. `/templates/blank` and `/templates/starter` show the built-in templates' own words. A repository that does not exist, is not marked a template, or that the viewer cannot read answers the same 404. Settings → App templates links each row to its page. | |
| 8 | 8 | |
| 9 | Code: `src/web/pages/template.ts`, `src/core/template_view.ts`, `src/core/readme_paragraph.ts`. Test: `test/template-page.test.ts`. | |
| 9 | In the terminal, `p` on a row of Templates (`/templates` screen) opens the same three parts: files, first issue, crew seats, `j`/`k` scroll. Blank has no page, so `p` does nothing on it. | |
| 10 | ||
| 11 | Code: `src/web/pages/template.ts`, `src/tui/screens/template_preview.ts`, `src/core/template_view.ts`, `src/core/readme_paragraph.ts`. Tests: `test/template-page.test.ts`, `test/template-preview.test.ts`, `test/template-preview-tui.test.ts`. |
scripts/feature-doors-allow.json −1
| @@ -11,7 +11,6 @@ | ||
| 11 | 11 | "docs/features/speak.md", |
| 12 | 12 | "docs/features/stranger-pulls.md", |
| 13 | 13 | "docs/features/talk-resume-writes.md", |
| 14 | "docs/features/template-page.md", | |
| 15 | 14 | "docs/features/template-timing.md", |
| 16 | 15 | "docs/features/thread-fold.md", |
| 17 | 16 | "docs/features/wave-standing.md", |
src/core/template_view.ts +25 −10
| @@ -1,48 +1,63 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * What a template starts a forge with (#13407), read-only from its repository: the one reading the |
| 3 | * page shows, behind one gate. A repository that is missing, | |
| 3 | * page and the terminal screen show, behind one gate. A repository that is missing, | |
| 4 | 4 | * not marked a template or not readable by the viewer is `undefined` (the same not-found for all). |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import type { App } from "../app.ts"; |
| 8 | 8 | import { readFileAt } from "../apps/checkout.ts"; |
| 9 | 9 | import { firstAskOf } from "../apps/first_ask.ts"; |
| 10 | import { blobForRender, listTree, refTip } from "../git/read.ts"; | |
| 10 | import { bakedFiles } from "../apps/template.ts"; | |
| 11 | import { blobForRender, listPaths, refTip } from "../git/read.ts"; | |
| 11 | 12 | import { repoDir } from "../git/repos.ts"; |
| 13 | import { STARTER_SEATS } from "../spawn/starter_crew.ts"; | |
| 12 | 14 | import { repoTemplateRef, TEMPLATES } from "../spawn/templates.ts"; |
| 13 | 15 | import type { User } from "./users.ts"; |
| 14 | 16 | import { readmeParagraph } from "./readme_paragraph.ts"; |
| 15 | 17 | import { canRead, getRepo } from "./repos.ts"; |
| 16 | 18 | |
| 17 | 19 | const README_BYTES = 16 * 1024; |
| 18 | const FILES_MAX = 200; | |
| 19 | 20 | |
| 20 | 21 | export type TemplateView = { |
| 21 | 22 | readonly title: string; |
| 22 | 23 | readonly about: string; |
| 23 | /** Top-level names; a folder ends in `/`. Empty for a built-in template. */ | |
| 24 | readonly files: readonly string[]; | |
| 25 | 24 | /** The valid `firstAsk` its forge.json declares, if any. */ |
| 26 | 25 | readonly firstAsk?: string; |
| 26 | /** The first {@link PATHS_MAX} file paths it will create, in order. */ | |
| 27 | readonly paths: readonly string[]; | |
| 28 | /** How many more paths there are beyond `paths`. */ | |
| 29 | readonly morePaths: number; | |
| 30 | /** The seats a new forge fills: `name: role`, one per starter seat. */ | |
| 31 | readonly seats: readonly string[]; | |
| 27 | 32 | }; |
| 28 | 33 | |
| 34 | export const PATHS_MAX = 30; | |
| 35 | const SEATS: readonly string[] = STARTER_SEATS.map((s) => `${s.name}: ${s.role}`); | |
| 36 | ||
| 37 | function capped(all: readonly string[]): { paths: string[]; morePaths: number } { | |
| 38 | return { paths: all.slice(0, PATHS_MAX), morePaths: Math.max(0, all.length - PATHS_MAX) }; | |
| 39 | } | |
| 40 | ||
| 29 | 41 | /** A built-in template by id, or a marked repository by `owner/name`. */ |
| 30 | 42 | export async function templateView(app: App, viewer: User | undefined, ref: string): Promise<TemplateView | undefined> { |
| 31 | 43 | const built = TEMPLATES.find((t) => t.id === ref); |
| 32 | if (built) return { title: built.title, about: built.says, files: [] }; | |
| 44 | if (built) { | |
| 45 | const all = built.id === "starter" ? bakedFiles({ name: "app", description: "" }).map((f) => f.path).sort() : []; | |
| 46 | return { title: built.title, about: built.says, ...capped(all), seats: SEATS }; | |
| 47 | } | |
| 33 | 48 | if (repoTemplateRef(ref) === undefined) return undefined; |
| 34 | 49 | const [owner = "", name = ""] = ref.split("/"); |
| 35 | 50 | const repo = getRepo(app.db, owner, name); |
| 36 | 51 | if (!repo || !repo.template || !canRead(app.db, repo, viewer)) return undefined; |
| 37 | 52 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); |
| 38 | let files: string[] = []; | |
| 39 | 53 | let about = ""; |
| 40 | 54 | let firstAsk: string | undefined; |
| 55 | let all: string[] = []; | |
| 41 | 56 | try { |
| 42 | 57 | const tip = await refTip(dir, `refs/heads/${repo.defaultBranch}`); |
| 43 | 58 | if (tip !== undefined) { |
| 44 | const tree = await listTree(dir, tip, ""); | |
| 45 | if (tree.ok) files = tree.value.slice(0, FILES_MAX).map((e) => (e.type === "tree" ? `${e.name}/` : e.name)); | |
| 59 | const listed = await listPaths(dir, tip); | |
| 60 | if (listed.ok) all = listed.value.paths.slice().sort(); | |
| 46 | 61 | const blob = await blobForRender(dir, tip, "README.md", README_BYTES); |
| 47 | 62 | if (blob.kind === "text") about = readmeParagraph(new TextDecoder().decode(blob.body)); |
| 48 | 63 | firstAsk = firstAskOf(await readFileAt(dir, tip, "forge.json")); |
| @@ -50,5 +65,5 @@ export async function templateView(app: App, viewer: User | undefined, ref: stri | ||
| 50 | 65 | } catch { |
| 51 | 66 | // an unreadable repository shows what it can: the name and nothing else |
| 52 | 67 | } |
| 53 | return { title: `${repo.ownerName}/${repo.name}`, about, files, ...(firstAsk === undefined ? {} : { firstAsk }) }; | |
| 68 | return { title: `${repo.ownerName}/${repo.name}`, about, ...capped(all), seats: SEATS, ...(firstAsk === undefined ? {} : { firstAsk }) }; | |
| 54 | 69 | } |
src/tui/screens/template_preview.ts added +60
| @@ -0,0 +1,60 @@ | ||
| 1 | /** | |
| 2 | * A template's page in the terminal (#15855): the files a forge started from it will create, its | |
| 3 | * first issue and the crew seats it fills, from the page's own reading (`templateView`). Read-only; | |
| 4 | * opened with `p` on a row of /templates. | |
| 5 | */ | |
| 6 | ||
| 7 | import { templateView, type TemplateView } from "../../core/template_view.ts"; | |
| 8 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 9 | import { binding } from "../app/keys.ts"; | |
| 10 | import type { Session } from "../session.ts"; | |
| 11 | import { style } from "../term/style.ts"; | |
| 12 | import { truncate } from "../term/width.ts"; | |
| 13 | ||
| 14 | /** The lines the screen shows, from the page's reading: one list for every width. */ | |
| 15 | export function previewLines(v: TemplateView): string[] { | |
| 16 | return [ | |
| 17 | ...(v.about === "" ? [] : [v.about, ""]), | |
| 18 | ...(v.paths.length === 0 ? [] : ["Files it will create", ...v.paths.map((p) => ` ${p}`), ...(v.morePaths > 0 ? [` and ${v.morePaths} more`] : []), ""]), | |
| 19 | "First issue", | |
| 20 | ` ${v.firstAsk === undefined ? "None: you tell the crew what to build." : v.firstAsk.replace(/\s+/g, " ")}`, | |
| 21 | "", | |
| 22 | "Crew seats", | |
| 23 | ...v.seats.map((s) => ` ${s}`), | |
| 24 | ]; | |
| 25 | } | |
| 26 | ||
| 27 | export function templatePreviewScreen(f: Session, ctx: Ctx, ref: string): Screen { | |
| 28 | let view: TemplateView | undefined; | |
| 29 | let gone = false; | |
| 30 | let at = 0; | |
| 31 | return { | |
| 32 | title: `Template ${ref}`, | |
| 33 | keys: [binding(["j", "k"], "scroll"), binding(["esc"], "back")], | |
| 34 | async load(signal) { | |
| 35 | const v = await templateView(f.app, f.user, ref); | |
| 36 | if (signal.aborted) return; | |
| 37 | view = v; | |
| 38 | gone = v === undefined; | |
| 39 | ctx.invalidate(); | |
| 40 | }, | |
| 41 | render(area, s) { | |
| 42 | const c = ctx.theme.c; | |
| 43 | const lines = view === undefined ? [gone ? "No such template." : "Reading…"] : previewLines(view); | |
| 44 | at = Math.min(at, Math.max(0, lines.length - area.h)); | |
| 45 | lines.slice(at, at + area.h).forEach((l, i) => { | |
| 46 | const head = !l.startsWith(" ") && l !== "" && view !== undefined && (l === "First issue" || l === "Crew seats" || l === "Files it will create"); | |
| 47 | s.text(area.x + 1, area.y + i, truncate(l, area.w - 2), style(head ? c.fg : c.mutedFg, c.bg), area.x + area.w); | |
| 48 | }); | |
| 49 | }, | |
| 50 | key(e) { | |
| 51 | if (e.ctrl || e.alt) return false; | |
| 52 | if (e.key === "esc" || e.key === "q") ctx.pop(); | |
| 53 | else if (e.key === "j" || e.key === "down") at += 1; | |
| 54 | else if (e.key === "k" || e.key === "up") at = Math.max(0, at - 1); | |
| 55 | else return false; | |
| 56 | ctx.invalidate(); | |
| 57 | return true; | |
| 58 | }, | |
| 59 | }; | |
| 60 | } |
src/tui/screens/templates.ts +6 −2
| @@ -17,6 +17,7 @@ import { templateUse, useWords } from "../../spawn/template_use.ts"; | ||
| 17 | 17 | import { NO_CHANGES_YET, templateRecency } from "../../spawn/template_recent.ts"; |
| 18 | 18 | import { timeAgo } from "../../web/time_element.ts"; |
| 19 | 19 | import { newForgeScreen } from "./newforge.ts"; |
| 20 | import { templatePreviewScreen } from "./template_preview.ts"; | |
| 20 | 21 | |
| 21 | 22 | /** `updated 3d ago`, in the shared clock's words, or that nothing has landed. */ |
| 22 | 23 | const recencyLine = (at: string | undefined): string => (at === undefined ? NO_CHANGES_YET : `updated ${timeAgo(at)}`); |
| @@ -37,7 +38,7 @@ export function templatesScreen(session: Session, ctx: Ctx): Screen { | ||
| 37 | 38 | const rows = () => filterTemplates(listed, q); |
| 38 | 39 | return { |
| 39 | 40 | title: "Templates", |
| 40 | keys: [binding(["j", "k"], "move"), ...(session.user === undefined ? [] : [binding(["enter"], "start a forge from it")]), binding(["/"], "filter by name or description"), binding(["esc"], "back")], | |
| 41 | keys: [binding(["j", "k"], "move"), ...(session.user === undefined ? [] : [binding(["enter"], "start a forge from it")]), binding(["p"], "what it will create"), binding(["/"], "filter by name or description"), binding(["esc"], "back")], | |
| 41 | 42 | render(area, s) { |
| 42 | 43 | const c = ctx.theme.c; |
| 43 | 44 | const list = rows(); |
| @@ -77,7 +78,10 @@ export function templatesScreen(session: Session, ctx: Ctx): Screen { | ||
| 77 | 78 | if (e.key === "esc" || e.key === "q") ctx.pop(); |
| 78 | 79 | else if (e.key === "j" || e.key === "down") at = Math.min(Math.max(0, rows().length - 1), at + 1); |
| 79 | 80 | else if (e.key === "k" || e.key === "up") at = Math.max(0, at - 1); |
| 80 | else if (e.key === "enter" && session.user !== undefined) { | |
| 81 | else if (e.key === "p") { | |
| 82 | const row = rows()[at]; | |
| 83 | if (row !== undefined && row.id !== DEFAULT_TEMPLATE) ctx.push(templatePreviewScreen(session, ctx, row.id)); | |
| 84 | } else if (e.key === "enter" && session.user !== undefined) { | |
| 81 | 85 | const row = rows()[at]; |
| 82 | 86 | if (row !== undefined) ctx.push(newForgeScreen(session, ctx, { template: row.id })); |
| 83 | 87 | } else return false; |
src/web/pages/template.ts +13 −13
| @@ -1,10 +1,10 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * A template's page (#13407): what a forge started from it begins with, read-only from its |
| 3 | * repository: the top-level file names and the README's first paragraph. A built-in template shows | |
| 4 | * its own words. A repository that is missing, not marked a template or not readable by the viewer | |
| 5 | * is the same 404 (`core/template_view.ts`). When a forge has been made from it, one line says how | |
| 6 | * long the last took to its first live answer, read from stored events | |
| 7 | * (`actions/template_timing.ts`). | |
| 3 | * repository: the README's first paragraph, the files it will create, its first issue and the crew | |
| 4 | * seats. A built-in template shows its own words. A repository that is missing, not marked a | |
| 5 | * template or not readable by the viewer is the same 404 (`core/template_view.ts`). When a forge | |
| 6 | * has been made from it, one line says how long the last took to its first live answer, read from | |
| 7 | * stored events (`actions/template_timing.ts`). | |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | import { templateTimingLine } from "../../actions/template_timing.ts"; |
| @@ -42,16 +42,16 @@ export async function templatePage(ctx: Ctx): Promise<Response> { | ||
| 42 | 42 | ...(shared !== undefined ? { ogImage: `/og/template/${templateCardRef(ref)}.png` } : {}), |
| 43 | 43 | body: html`${pageHead(view.title, { sub: view.about !== "" ? view.about : "The files a forge starts with." })} |
| 44 | 44 | <section> |
| 45 | ${view.firstAsk === undefined ? null : html`<p id="template-first-ask"><span class="muted">A new forge opens with this ask:</span> <q>${view.firstAsk}</q></p>`} | |
| 46 | 45 | ${row ? html`<p>${btn("Start a forge", { variant: "primary", href: `/forges/new?template=${encodeURIComponent(ref)}` })}</p>` : null} |
outdated
Principle 1: this line and the First issue section below say the same ask twice. Keep one: drop this paragraph and let the section carry it. |
||
| 47 | ${built ? null : html`<p>A forge started from this template begins with these files.</p>`} | |
| 48 | ${built | |
| 46 | ${view.paths.length === 0 | |
| 49 | 47 | ? null |
| 50 | : view.files.length === 0 | |
| 51 | ? html`<p class="muted">No files yet.</p>` | |
| 52 | : html`<dl class="status-rows">${view.files.map((f) => | |
| 53 | statusRow(f, html`${f.endsWith("/") ? "folder" : "file"}`), | |
| 54 | )}</dl>`} | |
| 48 | : html`<h2>Files it will create</h2> | |
| 49 | <dl class="status-rows" id="template-paths">${view.paths.map((f) => statusRow(f, html`file`))}</dl> | |
| 50 | ${view.morePaths > 0 ? html`<p class="small muted">and ${view.morePaths} more</p>` : null}`} | |
| 51 | <h2>First issue</h2> | |
| 52 | <p id="template-first-issue">${view.firstAsk === undefined ? html`<span class="muted">None: you tell the crew what to build.</span>` : html`<q>${view.firstAsk}</q>`}</p> | |
| 53 | <h2>Crew seats</h2> | |
| 54 | <dl class="status-rows" id="template-seats">${view.seats.map((s) => statusRow(s, html`seat`))}</dl> | |
| 55 | 55 | ${line === null ? null : html`<p class="small muted" id="template-timing">${line}</p>`} |
| 56 | 56 | ${built ? null : html`<p class="muted"><a href="/${view.title}">Open the repository</a></p>`} |
| 57 | 57 | </section>`, |
test/feature-doors.test.ts +1 −1
| @@ -29,7 +29,7 @@ test("a listed how-to that gained its terminal file fails: prune the list", () = | ||
| 29 | 29 | /** |
| 30 | 30 | * Lower this when the list shrinks; raising it is adding a page-only how-to, which review refuses. |
| 31 | 31 | */ |
| 32 | const CEILING = 19; | |
| 32 | const CEILING = 18; | |
| 33 | 33 | |
| 34 | 34 | test("the allow list cannot gain a how-to", () => { |
| 35 | 35 | expect(allowed.length, "the list only shrinks: name the terminal door in the how-to instead of listing it").toBeLessThanOrEqual(CEILING); |
test/template-page.test.ts +27 −9
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | /** |
| 2 | * A template's page lists its top-level files and the README's first paragraph, escaped; unreadable | |
| 3 | * is 404. | |
| 2 | * A template's page lists its files, first issue, seats and the README's first paragraph, escaped; | |
| 3 | * unreadable is 404. | |
| 4 | 4 | */ |
| 5 | 5 | import { afterAll, beforeAll, expect, setDefaultTimeout, test } from "bun:test"; |
| 6 | 6 | import { mkdtemp, rm } from "node:fs/promises"; |
| @@ -42,6 +42,9 @@ beforeAll(async () => { | ||
| 42 | 42 | const forgeJsons: Record<string, string> = { |
| 43 | 43 | asky: JSON.stringify({ app: { run: "bun main.ts" }, firstAsk: "Add a <b>weekly</b> menu page" }), |
| 44 | 44 | }; |
| 45 | const extra: Record<string, { path: string; content: string }[]> = { | |
| 46 | big: Array.from({ length: 35 }, (_, i) => ({ path: `docs/n${String(i).padStart(2, "0")}.md`, content: "x" })), | |
| 47 | }; | |
| 45 | 48 | const make = async (name: string, visibility: "public" | "private", template: boolean) => { |
| 46 | 49 | const repo = await createRepo(app, owner, { name, visibility, description: "" }); |
| 47 | 50 | if (!repo.ok) throw new Error(repo.error); |
| @@ -52,6 +55,7 @@ beforeAll(async () => { | ||
| 52 | 55 | { path: "forge.json", content: forgeJsons[name] ?? '{"app":{"run":"bun main.ts"}}' }, |
| 53 | 56 | { path: "server.ts", content: "x" }, |
| 54 | 57 | { path: "src/a.ts", content: "x" }, |
| 58 | ...(extra[name] ?? []), | |
| 55 | 59 | ], |
| 56 | 60 | }); |
| 57 | 61 | if (!done.ok) throw new Error(String(done.error)); |
| @@ -62,6 +66,7 @@ beforeAll(async () => { | ||
| 62 | 66 | }; |
| 63 | 67 | await make("kit", "public", true); |
| 64 | 68 | await make("asky", "public", true); |
| 69 | await make("big", "public", true); | |
| 65 | 70 | await make("badask", "public", true); |
| 66 | 71 | // marked while valid, then changed to a value the check would refuse: the page ignores it |
| 67 | 72 | const bad = await commitToBranch(app, owner, getRepo(app.db, "giver", "badask")!, { |
| @@ -88,14 +93,14 @@ afterAll(async () => { | ||
| 88 | 93 | |
| 89 | 94 | const get = (path: string, cookie = "") => fetch(`${base}${path}`, { headers: { cookie } }); |
| 90 | 95 | |
| 91 | test("a public template lists its top-level files and the README's first paragraph, escaped", async () => { | |
| 96 | test("a public template lists its files and the README's first paragraph, escaped", async () => { | |
| 92 | 97 | const res = await get("/templates/giver/kit"); |
| 93 | 98 | expect(res.status).toBe(200); |
| 94 | 99 | const body = await res.text(); |
| 95 | 100 | expect(body).toContain("server.ts"); |
| 96 | 101 | expect(body).toContain("src/"); |
| 97 | 102 | expect(body).toContain("README.md"); |
| 98 | expect(body).not.toContain("a.ts"); | |
| 103 | expect(body).toContain("src/a.ts"); // the tree is listed in full since #15855 | |
| 99 | 104 | expect(body).toContain("<script>alert(1)</script> starter. Second line."); |
| 100 | 105 | expect(body).not.toContain("<script>alert(1)"); |
| 101 | 106 | expect(body).not.toContain("Not this."); |
| @@ -130,11 +135,24 @@ test("readmeParagraph skips the heading and fences and stops at the blank line", | ||
| 130 | 135 | expect(readmeParagraph("")).toBe(""); |
| 131 | 136 | }); |
| 132 | 137 | |
| 133 | test("a template that declares a first ask shows it, escaped; none declared or invalid shows nothing", async () => { | |
| 138 | test("a template that declares a first ask shows it once, under First issue, escaped; none declared says so", async () => { | |
| 134 | 139 | const shown = await (await get("/templates/giver/asky")).text(); |
| 135 | expect(shown).toContain('id="template-first-ask"'); | |
| 136 | expect(shown).toContain("Add a <b>weekly</b> menu page"); | |
| 140 | const issue = shown.split('id="template-first-issue"')[1]!.split("</p>")[0]!; | |
| 141 | expect(issue).toContain("Add a <b>weekly</b> menu page"); | |
| 137 | 142 | expect(shown).not.toContain("<b>weekly"); |
| 138 | expect(await (await get("/templates/giver/kit")).text()).not.toContain("template-first-ask"); | |
| 139 | expect(await (await get("/templates/giver/badask")).text()).not.toContain("template-first-ask"); | |
| 143 | expect(shown.split("menu page").length - 1).toBe(1); | |
| 144 | expect(shown).not.toContain("template-first-ask"); | |
| 145 | for (const none of ["kit", "badask"]) { | |
| 146 | const text = await (await get(`/templates/giver/${none}`)).text(); | |
| 147 | expect(text.split('id="template-first-issue"')[1]!.split("</p>")[0]!).toContain("None: you tell the crew what to build."); | |
| 148 | } | |
| 149 | }); | |
| 150 | ||
| 151 | test("past 30 paths the page lists 30 and says how many more", async () => { | |
| 152 | const text = await (await get("/templates/giver/big")).text(); | |
| 153 | const paths = text.split('id="template-paths"')[1]!.split("</dl>")[0]!; | |
| 154 | expect((paths.match(/status-row"/g) ?? []).length).toBe(30); | |
| 155 | // 35 docs + README.md, forge.json, server.ts, src/a.ts | |
| 156 | expect(text).toContain("and 9 more"); | |
| 157 | expect((await (await get("/templates/giver/kit")).text())).not.toContain(" more</p>"); | |
| 140 | 158 | }); |
test/template-preview-tui.test.ts added +63
| @@ -0,0 +1,63 @@ | ||
| 1 | /** | |
| 2 | * A template's page in the terminal: `p` on a /templates row shows the files it will create, its | |
| 3 | * first issue and the crew seats, the lines the page shows. | |
| 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 { PATHS_MAX, templateView } from "../src/core/template_view.ts"; | |
| 11 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 12 | import { previewLines } from "../src/tui/screens/template_preview.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 | beforeAll(async () => { | |
| 20 | dir = await mkdtemp(join(tmpdir(), "forge-template-preview-tui-")); | |
| 21 | app = createApp({ dataDir: dir, port: 0 }); | |
| 22 | }); | |
| 23 | afterAll(async () => { | |
| 24 | app.close(); | |
| 25 | await rm(dir, { recursive: true, force: true }); | |
| 26 | }); | |
| 27 | ||
| 28 | const key = (k: string) => ({ type: "key" as const, key: k, ctrl: false, alt: false, shift: false }); | |
| 29 | ||
| 30 | test("p on Starter shows its files, first issue and crew seats; the blank row has no page", async () => { | |
| 31 | const { app: tui, session } = createForgeApp( | |
| 32 | app, | |
| 33 | { sshUsername: "guest", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test" }, | |
| 34 | { terminal: headlessTerminal(120, 60), exitHooks: false }, | |
| 35 | ); | |
| 36 | tui.start(); | |
| 37 | const list = templatesScreen(session, tui.ctx); | |
| 38 | tui.ctx.push(list); | |
| 39 | await settleApp(tui); | |
| 40 | list.key?.(key("p")); // blank is first: nothing to open | |
| 41 | await settleApp(tui); | |
| 42 | expect(tui.screen.toText()).not.toContain("First issue"); | |
| 43 | list.key?.(key("j")); | |
| 44 | list.key?.(key("p")); | |
| 45 | await settleApp(tui); | |
| 46 | const text = tui.screen.toText(); | |
| 47 | expect(text).toContain("Files it will create"); | |
| 48 | expect(text).toContain("forge.json"); | |
| 49 | expect(text).toContain("First issue"); | |
| 50 | expect(text).toContain("None: you tell the crew what to build."); | |
| 51 | expect(text).toContain("Crew seats"); | |
| 52 | expect(text).toContain("builder"); | |
| 53 | expect(text).toContain("reviewer"); | |
| 54 | }); | |
| 55 | ||
| 56 | test("the lines carry the ask once and say how many paths are not shown", async () => { | |
| 57 | const v = await templateView(app, undefined, "starter"); | |
| 58 | if (v === undefined) throw new Error("no view"); | |
| 59 | const lines = previewLines({ ...v, firstAsk: "Add a menu", morePaths: 9 }); | |
| 60 | expect(lines.filter((l) => l.includes("Add a menu"))).toHaveLength(1); | |
| 61 | expect(lines).toContain(" and 9 more"); | |
| 62 | expect(v.paths.length).toBeLessThanOrEqual(PATHS_MAX); | |
| 63 | }); |
test/template-preview.test.ts +13
| @@ -53,3 +53,16 @@ test("a repository template with nothing to read has no preview", () => { | ||
| 53 | 53 | expect(String(previewLink("alice/full", "Full", { files: ["a"], more: 0, run: undefined, check: undefined }))).toContain('href="/templates/alice/full"'); |
| 54 | 54 | expect(String(previewLink("alice/full", "Full", { files: ["a"], more: 0, run: undefined, check: undefined }))).toContain('aria-label="Preview Full"'); |
| 55 | 55 | }); |
| 56 | ||
| 57 | test("a template's page lists its files, first issue and seats", async () => { | |
| 58 | const text = await (await fetch(`${servers.url}/templates/starter`)).text(); | |
| 59 | const paths = text.split('id="template-paths"')[1]!.split("</dl>")[0]!; | |
| 60 | expect(paths).toContain("<dt>forge.json</dt>"); | |
| 61 | expect((paths.match(/status-row"/g) ?? []).length).toBeLessThanOrEqual(30); | |
| 62 | expect(text).toContain('id="template-first-issue"'); | |
This passes for any page since the id is always rendered. Add a repository template with a firstAsk and assert the ask sits inside template-first-issue, plus a tree over 30 paths asserting 'and N more'. Also add the terminal screen and a test for it. |
||
| 63 | const seats = text.split('id="template-seats"')[1]!.split("</dl>")[0]!; | |
| 64 | expect(seats).toContain("builder"); | |
| 65 | expect(seats).toContain("reviewer"); | |
| 66 | expect((await fetch(`${servers.url}/templates/nope`)).status).toBe(404); | |
| 67 | expect(text).toContain("/forges/new?template=starter"); | |
| 68 | }); |
new above
offline
The comment says
builder (builder)style but SEATS buildsname: role. Fix the comment. Alsofilesabove is no longer read by the page; remove it and its top-level listTree read.