todo-tool.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. createSessionId,
  4. formatTodoStateForModel,
  5. todoItemsFromToolResultContent,
  6. type SessionStorePort,
  7. type TodoItem,
  8. } from "@zcode/contracts";
  9. import { todoReadHandler, todoWriteHandler } from "../src/tool/handlers/todo.js";
  10. import type { ToolExecutionContext } from "../src/tool/types.js";
  11. describe("todo tools", () => {
  12. it("writes and reads session-local todos through the session store port", async () => {
  13. const store = createTodoStore();
  14. const context = createContext(store);
  15. const write = await todoWriteHandler(
  16. {
  17. todos: [
  18. {
  19. content: "Review v2 spec",
  20. priority: "high",
  21. status: "completed",
  22. },
  23. {
  24. content: "Implement TodoWrite",
  25. priority: "medium",
  26. status: "in_progress",
  27. },
  28. ],
  29. },
  30. context,
  31. );
  32. const read = await todoReadHandler({}, context);
  33. expect(write).toEqual({
  34. oldTodos: [],
  35. todos: [
  36. {
  37. content: "Review v2 spec",
  38. priority: "high",
  39. status: "completed",
  40. },
  41. {
  42. content: "Implement TodoWrite",
  43. priority: "medium",
  44. status: "in_progress",
  45. },
  46. ],
  47. summary: {
  48. completed: 1,
  49. inProgress: 1,
  50. pending: 0,
  51. total: 2,
  52. },
  53. });
  54. expect(read).toEqual({
  55. todos: write.todos,
  56. });
  57. });
  58. it("allows multiple in-progress todos and updates state", async () => {
  59. const store = createTodoStore();
  60. const context = createContext(store);
  61. const write = await todoWriteHandler(
  62. {
  63. todos: [
  64. {
  65. content: "First active task",
  66. priority: "high",
  67. status: "in_progress",
  68. },
  69. {
  70. content: "Second active task",
  71. priority: "medium",
  72. status: "in_progress",
  73. },
  74. ],
  75. },
  76. context,
  77. );
  78. expect(write.summary.inProgress).toBe(2);
  79. await expect(todoReadHandler({}, context)).resolves.toEqual({ todos: write.todos });
  80. });
  81. it("parses serialized TodoRead and TodoWrite outputs for projections", () => {
  82. const todos: TodoItem[] = [
  83. {
  84. content: "Project ACP plan entries",
  85. priority: "high",
  86. status: "in_progress",
  87. },
  88. ];
  89. expect(todoItemsFromToolResultContent(JSON.stringify({ todos }))).toEqual(todos);
  90. expect(
  91. todoItemsFromToolResultContent(
  92. JSON.stringify({
  93. oldTodos: [],
  94. todos,
  95. summary: {
  96. completed: 0,
  97. inProgress: 1,
  98. pending: 0,
  99. total: 1,
  100. },
  101. }),
  102. ),
  103. ).toEqual(todos);
  104. expect(formatTodoStateForModel(todos)).toContain(
  105. "1. [in_progress][high] Project ACP plan entries",
  106. );
  107. expect(todoItemsFromToolResultContent("{not-json")).toBeUndefined();
  108. });
  109. });
  110. function createTodoStore(): SessionStorePort {
  111. const todos = new Map<string, TodoItem[]>();
  112. return {
  113. async readTodos(input) {
  114. return [...(todos.get(input.sessionID) ?? [])];
  115. },
  116. async updateTodos(input) {
  117. todos.set(
  118. input.sessionID,
  119. input.todos.map((todo) => ({ ...todo })),
  120. );
  121. },
  122. } as unknown as SessionStorePort;
  123. }
  124. function createContext(sessionStore: SessionStorePort): ToolExecutionContext {
  125. return {
  126. abortSignal: new AbortController().signal,
  127. sessionId: createSessionId("todo-tool"),
  128. sessionStore,
  129. toolCallId: "todo-call",
  130. traceId: "trace-todo" as never,
  131. workingDirectory: "/tmp/zcode-todo-tool",
  132. workspaceRoot: "/tmp/zcode-todo-tool",
  133. };
  134. }