@ftrdotdev/pi-tracker 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,358 @@
1
+ /**
2
+ * Bridge tests for `src/index.ts`. The store and the planner have their own
3
+ * suites; these tests pin the thin wiring between them, including the result
4
+ * contract that both `update_item` forms report an affected-items array. That
5
+ * contract broke once: the scalar form returned one `TodoItem`, the renderer
6
+ * called `.map` on it, and the tool failed with `items.map is not a function`.
7
+ */
8
+ import type {
9
+ AgentToolResult,
10
+ ExtensionAPI,
11
+ ExtensionContext,
12
+ ToolDefinition,
13
+ } from "@earendil-works/pi-coding-agent";
14
+ import { describe, expect, it } from "vitest";
15
+ import tracker from "./index.ts";
16
+ import type { TrackerToolDetails, TrackerToolParams } from "./tool-metadata.ts";
17
+
18
+ interface Harness {
19
+ readonly tool: ToolDefinition<any, unknown, any>;
20
+ readonly ctx: ExtensionContext;
21
+ /** Every persisted snapshot, in the order the session received it. */
22
+ readonly appends: unknown[];
23
+ }
24
+
25
+ /**
26
+ * Register the extension against a minimal fake API and return the captured
27
+ * tool plus a context that satisfies the paths `execute` touches (widget
28
+ * refresh and persistence).
29
+ */
30
+ const makeHarness = (): Harness => {
31
+ let registered: ToolDefinition<any, unknown, any> | undefined;
32
+ const appends: unknown[] = [];
33
+ const api = {
34
+ registerTool: (tool: ToolDefinition<any, unknown, any>) => {
35
+ registered = tool;
36
+ },
37
+ registerCommand: () => {},
38
+ on: () => {},
39
+ appendEntry: (_type: string, data: unknown) => {
40
+ appends.push(data);
41
+ },
42
+ } as unknown as ExtensionAPI;
43
+ tracker(api);
44
+ if (!registered) throw new Error("tracker did not register a tool");
45
+ const ctx = {
46
+ ui: { setWidget: () => {}, notify: () => {} },
47
+ } as unknown as ExtensionContext;
48
+ return { tool: registered, ctx, appends };
49
+ };
50
+
51
+ const run = (
52
+ harness: Harness,
53
+ params: TrackerToolParams,
54
+ ): Promise<AgentToolResult<TrackerToolDetails>> =>
55
+ harness.tool.execute("test-call", params, undefined, undefined, harness.ctx) as Promise<
56
+ AgentToolResult<TrackerToolDetails>
57
+ >;
58
+
59
+ const textOf = (result: AgentToolResult<TrackerToolDetails>): string => {
60
+ const block = result.content[0];
61
+ return block?.type === "text" ? block.text : "";
62
+ };
63
+
64
+ describe("tracker tool bridge", () => {
65
+ it("returns an affected-items array for the scalar update_item form", async () => {
66
+ const harness = makeHarness();
67
+ await run(harness, {
68
+ action: "create_list",
69
+ name: "Work",
70
+ initial_items: ["write plan", "ship it"],
71
+ });
72
+
73
+ const result = await run(harness, { action: "update_item", item_id: "Work:1", done: true });
74
+
75
+ expect(result.details?.items).toHaveLength(1);
76
+ expect(result.details?.items?.[0]?.done).toBe(true);
77
+ expect(textOf(result)).toContain("Updated 1 item");
78
+ expect(textOf(result)).toContain("Work:1 (completed)");
79
+ });
80
+
81
+ it("keeps the text patch for the scalar update_item form", async () => {
82
+ const harness = makeHarness();
83
+ await run(harness, { action: "create_list", name: "Work", initial_items: ["old"] });
84
+
85
+ const result = await run(harness, { action: "update_item", item_id: "Work:1", text: "new" });
86
+
87
+ expect(result.details?.items).toHaveLength(1);
88
+ expect(result.details?.items?.[0]?.text).toBe("new");
89
+ expect(textOf(result)).toContain("Work:1 (text: new)");
90
+ });
91
+
92
+ it("returns one entry per patch for the batch update_item form", async () => {
93
+ const harness = makeHarness();
94
+ await run(harness, { action: "create_list", name: "Work", initial_items: ["a", "b", "c"] });
95
+
96
+ const result = await run(harness, {
97
+ action: "update_item",
98
+ list_id: 1,
99
+ items: [
100
+ { item_id: "Work:1", done: true },
101
+ { item_id: "Work:3", text: "cee" },
102
+ ],
103
+ });
104
+
105
+ expect(result.details?.items).toHaveLength(2);
106
+ expect(textOf(result)).toContain("Updated 2 items");
107
+ expect(textOf(result)).toContain("Work:3 (text: cee)");
108
+ });
109
+
110
+ it("shows id-based references in the list output", async () => {
111
+ const harness = makeHarness();
112
+ await run(harness, {
113
+ action: "create_list",
114
+ name: "Work",
115
+ initial_items: ["write plan", "ship it"],
116
+ });
117
+
118
+ await run(harness, { action: "update_item", item_id: "Work:2", done: true });
119
+ const result = await run(harness, { action: "list" });
120
+
121
+ expect(textOf(result)).toContain("[x] #Work:2: ship it");
122
+ });
123
+
124
+ it("shows stable ids after a removal", async () => {
125
+ const harness = makeHarness();
126
+ await run(harness, { action: "create_list", name: "Work", initial_items: ["a", "b", "c"] });
127
+
128
+ await run(harness, { action: "remove_item", item_id: "Work:1" });
129
+ const result = await run(harness, { action: "list" });
130
+
131
+ // The remaining items keep their ids instead of shifting down.
132
+ expect(textOf(result)).toContain("#Work:2: b");
133
+ expect(textOf(result)).toContain("#Work:3: c");
134
+ expect(textOf(result)).not.toContain("#Work:1");
135
+ });
136
+
137
+ it("reports an error result for a missing item instead of throwing", async () => {
138
+ const harness = makeHarness();
139
+ await run(harness, { action: "create_list", name: "Work", initial_items: ["only"] });
140
+
141
+ const result = await run(harness, { action: "update_item", item_id: "Work:9", done: true });
142
+
143
+ expect(result.details?.error).toContain("Work:9");
144
+ expect(result.content[0]).toMatchObject({ type: "text" });
145
+ });
146
+
147
+ it("accepts dependencies in the object item form", async () => {
148
+ const harness = makeHarness();
149
+ await run(harness, {
150
+ action: "create_list",
151
+ name: "Work",
152
+ initial_items: ["a", { text: "b", deps: ["Work:1"] }],
153
+ });
154
+
155
+ const added = await run(harness, {
156
+ action: "add_item",
157
+ list_id: 1,
158
+ text: { text: "c", deps: ["Work:2"] },
159
+ });
160
+
161
+ expect(added.details?.error).toBeUndefined();
162
+ expect(added.details?.items?.[0]?.deps).toEqual(["Work:2"]);
163
+ expect(textOf(added)).toContain("Work:3");
164
+ });
165
+
166
+ it("rejects a malformed dependency reference with an actionable message", async () => {
167
+ const harness = makeHarness();
168
+ await run(harness, { action: "create_list", name: "Work", initial_items: ["a"] });
169
+
170
+ const result = await run(harness, {
171
+ action: "add_item",
172
+ list_id: 1,
173
+ text: { text: "b", deps: ["nonsense"] },
174
+ });
175
+
176
+ expect(result.details?.error).toContain("listName:id");
177
+ });
178
+
179
+ it("names the cycle when a dependency would close one", async () => {
180
+ const harness = makeHarness();
181
+ await run(harness, {
182
+ action: "create_list",
183
+ name: "Work",
184
+ initial_items: ["a", { text: "b", deps: ["Work:1"] }],
185
+ });
186
+
187
+ const result = await run(harness, {
188
+ action: "update_item",
189
+ item_id: "Work:1",
190
+ deps: ["Work:2"],
191
+ });
192
+
193
+ expect(result.details?.error).toContain("Work:1");
194
+ expect(result.details?.error).toContain("Work:2");
195
+ });
196
+
197
+ it("annotates blocked items and names what is ready", async () => {
198
+ const harness = makeHarness();
199
+ await run(harness, {
200
+ action: "create_list",
201
+ name: "Work",
202
+ initial_items: ["a", { text: "b", deps: ["Work:1"] }, { text: "c", deps: ["Work:2"] }],
203
+ });
204
+
205
+ const text = textOf(await run(harness, { action: "list" }));
206
+
207
+ expect(text).toContain("blocked by #Work:1");
208
+ expect(text).toContain("blocked by #Work:2");
209
+ expect(text).toContain("Ready now: #Work:1");
210
+ });
211
+
212
+ it("leaves the list output untouched for a dependency-free list", async () => {
213
+ const harness = makeHarness();
214
+ await run(harness, { action: "create_list", name: "Work", initial_items: ["a", "b"] });
215
+
216
+ const text = textOf(await run(harness, { action: "list" }));
217
+
218
+ expect(text).not.toContain("blocked by");
219
+ expect(text).not.toContain("Ready now");
220
+ expect(text).toBe("[1] Work — 0/2 (active)\n [ ] #Work:1: a\n [ ] #Work:2: b");
221
+ });
222
+
223
+ it("names the blockers when a completion is refused", async () => {
224
+ const harness = makeHarness();
225
+ await run(harness, {
226
+ action: "create_list",
227
+ name: "Work",
228
+ initial_items: ["a", { text: "b", deps: ["Work:1"] }],
229
+ });
230
+
231
+ const result = await run(harness, { action: "update_item", item_id: "Work:2", done: true });
232
+
233
+ expect(result.details?.error).toContain("Work:1");
234
+ expect(result.details?.error).toContain("blocked");
235
+ });
236
+
237
+ it("notes the done dependents when a dependency is reopened", async () => {
238
+ const harness = makeHarness();
239
+ await run(harness, {
240
+ action: "create_list",
241
+ name: "Work",
242
+ initial_items: ["a", { text: "b", deps: ["Work:1"] }],
243
+ });
244
+ await run(harness, { action: "update_item", item_id: "Work:1", done: true });
245
+ await run(harness, { action: "update_item", item_id: "Work:2", done: true });
246
+
247
+ const text = textOf(
248
+ await run(harness, { action: "update_item", item_id: "Work:1", done: false }),
249
+ );
250
+
251
+ expect(text).toContain("Note:");
252
+ expect(text).toContain("Work:2");
253
+ });
254
+
255
+ it("adds no reopen note when no dependent is done", async () => {
256
+ const harness = makeHarness();
257
+ await run(harness, {
258
+ action: "create_list",
259
+ name: "Work",
260
+ initial_items: ["a", { text: "b", deps: ["Work:1"] }],
261
+ });
262
+ await run(harness, { action: "update_item", item_id: "Work:1", done: true });
263
+
264
+ const text = textOf(
265
+ await run(harness, { action: "update_item", item_id: "Work:1", done: false }),
266
+ );
267
+
268
+ expect(text).not.toContain("Note:");
269
+ });
270
+
271
+ it("marks a done item whose dependency is open again", async () => {
272
+ const harness = makeHarness();
273
+ await run(harness, {
274
+ action: "create_list",
275
+ name: "Work",
276
+ initial_items: ["a", { text: "b", deps: ["Work:1"] }],
277
+ });
278
+ await run(harness, { action: "update_item", item_id: "Work:1", done: true });
279
+ await run(harness, { action: "update_item", item_id: "Work:2", done: true });
280
+ await run(harness, { action: "update_item", item_id: "Work:1", done: false });
281
+
282
+ const text = textOf(await run(harness, { action: "list" }));
283
+
284
+ // The row is done, so it is not "blocked": it says what it waits on.
285
+ expect(text).toContain("[x] #Work:2: b (waiting on #Work:1)");
286
+ expect(text).not.toContain("blocked by");
287
+ expect(text).toContain("Ready now: #Work:1");
288
+ });
289
+
290
+ it("notes a done item that a dependency edit left waiting", async () => {
291
+ const harness = makeHarness();
292
+ await run(harness, { action: "create_list", name: "Work", initial_items: ["a", "b"] });
293
+ await run(harness, { action: "update_item", item_id: "Work:2", done: true });
294
+
295
+ const text = textOf(
296
+ await run(harness, { action: "update_item", item_id: "Work:2", deps: ["Work:1"] }),
297
+ );
298
+
299
+ expect(text).toContain("Note: Work:2 is done but now waits on Work:1");
300
+ });
301
+
302
+ it("names the dependencies a deps edit removed", async () => {
303
+ const harness = makeHarness();
304
+ await run(harness, {
305
+ action: "create_list",
306
+ name: "Work",
307
+ initial_items: ["a", { text: "b", deps: ["Work:1"] }, "c"],
308
+ });
309
+
310
+ const cleared = await run(harness, { action: "update_item", item_id: "Work:2", deps: [] });
311
+ expect(textOf(cleared)).toContain("deps cleared (was Work:1)");
312
+
313
+ await run(harness, { action: "update_item", item_id: "Work:2", deps: ["Work:1"] });
314
+ const replaced = await run(harness, {
315
+ action: "update_item",
316
+ item_id: "Work:2",
317
+ deps: ["Work:3"],
318
+ });
319
+ expect(textOf(replaced)).toContain("deps: Work:1 → Work:3");
320
+ });
321
+
322
+ it("persists overlapping mutations in order", async () => {
323
+ const harness = makeHarness();
324
+ await run(harness, { action: "create_list", name: "Work", initial_items: ["a", "b"] });
325
+
326
+ // Two calls in flight at once. The last snapshot the session receives must
327
+ // be the state after both, or a later restore resurrects an older one.
328
+ await Promise.all([
329
+ run(harness, { action: "update_item", item_id: "Work:1", done: true }),
330
+ run(harness, { action: "update_item", item_id: "Work:2", done: true }),
331
+ ]);
332
+
333
+ const last = harness.appends.at(-1) as {
334
+ lists: Array<{ items: Array<{ done: boolean }> }>;
335
+ };
336
+ expect(last.lists[0]?.items.map((item) => item.done)).toEqual([true, true]);
337
+
338
+ const text = textOf(await run(harness, { action: "list" }));
339
+ expect(text).toContain("[x] #Work:1: a");
340
+ expect(text).toContain("[x] #Work:2: b");
341
+ });
342
+
343
+ it("shows items in dependency order, not stored order", async () => {
344
+ const harness = makeHarness();
345
+ await run(harness, {
346
+ action: "create_list",
347
+ name: "Work",
348
+ initial_items: [{ text: "ship", deps: ["Work:2"] }, "write plan"],
349
+ });
350
+
351
+ const text = textOf(await run(harness, { action: "list" }));
352
+
353
+ // "write plan" is the dependency, so it is listed before "ship" even
354
+ // though "ship" was created first.
355
+ expect(text.indexOf("write plan")).toBeLessThan(text.indexOf("ship"));
356
+ expect(text).toContain("#Work:2: write plan");
357
+ });
358
+ });