Public: anyone can read this; the crew can change it.
scripts/smoke.ts waits for --version on /healthz and checks that /sw.js never answers a navigation with an error #2362
scout/forge:scout/2340-scripts-smoke-ts-waits-for-version-on-he → main 1 commit fork 3 threads · 3 unresolved
2 files
changed +120
−6
scripts/smoke.ts +75 −3
| @@ -4,6 +4,9 @@ | ||
| 4 | 4 | * `/healthz` is fetched once per user agent and must answer 200; the |
| 5 | 5 | * stylesheet linked from `/` must be a non-empty `text/css` 200. Exit 1 and |
| 6 | 6 | * one line per failure (url, user agent, status, content type) otherwise. |
| 7 | * `--version <v>` first polls `/healthz` until `X-Forge-Version` is `<v>`, and | |
| 8 | * `/sw.js` must be a 200 JavaScript response that never answers a navigation | |
| 9 | * with `Response.error()`. | |
| 7 | 10 | */ |
| 8 | 11 | |
| 9 | 12 | export const AGENTS: Record<string, string> = { |
| @@ -32,6 +35,55 @@ export function stylesheetHref(html: string): string | null { | ||
| 32 | 35 | return null; |
| 33 | 36 | } |
| 34 | 37 | |
| 38 | /** `source` with comments removed, so a word in a comment is not code. */ | |
| 39 | function stripComments(source: string): string { | |
| 40 | return source.replace(/\/\*[\s\S]*?\*\//g, "").replace(/(^|[^:\\'"`])\/\/[^\n]*/g, "$1"); | |
| 41 | } | |
| 42 | ||
| 43 | /** The code of every `mode === 'navigate'` branch: from the test to its block's closing brace. */ | |
| 44 | export function navigationBranches(source: string): string[] { | |
| 45 | const code = stripComments(source); | |
| 46 | const out: string[] = []; | |
| 47 | for (const m of code.matchAll(/\.mode\s*===?\s*(["'])navigate\1/g)) { | |
| 48 | const open = code.indexOf("{", m.index); | |
| 49 | if (open < 0) continue; | |
| 50 | let depth = 0; | |
| 51 | let end = code.length; | |
| 52 | for (let i = open; i < code.length; i++) { | |
| 53 | if (code[i] === "{") depth++; | |
| 54 | else if (code[i] === "}" && --depth === 0) { | |
| 55 | end = i + 1; | |
| 56 | break; | |
| 57 | } | |
| 58 | } | |
| 59 | out.push(code.slice(open, end)); | |
| 60 | } | |
| 61 | return out; | |
| 62 | } | |
| 63 | ||
| 64 | /** Polls `/healthz` until it reports `version`; the last version seen when it never does, else null. */ | |
| 65 | export async function waitForVersion( | |
| 66 | base: string, | |
| 67 | version: string, | |
| 68 | { limitMs = 60_000, everyMs = 1_000 }: { limitMs?: number; everyMs?: number } = {}, | |
| 69 | ): Promise<string | null> { | |
| 70 | const url = new URL("/healthz", base).href; | |
| 71 | const until = Date.now() + limitMs; | |
| 72 | let seen = "none"; | |
| 73 | for (;;) { | |
| 74 | try { | |
| 75 | const res = await fetch(url, { redirect: "manual", signal: AbortSignal.timeout(5_000) }); | |
| 76 | seen = res.headers.get("x-forge-version") ?? "none"; | |
| 77 | await res.body?.cancel(); | |
| 78 | if (seen === version) return null; | |
| 79 | } catch (error) { | |
| 80 | seen = `unreachable (${error})`; | |
| 81 | } | |
| 82 | if (Date.now() >= until) return seen; | |
| 83 | await Bun.sleep(everyMs); | |
| 84 | } | |
| 85 | } | |
| 86 | ||
| 35 | 87 | /** Runs every check against `base`; returns the failures (empty is green). */ |
| 36 | 88 | export async function smoke(base: string, token = ""): Promise<Failure[]> { |
| 37 | 89 | const root = new URL(base); |
| @@ -78,16 +130,36 @@ export async function smoke(base: string, token = ""): Promise<Failure[]> { | ||
| 78 | 130 | if (board && board.res.status !== 200) fail(board.url, agent, board.res, "expected 200 (a 302 is sign-in)"); |
| 79 | 131 | const health = await get("/healthz", agent); |
| 80 | 132 | if (health && health.res.status !== 200) fail(health.url, agent, health.res, "expected 200"); |
| 133 | const sw = await get("/sw.js", agent); | |
| 134 | if (sw) { | |
| 135 | const type = sw.res.headers.get("content-type") ?? ""; | |
| 136 | if (sw.res.status !== 200) fail(sw.url, agent, sw.res, "expected 200"); | |
| 137 | else if (!/^(text|application)\/(javascript|x-javascript|ecmascript)\b/i.test(type)) fail(sw.url, agent, sw.res, "expected JavaScript"); | |
navigationBranches(sw.body) runs twice; bind it once to a const. |
||
| 138 | else if (navigationBranches(sw.body).length === 0) fail(sw.url, agent, sw.res, "no navigation branch found"); | |
| 139 | else if (navigationBranches(sw.body).some((b) => /Response\s*\.\s*error\s*\(/.test(b))) | |
| 140 | fail(sw.url, agent, sw.res, "a navigation can answer Response.error()"); | |
| 141 | } | |
| 81 | 142 | } |
| 82 | 143 | return failures; |
| 83 | 144 | } |
| 84 | 145 | |
| 85 | 146 | if (import.meta.main) { |
| 86 | const base = process.argv[2]; | |
| 87 | if (!base) { | |
| 88 | console.error("usage: bun scripts/smoke.ts <url>"); | |
| 147 | const args = process.argv.slice(2); | |
| 148 | let version = ""; | |
| 149 | const at = args.indexOf("--version"); | |
| 150 | if (at >= 0) version = args.splice(at, 2)[1] ?? ""; | |
| 151 | const base = args[0]; | |
| 152 | if (!base || (at >= 0 && !version)) { | |
| 153 | console.error("usage: bun scripts/smoke.ts <url> [--version <v>]"); | |
| 89 | 154 | process.exit(2); |
| 90 | 155 | } |
| 156 | if (version) { | |
| 157 | const seen = await waitForVersion(base, version); | |
| 158 | if (seen !== null) { | |
| 159 | console.error(`FAIL ${new URL("/healthz", base).href} expected X-Forge-Version=${version}, last saw ${seen}`); | |
| 160 | process.exit(1); | |
| 161 | } | |
| 162 | } | |
| 91 | 163 | const failures = await smoke(base, process.env["FORGE_TOKEN"] ?? ""); |
| 92 | 164 | for (const f of failures) |
| 93 | 165 | console.error(`FAIL ${f.url} agent=${f.agent} status=${f.status} content-type=${f.contentType || "-"} (${f.why})`); |
test/smoke.test.ts +45 −3
| @@ -1,10 +1,16 @@ | ||
| 1 | 1 | import { afterEach, expect, test } from "bun:test"; |
| 2 | import { smoke, stylesheetHref } from "../scripts/smoke.ts"; | |
| 2 | import { swSource } from "../src/web/sw.ts"; | |
| 3 | import { navigationBranches, smoke, stylesheetHref, waitForVersion } from "../scripts/smoke.ts"; | |
| 3 | 4 | |
| 4 | 5 | let server: ReturnType<typeof Bun.serve> | undefined; |
| 5 | 6 | afterEach(() => server?.stop(true)); |
| 6 | 7 | |
| 7 | function stub(cssType: string, seen: string[] = []) { | |
| 8 | const GOOD_SW = swSource; | |
| 9 | const BAD_SW = `self.addEventListener("fetch", (e) => { | |
| 10 | if (e.request.mode === "navigate") { e.respondWith(fetch(e.request).catch(() => Response.error())); return; } | |
| 11 | });`; | |
| 12 | ||
| 13 | function stub(cssType: string, seen: string[] = [], sw: string = GOOD_SW, healthz: () => Response = () => new Response("ok")) { | |
| 8 | 14 | server = Bun.serve({ |
| 9 | 15 | port: 0, |
| 10 | 16 | hostname: "127.0.0.1", |
| @@ -17,7 +23,8 @@ function stub(cssType: string, seen: string[] = []) { | ||
| 17 | 23 | return req.headers.get("authorization") === "Bearer t" |
| 18 | 24 | ? new Response("board") |
| 19 | 25 | : new Response(null, { status: 302, headers: { location: "/signin" } }); |
| 20 | if (path === "/healthz") return new Response("ok"); | |
| 26 | if (path === "/healthz") return healthz(); | |
| 27 | if (path === "/sw.js") return new Response(sw, { headers: { "content-type": "text/javascript; charset=utf-8" } }); | |
| 21 | 28 | return new Response("no", { status: 404 }); |
| 22 | 29 | }, |
| 23 | 30 | }); |
| @@ -56,3 +63,38 @@ test("stylesheetHref finds the link", () => { | ||
| 56 | 63 | expect(stylesheetHref('<link rel="icon" href="/x"><link rel="stylesheet" href="/s.css">')).toBe("/s.css"); |
| 57 | 64 | expect(stylesheetHref("<p>none</p>")).toBeNull(); |
| 58 | 65 | }); |
| 66 | ||
| 67 | test("the real service worker passes; Response.error() on navigation fails", async () => { | |
| 68 | expect(navigationBranches(swSource).length).toBeGreaterThan(0); | |
| 69 | const failures = await smoke(stub("text/css", [], BAD_SW), "t"); | |
| 70 | expect(failures.length).toBe(2); | |
| 71 | expect(failures[0]!.url).toEndWith("/sw.js"); | |
| 72 | expect(failures[0]!.why).toContain("Response.error()"); | |
| 73 | }); | |
| 74 | ||
| 75 | test("Response.error() outside a navigation, or in a comment, is fine", async () => { | |
| 76 | const sw = `// Response.error()\nself.onfetch = (e) => { if (e.request.mode === "navigate") { /* Response.error() */ return; } return Response.error(); };`; | |
| 77 | expect(await smoke(stub("text/css", [], sw), "t")).toEqual([]); | |
| 78 | }); | |
| 79 | ||
| 80 | test("a sw.js that is not 200 JavaScript fails", async () => { | |
| 81 | server = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: () => new Response("x", { headers: { "content-type": "text/html" } }) }); | |
| 82 | const failures = await smoke(`http://127.0.0.1:${server.port}`, "t"); | |
| 83 | expect(failures.some((f) => f.url.endsWith("/sw.js") && f.why === "expected JavaScript")).toBe(true); | |
| 84 | }); | |
| 85 | ||
| 86 | test("a version that arrives late passes, one that never arrives fails with the last seen", async () => { | |
| 87 | let n = 0; | |
| 88 | const late = stub("text/css", [], GOOD_SW, () => new Response("ok", { headers: { "x-forge-version": ++n >= 3 ? "v2" : "v1" } })); | |
| 89 | expect(await waitForVersion(late, "v2", { limitMs: 5000, everyMs: 5 })).toBeNull(); | |
| 90 | server?.stop(true); | |
| 91 | const never = stub("text/css", [], GOOD_SW, () => new Response("ok", { headers: { "x-forge-version": "v1" } })); | |
| 92 | expect(await waitForVersion(never, "v2", { limitMs: 100, everyMs: 10 })).toBe("v1"); | |
| 93 | }); | |
| 94 | ||
| 95 | test("the CLI with --version passes once /healthz reports it", async () => { | |
| 96 | const seen: string[] = []; | |
| 97 | const url = stub("text/css", seen, GOOD_SW, () => new Response("ok", { headers: { "x-forge-version": "v1" } })); | |
| 98 | const ok = Bun.spawn(["bun", "scripts/smoke.ts", url, "--version", "v1"], { env: { ...process.env, FORGE_TOKEN: "t" }, stdout: "pipe", stderr: "pipe" }); | |
| 99 | expect(await ok.exited).toBe(0); | |
| 100 | }); | |
Only the passing CLI run is covered. Add the never-arrives case (exit 1) and the empty --version case (exit 2). |
new above
offline
The brace scan assumes the navigate branch is braced; an unbraced if would take a later block or none. Handle it or document it.