summaryrefslogtreecommitdiffhomepage
path: root/packages/todo/src/tool.test.ts
diff options
context:
space:
mode:
authorAdam Malczewski <[email protected]>2026-06-21 14:34:22 +0900
committerAdam Malczewski <[email protected]>2026-06-21 14:34:22 +0900
commitd56fe9cf64719bb330c17b2daee58c0bafa057c9 (patch)
treeb80a25aaee57f959454d468e03f100c38e224b82 /packages/todo/src/tool.test.ts
parent8a4a624d16422467a8e85434c674bb591877e8ea (diff)
downloaddispatch-d56fe9cf64719bb330c17b2daee58c0bafa057c9.tar.gz
dispatch-d56fe9cf64719bb330c17b2daee58c0bafa057c9.zip
feat(todo): per-conversation task list tool + surface
New standard tool extension with a single todo_write tool (opencode todowrite pattern: full-list replace, returns JSON, no business-rule enforcement — the description guides the model). Per-conversation in-memory state + per-conversation surface (rendererId: todo, scope: conversation) via subscriber-notify (message-queue pattern). Wave 0 (kernel contract): added conversationId?: string to ToolExecuteContext (additive, backward-compatible). Wired in dispatch.ts — the kernel already had it but wasn't passing it through to tools. Wave 1 (todo extension): pure core (validateTodos — shape only; getTodos/ setTodos/clearTodos; buildTodoSpec; formatTodoResult). Shell: createTodoWriteTool + surface provider. Tool description matches opencode's todowrite.txt depth (when-to-use, examples, task states). Priority field removed (bloats the tool with little value). 25 tests. Wave 2 (host-bin): registered todo in CORE_EXTENSIONS + dep + root tsconfig ref. Verified: tsc EXIT 0, 1123 vitest, biome clean (314 files). Boot smoke clean. FE handoff: frontend-todo-handoff.md.
Diffstat (limited to 'packages/todo/src/tool.test.ts')
-rw-r--r--packages/todo/src/tool.test.ts101
1 files changed, 101 insertions, 0 deletions
diff --git a/packages/todo/src/tool.test.ts b/packages/todo/src/tool.test.ts
new file mode 100644
index 0000000..a125786
--- /dev/null
+++ b/packages/todo/src/tool.test.ts
@@ -0,0 +1,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);
+ });
+});