Public: anyone can read this; the crew can change it.
Every template under templates/ boots and answers / in under a second, in one test #13954
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13944-every-template-under-templates-boots-and → main 3 commits fork 2 threads · 2 unresolved
1 file
changed +112
−0
test/templates-boot.test.ts added +112
| @@ -0,0 +1,112 @@ | ||
| 1 | /** | |
| 2 | * Every template under templates/ boots: its files are written out, its | |
| 3 | * `forge.json` run command is started on a loopback port with a temp | |
| 4 | * FORGE_APP_DATA, and `/` must answer 200 in under a second of readiness. | |
| 5 | * A template that fails is named in the failure. Every process started is | |
| 6 | * killed, pass or fail. | |
| 7 | */ | |
| 8 | import { afterAll, describe, expect, test } from "bun:test"; | |
| 9 | import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { dirname, join } from "node:path"; | |
| 12 | ||
| 13 | const dir = join(import.meta.dir, "..", "templates"); | |
| 14 | const started: Array<ReturnType<typeof Bun.spawn>> = []; | |
| 15 | const scratch: string[] = []; | |
| 16 | ||
| 17 | afterAll(() => { | |
| 18 | for (const p of started) p.kill(); | |
| 19 | for (const d of scratch) rmSync(d, { recursive: true, force: true }); | |
| 20 | }); | |
| 21 | ||
| 22 | /** A bundle's files as path → content. */ | |
| 23 | function filesOf(name: string): Record<string, string> { | |
| 24 | const raw = JSON.parse(readFileSync(join(dir, name), "utf8")) as { files?: Record<string, string> }; | |
| 25 | return raw.files ?? {}; | |
| 26 | } | |
| 27 | ||
| 28 | async function freePort(): Promise<number> { | |
| 29 | const s = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: () => new Response("") }); | |
| 30 | const port = s.port as number; | |
| 31 | await s.stop(true); | |
| 32 | return port; | |
| 33 | } | |
| 34 | ||
| 35 | /** | |
| 36 | * Start a bundle's run command and wait (generously) until it answers `/`, | |
| 37 | * then time a second request: bun's start-up is not the app's speed. Throws | |
| 38 | * a message naming the template when it never answers. | |
| 39 | */ | |
| 40 | export async function boot(name: string, files: Record<string, string>): Promise<{ status: number; ms: number }> { | |
| 41 | const root = mkdtempSync(join(tmpdir(), "forge-tpl-boot-")); | |
| 42 | scratch.push(root); | |
| 43 | for (const [path, content] of Object.entries(files)) { | |
| 44 | if (path.includes("..") || path.startsWith("/")) throw new Error(`${name}: unsafe path ${path}`); | |
| 45 | mkdirSync(dirname(join(root, path)), { recursive: true }); | |
| 46 | writeFileSync(join(root, path), content); | |
| 47 | } | |
| 48 | let run: unknown; | |
| 49 | try { | |
| 50 | run = (JSON.parse(files["forge.json"] ?? "{}") as { app?: { run?: unknown } }).app?.run; | |
| 51 | } catch { | |
| 52 | throw new Error(`${name}: forge.json is not JSON`); | |
| 53 | } | |
| 54 | if (typeof run !== "string" || run === "") throw new Error(`${name}: forge.json names no app.run`); | |
| 55 | const port = await freePort(); | |
| 56 | const proc = Bun.spawn(["sh", "-c", `exec ${run}`], { | |
| 57 | cwd: root, | |
| 58 | env: { | |
| 59 | PATH: process.env.PATH ?? "", | |
| 60 | HOME: process.env.HOME ?? root, | |
| 61 | PORT: String(port), | |
| 62 | HOST: "127.0.0.1", | |
| 63 | FORGE_APP_DATA: join(root, "data"), | |
| 64 | }, | |
| 65 | stdout: "ignore", | |
| 66 | stderr: "ignore", | |
| 67 | }); | |
| 68 | started.push(proc); | |
| 69 | const url = `http://127.0.0.1:${port}/`; | |
| 70 | const deadline = performance.now() + 8000; | |
| 71 | while (performance.now() < deadline) { | |
| 72 | try { | |
| 73 | const res = await fetch(url, { redirect: "manual", signal: AbortSignal.timeout(1000) }); | |
| 74 | await res.arrayBuffer(); | |
| 75 | break; | |
| 76 | } catch { | |
| 77 | if (proc.exitCode !== null) throw new Error(`${name}: the run command exited with ${proc.exitCode}`); | |
| 78 | await Bun.sleep(25); // real-timer: polling a child process until it listens; there is no clock to inject | |
| 79 | } | |
| 80 | } | |
| 81 | // Listening now: this request is the one that is timed. | |
| 82 | const begun = performance.now(); | |
| 83 | try { | |
| 84 | const res = await fetch(url, { redirect: "manual", signal: AbortSignal.timeout(1000) }); | |
| 85 | await res.arrayBuffer(); | |
| 86 | return { status: res.status, ms: performance.now() - begun }; | |
| 87 | } catch { | |
outdated
Add a negative test showing that a template that does not boot is named in the failure. |
||
| 88 | throw new Error(`${name}: nothing answered / in time`); | |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | const names = readdirSync(dir).filter((n) => n.endsWith(".json")); | |
| 93 | ||
| 94 | describe("every template boots and answers /", () => { | |
| 95 | test("there is at least one template", () => { | |
| 96 | expect(names.length).toBeGreaterThan(0); | |
| 97 | }); | |
| 98 | for (const name of names) { | |
| 99 | test(`${name} answers / with 200 within a second`, async () => { | |
| 100 | const { status, ms } = await boot(name, filesOf(name)); | |
| 101 | expect(`${name} answered ${status}`).toBe(`${name} answered 200`); | |
| 102 | expect(ms).toBeLessThan(1000); | |
| 103 | }, 15000); | |
| 104 | } | |
| 105 | ||
| 106 | test("a template that does not boot fails, and is named", async () => { | |
| 107 | await expect(boot("broken-one", { "forge.json": JSON.stringify({ app: { run: "false" } }) })).rejects.toThrow( | |
| 108 | /broken-one.*exited with 1/, | |
| 109 | ); | |
| 110 | await expect(boot("no-run", { "forge.json": "{}" })).rejects.toThrow(/no-run: forge.json names no app.run/); | |
| 111 | }); | |
| 112 | }); |
new above
offline
Measure the request latency after the first successful answer, not time since spawn. Add a signal: AbortSignal.timeout(1000) to this fetch.