|
8 | 8 | getTodoSnapshot, |
9 | 9 | registerTodoOverlay, |
10 | 10 | registerTodosCommand, |
| 11 | + rememberTodowriteToolCallTodos, |
11 | 12 | setTodoSnapshot, |
12 | 13 | TodoOverlay, |
13 | 14 | } from "./todo-view-pi"; |
@@ -89,6 +90,129 @@ describe("todowrite tool rendering", () => { |
89 | 90 | renderCall({ todos } as never, identityTheme, {} as never).render(80)[0], |
90 | 91 | ).toContain("Todos — 3 active"); |
91 | 92 | }); |
| 93 | + it("uses cached todos when the transcript component was created with empty args", () => { |
| 94 | + const tool = createTodowriteTool(); |
| 95 | + const renderCall = tool.renderCall; |
| 96 | + if (!renderCall) throw new Error("todowrite renderCall missing"); |
| 97 | + const todos = [ |
| 98 | + { id: "1", content: "Work", status: "in_progress" as const }, |
| 99 | + ...Array.from({ length: 9 }, (_, index) => ({ |
| 100 | + id: String(index + 2), |
| 101 | + content: `Done ${index + 2}`, |
| 102 | + status: "completed" as const, |
| 103 | + })), |
| 104 | + ]; |
| 105 | + rememberTodowriteToolCallTodos("call-stale", todos); |
| 106 | + expect( |
| 107 | + renderCall({}, identityTheme, { |
| 108 | + toolCallId: "call-stale", |
| 109 | + } as never).render(80)[0], |
| 110 | + ).toContain("Todos — 1 active"); |
| 111 | + }); |
| 112 | + it("falls back to args when no cached tool-call todos exist", () => { |
| 113 | + const tool = createTodowriteTool(); |
| 114 | + const renderCall = tool.renderCall; |
| 115 | + if (!renderCall) throw new Error("todowrite renderCall missing"); |
| 116 | + const todos = [ |
| 117 | + { id: "1", content: "Plan", status: "pending" as const }, |
| 118 | + { id: "2", content: "Build", status: "in_progress" as const }, |
| 119 | + { id: "3", content: "Done", status: "completed" as const }, |
| 120 | + ]; |
| 121 | + expect( |
| 122 | + renderCall({ todos } as never, identityTheme, { |
| 123 | + toolCallId: "call-miss", |
| 124 | + } as never).render(80)[0], |
| 125 | + ).toContain("Todos — 2 active"); |
| 126 | + }); |
| 127 | + it("renders identically when cache and args already agree", () => { |
| 128 | + const tool = createTodowriteTool(); |
| 129 | + const renderCall = tool.renderCall; |
| 130 | + if (!renderCall) throw new Error("todowrite renderCall missing"); |
| 131 | + const todos = [ |
| 132 | + { id: "1", content: "Plan", status: "pending" as const }, |
| 133 | + { id: "2", content: "Done", status: "completed" as const }, |
| 134 | + ]; |
| 135 | + rememberTodowriteToolCallTodos("call-equal", todos); |
| 136 | + expect( |
| 137 | + renderCall({ todos } as never, identityTheme, { |
| 138 | + toolCallId: "call-equal", |
| 139 | + } as never).render(80), |
| 140 | + ).toEqual( |
| 141 | + renderCall({ todos } as never, identityTheme, { |
| 142 | + toolCallId: "call-equal-miss", |
| 143 | + } as never).render(80), |
| 144 | + ); |
| 145 | + }); |
| 146 | + it("uses cached todos for stale-empty results", () => { |
| 147 | + const tool = createTodowriteTool(); |
| 148 | + const renderResult = tool.renderResult; |
| 149 | + if (!renderResult) throw new Error("todowrite renderResult missing"); |
| 150 | + const todos = [ |
| 151 | + { id: "1", content: "Work", status: "in_progress" as const }, |
| 152 | + { id: "2", content: "Done", status: "completed" as const }, |
| 153 | + ]; |
| 154 | + rememberTodowriteToolCallTodos("call-stale-result", todos); |
| 155 | + const rendered = renderResult( |
| 156 | + { |
| 157 | + details: { todos: [] }, |
| 158 | + content: [{ type: "text", text: "[]" }], |
| 159 | + }, |
| 160 | + { expanded: false, isPartial: false }, |
| 161 | + identityTheme, |
| 162 | + { toolCallId: "call-stale-result" } as never, |
| 163 | + ) |
| 164 | + .render(120) |
| 165 | + .join("\n"); |
| 166 | + expect(rendered).toContain("◐ #1 Work"); |
| 167 | + expect(rendered).toContain("✓ #2 Done"); |
| 168 | + }); |
| 169 | + it("prefers parsed tool results over cached todos when both are present", () => { |
| 170 | + const tool = createTodowriteTool(); |
| 171 | + const renderResult = tool.renderResult; |
| 172 | + if (!renderResult) throw new Error("todowrite renderResult missing"); |
| 173 | + rememberTodowriteToolCallTodos("call-real-result", [ |
| 174 | + { id: "stale", content: "Stale", status: "pending" }, |
| 175 | + ]); |
| 176 | + const rendered = renderResult( |
| 177 | + { |
| 178 | + details: { |
| 179 | + todos: [{ id: "fresh", content: "Fresh", status: "pending" }], |
| 180 | + }, |
| 181 | + content: [{ type: "text", text: "[]" }], |
| 182 | + }, |
| 183 | + { expanded: false, isPartial: false }, |
| 184 | + identityTheme, |
| 185 | + { toolCallId: "call-real-result" } as never, |
| 186 | + ) |
| 187 | + .render(120) |
| 188 | + .join("\n"); |
| 189 | + expect(rendered).toContain("○ #fresh Fresh"); |
| 190 | + expect(rendered).not.toContain("Stale"); |
| 191 | + }); |
| 192 | + it("evicts the oldest cached tool-call todos after the cap", () => { |
| 193 | + const tool = createTodowriteTool(); |
| 194 | + const renderCall = tool.renderCall; |
| 195 | + if (!renderCall) throw new Error("todowrite renderCall missing"); |
| 196 | + for (let index = 1; index <= 51; index++) { |
| 197 | + rememberTodowriteToolCallTodos(`call-${index}`, [ |
| 198 | + { |
| 199 | + id: String(index), |
| 200 | + content: `Task ${index}`, |
| 201 | + status: "pending", |
| 202 | + }, |
| 203 | + ]); |
| 204 | + } |
| 205 | + expect( |
| 206 | + renderCall({}, identityTheme, { toolCallId: "call-1" } as never).render( |
| 207 | + 80, |
| 208 | + )[0], |
| 209 | + ).toContain("Todos — 0 active"); |
| 210 | + expect( |
| 211 | + renderCall({}, identityTheme, { toolCallId: "call-51" } as never).render( |
| 212 | + 80, |
| 213 | + )[0], |
| 214 | + ).toContain("Todos — 1 active"); |
| 215 | + }); |
92 | 216 | it("caps result rows at 12 with a +N more tail", async () => { |
93 | 217 | const tool = createTodowriteTool(); |
94 | 218 | const todos = Array.from({ length: 14 }, (_, index) => ({ |
@@ -258,10 +382,16 @@ describe("TodoOverlay lifecycle", () => { |
258 | 382 | expect(widget.render(120)).toEqual([]); |
259 | 383 | }); |
260 | 384 | it("seeds from session_meta.last_todo_state and clears on session switch", async () => { |
| 385 | + const tool = createTodowriteTool(); |
| 386 | + const renderCall = tool.renderCall; |
| 387 | + if (!renderCall) throw new Error("todowrite renderCall missing"); |
261 | 388 | const handlers = new Map< |
262 | 389 | string, |
263 | 390 | (event: unknown, ctx: unknown) => Promise<void> | void |
264 | 391 | >(); |
| 392 | + rememberTodowriteToolCallTodos("call-session-switch", [ |
| 393 | + { id: "cached", content: "Cached", status: "pending" }, |
| 394 | + ]); |
265 | 395 | registerTodoOverlay( |
266 | 396 | { |
267 | 397 | on: (event, handler) => handlers.set(event, handler as never), |
@@ -293,6 +423,11 @@ describe("TodoOverlay lifecycle", () => { |
293 | 423 | "magic-context-todos", |
294 | 424 | undefined, |
295 | 425 | ]); |
| 426 | + expect( |
| 427 | + renderCall({}, identityTheme, { |
| 428 | + toolCallId: "call-session-switch", |
| 429 | + } as never).render(80)[0], |
| 430 | + ).toContain("Todos — 0 active"); |
296 | 431 | }); |
297 | 432 | it("caps content rows with a +N more tail", () => { |
298 | 433 | setTodoSnapshot( |
|
0 commit comments