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
|
import { createLogger, type ToolExecuteContext } from "@dispatch/kernel";
import { describe, expect, it, vi } from "vitest";
import { getTodos, type TodoState } from "./pure.js";
import { createTodoWriteTool } from "./tool.js";
function stubCtx(overrides?: Partial<ToolExecuteContext>): ToolExecuteContext {
return {
toolCallId: "test-call-1",
onOutput: () => {},
signal: new AbortController().signal,
log: createLogger(
{ extensionId: "test" },
{ emit: () => {} },
{ now: () => 0, newId: () => "id" },
),
...overrides,
};
}
describe("todo_write", () => {
it("todo_write: replaces list + returns JSON result", async () => {
const state: TodoState = new Map();
const notify = vi.fn();
const tool = createTodoWriteTool({ state, notify });
const todos = [
{ content: "a", status: "pending" },
{ content: "b", status: "in_progress" },
];
const result = await tool.execute({ todos }, stubCtx({ conversationId: "c1" }));
expect(result.isError).toBeUndefined();
expect(result.content).toBe(JSON.stringify(todos, null, 2));
expect(getTodos(state, "c1")).toEqual(todos);
});
it("todo_write: calls notify after write", async () => {
const state: TodoState = new Map();
const notify = vi.fn();
const tool = createTodoWriteTool({ state, notify });
expect(notify).not.toHaveBeenCalled();
await tool.execute(
{ todos: [{ content: "x", status: "pending" }] },
stubCtx({ conversationId: "c1" }),
);
expect(notify).toHaveBeenCalledTimes(1);
});
it("todo_write: validation error returns isError", async () => {
const state: TodoState = new Map();
const notify = vi.fn();
const tool = createTodoWriteTool({ state, notify });
const result = await tool.execute(
{ todos: [{ content: "x", status: "bogus" }] },
stubCtx({ conversationId: "c1" }),
);
expect(result.isError).toBe(true);
expect(result.content).toContain("Error:");
expect(notify).not.toHaveBeenCalled();
});
it("todo_write: uses conversationId from ctx", async () => {
const state: TodoState = new Map();
const notify = vi.fn();
const tool = createTodoWriteTool({ state, notify });
await tool.execute(
{ todos: [{ content: "x", status: "pending" }] },
stubCtx({ conversationId: "conv-42" }),
);
expect(getTodos(state, "conv-42")).toHaveLength(1);
// a different conversation is unaffected
expect(getTodos(state, "conv-other")).toEqual([]);
});
it("todo_write: errors when conversationId is absent", async () => {
const state: TodoState = new Map();
const notify = vi.fn();
const tool = createTodoWriteTool({ state, notify });
const result = await tool.execute({ todos: [{ content: "x", status: "pending" }] }, stubCtx());
expect(result.isError).toBe(true);
expect(result.content).toBe("Error: no conversation context for todo.");
expect(notify).not.toHaveBeenCalled();
expect(state.size).toBe(0);
});
it("todo_write: accepts empty array (clears list)", async () => {
const state: TodoState = new Map();
const notify = vi.fn();
const tool = createTodoWriteTool({ state, notify });
// seed
await tool.execute(
{ todos: [{ content: "seed", status: "pending" }] },
stubCtx({ conversationId: "c1" }),
);
expect(getTodos(state, "c1")).toHaveLength(1);
// clear via empty list
const result = await tool.execute({ todos: [] }, stubCtx({ conversationId: "c1" }));
expect(result.isError).toBeUndefined();
expect(result.content).toBe("[]");
expect(getTodos(state, "c1")).toEqual([]);
expect(notify).toHaveBeenCalledTimes(2);
});
});
|