import { describe, expect, it } from"vitest";import { ACTS } from"../src/acts.ts";import catalogue from"../src/mcp/catalogue.json";import { HANDLERS } from"../src/mcp/tools/index.ts";import { http, KIT_MANIFESTS, PERSONAL_TABLES, personalObject } from"./personal.ts";/** A name written like a tool's: lower-case words joined by underscores. */const NAME = /\b[a-z][a-z0-9]*(?:_[a-z0-9]+)+\b/g;/** * Names in the words that are not tools, each kept for a reason: a key of an * answer the tools give (a record's frame, a page's cursor, merge_people's * parts, a change's `undone_by`), of a secret's typed payload, or of * list_records' `where`; or a refusal's code (undo's `changed_since`, * fetch_progress's `fetch_cancelled`, a sealed read's `window_needed`). */const ANSWER_KEYS = newSet(["created_at", "updated_at", "next_page", "device_card", "repeats_binned", "id_type", "within_days", "undone_by", "changed_since", "fetch_cancelled", "window_needed", "cover_unanswered", "details_unanswered", "missing_media", "episode_actions", "in_days", "travel_words", "left_out", "not_waiting", "not_yours", "no_audio", "engine_failed", "too_long", "not_enabled", "left_for_the_owner"]);/** Every `key`, `id` and choice a definition holds: the tables' columns, ids and choice values (a document's bank_statement), the kit's tools and their parts. */function definitionWords(docs: unknown[]): Set<string> {const out = newSet<string>();const walk = (v: unknown): void => {if (Array.isArray(v)) v.forEach(walk);elseif (v && typeof v === "object") {const o = v asRecord<string, unknown>;for (const k of ["key", "id"]) if (typeof o[k] === "string") out.add(o[k] as string);if (Array.isArray(o.choices)) for (const c of o.choices) if (typeof c === "string") out.add(c);Object.values(o).forEach(walk); } }; walk(docs);return out;}/** The property names and enum values a schema declares. */function schemaWords(schema: unknown, out = newSet<string>()): Set<string> {if (Array.isArray(schema)) schema.forEach((s) => schemaWords(s, out));elseif (schema && typeof schema === "object") {const o = schema asRecord<string, unknown>;if (o.properties && typeof o.properties === "object") for (const k ofObject.keys(o.properties)) out.add(k);if (Array.isArray(o.enum)) for (const v of o.enum) if (typeof v === "string") out.add(v);Object.values(o).forEach((v) => schemaWords(v, out)); }return out;}/** Every string a value holds, with where it sits. */function texts(where: string, v: unknown, out: [string, string][] = []): [string, string][] {if (typeof v === "string") out.push([where, v]);elseif (Array.isArray(v)) v.forEach((x, i) => texts(`${where}[${i}]`, x, out));elseif (v && typeof v === "object") for (const [k, x] ofObject.entries(v)) texts(`${where}.${k}`, x, out);return out;}interfaceTool { name: string; title: string; description: string; inputSchema: unknown;}/** RULE (owner, 26 Oct 2026): every tool description and the server's instructions name only tools that exist; a name that is not a tool fails here. */describe("the catalogue's words", () => {const tools = catalogue.tools asTool[];const served = newSet(tools.map((t) => t.name).filter((n) => n in HANDLERS));const known = newSet([...served, ...Object.keys(ACTS), ...Object.values(ACTS).flatMap((a) => a.args.map((x) => x.replace(/\?$/, ""))), ...definitionWords([...PERSONAL_TABLES, ...KIT_MANIFESTS]), ...schemaWords(tools.map((t) => t.inputSchema)), ...ANSWER_KEYS]);// A value an answer carries, not a tool: request_unlock's `card` (src/mcp/tools/window.ts `cardOf`). known.add("each_time");// The refusal code an update answers when its row moved since the caller read it. known.add("version_moved");function strangers(found: [string, string][]): string[] {const out: string[] = [];for (const [where, text] of found) for (const name of text.match(NAME) ?? []) if (!known.has(name)) out.push(`${where}: ${name}`);return out; } it("lists only tools this object serves, and serves only tools it lists", () => { expect(tools.filter((t) => !served.has(t.name)).map((t) => t.name)).toEqual([]); expect(Object.keys(HANDLERS).filter((name) => !tools.some((t) => t.name === name))).toEqual([]); }); it("names no tool that is not there, in a description, a schema or the instructions", () => {const found: [string, string][] = [["kernel", catalogue.kernel], ["staticKernel", catalogue.staticKernel]];for (const t of tools) texts(t.name, { title: t.title, description: t.description, inputSchema: t.inputSchema }, found); expect(strangers(found)).toEqual([]); }); it("names none in what tools/list and initialize serve either", async () => {const { owner } = await personalObject("catalogue-words");const auth = { authorization: `Bearer ${owner}` };const init = await http<{ result: { instructions: string } }>("catalogue-words", "POST", "/mcp", { jsonrpc: "2.0", id: 1, method: "initialize", params: {} }, auth);const listed = await http<{ result: { tools: Tool[] } }>("catalogue-words", "POST", "/mcp", { jsonrpc: "2.0", id: 1, method: "tools/list" }, auth); expect(listed.body.result.tools.map((t) => t.name).sort()).toEqual([...served].sort()); expect(strangers([["instructions", init.body.result.instructions], ...texts("tools/list", listed.body.result.tools)])).toEqual([]); }); it("would catch one: a name like a tool that is not one", () => { expect(strangers([["probe", "finish it with complete_todo, then today_agenda"]])).toEqual(["probe: complete_todo", "probe: today_agenda"]); });});