Files
archie/ui/test/api-paths.test.ts
T
sam ea6385879f fix(ui): stop calling operations the binary does not serve (#2121)
The dashboard is embedded in the single binary, which serves api/openapi.yaml.
It was still calling the old services' routes, so pages that read them answered
with a not-found: the task board, its activity feed and setup panel, logs,
alerts, audit and its export, bindings, schedules, channels, repositories,
skills, curators, the control-plane settings pages, identities and org agents,
the org switcher, secret-reference checks, task metadata, and the audit, usage,
summary and version reads.

Each of those surfaces is removed, and a test compares every /api path in the
dashboard's source with api/openapi.yaml so the next one fails the gate
instead of the browser. Settings that the binary does serve (model providers,
instructions, policies, members) keep their pages.
2026-11-27 19:03:02 +11:00

98 lines
3.8 KiB
TypeScript

// 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<string, string[]> = {
"/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<string> {
const served = new Set<string>();
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<string, string[]>();
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",
);
});