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

opened this

Merged 17h 50m ago, live in production

fix/openapi-served-cpu → main 0 commits 2 threads · 2 unresolved

  • Merged. fix/openapi-served-cpu was merged into main as b482465.

Review

Sign in to review this change.
1 file changed +25 −3
test/openapi-served.test.ts +25 −3
@@ -1,5 +1,6 @@
11 /** Every route the OpenAPI document describes is one the router serves. */
22 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { readdirSync } from "node:fs";
34 import { mkdtemp, rm } from "node:fs/promises";
45 import { tmpdir } from "node:os";
56 import { join } from "node:path";
@@ -15,6 +16,13 @@ beforeAll(async () => {
1516 dataDir = await mkdtemp(join(tmpdir(), "forge-openapi-served-"));
1617 app = createApp({ dataDir, port: 0 });
1718 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"])

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.

25 for (const file of readdirSync(join(dir, sub)).filter((f) => f.endsWith(".ts")).toSorted()) await import(join(dir, sub, file));
1826 });
1927
2028 afterAll(async () => {
@@ -25,10 +33,10 @@ afterAll(async () => {
2533
2634 const sample = (name: string) => (name === "number" ? "1" : "sample");
2735
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[]> {
3038 const missing: string[] = [];
31 for (const r of ROUTES) {
39 for (const r of routes) {
3240 const path = r.path.replace(/\{(\w+)\}/g, (_, n: string) => sample(n));
3341 const res = await fetch(`${servers.url}${path}`, {
3442 method: r.method.toUpperCase(),
@@ -38,5 +46,19 @@ test("every described method and path resolves to a handler, not no_route", asyn
3846 const body = (await res.json().catch(() => ({}))) as { error?: string };
3947 if (res.status === 404 && body.error === "no_route") missing.push(`${r.method.toUpperCase()} ${r.path}`);
4048 }
49 return missing;
50 }
51
52 // Two halves, two tests: each well inside the 2 s per-test CPU budget on a check runner.

A fixed two-way split is a stopgap. Next step: slice ROUTES into chunks of a fixed size and generate one test per chunk, so growth never puts a test back over the budget.

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));
4163 expect(missing, `described but not served: ${missing.join(", ")}`).toEqual([]);
4264 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs