Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

toolActivity.test.ts

123 lines4.0 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 classifyToolActivity,
5 collectToolFilePaths,
6 deriveToolActivityPresentation,
7 formatReadToolLabel,
8 formatSearchToolLabel,
9 mergeToolActivityData,
10} from "./toolActivity.ts";
11​
12describe("toolActivity", () => {
13 it("normalizes command tools to a stable ran-command label", () => {
14 expect(
15 deriveToolActivityPresentation({
16 itemType: "command_execution",
17 title: "Terminal",
18 detail: "Terminal",
19 data: {
20 command: "bun run lint",
21 },
22 fallbackSummary: "Terminal",
23 }),
24 ).toEqual({
25 summary: "Ran command",
26 detail: "bun run lint",
27 });
28 });
29​
30 it("uses structured file paths for read-file tools when available", () => {
31 expect(
32 deriveToolActivityPresentation({
33 itemType: "dynamic_tool_call",
34 title: "Read File",
35 detail: "Read File",
36 data: {
37 kind: "read",
38 locations: [{ path: "/tmp/app.ts" }],
39 },
40 fallbackSummary: "Read File",
41 }),
42 ).toEqual({
43 summary: "Read /tmp/app.ts",
44 });
45 });
46​
47 it("drops duplicated generic read-file detail when no path is available", () => {
48 expect(
49 deriveToolActivityPresentation({
50 itemType: "dynamic_tool_call",
51 title: "Read File",
52 detail: "Read File",
53 data: {
54 kind: "read",
55 rawInput: {},
56 },
57 fallbackSummary: "Read File",
58 }),
59 ).toEqual({
60 summary: "Read file",
61 });
62 });
63​
64 it("classifies from kind and toolName without sniffing titles", () => {
65 expect(classifyToolActivity({ data: { kind: "read" } })).toBe("read");
66 expect(classifyToolActivity({ data: { toolName: "Grep" } })).toBe("search");
67 expect(classifyToolActivity({ data: { toolName: "Read" } })).toBe("read");
68 for (const toolName of ["github.read_file", "mongodb.find", "mcp__github__read_file"]) {
69 expect(classifyToolActivity({ data: { toolName } })).toBe("other");
70 }
71 expect(classifyToolActivity({ title: "Find", data: {} })).toBe("other");
72 });
73​
74 it("classifies Claude search tools ahead of their broad file-read request kind", () => {
75 for (const toolName of ["Glob", "Grep", "LS"]) {
76 expect(classifyToolActivity({ requestKind: "file-read", data: { toolName } })).toBe("search");
77 }
78 expect(classifyToolActivity({ requestKind: "file-read", data: { toolName: "Read" } })).toBe(
79 "read",
80 );
81 });
82​
83 it("formats read and search labels from structured input", () => {
84 expect(formatReadToolLabel("src/env.ts")).toBe("Read src/env.ts");
85 expect(formatReadToolLabel("src/env.ts", 2)).toBe("Read src/env.ts +2 more");
86 expect(formatReadToolLabel("")).toBe("Read file");
87 expect(
88 formatSearchToolLabel({
89 input: { pattern: "TODO", path: "apps/web" },
90 }),
91 ).toBe("Searched TODO in web");
92 expect(
93 formatSearchToolLabel({
94 input: { glob: "*.ts", path: "/tmp/t3chat-new" },
95 }),
96 ).toBe("Searched files *.ts in t3chat-new");
97 expect(
98 formatSearchToolLabel({ rawInput: {}, input: { pattern: "TODO", path: "apps/web" } }),
99 ).toBe("Searched TODO in web");
100 expect(formatSearchToolLabel({ input: { globPattern: "*.tsx", path: "apps/web" } })).toBe(
101 "Searched files *.tsx in web",
102 );
103 expect(
104 formatSearchToolLabel({ input: { pattern: "TODO", glob: "*.ts", path: "apps/web" } }),
105 ).toBe("Searched TODO in web");
106 });
107​
108 it("keeps bare filenames from explicit path fields", () => {
109 expect(collectToolFilePaths({ input: { file_path: "README" } })).toEqual(["README"]);
110 });
111​
112 it("keeps the first non-empty rawInput when a later update is empty", () => {
113 expect(
114 mergeToolActivityData({ rawInput: { path: "src/a.ts" } }, { rawInput: {}, kind: "read" }),
115 ).toEqual({
116 rawInput: { path: "src/a.ts" },
117 kind: "read",
118 });
119 expect(
120 mergeToolActivityData({ rawInput: { path: "src/a.ts" } }, { rawInput: { startLine: 4 } }),
121 ).toEqual({ rawInput: { path: "src/a.ts", startLine: 4 } });
122 });
123});