// 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 = path .replace(/\$\{encodeURIComponent\([^)]*\)\}/g, "{}") .replace(/\$\{[^}]*\}/g, "{}") .replace(/\{[^}]*\}/g, "{}"); const query = out.indexOf("?"); if (query >= 0) out = out.slice(0, query); const partial = out.indexOf("$"); if (partial >= 0) out = out.slice(0, partial); return out.replace(/[.,;:)]+$/, "").replace(/\/+$/, ""); } function yamlPaths(yaml: string): Set { const served = new Set(); for (const line of yaml.split("\n")) { const m = /^ {2}(\/[A-Za-z0-9_\-{}/.]*):\s*$/.exec(line); if (m) served.add(normalise(m[1])); } return served; } 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 = yamlPaths(readFileSync(OPENAPI, "utf8")); for (const [template, values] of Object.entries(TYPED_TEMPLATES)) { for (const value of values) { assert.ok( served.has(normalise(template.replace("{}", value))), `${template} expands to ${value}, which the binary does not serve`, ); } } }); test("every API path the dashboard calls is served", () => { const served = yamlPaths(readFileSync(OPENAPI, "utf8")); 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", ); });