| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- import { describe, expect, it } from "vitest";
- import { buildToolCallTree, reconcileToolCallTree } from "../src/lib/toolCallTree.js";
- describe("buildToolCallTree", () => {
- it("groups child tool calls under their parent while preserving order", () => {
- const tree = buildToolCallTree([
- {
- toolId: "tool-root-before",
- kind: "other",
- title: "EnterPlanMode",
- input: null,
- status: "completed",
- },
- {
- toolId: "tool-task",
- kind: "think",
- title: "Task",
- input: { prompt: "Explore project" },
- status: "completed",
- },
- {
- toolId: "tool-child-read",
- parentToolUseId: "tool-task",
- kind: "read",
- title: "Read package.json",
- input: { file_path: "package.json" },
- status: "completed",
- },
- {
- toolId: "tool-child-list",
- parentToolUseId: "tool-task",
- kind: "execute",
- title: "ls -la",
- input: { command: "ls -la" },
- status: "completed",
- },
- {
- toolId: "tool-root-after",
- kind: "read",
- title: "Read README.md",
- input: { file_path: "README.md" },
- status: "completed",
- },
- ]);
- expect(tree.map((node) => node.toolCall.toolId)).toEqual([
- "tool-root-before",
- "tool-task",
- "tool-root-after",
- ]);
- expect(tree[1]?.childToolCalls.map((node) => node.toolCall.toolId)).toEqual([
- "tool-child-read",
- "tool-child-list",
- ]);
- });
- it("falls back to root when parent tool is missing", () => {
- const tree = buildToolCallTree([
- {
- toolId: "tool-child",
- parentToolUseId: "missing-parent",
- kind: "read",
- title: "Read package.json",
- input: { file_path: "package.json" },
- status: "completed",
- },
- ]);
- expect(tree).toHaveLength(1);
- expect(tree[0]?.toolCall.toolId).toBe("tool-child");
- });
- it("does not attach a tool call to itself", () => {
- const tree = buildToolCallTree([
- {
- toolId: "tool-self",
- parentToolUseId: "tool-self",
- kind: "think",
- title: "Task",
- input: null,
- status: "completed",
- },
- ]);
- expect(tree).toHaveLength(1);
- expect(tree[0]?.toolCall.toolId).toBe("tool-self");
- expect(tree[0]?.childToolCalls).toHaveLength(0);
- });
- it("reuses unchanged nodes and child arrays across equivalent inputs", () => {
- const firstTree = reconcileToolCallTree([
- {
- toolId: "tool-task",
- kind: "think",
- title: "Task",
- input: { prompt: "Explore project" },
- status: "completed",
- },
- {
- toolId: "tool-child-read",
- parentToolUseId: "tool-task",
- kind: "read",
- title: "Read package.json",
- input: { file_path: "package.json" },
- status: "completed",
- },
- ]);
- const secondTree = reconcileToolCallTree(
- [
- {
- toolId: "tool-task",
- kind: "think",
- title: "Task",
- input: { prompt: "Explore project" },
- status: "completed",
- },
- {
- toolId: "tool-child-read",
- parentToolUseId: "tool-task",
- kind: "read",
- title: "Read package.json",
- input: { file_path: "package.json" },
- status: "completed",
- },
- ],
- firstTree,
- );
- expect(secondTree).toBe(firstTree);
- expect(secondTree[0]).toBe(firstTree[0]);
- expect(secondTree[0]?.toolCall).toBe(firstTree[0]?.toolCall);
- expect(secondTree[0]?.childToolCalls).toBe(firstTree[0]?.childToolCalls);
- expect(secondTree[0]?.childToolCalls[0]).toBe(firstTree[0]?.childToolCalls[0]);
- });
- it("replaces only the changed branch when a child tool call changes", () => {
- const firstTree = reconcileToolCallTree([
- {
- toolId: "tool-task",
- kind: "think",
- title: "Task",
- input: { prompt: "Explore project" },
- status: "completed",
- },
- {
- toolId: "tool-child-read",
- parentToolUseId: "tool-task",
- kind: "read",
- title: "Read package.json",
- input: { file_path: "package.json" },
- status: "pending",
- },
- {
- toolId: "tool-root-after",
- kind: "read",
- title: "Read README.md",
- input: { file_path: "README.md" },
- status: "completed",
- },
- ]);
- const secondTree = reconcileToolCallTree(
- [
- {
- toolId: "tool-task",
- kind: "think",
- title: "Task",
- input: { prompt: "Explore project" },
- status: "completed",
- },
- {
- toolId: "tool-child-read",
- parentToolUseId: "tool-task",
- kind: "read",
- title: "Read package.json",
- input: { file_path: "package.json" },
- status: "completed",
- },
- {
- toolId: "tool-root-after",
- kind: "read",
- title: "Read README.md",
- input: { file_path: "README.md" },
- status: "completed",
- },
- ],
- firstTree,
- );
- expect(secondTree).not.toBe(firstTree);
- expect(secondTree[0]).not.toBe(firstTree[0]);
- expect(secondTree[0]?.toolCall).toBe(firstTree[0]?.toolCall);
- expect(secondTree[0]?.childToolCalls[0]).not.toBe(firstTree[0]?.childToolCalls[0]);
- expect(secondTree[1]).toBe(firstTree[1]);
- });
- });
|