Public: anyone can read this; the crew can change it.

Every JSON route the router serves is in /contract/openapi.json, and a test names the ones that are not #15650

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15636-every-json-route-the-router-serves-is-in → main 0 commits fork 3 threads · 3 unresolved

  • Merged. scout/15636-every-json-route-the-router-serves-is-in was merged into main as 452508c.

Review

Sign in to review this change.
1 file changed +167 −0
test/openapi-covers-routes.test.ts added +167
@@ -0,0 +1,167 @@
1 /**
2 * Every JSON route the routers serve is in the OpenAPI document, or sits in
3 * `NOT_YET_DESCRIBED` below, which may only shrink (#15636). Unlike
4 * openapi-coverage.test.ts, which reads the /api/v1 source, this walks the
5 * live route tables (the page router and the API router), so a JSON route
6 * outside /api/v1 cannot slip past. Admin-only routes count too: the spec is
7 * cut by role when served, and this checks only that a row exists.
8 *
9 * A route is JSON when its path is under /api/v1 or ends in .json, when an
10 * anonymous call to it answers a JSON content type (derived, so a new JSON
11 * route is found without anyone remembering it), or when `JSON_EXTRA` names
12 * it: only for routes an anonymous call cannot show (a redirect, a refusal in
13 * another type).
14 */
15 import { afterAll, beforeAll, expect, test } from "bun:test";
outdated

Wrap this to one entry per line. JSON classification is manual here: a JSON route without a .json suffix or /api/v1 prefix escapes unless someone edits this set. Make unclassified routes fail instead.

16 import { mkdtemp, rm } from "node:fs/promises";
17 import { tmpdir } from "node:os";
18 import { join } from "node:path";
19 import { createApp, type App } from "../src/app.ts";
20 import { ROUTES } from "../src/contract/openapi.ts";
21 import { buildApiRouter, buildRouter, forgeHttp, type ForgeHttp } from "../src/http/server.ts";
22 import { norm } from "./helpers/registered_routes.ts";
outdated

Nothing stops this list from growing: adding a new route's key here makes the test pass. Pin a ceiling (length <= 24, lowered on each deletion).

