60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import { uiAgentWebMcpTools, type UiAgent } from "../src/lib/ui-agent.ts";
|
|
|
|
function fakeUiAgent(): {
|
|
calls: Array<{ method: string; args: unknown[] }>;
|
|
agent: UiAgent;
|
|
} {
|
|
const calls: Array<{ method: string; args: unknown[] }> = [];
|
|
const agent = {
|
|
snapshot: () => {
|
|
calls.push({ method: "snapshot", args: [] });
|
|
return [{ ref: "c1", component: "radio-group", label: "Repeats", value: "weekly", actions: ["setValue"] }];
|
|
},
|
|
query: async (input: { text?: string; within?: string; kind?: string }) => {
|
|
calls.push({ method: "query", args: [input] });
|
|
return [];
|
|
},
|
|
act: async (input: { steps: unknown[] }) => {
|
|
calls.push({ method: "act", args: [input] });
|
|
return { ok: true, appeared: [], values: {}, steps: [] };
|
|
},
|
|
subscribe: () => () => {},
|
|
} as unknown as UiAgent;
|
|
return { calls, agent };
|
|
}
|
|
|
|
test("uiAgentWebMcpTools publishes snapshot, query, and act with expected annotations", async () => {
|
|
const { calls, agent } = fakeUiAgent();
|
|
const tools = uiAgentWebMcpTools(agent);
|
|
assert.deepEqual(
|
|
tools.map((tool) => tool.name),
|
|
["ui_snapshot", "ui_query", "ui_act"],
|
|
);
|
|
|
|
const snapshot = tools[0]!;
|
|
const query = tools[1]!;
|
|
const act = tools[2]!;
|
|
|
|
assert.equal(snapshot.annotations.readOnlyHint, true);
|
|
assert.equal(query.annotations.readOnlyHint, true);
|
|
assert.equal(act.annotations.readOnlyHint, false);
|
|
assert.equal(act.annotations.consequentialHint, true);
|
|
|
|
await snapshot.execute({});
|
|
await query.execute({ text: "Repeats", kind: "radio-group" });
|
|
await act.execute({ steps: [{ ref: "Repeats", action: "setValue", args: ["daily"] }] });
|
|
|
|
assert.deepEqual(
|
|
calls.map((c) => c.method),
|
|
["snapshot", "query", "act"],
|
|
);
|
|
|
|
await assert.rejects(
|
|
async () => act.execute({ steps: "not-an-array" }),
|
|
/steps must be an array/,
|
|
);
|
|
});
|