// The dashboard may only call API operations the single binary serves. // // The dashboard is embedded in more than one composition, and a page that // calls an operation the serving binary doesn't answer fails in the browser as // an empty list or a not-found. api/openapi.yaml is what `archie` serves, so // every /api path in the dashboard's source must be one of its paths. This // guards the regression that shipped pages calling the old services. import assert from "node:assert/strict"; import { readFileSync, readdirSync, statSync } from "node:fs"; import { join } from "node:path"; import test from "node:test"; const SRC = new URL("../src", import.meta.url).pathname; const OPENAPI = new URL("../../api/openapi.yaml", import.meta.url).pathname; // A path whose substitution the server itself names. `api.extensions(kind)`, // `api.acceptExtension(kind, name)` and `api.removeExtension(kind, name)` build // the collection from a kind the server lists at /api/extension-kinds, so every // value they can produce is one of the server's own collections. const TEMPLATED_BY_SERVER = /^\/api\/\{\}(\/\{\})?$/; // A path whose one substitution is a closed union of served operations. Every // value of the union is checked against the served set, so the client may build // the path by interpolation. const TYPED_TEMPLATES: Record = { "/api/policies/{}": ["hard-rules", "change-request"], }; // normalise rewrites a written path into the shape the OpenAPI document uses: // one parameter per substitution, no query string, no trailing punctuation. function normalise(path: string): string { let out = ""; for (let i = 0; i < path.length; i++) { if (path[i] !== "$" || path[i + 1] !== "{") { out += path[i]; continue; } // A substitution is one parameter. One that builds a query (an `after` // cursor, say) is not part of the path, so the path ends there. let depth = 0; let end = i + 1; for (; end < path.length; end++) { if (path[end] === "{") depth++; else if (path[end] === "}" && --depth === 0) break; } // A line the scan cut short leaves the substitution unclosed: the path // ends there. if (end >= path.length) break; const inner = path.slice(i + 2, end); if (inner.includes("?") || inner.includes("qs(")) break; out += "{}"; i = end; } const query = out.indexOf("?"); if (query >= 0) out = out.slice(0, query); return out.replace(/\{[^}]*\}/g, "{}").replace(/[.,;:)]+$/, "").replace(/\/+$/, ""); } interface Operation { method: string; path: string; } // operations reads what the binary serves: every method under every path. function operations(yaml: string): Operation[] { const out: Operation[] = []; let path = ""; for (const line of yaml.split("\n")) { const dir = /^ {2}(\/[A-Za-z0-9_\-{}/.]*):\s*$/.exec(line); if (dir) { path = normalise(dir[1]); continue; } const op = /^ {4}(get|post|put|delete|patch):\s*$/.exec(line); if (op && path) out.push({ method: op[1].toUpperCase(), path }); } return out; } // calls reads every request the client makes: the method it asks for and the // path it asks it on. A call that names two paths (one per branch) is two // requests, which is what a person pressing either button causes. function calls(src: string): Operation[] { const out: Operation[] = []; for (const m of src.matchAll(/\b(request|send)\b/g)) { const open = src.indexOf("(", m.index ?? 0); if (open < 0) continue; let depth = 0; let end = open; for (; end < src.length; end++) { if (src[end] === "(") depth++; else if (src[end] === ")" && --depth === 0) break; } const call = src.slice(open, end + 1); const method = (/method:\s*"([A-Za-z]+)"/.exec(call)?.[1] ?? "GET").toUpperCase(); for (const p of call.matchAll(/["\'`](\/api\/[^"\'`]*)["\'`]/g)) out.push({ method, path: normalise(p[1]) }); } return out; } function sourceFiles(dir: string): string[] { const out: string[] = []; for (const entry of readdirSync(dir)) { const full = join(dir, entry); if (statSync(full).isDirectory()) out.push(...sourceFiles(full)); else if (/\.(ts|vue)$/.test(entry)) out.push(full); } return out; } test("every typed template expands to a served operation", () => { const served = new Set(operations(readFileSync(OPENAPI, "utf8")).map((o) => `${o.method} ${o.path}`)); for (const [template, values] of Object.entries(TYPED_TEMPLATES)) { for (const value of values) { const path = normalise(template.replace("{}", value)); for (const method of ["GET", "PUT"]) { assert.ok( served.has(`${method} ${path}`), `${method} ${template} expands to ${value}, which the binary does not serve`, ); } } } }); // Guards the hole the first version of this test had: it compared paths, so a // call asking for a method the binary does not serve on a path it does passed. test("every operation the client asks for is served, with that method", () => { const served = new Set(operations(readFileSync(OPENAPI, "utf8")).map((o) => `${o.method} ${o.path}`)); assert.ok(served.size > 100, `read ${served.size} served operations`); const missing = calls(readFileSync(join(SRC, "lib", "api.ts"), "utf8")) .filter((call) => !TEMPLATED_BY_SERVER.test(call.path) && !TYPED_TEMPLATES[call.path]) .filter((call) => !served.has(`${call.method} ${call.path}`)) .map((call) => `${call.method} ${call.path}`); assert.deepEqual([...new Set(missing)].sort(), [], "the client asks for operations the binary does not serve"); }); test("every API path the dashboard calls is served", () => { const served = new Set(operations(readFileSync(OPENAPI, "utf8")).map((o) => o.path)); assert.ok(served.size > 100, `read ${served.size} served paths`); const unserved = new Map(); for (const file of sourceFiles(SRC)) { for (const line of readFileSync(file, "utf8").split("\n")) { for (const m of line.matchAll(/\/api\/[^\s`'"]*/g)) { const path = normalise(m[0]); if (!path.startsWith("/api/") || served.has(path)) continue; if (TEMPLATED_BY_SERVER.test(path)) continue; if (TYPED_TEMPLATES[path]) continue; const where = file.slice(SRC.length + 1); unserved.set(path, [...(unserved.get(path) ?? []), where]); } } } assert.deepEqual( [...unserved.entries()].sort().map(([p, files]) => `${p} (${files.join(", ")})`), [], "the dashboard calls paths the binary does not serve", ); });