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

t3ToolSummary.test.ts

151 lines5.4 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { summarizeT3ToolCalls, type T3ToolSummaryCall } from "./t3ToolSummary.ts";
4​
5function completed(input: unknown, output?: unknown): T3ToolSummaryCall {
6 return { input, output, outcome: "completed" };
7}
8​
9describe("summarizeT3ToolCalls", () => {
10 it("counts messages and distinct destinations across delivery modes, deduplicating retries", () => {
11 const calls = Array.from({ length: 5 }, (_, i) =>
12 completed(
13 { threadId: `thread-${i % 2}`, mode: ["auto", "queue", "steer", "restart"][i % 4] },
14 { messageId: `message-${i}`, threadId: `thread-${i % 2}` },
15 ),
16 );
17 expect(summarizeT3ToolCalls("thread-send", [...calls, calls[0]!])).toEqual({
18 label: "Sent 5 messages to 2 threads",
19 failedCount: 0,
20 });
21 });
22​
23 it("reads provider result envelopes without treating JSON in the message as result data", () => {
24 const result = { messageId: "message-1", threadId: "actual-thread" };
25 const json = JSON.stringify(result);
26 const outputs = [
27 result,
28 { structuredContent: result },
29 [{ type: "text", text: json }],
30 { content: [{ text: { text: json } }], isError: false },
31 json,
32 ];
33 const calls = outputs.map((output) =>
34 completed(
35 {
36 toolName: "t3_thread_send",
37 args: { threadId: "input-thread", message: '{"threadId":"fake"}' },
38 },
39 output,
40 ),
41 );
42 expect(summarizeT3ToolCalls("thread-send", calls).label).toBe("Sent 1 message to 1 thread");
43 expect(
44 summarizeT3ToolCalls("thread-send", [
45 completed({ toolName: "t3_thread_send", args: { threadId: "input-thread" } }),
46 completed({ threadId: "input-thread" }),
47 ]).label,
48 ).toBe("Sent 2 messages to 1 thread");
49 });
50​
51 it("falls back to message counts when a destination is missing or a result is malformed", () => {
52 expect(
53 summarizeT3ToolCalls("thread-send", [
54 completed({ threadId: "known" }),
55 completed({ message: '{"threadId":"not-a-destination"}' }, "{truncated"),
56 completed(undefined, "Message sent"),
57 ]).label,
58 ).toBe("Sent 3 messages");
59 });
60​
61 it("counts batch-created threads, excludes rollbacks, and deduplicates returned thread IDs", () => {
62 const threads = Array.from({ length: 4 }, (_, i) => ({
63 threadId: `thread-${i}`,
64 status: "running",
65 }));
66 expect(
67 summarizeT3ToolCalls("thread-create", [
68 completed(
69 {},
70 { threads: [...threads, { threadId: "rolled-back", status: "rolled_back" }] },
71 ),
72 completed({}, { threadId: "thread-0", status: "running" }),
73 ]).label,
74 ).toBe("Created 4 threads");
75 expect(
76 summarizeT3ToolCalls("thread-create", [
77 completed({ threads: [{ title: "Requested, not confirmed" }] }),
78 ]).label,
79 ).toBe("Requested thread creation 1 time");
80 });
81​
82 it("excludes failed and unfinished sends even if the provider reports completed", () => {
83 const calls: T3ToolSummaryCall[] = [
84 completed({ threadId: "success" }, { messageId: "ok", threadId: "success" }),
85 completed(
86 { threadId: "failed" },
87 { isError: true, structuredContent: { threadId: "failed" } },
88 ),
89 completed({ threadId: "failed" }, [
90 { type: "text", text: JSON.stringify({ _tag: "OrchestratorMcpFailure" }) },
91 ]),
92 { input: { threadId: "cancelled" }, output: undefined, outcome: "unfinished" },
93 ];
94 expect(summarizeT3ToolCalls("thread-send", calls)).toEqual({
95 label: "Sent 1 message to 1 thread",
96 failedCount: 2,
97 });
98 expect(summarizeT3ToolCalls("thread-send", [calls[1]!])).toEqual({
99 label: "Tried to send 1 message to 1 thread",
100 failedCount: 1,
101 });
102 });
103​
104 it("does not confuse a child's failure or wait timeout with failure of the orchestration call", () => {
105 const failedChild = { taskId: "task-1", status: "failed", summary: "command not found" };
106 expect(
107 summarizeT3ToolCalls("delegate", [completed({}, failedChild), completed({}, failedChild)]),
108 ).toEqual({
109 label: "Delegated 1 task",
110 failedCount: 0,
111 });
112 expect(
113 summarizeT3ToolCalls(
114 "task-status",
115 Array.from({ length: 4 }, () => completed({ taskId: "task-1" }, failedChild)),
116 ).label,
117 ).toBe("Checked task status 4 times");
118 expect(
119 summarizeT3ToolCalls("thread-wait", [
120 completed({ threadId: "thread-1" }, { threadId: "thread-1", timedOut: true }),
121 ]),
122 ).toEqual({
123 label: "Waited on 1 thread",
124 failedCount: 0,
125 });
126 });
127​
128 it("describes control requests without claiming that a thread stopped or a task was deleted", () => {
129 expect(
130 summarizeT3ToolCalls("thread-interrupt", [
131 completed(
132 { threadId: "thread-1" },
133 { threadId: "thread-1", status: "interrupt_requested" },
134 ),
135 ]).label,
136 ).toBe("Requested interrupts for 1 thread");
137 expect(
138 summarizeT3ToolCalls("task-cancel", [
139 completed({ taskId: "task-1" }, { taskId: "task-1", status: "cancel_requested" }),
140 ]).label,
141 ).toBe("Requested cancellation of 1 task");
142 expect(
143 summarizeT3ToolCalls("schedule-delete", [
144 completed(
145 { scheduledTaskId: "schedule-1" },
146 { scheduledTaskId: "schedule-1", deleted: false },
147 ),
148 ]).label,
149 ).toBe("Requested deletion of 1 scheduled task");
150 });
151});