-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.tools.test.ts
More file actions
133 lines (110 loc) · 4.29 KB
/
example.tools.test.ts
File metadata and controls
133 lines (110 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import { NotFoundError } from "../../src/example-client/client.js";
import { exampleTools, handleExampleTool } from "../../src/tools/example.tools.js";
import { ToolRegistry } from "../../src/tools/tool-registry.js";
describe("exampleTools definitions", () => {
it("registers without duplicates", () => {
const registry = new ToolRegistry<unknown>();
expect(() => registry.register({ tools: exampleTools, handler: async () => ({ content: [] }) })).not.toThrow();
});
it("fails on duplicate registrations", () => {
const registry = new ToolRegistry<unknown>();
registry.register({ tools: exampleTools, handler: async () => ({ content: [] }) });
expect(() => registry.register({ tools: exampleTools, handler: async () => ({ content: [] }) })).toThrow(
/Duplicate tool/
);
});
it("sets all required annotations", () => {
for (const tool of exampleTools) {
expect(tool.annotations).toMatchObject({
readOnlyHint: expect.any(Boolean),
destructiveHint: expect.any(Boolean),
idempotentHint: expect.any(Boolean),
openWorldHint: expect.any(Boolean)
});
}
});
});
describe("handleExampleTool", () => {
const mockClient = {
getThing: jest.fn(),
listThings: jest.fn(),
deleteThing: jest.fn()
};
beforeEach(() => {
jest.clearAllMocks();
});
it("returns summary and data for example_get_thing", async () => {
mockClient.getThing.mockResolvedValue({
id: "00000000-0000-4000-8000-000000000001",
name: "Alpha"
});
const result = await handleExampleTool(
"example_get_thing",
{ id: "00000000-0000-4000-8000-000000000001" },
mockClient as any
);
const parsed = JSON.parse(result.content[0]!.text);
expect(parsed.summary).toMatch(/retrieved successfully/i);
expect(parsed.data).toHaveProperty("id");
expect(Array.isArray(parsed.suggestions)).toBe(true);
});
it("returns structured not found for expected not found error", async () => {
mockClient.getThing.mockRejectedValue(new NotFoundError("Missing"));
const result = await handleExampleTool(
"example_get_thing",
{ id: "00000000-0000-4000-8000-000000000001" },
mockClient as any
);
const parsed = JSON.parse(result.content[0]!.text);
expect(parsed.data.error).toBe("NOT_FOUND");
expect(parsed.summary).toMatch(/not found/i);
});
it("rejects invalid UUID", async () => {
await expect(handleExampleTool("example_get_thing", { id: "not-a-uuid" }, mockClient as any)).rejects.toThrow();
});
it("uses default top for example_list_things", async () => {
mockClient.listThings.mockResolvedValue([{ id: "a", name: "Thing A" }]);
const result = await handleExampleTool("example_list_things", {}, mockClient as any);
expect(mockClient.listThings).toHaveBeenCalledWith({ filter: undefined, top: 25 });
const parsed = JSON.parse(result.content[0]!.text);
expect(parsed.data.total).toBe(1);
});
it("deletes one record for example_delete_thing", async () => {
mockClient.deleteThing.mockResolvedValue(undefined);
const result = await handleExampleTool(
"example_delete_thing",
{
id: "00000000-0000-4000-8000-000000000001",
confirmation: "DELETE"
},
mockClient as any
);
expect(mockClient.deleteThing).toHaveBeenCalledWith("00000000-0000-4000-8000-000000000001");
const parsed = JSON.parse(result.content[0]!.text);
expect(parsed.data.deleted).toBe(true);
});
it("rejects delete when confirmation token is missing", async () => {
await expect(
handleExampleTool(
"example_delete_thing",
{ id: "00000000-0000-4000-8000-000000000001" },
mockClient as any
)
).rejects.toThrow();
});
it("returns structured not found for delete on missing record", async () => {
mockClient.deleteThing.mockRejectedValue(new NotFoundError("Missing"));
const result = await handleExampleTool(
"example_delete_thing",
{
id: "00000000-0000-4000-8000-000000000001",
confirmation: "DELETE"
},
mockClient as any
);
const parsed = JSON.parse(result.content[0]!.text);
expect(parsed.data.deleted).toBe(false);
expect(parsed.data.error).toBe("NOT_FOUND");
});
});