Public: anyone can read this; the crew can change it.
A template's page lists the files its forge starts with, from its repository, read-only #13432
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13407-a-template-s-page-lists-the-files-its-fo → main 9 commits fork 3 threads · 3 unresolved
16 files
changed +300
−44
docs/features/README.md +1
| @@ -222,6 +222,7 @@ Read with the guide page [Your apps](../guide-your-apps.md). | ||
| 222 | 222 | - [](app-asking.md) |
| 223 | 223 | - [](app-own-domain.md) |
| 224 | 224 | - [](app-templates.md) |
| 225 | - [](template-page.md) | |
| 225 | 226 | - [](archive-listing.md) |
| 226 | 227 | - [](binary-file.md) |
| 227 | 228 | - [](crawl-budget.md) |
docs/features/template-page.md added +5
| @@ -0,0 +1,5 @@ | ||
| 1 | # Template page | |
| 2 | ||
| 3 | `/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. | |
| 4 | ||
| 5 | Code: `src/web/pages/template.ts`, `src/core/template_view.ts`, `src/core/readme_paragraph.ts`. Test: `test/template-page.test.ts`. |
src/actions/talk.ts +4 −1
| @@ -68,7 +68,10 @@ export function talk(app: App, user: User | undefined, text: string, files: read | ||
| 68 | 68 | return said; |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | /** Edit the last message and resend it, regenerate the last reply, or retry a failed turn (#8817), in the named conversation or the latest. */ | |
| 71 | /** | |
| 72 | * Edit the last message and resend it, regenerate the last reply, or retry a failed turn (#8817), | |
| 73 | * in the named conversation or the latest. | |
| 74 | */ | |
| 72 | 75 | export function talkAgain(app: App, user: User | undefined, how: RedoHow, text = "", conversationId?: string): Result<Said, TalkRefusal> { |
| 73 | 76 | if (user === undefined) return err("unauthorized"); |
| 74 | 77 | const options = conversationOptions(app); |
src/core/readme_paragraph.ts added +35
| @@ -0,0 +1,35 @@ | ||
| 1 | /** | |
| 2 | * The first prose paragraph of a README as plain text, for a page that shows what a template is. | |
| 3 | * Pure: markdown in, text out (never more than `PARAGRAPH_MAX` characters). Headings, fences, | |
| 4 | * badges, tables and html lines are skipped; escaping stays with the page that draws it. | |
| 5 | */ | |
| 6 | ||
| 7 | export const PARAGRAPH_MAX = 600; | |
| 8 | ||
| 9 | export function readmeParagraph(markdown: string): string { | |
| 10 | let fence = ""; | |
| 11 | const lines: string[] = []; | |
| 12 | for (const raw of markdown.replace(/\r\n?/g, "\n").replace(/^---\n[\s\S]*?\n---\n/, "").split("\n")) { | |
| 13 | const line = raw.trim(); | |
| 14 | const f = /^(`{3,}|~{3,})/.exec(line); | |
| 15 | if (f) { | |
| 16 | if (fence === "") fence = f[1]![0]!; | |
| 17 | else if (f[1]![0] === fence) fence = ""; | |
| 18 | continue; | |
| 19 | } | |
| 20 | if (fence !== "") continue; | |
| 21 | if (line === "" || /^(#{1,6}\s|[-*_]{3,}$|={2,}$|-{2,}$|\||<|\[!\[|!\[)/.test(line)) { | |
| 22 | if (lines.length > 0) break; | |
| 23 | continue; | |
| 24 | } | |
| 25 | lines.push(line.replace(/^>\s?/, "")); | |
| 26 | } | |
| 27 | const text = lines | |
| 28 | .join(" ") | |
| 29 | .replace(/\[([^\]]*)\]\([^)]*\)/g, "$1") | |
| 30 | .replace(/[`*]/g, "") | |
| 31 | .replace(/[\u0000-\u001f\u007f]/g, " ") | |
| 32 | .replace(/\s+/g, " ") | |
| 33 | .trim(); | |
| 34 | return text.length > PARAGRAPH_MAX ? `${text.slice(0, PARAGRAPH_MAX - 1).trimEnd()}…` : text; | |
| 35 | } |
src/core/reserved.ts +1
| @@ -136,6 +136,7 @@ export const SYSTEM_NAMES: readonly string[] = [ | ||
| 136 | 136 | "tell", |
| 137 | 137 | "talk", |
| 138 | 138 | "notes", |
| 139 | "templates", | |
| 139 | 140 | "theme", |
| 140 | 141 | "verify", |
| 141 | 142 | "recover", |
src/core/template_view.ts added +48
| @@ -0,0 +1,48 @@ | ||
| 1 | /** | |
| 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, | |
| 4 | * not marked a template or not readable by the viewer is `undefined` (the same not-found for all). | |
| 5 | */ | |
| 6 | ||
| 7 | import type { App } from "../app.ts"; | |
| 8 | import { blobForRender, listTree, refTip } from "../git/read.ts"; | |
| 9 | import { repoDir } from "../git/repos.ts"; | |
| 10 | import { repoTemplateRef, TEMPLATES } from "../spawn/templates.ts"; | |
| 11 | import type { User } from "./users.ts"; | |
| 12 | import { readmeParagraph } from "./readme_paragraph.ts"; | |
| 13 | import { canRead, getRepo } from "./repos.ts"; | |
| 14 | ||
| 15 | const README_BYTES = 16 * 1024; | |
| 16 | const FILES_MAX = 200; | |
| 17 | ||
| 18 | export type TemplateView = { | |
| 19 | readonly title: string; | |
| 20 | readonly about: string; | |
| 21 | /** Top-level names; a folder ends in `/`. Empty for a built-in template. */ | |
| 22 | readonly files: readonly string[]; | |
| 23 | }; | |
| 24 | ||
| 25 | /** A built-in template by id, or a marked repository by `owner/name`. */ | |
| 26 | export async function templateView(app: App, viewer: User | undefined, ref: string): Promise<TemplateView | undefined> { | |
| 27 | const built = TEMPLATES.find((t) => t.id === ref); | |
| 28 | if (built) return { title: built.title, about: built.says, files: [] }; | |
| 29 | if (repoTemplateRef(ref) === undefined) return undefined; | |
| 30 | const [owner = "", name = ""] = ref.split("/"); | |
| 31 | const repo = getRepo(app.db, owner, name); | |
| 32 | if (!repo || !repo.template || !canRead(app.db, repo, viewer)) return undefined; | |
| 33 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 34 | let files: string[] = []; | |
| 35 | let about = ""; | |
| 36 | try { | |
| 37 | const tip = await refTip(dir, `refs/heads/${repo.defaultBranch}`); | |
| 38 | if (tip !== undefined) { | |
| 39 | const tree = await listTree(dir, tip, ""); | |
| 40 | if (tree.ok) files = tree.value.slice(0, FILES_MAX).map((e) => (e.type === "tree" ? `${e.name}/` : e.name)); | |
| 41 | const blob = await blobForRender(dir, tip, "README.md", README_BYTES); | |
| 42 | if (blob.kind === "text") about = readmeParagraph(new TextDecoder().decode(blob.body)); | |
| 43 | } | |
| 44 | } catch { | |
| 45 | // an unreadable repository shows what it can: the name and nothing else | |
| 46 | } | |
| 47 | return { title: `${repo.ownerName}/${repo.name}`, about, files }; | |
| 48 | } |
src/crew/converge.ts +3 −1
| @@ -313,7 +313,9 @@ export async function settleConverge( | ||
| 313 | 313 | app: App, |
| 314 | 314 | fork: Repo, |
| 315 | 315 | parent: Repo, |
| 316 | /** Whoever moved the fork's branch (a merge, a push): the only name the close may be said under. */ | |
| 316 | /** | |
| 317 | * Whoever moved the fork's branch (a merge, a push): the only name the close may be said under. | |
| 318 | */ | |
| 317 | 319 | by?: User, |
| 318 | 320 | ): Promise<boolean> { |
| 319 | 321 | const issue = convergeIssue(app, fork, parent); |
src/spawn/live.ts +4 −1
| @@ -16,7 +16,10 @@ export const SLOW_POLL_MS = 5000; | ||
| 16 | 16 | /** At most this many watchers at once per forge; a spawn past it is made but not timed. */ |
| 17 | 17 | export const MAX_WATCHERS = 20; |
| 18 | 18 | const open = new WeakMap<App, number>(); |
| 19 | /** Give up after this long: well past the 3-minute budget, so a slow forge still counts as live, late. */ | |
| 19 | /** | |
| 20 | * Give up after this long: well past the 3-minute budget, so a slow forge still counts as live, | |
| 21 | * late. | |
| 22 | */ | |
| 20 | 23 | export const GIVE_UP_MS = 10 * 60_000; |
| 21 | 24 | |
| 22 | 25 | export type Probe = () => Promise<boolean>; |
src/tui/screens/changelog.ts +14 −4
| @@ -51,7 +51,8 @@ export interface Line { | ||
| 51 | 51 | /** |
| 52 | 52 | * The landed pulls told in chapters, as the page tells them (`release/chapter_groups.ts` decides |
| 53 | 53 | * membership): a heading line per chapter, newest first, with its closing day or "open", then its |
| 54 | * pulls, one line each; pulls that served none sit under "Between chapters". `keep` narrows the pulls. | |
| 54 | * pulls, one line each; pulls that served none sit under "Between chapters". `keep` narrows the | |
| 55 | * pulls. | |
| 55 | 56 | */ |
| 56 | 57 | export function chapterLines(groups: readonly Group[], keep: (l: Landing) => boolean = () => true, filtering = false): Line[] { |
| 57 | 58 | const out: Line[] = []; |
| @@ -79,7 +80,10 @@ export function nearLines(near: readonly Near[]): string[] { | ||
| 79 | 80 | |
| 80 | 81 | type Repo = { owner: string; name: string }; |
| 81 | 82 | |
| 82 | /** Who built a pull, in plain words: "by Scout, with Claude Sonnet 5.5"; empty when the commit says nothing. */ | |
| 83 | /** | |
| 84 | * Who built a pull, in plain words: "by Scout, with Claude Sonnet 5.5"; empty when the commit says | |
| 85 | * nothing. | |
| 86 | */ | |
| 83 | 87 | export function creditWords(by: string | undefined, withNames: readonly string[] | undefined): string { |
| 84 | 88 | const parts = [by === undefined ? "" : `by ${by}`, withNames === undefined || withNames.length === 0 ? "" : `with ${withNames.join(", ")}`]; |
| 85 | 89 | return parts.filter((p) => p !== "").join(", "); |
| @@ -87,7 +91,10 @@ export function creditWords(by: string | undefined, withNames: readonly string[] | ||
| 87 | 91 | |
| 88 | 92 | type Pull = ReleaseEntry["pulls"][number]; |
| 89 | 93 | |
| 90 | /** A release's pulls as lines: pulls of one shape gathered ("Keys on … · 9 pulls"), the rest `#N title`. */ | |
| 94 | /** | |
| 95 | * A release's pulls as lines: pulls of one shape gathered ("Keys on … · 9 pulls"), the rest | |
| 96 | * `#N title`. | |
| 97 | */ | |
| 91 | 98 | export function pullLines(pulls: readonly Pull[], credits: ReadonlyMap<number, string>): { row: ListRow; pulls: readonly Pull[] }[] { |
| 92 | 99 | return gather(pulls).map((g) => |
| 93 | 100 | g.landings.length === 1 |
| @@ -96,7 +103,10 @@ export function pullLines(pulls: readonly Pull[], credits: ReadonlyMap<number, s | ||
| 96 | 103 | ); |
| 97 | 104 | } |
| 98 | 105 | |
| 99 | /** One release's pulls; Enter opens a pull as the changelog's Enter once did, and a gathered line lists its own pulls. */ | |
| 106 | /** | |
| 107 | * One release's pulls; Enter opens a pull as the changelog's Enter once did, and a gathered line | |
| 108 | * lists its own pulls. | |
| 109 | */ | |
| 100 | 110 | function releasePulls(f: Session, ctx: Ctx, repoRef: Repo, entry: ReleaseEntry, credits: ReadonlyMap<number, string>, title = entry.version): Screen { |
| 101 | 111 | const state = new ListState(); |
| 102 | 112 | const lines = pullLines(entry.pulls, credits); |
src/tui/screens/talk_chats.ts +4 −1
| @@ -39,7 +39,10 @@ const BAD_TITLE = `A title is up to ${TITLE_MAX} characters, on one line.`; | ||
| 39 | 39 | |
| 40 | 40 | const WHO: Readonly<Record<string, string>> = { person: "You", agent: "Forge", tool: "Tool", confirm: "Asks" }; |
| 41 | 41 | |
| 42 | /** The conversation as the Markdown download holds it, to read and scroll; the file itself is the web's /talk/:id/download. */ | |
| 42 | /** | |
| 43 | * The conversation as the Markdown download holds it, to read and scroll; the file itself is the | |
| 44 | * web's /talk/:id/download. | |
| 45 | */ | |
| 43 | 46 | function downloadScreen(f: Session, ctx: Ctx, chat: Conversation): Screen { |
| 44 | 47 | const scroll = new Scroll(); |
| 45 | 48 | const made = f.user === undefined ? undefined : conversationMarkdown(f.app.db, f.user.id, chat.id, publicOrigin(f.app.config.url, `http://localhost:${f.app.config.port}`)); |
src/web/pages/app_templates.ts +1 −1
| @@ -45,7 +45,7 @@ export function appTemplatesPage(ctx: Ctx): Response { | ||
| 45 | 45 | return row({ |
| 46 | 46 | lead: icon("rocket"), |
| 47 | 47 | title: html`<a class="mono" href="/${name}">${name}</a>`, |
| 48 | meta: html`${name === DEFAULT_TEMPLATE ? "Always offered, first." : repo.visibility === "private" ? "Private: offered only to people who can read it." : ""}`, | |
| 48 | meta: html`<a href="/templates/${name}">What it starts with</a> ${name === DEFAULT_TEMPLATE ? "Always offered, first." : repo.visibility === "private" ? "Private: offered only to people who can read it." : ""}`, | |
| 49 | 49 | trail: |
| 50 | 50 | name === DEFAULT_TEMPLATE |
| 51 | 51 | ? html`` |
src/web/pages/template.ts +34 −28
| @@ -1,45 +1,51 @@ | ||
| 1 | 1 | /** |
| 2 | * GET /templates/:name (a built-in template) and /templates/:owner/:name (a template repository): | |
| 3 | * what it is and, when a forge has been made from it, one line on how long the last took from | |
| 4 | * the click to its first live answer. The number is read from stored events | |
| 5 | * (`actions/template_timing.ts`), never measured here; with no spawn there is no line. | |
| 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`). | |
| 6 | 8 | */ |
| 7 | 9 | |
| 8 | 10 | import { templateTimingLine } from "../../actions/template_timing.ts"; |
| 9 | import { canRead, getRepo } from "../../core/repos.ts"; | |
| 11 | import { templateView } from "../../core/template_view.ts"; | |
| 10 | 12 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; |
| 11 | 13 | import type { Ctx } from "../../http/context.ts"; |
| 12 | import { DEFAULT_TEMPLATE, TEMPLATES, templateOf } from "../../spawn/templates.ts"; | |
| 14 | import { DEFAULT_TEMPLATE, TEMPLATES } from "../../spawn/templates.ts"; | |
| 13 | 15 | import { notFoundPage } from "../chrome.ts"; |
| 14 | 16 | import { html } from "../html.ts"; |
| 15 | 17 | import { page } from "../layout.ts"; |
| 18 | import { statusRow } from "./security.ts"; | |
| 16 | 19 | |
| 17 | export function templatePage(ctx: Ctx): Response { | |
| 20 | // GET /templates/:name (built-in) and /templates/:owner/:name | |
| 21 | export async function templatePage(ctx: Ctx): Promise<Response> { | |
| 18 | 22 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 19 | const owner = ctx.params["owner"]; | |
| 20 | const name = ctx.params["name"] ?? ""; | |
| 21 | let id: string; | |
| 22 | let link = html``; | |
| 23 | if (owner === undefined) { | |
| 24 | if (name === DEFAULT_TEMPLATE || !TEMPLATES.some((t) => t.id === name)) return notFoundPage(ctx); | |
| 25 | id = name; | |
| 26 | } else { | |
| 27 | const repo = getRepo(ctx.app.db, owner, name); | |
| 28 | if (repo === undefined || !repo.template || !canRead(ctx.app.db, repo, ctx.auth.user)) return notFoundPage(ctx); | |
| 29 | id = `${repo.ownerName}/${repo.name}`; | |
| 30 | link = html`<p><a href="/${id}">Open the repository</a></p>`; | |
| 31 | } | |
| 32 | const t = templateOf(id); | |
| 23 | const { owner, name = "" } = ctx.params; | |
| 24 | const built = owner === undefined; | |
| 25 | if (built && (name === DEFAULT_TEMPLATE || !TEMPLATES.some((t) => t.id === name))) return notFoundPage(ctx); | |
| 26 | const ref = built ? name : `${owner}/${name}`; | |
| 27 | const view = await templateView(ctx.app, ctx.auth.user, ref); | |
| 28 | if (!view) return notFoundPage(ctx); | |
| 33 | 29 | return timePhase(ctx.request, "render", () => { |
| 34 | const line = templateTimingLine(ctx.app.db, id); | |
| 30 | const line = templateTimingLine(ctx.app.db, ref); | |
| 35 | 31 | return page({ |
| 36 | 32 | ctx, |
| 37 | title: t.title, | |
| 38 | description: t.says, | |
| 39 | body: html`<div class="page-head"><h1>${t.title}</h1></div> | |
| 40 | <p class="muted">${t.says}</p> | |
| 41 | ${line === null ? null : html`<p class="small muted" id="template-timing">${line}</p>`} | |
| 42 | ${link}`, | |
| 33 | title: view.title, | |
| 34 | ...(built ? { description: view.about } : {}), | |
| 35 | body: html`<div class="page-head"><h1>${view.title}</h1></div> | |
| 36 | <section> | |
| 37 | <p>${built ? view.about : "A forge started from this template begins with these files."}</p> | |
| 38 | ${!built && view.about !== "" ? html`<p class="muted">${view.about}</p>` : null} | |
| 39 | ${built | |
| 40 | ? null | |
| 41 | : view.files.length === 0 | |
| 42 | ? html`<p class="muted">No files yet.</p>` | |
| 43 | : html`<dl class="status-rows">${view.files.map((f) => | |
| 44 | statusRow(f, html`${f.endsWith("/") ? "folder" : "file"}`), | |
| 45 | )}</dl>`} | |
| 46 | ${line === null ? null : html`<p class="small muted" id="template-timing">${line}</p>`} | |
| 47 | ${built ? null : html`<p class="muted"><a href="/${view.title}">Open the repository</a></p>`} | |
| 48 | </section>`, | |
| 43 | 49 | }); |
| 44 | 50 | }); |
| 45 | 51 | } |
test/fork-follow-conflict.test.ts +4 −2
| @@ -93,14 +93,16 @@ test("a conflicting update opens exactly one issue; a repeat and a clean update | ||
| 93 | 93 | expect(issue!.body).toContain(`Upstream commit: ${parentTip}`); |
| 94 | 94 | expect(issue!.body).toContain("bob: README.md"); |
| 95 | 95 | expect(issue!.body).toContain("one file changed on both sides: README.md."); |
| 96 | // Stopped: the fork's code is as it was, the update staged beside it and the default branch untouched. | |
| 96 | // Stopped: the fork's code is as it was, the update staged beside it and the default branch | |
| 97 | // untouched. | |
| 97 | 98 | const dir = repoDir(app.reposRoot, fork.ownerName, fork.name); |
| 98 | 99 | expect(await tipOf(fresh(fork))).toBe(before); |
| 99 | 100 | expect(await resolveOid(dir, "refs/following/main")).toBe(parentTip); |
| 100 | 101 | // Autonomy is off: the issue is for people, and no agent work is queued for it. |
| 101 | 102 | expect(listRuns(app.db, fork.id, { kind: "agent", limit: 50 }).length).toBe(0); |
| 102 | 103 | |
| 103 | // The same upstream commit asked again, with the first issue closed: nothing new, deterministically. | |
| 104 | // The same upstream commit asked again, with the first issue closed: nothing new, | |
| 105 | // deterministically. | |
| 104 | 106 | setIssueState(app.db, issue!.id, "closed", "dropped"); |
| 105 | 107 | const again = await convergeFork(app, fresh(fork), fresh(parent), undefined); |
| 106 | 108 | expect(again.kind).toBe("nothing"); |
test/no-polling.test.ts +19 −5
| @@ -36,10 +36,15 @@ const NO_TIMER_PAGES = ["/system", "/flow"]; | ||
| 36 | 36 | * that only the owner's own submit changes; a pushed patch would move a handle under a drag. */ |
| 37 | 37 | const STREAM_PAGES = ["/system"]; |
| 38 | 38 | |
| 39 | /** A page as the browser gets it: its markup, and every script it runs (fetched files and inline bodies), apart. */ | |
| 39 | /** | |
| 40 | * A page as the browser gets it: its markup, and every script it runs (fetched files and inline | |
| 41 | * bodies), apart. | |
| 42 | */ | |
| 40 | 43 | export interface RenderedPage { html: string; scripts: string[] } |
| 41 | 44 | |
| 42 | /** A stream a script opens. `data-live` alone, never `data-live-every`, the beat this guard forbids. */ | |
| 45 | /** | |
| 46 | * A stream a script opens. `data-live` alone, never `data-live-every`, the beat this guard forbids. | |
| 47 | */ | |
| 43 | 48 | const STREAM = /EventSource\s*\(|data-live(?![-\w])/; |
| 44 | 49 | |
| 45 | 50 | /** Script types that hold data, not code. */ |
| @@ -230,7 +235,10 @@ describe("no rendered page polls", () => { | ||
| 230 | 235 | * reads each page's markup and the scripts it loads. |
| 231 | 236 | */ |
| 232 | 237 | |
| 233 | /** The text of the balanced `(…)` or `{…}` whose opener is at `open`. Strings and comments are not understood: a bracket inside one skews it. */ | |
| 238 | /** | |
| 239 | * The text of the balanced `(…)` or `{…}` whose opener is at `open`. Strings and comments are not | |
| 240 | * understood: a bracket inside one skews it. | |
| 241 | */ | |
| 234 | 242 | function balanced(src: string, open: number): string { |
| 235 | 243 | const pairs: Record<string, string> = { "(": ")", "{": "}" }; |
| 236 | 244 | const o = src[open]!, c = pairs[o]!; |
| @@ -242,7 +250,10 @@ function balanced(src: string, open: number): string { | ||
| 242 | 250 | return src.slice(open + 1); |
| 243 | 251 | } |
| 244 | 252 | |
| 245 | /** The body of a function a script names: `function name(…) {…}` or `name = (…) => {…}` / `name = function (…) {…}`. */ | |
| 253 | /** | |
| 254 | * The body of a function a script names: `function name(…) {…}` or `name = (…) => {…}` / | |
| 255 | * `name = function (…) {…}`. | |
| 256 | */ | |
| 246 | 257 | function bodyOf(src: string, name: string): string | undefined { |
| 247 | 258 | const m = new RegExp(`(?:function\\s+${name}\\s*\\(|\\b${name}\\s*=\\s*(?:async\\s*)?(?:function\\s*\\(|\\([^)]*\\)\\s*=>|\\w+\\s*=>))`).exec(src); |
| 248 | 259 | if (!m) return undefined; |
| @@ -413,7 +424,10 @@ describe("every page the router lists", () => { | ||
| 413 | 424 | }); |
| 414 | 425 | }); |
| 415 | 426 | |
| 416 | /** Fewest HTML pages the admin walk reaches; observed when written. A page lost from the walk drops under it. */ | |
| 427 | /** | |
| 428 | * Fewest HTML pages the admin walk reaches; observed when written. A page lost from the walk drops | |
| 429 | * under it. | |
| 430 | */ | |
| 417 | 431 | const WALKED_FLOOR = 130; |
| 418 | 432 | /** The same, signed out (observed 44). */ |
| 419 | 433 | const ANON_FLOOR = 40; |
test/pages-budget.test.ts +2
| @@ -76,6 +76,7 @@ const FIXTURE: Record<string, string> = { | ||
| 76 | 76 | "/:owner/:name/display": "/alice/forge/display", |
| 77 | 77 | "/api/v1/admin/machines/:machine/disks": "/api/v1/admin/machines/rack1/disks", |
| 78 | 78 | "/claim/start/:token": "/claim/start/claim_fixture", |
| 79 | "/templates/:owner/:name": "/templates/alice/forge", // marked a template in the setup below | |
| 79 | 80 | }; |
| 80 | 81 | |
| 81 | 82 | function registeredGets(): Set<string> { |
| @@ -145,6 +146,7 @@ beforeAll(async () => { | ||
| 145 | 146 | ], |
| 146 | 147 | }); |
| 147 | 148 | if (!files.ok) throw new Error(JSON.stringify(files.error)); |
| 149 | app.db.run("UPDATE repo SET is_template = 1 WHERE id = ?1", [forge.id]); | |
| 148 | 150 | pin(app.db, alice.id, forge.id); |
| 149 | 151 | pin(app.db, alice.id, getRepo(app.db, "bob", "notes")!.id); |
| 150 | 152 | const issue = createIssue(app.db, { repoId: forge.id, authorId: bob.id, title: "An issue", body: "body" }); |
test/template-page.test.ts added +121
| @@ -0,0 +1,121 @@ | ||
| 1 | /** | |
| 2 | * A template's page lists its top-level files and the README's first paragraph, escaped; unreadable | |
| 3 | * is 404. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, setDefaultTimeout, 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 { commitToBranch, createRepo, updateRepoSettings } from "../src/actions/repos.ts"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { readmeParagraph } from "../src/core/readme_paragraph.ts"; | |
| 12 | import { getRepo } from "../src/core/repos.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | import { quietLogs } from "./helpers/globals.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | setDefaultTimeout(30_000); | |
| 19 | ||
| 20 | let dir: string; | |
| 21 | let app: App; | |
| 22 | let servers: Servers; | |
| 23 | let base: string; | |
| 24 | let strangerCookie: string; | |
| 25 | const cookies: Record<string, string> = {}; | |
| 26 | ||
| 27 | beforeAll(async () => { | |
| 28 | dir = await mkdtemp(join(tmpdir(), "forge-tplpage-")); | |
| 29 | app = createApp({ dataDir: dir, port: 0 }); | |
| 30 | servers = await serve(app); | |
| 31 | base = servers.url; | |
| 32 | const mk = async (username: string, adminIfFirst = false) => { | |
| 33 | const made = await createUser(app.db, { | |
| 34 | origin: "seed", trusted: true, username, email: `${username}@x.test`, | |
| 35 | password: "a-good-passphrase", adminIfFirst, | |
| 36 | }); | |
| 37 | if (!made.ok) throw new Error(made.error); | |
| 38 | return made.value; | |
| 39 | }; | |
| 40 | const owner = await mk("giver", true); | |
| 41 | await mk("stranger"); | |
| 42 | const make = async (name: string, visibility: "public" | "private", template: boolean) => { | |
| 43 | const repo = await createRepo(app, owner, { name, visibility, description: "" }); | |
| 44 | if (!repo.ok) throw new Error(repo.error); | |
| 45 | const done = await commitToBranch(app, owner, repo.value, { | |
| 46 | message: "t", | |
| 47 | files: [ | |
| 48 | { path: "README.md", content: `# ${name}\n\nA <script>alert(1)</script> starter.\nSecond line.\n\nNot this.\n` }, | |
| 49 | { path: "forge.json", content: '{"app":{"run":"bun main.ts"}}' }, | |
| 50 | { path: "server.ts", content: "x" }, | |
| 51 | { path: "src/a.ts", content: "x" }, | |
| 52 | ], | |
| 53 | }); | |
| 54 | if (!done.ok) throw new Error(String(done.error)); | |
| 55 | if (template) { | |
| 56 | const r = await updateRepoSettings(app, owner, getRepo(app.db, "giver", name)!, { template: true }); | |
| 57 | if (!r.ok) throw new Error(String(r.error)); | |
| 58 | } | |
| 59 | }; | |
| 60 | await make("kit", "public", true); | |
| 61 | await make("secret", "private", true); | |
| 62 | await make("plain", "public", false); | |
| 63 | for (const who of ["stranger", "giver"]) { | |
| 64 | const res = await fetch(`${base}/signin`, { | |
| 65 | method: "POST", | |
| 66 | body: new URLSearchParams({ username: who, password: "a-good-passphrase" }), | |
| 67 | redirect: "manual", | |
| 68 | }); | |
| 69 | cookies[who] = (res.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 70 | } | |
| 71 | strangerCookie = cookies.stranger!; | |
| 72 | }); | |
| 73 | afterAll(async () => { | |
| 74 | servers.stop(); | |
| 75 | app.close(); | |
| 76 | await rm(dir, { recursive: true, force: true }); | |
| 77 | }); | |
| 78 | ||
| 79 | const get = (path: string, cookie = "") => fetch(`${base}${path}`, { headers: { cookie } }); | |
| 80 | ||
| 81 | test("a public template lists its top-level files and the README's first paragraph, escaped", async () => { | |
| 82 | const res = await get("/templates/giver/kit"); | |
| 83 | expect(res.status).toBe(200); | |
| 84 | const body = await res.text(); | |
| 85 | expect(body).toContain("server.ts"); | |
| 86 | expect(body).toContain("src/"); | |
| 87 | expect(body).toContain("README.md"); | |
| 88 | expect(body).not.toContain("a.ts"); | |
| 89 | expect(body).toContain("<script>alert(1)</script> starter. Second line."); | |
| 90 | expect(body).not.toContain("<script>alert(1)"); | |
| 91 | expect(body).not.toContain("Not this."); | |
| 92 | }); | |
| 93 | ||
| 94 | test("a private template is 404 to a stranger and to nobody signed in", async () => { | |
| 95 | expect((await get("/templates/giver/secret", strangerCookie)).status).toBe(404); | |
| 96 | expect((await get("/templates/giver/secret")).status).toBe(404); | |
| 97 | }); | |
| 98 | ||
| 99 | test("the owner, who is also the forge's admin, sees their own private template", async () => { | |
| 100 | const res = await get("/templates/giver/secret", cookies.giver ?? ""); | |
| 101 | expect(res.status).toBe(200); | |
| 102 | expect(await res.text()).toContain("server.ts"); | |
| 103 | }); | |
| 104 | ||
| 105 | test("a missing repository and a repository not marked as a template are 404", async () => { | |
| 106 | expect((await get("/templates/giver/nope")).status).toBe(404); | |
| 107 | expect((await get("/templates/giver/plain")).status).toBe(404); | |
| 108 | expect((await get("/templates/..%2Fx/y")).status).toBe(404); | |
| 109 | }); | |
| 110 | ||
| 111 | test("a built-in template shows its words", async () => { | |
| 112 | const res = await get("/templates/starter"); | |
| 113 | expect(res.status).toBe(200); | |
| 114 | expect(await res.text()).toContain("A server, a page, tests"); | |
| 115 | expect((await get("/templates/nothing")).status).toBe(404); | |
| 116 | }); | |
| 117 | ||
| 118 | test("readmeParagraph skips the heading and fences and stops at the blank line", () => { | |
| 119 | expect(readmeParagraph("# T\n\n```\ncode\n```\n\nOne *two*\nthree.\n\nfour")).toBe("One two three."); | |
| 120 | expect(readmeParagraph("")).toBe(""); | |
| 121 | }); |
new above
offline
Both doors: this page has no terminal screen in src/tui/. Add one that reads through the same canRead gate and the same listing, and wire its entry as AGENTS.md says.