Public: anyone can read this; the crew can change it.

A template on /templates links to a read-only live preview of what a new forge from it looks like #14698

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14675-a-template-on-templates-links-to-a-read → main 4 commits fork 5 threads · 5 unresolved

  • Merged. scout/14675-a-template-on-templates-links-to-a-read was merged into main as 4b14556.

Review

Sign in to review this change.
4 files changed +76 −1
docs/features/template-holds.md +4
@@ -19,3 +19,7 @@ template and its lines (apps, starter budget, model default) now end with `Files
1919 `templateHolds` the page uses.
2020
2121 Pinned by `test/templates-page.test.ts` and `test/tui-app-templates.test.ts`.
22
23 Each row also carries one link, **Preview**, to the template's read-only page (`/templates/<id>`, an ordinary page with
24 the same security headers), or says "no preview yet" when there is nothing to show: Blank, or a template repository with
25 no commits. In the terminal the lines under the cursor read `Share:` and `Preview:` with that same path.

Non-blocking: this says the terminal shows a Preview: line, but you removed it and only Share: remains. Correct the sentence.

src/web/pages/templates.ts +2 −1
@@ -15,6 +15,7 @@ import { templateUse, useWords, type TemplateUse } from "../../spawn/template_us
1515 import { pageHead } from "../chrome.ts";
1616 import { html, type Html } from "../html.ts";
1717 import { page } from "../layout.ts";
18 import { previewLink } from "../template_card.ts";
1819 import { btn } from "../ui.ts";
1920
2021 export const TEMPLATES_WORDS = "Pick where your forge starts. You can change everything after.";
@@ -38,7 +39,7 @@ export function templateRows(rows: readonly TemplateRow[], held?: ReadonlyMap<st
3839 (r) => html`<li data-template="${r.id}">
3940 <form method="get" action="/forges/new" class="row">
4041 <input type="hidden" name="template" value="${r.id}">
41 <span><b>${r.title}</b> ${r.says}</span>
42 <span><b>${r.title}</b> ${r.says}${held === undefined ? null : html` ${previewLink(r.id, r.title, held.get(r.id))}`}</span>
4243 ${btn(`Start a forge from ${r.title}`, { variant: "secondary" })}
4344 </form>
4445 ${used === undefined ? null : html`<p class="small muted">${useWords(used.get(r.id))}</p>`}
outdated

A separate <p> per row adds a stacked line; fold it into the row or the holds dl (principles 3 and 7).

src/web/template_card.ts +15
@@ -10,6 +10,9 @@ import { readFileAt } from "../apps/checkout.ts";
1010 import type { Repo } from "../core/repos.ts";
1111 import { resolveOid } from "../git/merge.ts";
1212 import { repoDir } from "../git/repos.ts";
13 import { DEFAULT_TEMPLATE } from "../spawn/templates.ts";
14 import { templatePath } from "../spawn/template_list.ts";
15 import type { TemplateHolds } from "../spawn/template_holds.ts";
1316 import { templateFacts, templateRows, type TemplateFacts } from "../spawn/template_facts.ts";
1417 import { html, type Html } from "./html.ts";
1518
@@ -32,3 +35,15 @@ export function templateCardHtml(facts: TemplateFacts): Html {
3235 export async function templateCard(app: App, repo: Repo): Promise<Html | null> {
3336 return repo.template ? templateCardHtml(await templateFactsOf(app, repo)) : null;
3437 }
38
39 export const PREVIEW_NONE = "no preview yet";
40
41 /**
42 * The one preview link on a template's row (#14675): the template's own read-only page
43 * (`/templates/...`, an ordinary page with the same security headers) when there is something to
44 * show (it holds files), else the words "no preview yet". Blank holds nothing, so it never has one.
45 */
46 export function previewLink(id: string, title: string, holds: TemplateHolds | undefined): Html {
47 if (id === DEFAULT_TEMPLATE || holds === undefined) return html`<span class="small muted" data-preview="none">${PREVIEW_NONE}</span>`;
48 return html`<a class="small" data-preview="link" href="${templatePath(id)}" aria-label="Preview ${title}">Preview</a>`;
outdated

Link text 'Preview' is identical on every row; give it a per-template name (aria-label or 'Preview ${title}'). Principle 8 and keyboard/screen-reader reach.

49 }
test/template-preview.test.ts added +55
@@ -0,0 +1,55 @@
1 /** A template's row links to its read-only preview, or says "no preview yet" (#14675). */
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 { serve, type Servers } from "../src/http/server.ts";
8 import { previewLink } from "../src/web/template_card.ts";
9
10 let dir: string;
11 let app: App;
12 let servers: Servers;
13
14 beforeAll(async () => {
15 dir = await mkdtemp(join(tmpdir(), "forge-template-preview-"));
16 app = createApp({ dataDir: dir, url: "https://forge.test", port: 0, forgeRepo: "alice/forge-code" });
17 servers = await serve(app);
18 }, 30_000);
19
20 afterAll(async () => {
21 servers.stop();
22 app.close();
23 await rm(dir, { recursive: true, force: true });
24 });
25
26 const row = (text: string, id: string): string => text.split(`data-template="${id}"`)[1]!.split("</li>")[0]!;
27
28 test("starter links to its preview, blank says no preview yet", async () => {
29 const res = await fetch(`${servers.url}/templates`);
30 const text = await res.text();
31 expect(row(text, "starter")).toContain('href="/templates/starter"');
32 expect(row(text, "starter")).toContain('aria-label="Preview Starter app"');
33 expect(row(text, "starter")).not.toContain("no preview yet");
34 expect((text.match(/<form[^>]*method="post"[^>]*>/gi) ?? []).filter((f) => !f.includes('action="/theme"'))).toEqual([]);
35 expect(row(text, "blank")).toContain("no preview yet");
36 expect(row(text, "blank")).not.toContain("/templates/blank");
37 });
38
39 test("the preview is read-only and carries the security headers", async () => {
40 const res = await fetch(`${servers.url}/templates/starter`);
41 expect(res.status).toBe(200);
42 expect(res.headers.get("content-security-policy")).toBeTruthy();
43 expect(res.headers.get("x-content-type-options")).toBe("nosniff");
44 expect(res.headers.get("referrer-policy")).toBeTruthy();
outdated

not.toBe(200) passes on a 500; assert the exact status, and assert the preview page has no mutating form.

45 const post = await fetch(`${servers.url}/templates/starter`, { method: "POST" });
46 expect([404, 405]).toContain(post.status);
47 const text = await (await fetch(`${servers.url}/templates/starter`)).text();
48 expect((text.match(/<form[^>]*method="post"[^>]*>/gi) ?? []).filter((f) => !f.includes('action="/theme"'))).toEqual([]);
49 });
50
51 test("a repository template with nothing to read has no preview", () => {
52 expect(String(previewLink("alice/empty", "Empty", undefined))).toContain("no preview yet");
53 expect(String(previewLink("alice/full", "Full", { files: ["a"], more: 0, run: undefined, check: undefined }))).toContain('href="/templates/alice/full"');
54 expect(String(previewLink("alice/full", "Full", { files: ["a"], more: 0, run: undefined, check: undefined }))).toContain('aria-label="Preview Full"');
55 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs