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 #2407
scout/forge:scout/2388-scripts-smoke-ts-waits-for-version-on-he → main 2 commits fork
2 files
changed +101
−6
scripts/smoke.ts +61 −3
| @@ -4,6 +4,10 @@ | ||
| 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 | * `/sw.js` must be a 200 JavaScript response whose navigation branch never | |
| 8 | * answers with `Response.error()`. With `--version <v>` the script first polls | |
| 9 | * `/healthz` until `X-Forge-Version` is `<v>`, for `--wait <seconds>` (default | |
| 10 | * 120), and fails naming the last version seen. | |
| 7 | 11 | */ |
| 8 | 12 | |
| 9 | 13 | export const AGENTS: Record<string, string> = { |
| @@ -32,6 +36,34 @@ export function stylesheetHref(html: string): string | null { | ||
| 32 | 36 | return null; |
| 33 | 37 | } |
| 34 | 38 | |
| 39 | /** The part of a service worker's source that handles navigations, comments removed. */ | |
| 40 | export function navigationBranch(source: string): string { | |
| 41 | const start = source.indexOf("mode === 'navigate'"); | |
| 42 | if (start < 0) return ""; | |
| 43 | const end = source.indexOf("return;", start); | |
| 44 | return source.slice(start, end < 0 ? undefined : end).replace(/\/\/[^\n]*/g, ""); | |
| 45 | } | |
| 46 | ||
| 47 | /** | |
| 48 | * Polls `/healthz` until `X-Forge-Version` is `version` or `limitMs` passes. | |
| 49 | * Returns null on arrival, else the last version seen ("" when none). | |
| 50 | */ | |
| 51 | export async function waitForVersion(base: string, version: string, limitMs: number, intervalMs = 1000): Promise<string | null> { | |
| 52 | const url = new URL("/healthz", base).href; | |
| 53 | const deadline = Date.now() + limitMs; | |
| 54 | let last = ""; | |
| 55 | for (;;) { | |
| 56 | try { | |
| 57 | const res = await fetch(url, { redirect: "manual", signal: AbortSignal.timeout(Math.min(10_000, Math.max(500, limitMs))) }); | |
| 58 | await res.body?.cancel(); | |
| 59 | last = res.headers.get("x-forge-version") ?? ""; | |
| 60 | if (last === version) return null; | |
| 61 | } catch {} | |
| 62 | if (Date.now() + intervalMs >= deadline) return last; | |
| 63 | await Bun.sleep(intervalMs); | |
| 64 | } | |
| 65 | } | |
| 66 | ||
| 35 | 67 | /** Runs every check against `base`; returns the failures (empty is green). */ |
| 36 | 68 | export async function smoke(base: string, token = ""): Promise<Failure[]> { |
| 37 | 69 | const root = new URL(base); |
| @@ -79,15 +111,41 @@ export async function smoke(base: string, token = ""): Promise<Failure[]> { | ||
| 79 | 111 | const health = await get("/healthz", agent); |
| 80 | 112 | if (health && health.res.status !== 200) fail(health.url, agent, health.res, "expected 200"); |
| 81 | 113 | } |
| 114 | ||
| 115 | const sw = await get("/sw.js", "desktop"); | |
| 116 | if (sw) { | |
| 117 | const type = sw.res.headers.get("content-type") ?? ""; | |
| 118 | if (sw.res.status !== 200) fail(sw.url, "desktop", sw.res, "expected 200"); | |
| 119 | else if (!/^(text|application)\/(javascript|ecmascript)\b/i.test(type)) fail(sw.url, "desktop", sw.res, "expected JavaScript"); | |
| 120 | else if (/Response\.error\s*\(/.test(navigationBranch(sw.body))) | |
| 121 | fail(sw.url, "desktop", sw.res, "a navigation can be answered with Response.error()"); | |
| 122 | else if (!navigationBranch(sw.body)) fail(sw.url, "desktop", sw.res, "no navigation handler"); | |
| 123 | } | |
| 82 | 124 | return failures; |
| 83 | 125 | } |
| 84 | 126 | |
| 85 | 127 | if (import.meta.main) { |
| 86 | const base = process.argv[2]; | |
| 87 | if (!base) { | |
| 88 | console.error("usage: bun scripts/smoke.ts <url>"); | |
| 128 | const args = process.argv.slice(2); | |
| 129 | const flag = (name: string) => { | |
| 130 | const at = args.indexOf(name); | |
| 131 | if (at < 0) return undefined; | |
| 132 | return args.splice(at, 2)[1] ?? ""; | |
| 133 | }; | |
| 134 | const version = flag("--version"); | |
| 135 | const waitText = flag("--wait"); | |
| 136 | const wait = waitText === undefined ? 120 : Number(waitText); | |
| 137 | const base = args[0]; | |
| 138 | if (!base || version === "" || !Number.isFinite(wait) || wait < 0) { | |
| 139 | console.error("usage: bun scripts/smoke.ts <url> [--version <v>] [--wait <seconds>]"); | |
| 89 | 140 | process.exit(2); |
| 90 | 141 | } |
| 142 | if (version !== undefined) { | |
| 143 | const last = await waitForVersion(base, version, wait * 1000); | |
| 144 | if (last !== null) { | |
| 145 | console.error(`FAIL ${new URL("/healthz", base).href} X-Forge-Version never became ${version} in ${wait}s (last seen: ${last || "none"})`); | |
| 146 | process.exit(1); | |
| 147 | } | |
| 148 | } | |
| 91 | 149 | const failures = await smoke(base, process.env["FORGE_TOKEN"] ?? ""); |
| 92 | 150 | for (const f of failures) |
| 93 | 151 | console.error(`FAIL ${f.url} agent=${f.agent} status=${f.status} content-type=${f.contentType || "-"} (${f.why})`); |
test/smoke.test.ts +40 −3
| @@ -1,10 +1,12 @@ | ||
| 1 | 1 | import { afterEach, expect, test } from "bun:test"; |
| 2 | import { smoke, stylesheetHref } from "../scripts/smoke.ts"; | |
| 2 | import { smoke, stylesheetHref, waitForVersion } from "../scripts/smoke.ts"; | |
| 3 | 3 | |
| 4 | 4 | let server: ReturnType<typeof Bun.serve> | undefined; |
| 5 | 5 | afterEach(() => server?.stop(true)); |
| 6 | 6 | |
| 7 | function stub(cssType: string, seen: string[] = []) { | |
| 7 | const GOOD_SW = "self.addEventListener('fetch', function (e) { if (e.request.mode === 'navigate') { e.respondWith(offline()); return; } });"; | |
| 8 | ||
| 9 | function stub(cssType: string, seen: string[] = [], sw: { body: string; type: string } = { body: GOOD_SW, type: "text/javascript" }, version = () => "v1") { | |
| 8 | 10 | server = Bun.serve({ |
| 9 | 11 | port: 0, |
| 10 | 12 | hostname: "127.0.0.1", |
| @@ -17,7 +19,8 @@ function stub(cssType: string, seen: string[] = []) { | ||
| 17 | 19 | return req.headers.get("authorization") === "Bearer t" |
| 18 | 20 | ? new Response("board") |
| 19 | 21 | : new Response(null, { status: 302, headers: { location: "/signin" } }); |
| 20 | if (path === "/healthz") return new Response("ok"); | |
| 22 | if (path === "/healthz") return new Response("ok", { headers: { "x-forge-version": version() } }); | |
| 23 | if (path === "/sw.js") return new Response(sw.body, { headers: { "content-type": sw.type } }); | |
| 21 | 24 | return new Response("no", { status: 404 }); |
| 22 | 25 | }, |
| 23 | 26 | }); |
| @@ -56,3 +59,37 @@ test("stylesheetHref finds the link", () => { | ||
| 56 | 59 | expect(stylesheetHref('<link rel="icon" href="/x"><link rel="stylesheet" href="/s.css">')).toBe("/s.css"); |
| 57 | 60 | expect(stylesheetHref("<p>none</p>")).toBeNull(); |
| 58 | 61 | }); |
| 62 | ||
| 63 | const run = async (...args: string[]) => { | |
| 64 | const p = Bun.spawn(["bun", "scripts/smoke.ts", ...args], { env: { ...process.env, FORGE_TOKEN: "t" }, stdout: "pipe", stderr: "pipe" }); | |
| 65 | return { code: await p.exited, err: await new Response(p.stderr).text() }; | |
| 66 | }; | |
| 67 | ||
| 68 | test("a sw.js that can answer a navigation with Response.error() fails", async () => { | |
| 69 | const bad = { body: "self.addEventListener('fetch', function (e) { if (e.request.mode === 'navigate') { e.respondWith(Response.error()); return; } });", type: "text/javascript" }; | |
| 70 | const failures = await smoke(stub("text/css", [], bad), "t"); | |
| 71 | expect(failures.length).toBe(1); | |
| 72 | expect(failures[0]!.url).toEndWith("/sw.js"); | |
| 73 | }); | |
| 74 | ||
| 75 | test("a sw.js served as text/html fails", async () => { | |
| 76 | const failures = await smoke(stub("text/css", [], { body: GOOD_SW, type: "text/html" }), "t"); | |
| 77 | expect(failures.map((f) => f.why)).toEqual(["expected JavaScript"]); | |
| 78 | }); | |
| 79 | ||
| 80 | test("a version that arrives late passes", async () => { | |
| 81 | const started = Date.now(); | |
| 82 | const base = stub("text/css", [], undefined, () => (Date.now() - started > 300 ? "v2" : "v1")); | |
| 83 | expect(await waitForVersion(base, "v2", 5000, 50)).toBeNull(); | |
| 84 | expect((await run(base, "--version", "v2", "--wait", "5")).code).toBe(0); | |
| 85 | }); | |
| 86 | ||
| 87 | test("a version that never arrives fails within the limit, naming the last seen", async () => { | |
| 88 | const base = stub("text/css"); | |
| 89 | expect(await waitForVersion(base, "v9", 400, 50)).toBe("v1"); | |
| 90 | const started = Date.now(); | |
| 91 | const out = await run(base, "--version", "v9", "--wait", "1"); | |
| 92 | expect(out.code).toBe(1); | |
| 93 | expect(out.err).toContain("last seen: v1"); | |
| 94 | expect(Date.now() - started).toBeLessThan(5000); | |
| 95 | }); |
new above
offline