Public: anyone can read this; the crew can change it.
The templates page says what to do when there are none #14877
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/14865-the-templates-page-says-what-to-do-when → main 5 commits fork
6 files
changed +118
−2
docs/features/templates-empty.md added +13
| @@ -0,0 +1,13 @@ | ||
| 1 | --- | |
| 2 | topic: Your apps | |
| 3 | row: See what to do when no repository is a template yet | |
| 4 | --- | |
| 5 | # When there are no templates | |
| 6 | ||
| 7 | The built-in templates (Blank, Starter app) are always listed. When no repository the viewer can read is marked as a template, `/templates` says what to do next, in a plain line under the list. | |
| 8 | ||
| 9 | - **Admin:** "Mark a repository as a template", a link to `/repos`; the flag is in a repository's settings, App card. | |
| 10 | - **Everyone else:** "No templates yet", with no link and no error text. | |
| 11 | - **Filter:** the line is not shown while `?q=` is set; "No template matches that name." answers that. | |
| 12 | - **Terminal:** `/templates` prints the same words (`TEMPLATES_EMPTY`, `TEMPLATES_EMPTY_ADMIN`, `src/spawn/template_list.ts`). | |
| 13 | - Code: `src/web/pages/templates.ts`, `src/tui/screens/templates.ts`. Tests: `test/templates-empty.test.ts`, `test/templates-tui.test.ts`. |
src/spawn/template_list.ts +10
| @@ -57,3 +57,13 @@ export function filterTemplates<T extends { id: string; title: string; says?: st | ||
| 57 | 57 | if (needle === "") return [...rows]; |
| 58 | 58 | return rows.filter((r) => [r.title, r.id, r.says ?? ""].some((f) => f.toLowerCase().includes(needle))); |
| 59 | 59 | } |
| 60 | ||
| 61 | /** | |
| 62 | * No repository is marked as a template yet: the built-ins are all there is. Page and terminal say | |
| 63 | * it. | |
| 64 | */ | |
| 65 | export const TEMPLATES_EMPTY = "No templates yet"; | |
| 66 | export const TEMPLATES_EMPTY_ADMIN = "Mark a repository as a template"; | |
| 67 | ||
| 68 | /** True when the viewer can read no template repository (the built-ins do not count). */ | |
| 69 | export const noRepoTemplates = (rows: readonly TemplateRow[]): boolean => !rows.some((r) => templateOf(r.id).source === "repo"); |
src/tui/screens/templates.ts +5 −1
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import { DEFAULT_TEMPLATE } from "../../spawn/templates.ts"; |
| 8 | import { filterTemplates, TEMPLATES_QUERY_MAX, templateList, templatePath } from "../../spawn/template_list.ts"; | |
| 8 | import { filterTemplates, noRepoTemplates, TEMPLATES_EMPTY, TEMPLATES_EMPTY_ADMIN, TEMPLATES_QUERY_MAX, templateList, templatePath } from "../../spawn/template_list.ts"; | |
| 9 | 9 | import type { Ctx, Screen } from "../app/app.ts"; |
| 10 | 10 | import { binding } from "../app/keys.ts"; |
| 11 | 11 | import { style } from "../term/style.ts"; |
| @@ -45,6 +45,10 @@ export function templatesScreen(session: Session, ctx: Ctx): Screen { | ||
| 45 | 45 | y += 1; |
| 46 | 46 | if (on) for (const line of [...wrap(r.says, area.w - 6), useWords(used.get(r.id)), ...(r.id === DEFAULT_TEMPLATE ? [] : [`Share: ${templatePath(r.id)}`])]) if (y < area.y + area.h) s.text(area.x + 5, y++, truncate(line, area.w - 6), style(c.mutedFg, c.bg), area.x + area.w); |
| 47 | 47 | }); |
| 48 | if (q === "" && noRepoTemplates(listed) && y + 1 < area.y + area.h) { | |
| 49 | s.text(area.x + 1, y + 1, truncate(session.user?.isAdmin === true ? `${TEMPLATES_EMPTY_ADMIN}: repository settings, App card.` : TEMPLATES_EMPTY, area.w - 2), style(c.mutedFg, c.bg), area.x + area.w); | |
| 50 | y += 2; | |
| 51 | } | |
| 48 | 52 | if (session.user === undefined && y < area.y + area.h) |
| 49 | 53 | 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); |
| 50 | 54 | }, |
src/web/pages/templates.ts +6 −1
| @@ -10,7 +10,7 @@ import type { Ctx } from "../../http/context.ts"; | ||
| 10 | 10 | import { listChildren } from "../../actions/spawn.ts"; |
| 11 | 11 | import { DEFAULT_TEMPLATE } from "../../spawn/templates.ts"; |
| 12 | 12 | import { HOLDS_BLANK, HOLDS_NONE, holdsRows, templateHolds, type TemplateHolds } from "../../spawn/template_holds.ts"; |
| 13 | import { filterTemplates, TEMPLATES_QUERY_MAX, templateList, type TemplateRow } from "../../spawn/template_list.ts"; | |
| 13 | import { filterTemplates, TEMPLATES_EMPTY, noRepoTemplates, TEMPLATES_EMPTY_ADMIN, TEMPLATES_QUERY_MAX, templateList, type TemplateRow } from "../../spawn/template_list.ts"; | |
| 14 | 14 | import { withReadmeSays } from "../../spawn/template_says.ts"; |
| 15 | 15 | import { templateUse, useWords, type TemplateUse } from "../../spawn/template_use.ts"; |
| 16 | 16 | import { json } from "../../http/respond.ts"; |
| @@ -76,6 +76,11 @@ export async function templatesPage(ctx: Ctx): Promise<Response> { | ||
| 76 | 76 | ${rows.length === 0 |
| 77 | 77 | ? html`<p class="muted" id="templates-none" role="status">${TEMPLATES_NONE} <a href="/templates">Show all templates</a></p>` |
| 78 | 78 | : templateRows(rows, held, used)} |
| 79 | ${q === "" && noRepoTemplates(listed) | |
| 80 | ? user?.isAdmin === true | |
| 81 | ? html`<p class="muted" id="templates-empty" role="status"><a href="/repos">${TEMPLATES_EMPTY_ADMIN}</a>: repository settings, App card.</p>` | |
| 82 | : html`<p class="muted" id="templates-empty" role="status">${TEMPLATES_EMPTY}</p>` | |
| 83 | : null} | |
| 79 | 84 | </div>`, |
| 80 | 85 | }), |
| 81 | 86 | ); |
test/templates-empty.test.ts added +53
| @@ -0,0 +1,53 @@ | ||
| 1 | /** An empty /templates is a next step: admins get the link, others a plain line (#14865). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createSession } from "../src/core/sessions.ts"; | |
| 8 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 9 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 10 | ||
| 11 | let dir: string; | |
| 12 | let app: App; | |
| 13 | let servers: Servers; | |
| 14 | const cookie: Record<string, string> = {}; | |
| 15 | ||
| 16 | beforeAll(async () => { | |
| 17 | dir = await mkdtemp(join(tmpdir(), "forge-templates-empty-")); | |
| 18 | app = createApp({ dataDir: dir, url: "https://forge.test", port: 0, forgeRepo: "alice/forge-code" }); | |
| 19 | servers = await serve(app); | |
| 20 | for (const [name, first] of [["boss", true], ["guest", false]] as const) { | |
| 21 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: name, email: `${name}@x.test`, password: "a-good-passphrase", adminIfFirst: first }); | |
| 22 | if (!made.ok) throw new Error(made.error); | |
| 23 | const s = await createSession(app.db, getUserByName(app.db, name)!.id); | |
| 24 | cookie[name] = `forge_session=${s.token}`; | |
| 25 | } | |
| 26 | }, 30_000); | |
| 27 | ||
| 28 | afterAll(async () => { | |
| 29 | servers.stop(); | |
| 30 | app.close(); | |
| 31 | await rm(dir, { recursive: true, force: true }); | |
| 32 | }); | |
| 33 | ||
| 34 | const get = async (q: string, who?: string) => (await fetch(`${servers.url}/templates${q}`, { headers: who ? { cookie: cookie[who]! } : {} })).text(); | |
| 35 | ||
| 36 | test("an admin sees the link to mark a repository", async () => { | |
| 37 | const text = await get("", "boss"); | |
| 38 | expect(text).toContain('<a href="/repos">Mark a repository as a template</a>'); | |
| 39 | expect(text).not.toContain("No templates yet"); | |
| 40 | }); | |
| 41 | ||
| 42 | test("everyone else sees 'No templates yet', with no error text", async () => { | |
| 43 | for (const who of [undefined, "guest"]) { | |
| 44 | const text = await get("", who); | |
| 45 | expect(text).toContain("No templates yet"); | |
| 46 | expect(text).not.toContain("Mark a repository as a template"); | |
| 47 | expect(text).not.toMatch(/error|failed/i); | |
| 48 | } | |
| 49 | }); | |
| 50 | ||
| 51 | test("a filter that matches nothing does not claim there are no templates", async () => { | |
| 52 | expect(await get("?q=zzzz", "boss")).not.toContain("templates-empty"); | |
| 53 | }); |
test/templates-tui.test.ts +31
| @@ -150,3 +150,34 @@ test("a template's README sentence appears without a keypress and is searchable" | ||
| 150 | 150 | expect(text).toContain("A zesty little starter."); |
| 151 | 151 | t.tui.stop?.(); |
| 152 | 152 | }); |
| 153 | ||
| 154 | test("with no template repository the terminal says what the page says, per audience (#14865)", async () => { | |
| 155 | const none = await mkdtemp(join(tmpdir(), "forge-templates-tui-none-")); | |
| 156 | const bare = createApp({ dataDir: none, port: 0 }); | |
| 157 | const mk = async (username: string) => { | |
| 158 | const made = await createUser(bare.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: username === "boss" }); | |
| 159 | if (!made.ok) throw new Error(made.error); | |
| 160 | return made.value; | |
| 161 | }; | |
| 162 | const boss = await mk("boss"); | |
| 163 | const guest = await mk("guest"); | |
| 164 | const text = async (user: User) => { | |
| 165 | const { app: tui, session } = createForgeApp( | |
| 166 | bare, | |
| 167 | { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user }, | |
| 168 | { terminal: headlessTerminal(120, 40), exitHooks: false }, | |
| 169 | ); | |
| 170 | tui.start(); | |
| 171 | tui.ctx.push(templatesScreen(session, tui.ctx)); | |
| 172 | await settleApp(tui); | |
| 173 | const out = tui.screen.toText(); | |
| 174 | tui.stop?.(); | |
| 175 | return out; | |
| 176 | }; | |
| 177 | expect(await text(boss)).toContain("Mark a repository as a template"); | |
| 178 | const other = await text(guest); | |
| 179 | expect(other).toContain("No templates yet"); | |
| 180 | expect(other).not.toContain("Mark a repository as a template"); | |
| 181 | bare.close(); | |
| 182 | await rm(none, { recursive: true, force: true }); | |
| 183 | }); |
new above
offline