23
24 /** JSON routes the anonymous probe does not show as JSON. Each must be served. */
25 const JSON_EXTRA: readonly string[] = [
26 "GET /.well-known/oauth-authorization-server",
27 "GET /.well-known/oauth-protected-resource",
28 "GET /.well-known/openid-configuration",
29 "GET /api",
30 "GET /health",
31 "GET /oauth/jwks",
32 "GET /oauth/userinfo",
33 "POST /oauth/revoke",
34 ];
35
36 /** Answers a JSON type that is not an API: an image's miss. Never counted as JSON. */
37 const NOT_JSON: readonly string[] = ["GET /icons/{}.png"];
38
39 /** The most routes `NOT_YET_DESCRIBED` may hold. Lower it with every deletion; never raise it. */
40 const CEILING = 41;
41
42 /**
43 * JSON routes with no OpenAPI row yet. Describe one, then delete its line: a described entry fails
44 * the test.
45 */
46 const NOT_YET_DESCRIBED: readonly string[] = [
47 "GET /.well-known/agent-card.json",
48 "GET /.well-known/api-catalog",
49 "GET /.well-known/forge",
50 "GET /.well-known/mcp",
51 "GET /.well-known/mcp.json",
52 "GET /.well-known/mcp/server-card.json",
53 "GET /.well-known/oauth-authorization-server",
54 "GET /.well-known/oauth-protected-resource",
55 "GET /.well-known/openid-configuration",
56 "GET /api",
57 "GET /assets/{}.json",
58 "GET /feed.json",
59 "GET /forges/name/{}",
60 "GET /forges/{}/bill",
61 "GET /forges/{}/ready",
62 "GET /maintenance.json",
outdated

Add the length ceiling assertion to this test.

63 "GET /manifest.webmanifest",
64 "GET /oauth/jwks",
65 "GET /oauth/userinfo",
66 "GET /openapi.json",
67 "GET /plugins/claude-code/marketplace.json",
68 "GET /releases.json",
69 "GET /templates.json",
70 "GET /{}/{}/space.json",
71 "POST /claim/options",
72 "POST /first-admin/options",
73 "POST /home/act",
74 "POST /oauth/register",
75 "POST /oauth/revoke",
76 "POST /oauth/token",
77 "POST /oauth/userinfo",
78 "POST /recover/options",
79 "POST /recover/passkey",
80 "POST /settings/everyday/options",
81 "POST /settings/passkeys",
82 "POST /settings/passkeys/options",
83 "POST /signin/passkey",
84 "POST /signin/passkey/options",
85 "POST /signup/passkey",
86 "POST /signup/passkey/options",
87 "POST /welcome/options",
88 ];
89
90 const key = (method: string, pattern: string) => `${method} ${norm(pattern)}`;
91 const FILL: Record<string, string> = { owner: "alice", username: "alice", name: "notes", number: "1" };
92 const fill = (p: string) =>
93 p.replace(/:([a-z_]+)(\.[a-z]+)?/g, (_, k: string, s?: string) => (FILL[k] ?? "x") + (s ?? "")).replace(/\*[a-z_]+/, "x");
94
95 let dataDir = "";
96 let app: App;
97 let http: ForgeHttp;
98 let served: string[] = [];
99 const cells = process.env["FORGE_CELLS"];
100
101 beforeAll(async () => {
102 process.env["FORGE_CELLS"] = "off";
103 dataDir = await mkdtemp(join(tmpdir(), "forge-openapi-covers-"));
104 app = createApp({ dataDir, port: 0 });
105 http = forgeHttp(app);
106 const found = new Set<string>();
107 const seen = new Set<string>();
108 for (const r of [...buildRouter().routes(), ...buildApiRouter().routes()]) {
109 const k = key(r.method, r.pattern);
110 if (seen.has(k)) continue;
111 seen.add(k);
112 if (r.pattern.startsWith("/api/v1") || r.pattern.endsWith(".json")) {
113 found.add(k);
114 continue;
115 }
116 const res = await http.handle(
117 new Request(`http://forge.test${fill(r.pattern)}`, {
118 method: r.method,
119 headers: { accept: "*/*", origin: "http://forge.test", "content-type": "application/json" },
120 ...(r.method === "GET" ? {} : { body: "{}" }),
121 }),
122 );
123 await res.body?.cancel();
124 if (/json/.test(res.headers.get("content-type") ?? "") && !NOT_JSON.includes(k)) found.add(k);
125 }
126 for (const k of JSON_EXTRA) found.add(k);
127 served = [...found].sort();
128 seenAll = seen;
129 }, 120_000);
130
131 let seenAll = new Set<string>();
132
133 afterAll(async () => {
134 app.close();
135 await rm(dataDir, { recursive: true, force: true });
136 if (cells === undefined) delete process.env["FORGE_CELLS"];
137 else process.env["FORGE_CELLS"] = cells;
138 });
139
140 const described = () => new Set(ROUTES.map((r) => key(r.method.toUpperCase(), r.path)));
141
142 test("every JSON route the router serves has an OpenAPI row or is listed as not yet described", () => {
143 const have = described();
144 const missing = served.filter((k) => !have.has(k) && !NOT_YET_DESCRIBED.includes(k));
145 expect(missing, `JSON routes absent from /contract/openapi.json (add a row under src/contract/openapi/routes/, then run bun scripts/openapi-routes.ts):\n${missing.join("\n")}`).toEqual([]);
146 });
147
148 test("NOT_YET_DESCRIBED is sorted, names served routes, holds none that is now described, and never grows", () => {
149 expect([...NOT_YET_DESCRIBED]).toEqual([...NOT_YET_DESCRIBED].sort());
150 const all = new Set(served);
151 expect(NOT_YET_DESCRIBED.filter((k) => !all.has(k)), "listed but not a served JSON route; delete the line").toEqual([]);
152 const have = described();
153 expect(NOT_YET_DESCRIBED.filter((k) => have.has(k)), "described now; delete the line").toEqual([]);
154 expect(NOT_YET_DESCRIBED.length, `the list may only shrink: lower CEILING to ${NOT_YET_DESCRIBED.length}`).toBeLessThanOrEqual(CEILING);
155 expect(CEILING, "CEILING is above the list: lower it to the list's length").toBeLessThanOrEqual(NOT_YET_DESCRIBED.length);
156 });
157
158 test("JSON_EXTRA names only served routes", () => {
159 expect(JSON_EXTRA.filter((k) => !seenAll.has(k)), "not a served route; delete the line").toEqual([]);
160 expect([...JSON_EXTRA]).toEqual([...JSON_EXTRA].sort());
161 });
162
163 test("the walk finds JSON routes outside /api/v1, by path and by what they answer", () => {
164 expect(served).toContain("GET /contract/openapi.json");
165 expect(served).toContain("GET /.well-known/mcp");
166 expect(served).toContain("POST /mcp");
167 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs