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
|
import { describe, expect, it, vi } from "vitest";
import { createTaskListTool, TaskList } from "../../src/tools/task-list.js";
import type { TaskItem } from "../../src/types/index.js";
describe("TaskList (declarative store)", () => {
it("starts empty", () => {
const list = new TaskList();
expect(list.getTasks()).toEqual([]);
});
it("setTasks replaces the whole list and assigns positional ids", () => {
const list = new TaskList();
const result = list.setTasks([
{ content: "first", status: "in_progress" },
{ content: "second", status: "pending" },
]);
expect(result).toEqual([
{ id: "task-1", content: "first", status: "in_progress" },
{ id: "task-2", content: "second", status: "pending" },
]);
expect(list.getTasks()).toEqual(result);
});
it("a second setTasks fully replaces the previous list (no append)", () => {
const list = new TaskList();
list.setTasks([
{ content: "a", status: "completed" },
{ content: "b", status: "completed" },
{ content: "c", status: "pending" },
]);
const next = list.setTasks([{ content: "only", status: "in_progress" }]);
expect(next).toEqual([{ id: "task-1", content: "only", status: "in_progress" }]);
expect(list.getTasks()).toHaveLength(1);
});
it("preserves all four statuses", () => {
const list = new TaskList();
const result = list.setTasks([
{ content: "p", status: "pending" },
{ content: "i", status: "in_progress" },
{ content: "c", status: "completed" },
{ content: "x", status: "cancelled" },
]);
expect(result.map((t) => t.status)).toEqual([
"pending",
"in_progress",
"completed",
"cancelled",
]);
});
it("defaults missing/invalid status to pending", () => {
const list = new TaskList();
const result = list.setTasks([
{ content: "no status" },
{ content: "bogus", status: "done" },
{ content: "junk", status: 42 },
]);
expect(result.map((t) => t.status)).toEqual(["pending", "pending", "pending"]);
});
it("an empty array clears the list", () => {
const list = new TaskList();
list.setTasks([{ content: "x", status: "pending" }]);
expect(list.setTasks([])).toEqual([]);
expect(list.getTasks()).toEqual([]);
});
it("getTasks returns copies (no external mutation leaks in)", () => {
const list = new TaskList();
list.setTasks([{ content: "x", status: "pending" }]);
const snapshot = list.getTasks();
snapshot[0].content = "mutated";
expect(list.getTasks()[0].content).toBe("x");
});
it("onChange fires on every setTasks with the new snapshot", () => {
const list = new TaskList();
const seen: TaskItem[][] = [];
const unsubscribe = list.onChange((tasks) => seen.push(tasks));
list.setTasks([{ content: "a", status: "pending" }]);
list.setTasks([{ content: "b", status: "completed" }]);
expect(seen).toHaveLength(2);
expect(seen[0]).toEqual([{ id: "task-1", content: "a", status: "pending" }]);
expect(seen[1]).toEqual([{ id: "task-1", content: "b", status: "completed" }]);
unsubscribe();
list.setTasks([{ content: "c", status: "pending" }]);
expect(seen).toHaveLength(2);
});
});
describe("createTaskListTool", () => {
it("exposes a single declarative `todos` parameter and the name `todo`", () => {
const tool = createTaskListTool(new TaskList());
expect(tool.name).toBe("todo");
// One top-level param: the whole-list `todos` array.
const shape = (tool.parameters as { shape: Record<string, unknown> }).shape;
expect(Object.keys(shape)).toEqual(["todos"]);
});
it("execute updates the store and echoes the list WITHOUT ids", async () => {
const list = new TaskList();
const tool = createTaskListTool(list);
const out = await tool.execute({
todos: [
{ content: "plan", status: "completed" },
{ content: "build", status: "in_progress" },
],
});
expect(JSON.parse(out)).toEqual([
{ content: "plan", status: "completed" },
{ content: "build", status: "in_progress" },
]);
// Store has ids; the echo does not.
expect(list.getTasks()).toEqual([
{ id: "task-1", content: "plan", status: "completed" },
{ id: "task-2", content: "build", status: "in_progress" },
]);
});
it("execute fires onChange so the UI broadcast is wired", async () => {
const list = new TaskList();
const cb = vi.fn();
list.onChange(cb);
const tool = createTaskListTool(list);
await tool.execute({ todos: [{ content: "x", status: "pending" }] });
expect(cb).toHaveBeenCalledTimes(1);
});
it("execute with an empty array clears the store", async () => {
const list = new TaskList();
list.setTasks([{ content: "x", status: "pending" }]);
const tool = createTaskListTool(list);
const out = await tool.execute({ todos: [] });
expect(JSON.parse(out)).toEqual([]);
expect(list.getTasks()).toEqual([]);
});
it("execute defaults invalid status to pending in both store and echo", async () => {
const list = new TaskList();
const tool = createTaskListTool(list);
const out = await tool.execute({ todos: [{ content: "x", status: "done" }] });
expect(JSON.parse(out)).toEqual([{ content: "x", status: "pending" }]);
expect(list.getTasks()[0].status).toBe("pending");
});
it("execute rejects a non-array todos param", async () => {
const tool = createTaskListTool(new TaskList());
const out = await tool.execute({ todos: "nope" });
expect(out).toMatch(/Error/);
});
it("execute rejects items missing a content string", async () => {
const tool = createTaskListTool(new TaskList());
const out = await tool.execute({ todos: [{ status: "pending" }] });
expect(out).toMatch(/Error/);
});
});
|