Repository navigation
Expand file tree
/
Copy pathsdk.test.ts
More file actions
334 lines (282 loc) · 10 KB
/
Copy pathsdk.test.ts
File metadata and controls
334 lines (282 loc) · 10 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { describe, it, before } from "node:test";
import assert from "node:assert/strict";
// Dynamic imports since the project is ESM
let query: typeof import("../dist/exports.js").query;
let tool: typeof import("../dist/exports.js").tool;
let loadAgent: typeof import("../dist/exports.js").loadAgent;
let buildTypeboxSchema: typeof import("../dist/tool-loader.js").buildTypeboxSchema;
let wrapToolWithProgrammaticHooks: typeof import("../dist/sdk-hooks.js").wrapToolWithProgrammaticHooks;
before(async () => {
const exports = await import("../dist/exports.js");
query = exports.query;
tool = exports.tool;
loadAgent = exports.loadAgent;
const toolLoader = await import("../dist/tool-loader.js");
buildTypeboxSchema = toolLoader.buildTypeboxSchema;
const sdkHooks = await import("../dist/sdk-hooks.js");
wrapToolWithProgrammaticHooks = sdkHooks.wrapToolWithProgrammaticHooks;
});
// ── Exports ────────────────────────────────────────────────────────────
describe("exports", () => {
it("exports query and tool functions", async () => {
const mod = await import("../dist/exports.js");
assert.equal(typeof mod.query, "function");
assert.equal(typeof mod.tool, "function");
assert.equal(typeof mod.loadAgent, "function");
});
});
// ── tool() helper ──────────────────────────────────────────────────────
describe("tool()", () => {
it("creates a GCToolDefinition with correct fields", () => {
const handler = async (args: any) => `result: ${args.q}`;
const t = tool("search", "Search things", {
properties: { q: { type: "string", description: "Query" } },
required: ["q"],
}, handler);
assert.equal(t.name, "search");
assert.equal(t.description, "Search things");
assert.deepEqual(t.inputSchema.required, ["q"]);
assert.equal(t.handler, handler);
});
it("handler returns string", async () => {
const t = tool("echo", "Echo input", {
properties: { text: { type: "string" } },
}, async (args) => args.text);
const result = await t.handler({ text: "hello" });
assert.equal(result, "hello");
});
it("handler returns object with text and details", async () => {
const t = tool("rich", "Rich output", {
properties: {},
}, async () => ({ text: "done", details: { count: 42 } }));
const result = await t.handler({});
assert.deepEqual(result, { text: "done", details: { count: 42 } });
});
});
// ── buildTypeboxSchema ─────────────────────────────────────────────────
describe("buildTypeboxSchema()", () => {
it("converts string property", () => {
const schema = buildTypeboxSchema({
properties: { name: { type: "string", description: "A name" } },
required: ["name"],
});
assert.equal(schema.type, "object");
assert.ok(schema.properties.name);
});
it("converts number property", () => {
const schema = buildTypeboxSchema({
properties: { count: { type: "number", description: "Count" } },
required: ["count"],
});
assert.ok(schema.properties.count);
});
it("converts boolean property", () => {
const schema = buildTypeboxSchema({
properties: { flag: { type: "boolean", description: "Flag" } },
});
assert.ok(schema.properties.flag);
});
it("converts array property", () => {
const schema = buildTypeboxSchema({
properties: { items: { type: "array", description: "Items" } },
});
assert.ok(schema.properties.items);
});
it("handles empty schema", () => {
const schema = buildTypeboxSchema({});
assert.equal(schema.type, "object");
});
it("marks non-required fields as optional", () => {
const schema = buildTypeboxSchema({
properties: {
required_field: { type: "string" },
optional_field: { type: "string" },
},
required: ["required_field"],
});
// Typebox Optional wraps with a modifier
assert.ok(schema.properties.required_field);
assert.ok(schema.properties.optional_field);
});
});
// ── wrapToolWithProgrammaticHooks ──────────────────────────────────────
describe("wrapToolWithProgrammaticHooks()", () => {
function makeMockTool(name: string = "test_tool") {
return {
name,
label: name,
description: "A test tool",
parameters: buildTypeboxSchema({ properties: { x: { type: "string" } } }),
execute: async (_id: string, args: any) => ({
content: [{ type: "text" as const, text: `executed with ${JSON.stringify(args)}` }],
details: undefined,
}),
};
}
it("returns tool unchanged when no preToolUse hook", () => {
const t = makeMockTool();
const wrapped = wrapToolWithProgrammaticHooks(t, {}, "sess-1", "agent");
assert.equal(wrapped, t);
});
it("allows execution when hook returns allow", async () => {
const t = makeMockTool();
const wrapped = wrapToolWithProgrammaticHooks(t, {
preToolUse: async () => ({ action: "allow" }),
}, "sess-1", "agent");
const result = await wrapped.execute("call-1", { x: "hello" });
assert.ok(result.content[0].text.includes("hello"));
});
it("blocks execution when hook returns block", async () => {
const t = makeMockTool();
const wrapped = wrapToolWithProgrammaticHooks(t, {
preToolUse: async () => ({ action: "block", reason: "not allowed" }),
}, "sess-1", "agent");
await assert.rejects(
() => wrapped.execute("call-1", { x: "hello" }),
(err: Error) => {
assert.ok(err.message.includes("blocked by hook"));
assert.ok(err.message.includes("not allowed"));
return true;
},
);
});
it("modifies args when hook returns modify", async () => {
const t = makeMockTool();
const wrapped = wrapToolWithProgrammaticHooks(t, {
preToolUse: async () => ({
action: "modify",
args: { x: "modified" },
}),
}, "sess-1", "agent");
const result = await wrapped.execute("call-1", { x: "original" });
assert.ok(result.content[0].text.includes("modified"));
assert.ok(!result.content[0].text.includes("original"));
});
it("passes correct context to hook", async () => {
const t = makeMockTool("my_tool");
let captured: any = null;
const wrapped = wrapToolWithProgrammaticHooks(t, {
preToolUse: async (ctx) => {
captured = ctx;
return { action: "allow" };
},
}, "sess-42", "my_agent");
await wrapped.execute("call-1", { x: "test" });
assert.equal(captured.sessionId, "sess-42");
assert.equal(captured.agentName, "my_agent");
assert.equal(captured.event, "PreToolUse");
assert.equal(captured.toolName, "my_tool");
assert.deepEqual(captured.args, { x: "test" });
});
});
// ── query() error handling ─────────────────────────────────────────────
describe("query()", () => {
it("emits error system message when agent dir is invalid", async () => {
const messages: any[] = [];
for await (const msg of query({
prompt: "hello",
dir: "/nonexistent/path/to/agent",
})) {
messages.push(msg);
}
assert.ok(messages.length > 0);
const errorMsg = messages.find((m) => m.type === "system" && m.subtype === "error");
assert.ok(errorMsg, "should have an error system message");
});
it("returns Query object with expected methods", () => {
const q = query({
prompt: "hello",
dir: "/nonexistent",
});
assert.equal(typeof q.abort, "function");
assert.equal(typeof q.steer, "function");
assert.equal(typeof q.sessionId, "function");
assert.equal(typeof q.messages, "function");
assert.equal(typeof q.next, "function");
assert.equal(typeof q[Symbol.asyncIterator], "function");
// Clean up - drain the generator
q.return();
});
it("fires onError hook on failure", async () => {
let errorCaptured: string | null = null;
const messages: any[] = [];
for await (const msg of query({
prompt: "hello",
dir: "/nonexistent/path",
hooks: {
onError: async (ctx) => {
errorCaptured = ctx.error;
},
},
})) {
messages.push(msg);
}
// Give the async hook a moment to fire
await new Promise((r) => setTimeout(r, 50));
assert.ok(errorCaptured, "onError hook should have been called");
});
it("messages() collects emitted messages", async () => {
const q = query({
prompt: "hello",
dir: "/nonexistent/path",
});
for await (const _msg of q) {
// drain
}
const collected = q.messages();
assert.ok(Array.isArray(collected));
assert.ok(collected.length > 0);
});
});
// ── query().continue() ──────────────────────────────────────────────────
describe("query().continue()", () => {
it("exposes a continue method on the Query object", () => {
const q = query({
prompt: "hello",
dir: "/nonexistent",
});
assert.equal(typeof q.continue, "function");
q.return();
});
it("rejects when called before the agent has finished loading", async () => {
const q = query({
prompt: "hello",
dir: "/nonexistent",
});
await assert.rejects(
() => q.continue(),
(err: Error) => {
assert.ok(err.message.includes("not yet initialized"));
return true;
},
);
q.return();
});
it("steer() also rejects before the agent has finished loading", async () => {
const q = query({
prompt: "hello",
dir: "/nonexistent",
});
assert.throws(
() => q.steer("nudge"),
(err: Error) => {
assert.ok(err.message.includes("not yet initialized"));
return true;
},
);
q.return();
});
it("continue() after a failed load surfaces the same 'not yet initialized' error rather than hanging", async () => {
const messages: any[] = [];
const q = query({
prompt: "hello",
dir: "/nonexistent/path/to/agent",
});
for await (const msg of q) {
messages.push(msg);
}
// The initial run already failed (agent dir invalid) and never got
// far enough to construct an Agent instance.
await assert.rejects(() => q.continue());
});
});