Public: anyone can read this; the crew can change it.
The OpenAPI served-routes walk loads its route modules in the hook and runs in two tests, under the 2 s CPU budget #17525
Merged 17h 50m ago, live in production
fix/openapi-served-cpu → main 0 commits 2 threads · 2 unresolved
1 file
changed +25
−3
test/openapi-served.test.ts +25 −3
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | /** Every route the OpenAPI document describes is one the router serves. */ |
| 2 | 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; |
| 3 | import { readdirSync } from "node:fs"; | |
| 3 | 4 | import { mkdtemp, rm } from "node:fs/promises"; |
| 4 | 5 | import { tmpdir } from "node:os"; |
| 5 | 6 | import { join } from "node:path"; |
| @@ -15,6 +16,13 @@ beforeAll(async () => { | ||
| 15 | 16 | dataDir = await mkdtemp(join(tmpdir(), "forge-openapi-served-")); |
| 16 | 17 | app = createApp({ dataDir, port: 0 }); |
| 17 | 18 | servers = await serve(app); |
| 19 | // Most of the walk's CPU was loading the route modules a first request imports (#12521): 491 ms | |
| 20 | // of a 545-route walk on a Mac, 63 ms warm, and 2.53 s on a check runner, over the 2 s test | |
| 21 | // budget. Loading is the file's cost, so the hook pays it (hooks are not a test's budget); every | |
| 22 | // request is still made, and every handler still answers, in the test. | |
| 23 | const dir = join(import.meta.dir, "../src/http"); | |
| 24 | for (const sub of ["routes", "api"]) | |
| 25 | for (const file of readdirSync(join(dir, sub)).filter((f) => f.endsWith(".ts")).toSorted()) await import(join(dir, sub, file)); | |
| 18 | 26 | }); |
| 19 | 27 | |
| 20 | 28 | afterAll(async () => { |
| @@ -25,10 +33,10 @@ afterAll(async () => { | ||
| 25 | 33 | |
| 26 | 34 | const sample = (name: string) => (name === "number" ? "1" : "sample"); |
| 27 | 35 | |
| 28 | test("every described method and path resolves to a handler, not no_route", async () => { | |
| 29 | expect(ROUTES.length).toBeGreaterThan(5); | |
| 36 | /** The described routes not served: each method and path asked once, `no_route` collected. */ | |
| 37 | async function unserved(routes: typeof ROUTES): Promise<string[]> { | |
| 30 | 38 | const missing: string[] = []; |
| 31 | for (const r of ROUTES) { | |
| 39 | for (const r of routes) { | |
| 32 | 40 | const path = r.path.replace(/\{(\w+)\}/g, (_, n: string) => sample(n)); |
| 33 | 41 | const res = await fetch(`${servers.url}${path}`, { |
| 34 | 42 | method: r.method.toUpperCase(), |
| @@ -38,5 +46,19 @@ test("every described method and path resolves to a handler, not no_route", asyn | ||
| 38 | 46 | const body = (await res.json().catch(() => ({}))) as { error?: string }; |
| 39 | 47 | if (res.status === 404 && body.error === "no_route") missing.push(`${r.method.toUpperCase()} ${r.path}`); |
| 40 | 48 | } |
| 49 | return missing; | |
| 50 | } | |
| 51 | ||
| 52 | // Two halves, two tests: each well inside the 2 s per-test CPU budget on a check runner. | |
| 53 | const HALF = Math.ceil(ROUTES.length / 2); | |
| 54 | ||
| 55 | test("every described method and path resolves to a handler, not no_route (first half)", async () => { | |
| 56 | expect(ROUTES.length).toBeGreaterThan(5); | |
| 57 | const missing = await unserved(ROUTES.slice(0, HALF)); | |
| 58 | expect(missing, `described but not served: ${missing.join(", ")}`).toEqual([]); | |
| 59 | }); | |
| 60 | ||
| 61 | test("every described method and path resolves to a handler, not no_route (second half)", async () => { | |
| 62 | const missing = await unserved(ROUTES.slice(HALF)); | |
| 41 | 63 | expect(missing, `described but not served: ${missing.join(", ")}`).toEqual([]); |
| 42 | 64 | }); |
new above
offline
This line is long. Wrap it across lines for readability. Next step: this hardcodes two directories, so a route module elsewhere will load inside a test again.