import assert from "node:assert/strict"; import test from "node:test"; const { registerWebMcpTools, createWebMcpRegistrar } = await import("../src/lib/webmcp.ts"); const { archieWebMcpTools } = await import("../src/lib/webmcp-tools.ts"); interface FakeRegistered { name: string; description: string; inputSchema: unknown; annotations: Record | undefined; signal: AbortSignal | undefined; execute: (input: Record) => unknown; } function installModelContext(): { registered: FakeRegistered[] } { const registered: FakeRegistered[] = []; Object.defineProperty(globalThis, "document", { configurable: true, value: { modelContext: { registerTool: async ( tool: Omit, options?: { signal?: AbortSignal }, ) => { registered.push({ ...tool, signal: options?.signal }); }, }, }, }); return { registered }; } function installNoModelContext(): void { Object.defineProperty(globalThis, "document", { configurable: true, value: {}, }); } interface Call { method: string; args: unknown[]; } function fakeClient(): { calls: Call[]; client: Parameters[0]; } { const calls: Call[] = []; const reply = (method: string, args: unknown[]) => { calls.push({ method, args }); return { ok: true, method, args }; }; return { calls, client: { health: async () => reply("health", []), tasks: async () => reply("tasks", []), task: async (id: string) => reply("task", [id]), captures: async (limit?: number) => reply("captures", [limit]), taskAction: async (id: string, action: string) => reply("taskAction", [id, action]), }, }; } function toolNamed( client: Parameters[0], name: string, ) { const tool = archieWebMcpTools(client).find( (candidate) => candidate.name === name, ); assert.ok(tool, `no tool named ${name}`); return tool; } test("registration no-ops without document.modelContext instead of throwing", async () => { installNoModelContext(); const registration = await registerWebMcpTools( archieWebMcpTools(fakeClient().client), ); assert.equal(registration.registered, false); assert.doesNotThrow(() => registration.dispose()); }); test("registration passes each tool's schema and annotations, and disposes via the signal", async () => { const { registered } = installModelContext(); const tools = archieWebMcpTools(fakeClient().client); const registration = await registerWebMcpTools(tools); assert.equal(registration.registered, true); assert.deepEqual( registered.map((tool) => tool.name), tools.map((tool) => tool.name), ); for (const tool of registered) { assert.ok(tool.description.length > 0, `${tool.name} has no description`); assert.equal(typeof tool.inputSchema, "object"); assert.ok( tool.signal instanceof AbortSignal, `${tool.name} has no abort signal`, ); assert.equal(tool.signal.aborted, false); } registration.dispose(); for (const tool of registered) { assert.equal( tool.signal?.aborted, true, `${tool.name} was not unregistered`, ); } }); test("read-only tools are read-only; recovery tools are consequential", () => { const annotations = new Map( archieWebMcpTools(fakeClient().client).map((tool) => [ tool.name, tool.annotations, ]), ); for (const name of [ "list_tasks", "get_task", "recent_events", "daemon_health", ]) { assert.equal( annotations.get(name)?.readOnlyHint, true, `${name} should be read-only`, ); assert.notEqual( annotations.get(name)?.consequentialHint, true, `${name} must not be consequential`, ); } for (const name of ["retry_task", "stop_task", "cancel_task"]) { assert.equal( annotations.get(name)?.consequentialHint, true, `${name} needs a confirmation`, ); assert.notEqual( annotations.get(name)?.readOnlyHint, true, `${name} must not be read-only`, ); } }); test("tools returning agent, operator or inbound content carry untrustedContentHint", () => { const annotations = new Map( archieWebMcpTools(fakeClient().client).map((tool) => [ tool.name, tool.annotations, ]), ); for (const name of ["list_tasks", "get_task", "recent_events"]) { assert.equal( annotations.get(name)?.untrustedContentHint, true, `${name} returns untrusted content`, ); } assert.equal(annotations.get("daemon_health")?.untrustedContentHint, false); assert.equal(annotations.get("list_tasks")?.consequentialHint, undefined); }); test("recovery tools post the dashboard's own action verbs", async () => { const { calls, client } = fakeClient(); await toolNamed(client, "retry_task").execute({ task_id: "9", confirm: true, }); await toolNamed(client, "stop_task").execute({ task_id: "9", confirm: true }); await toolNamed(client, "cancel_task").execute({ task_id: "9", confirm: true, }); assert.deepEqual(calls, [ { method: "taskAction", args: ["9", "retry"] }, { method: "taskAction", args: ["9", "stop"] }, { method: "taskAction", args: ["9", "cancel"] }, ]); }); test("recovery tools require an explicit confirm flag before touching the API", async () => { const { calls, client } = fakeClient(); const retry = toolNamed(client, "retry_task"); await assert.rejects( () => Promise.resolve(retry.execute({ task_id: "9" })), /confirm must be true/, ); await assert.rejects( () => Promise.resolve(retry.execute({ task_id: "9", confirm: false })), /confirm must be true/, ); assert.equal(calls.length, 0, "a call without confirm reached the API"); await retry.execute({ task_id: "9", confirm: true }); assert.deepEqual(calls, [{ method: "taskAction", args: ["9", "retry"] }]); }); test("the recovery schema requires both task_id and confirm", () => { const schema = archieWebMcpTools(fakeClient().client).find( (tool) => tool.name === "stop_task", )!.inputSchema as { required?: string[]; }; assert.deepEqual(schema.required, ["task_id", "confirm"]); }); test("a recovery tool without a task id fails before touching the API", async () => { const { calls, client } = fakeClient(); await assert.rejects( () => Promise.resolve(toolNamed(client, "retry_task").execute({})), /task_id is required/, ); assert.equal(calls.length, 0); }); test("re-registering replaces the previous registration", async () => { const { registered } = installModelContext(); const register = createWebMcpRegistrar( archieWebMcpTools(fakeClient().client), ); const count = archieWebMcpTools(fakeClient().client).length; const first = await register(); const second = await register(); assert.notEqual(first, second); assert.equal(registered.length, count * 2); for (const tool of registered.slice(0, count)) { assert.equal( tool.signal?.aborted, true, "the first registration was left live", ); } });