Open History to see the last commit
1import { compactT3McpToolOutput } from "@t3tools/shared/toolOutput";
2import { describe, expect, it } from "vite-plus/test";
3
4import { summarizeT3ToolCalls, type T3ToolSummaryCall } from "./t3ToolSummary.ts";
5
6function completed(input: unknown, output?: unknown): T3ToolSummaryCall {
7 return { input, output, outcome: "completed" };
8}
9
10describe("summarizeT3ToolCalls", () => {
11 it("reads provider result envelopes without treating JSON in the message as result data", () => {
12 const result = { messageId: "message-1", threadId: "actual-thread" };
13 const json = JSON.stringify(result);
14 const outputs = [
15 result,
16 { structuredContent: result },
17 [{ type: "text", text: json }],
18 { content: [{ text: { text: json } }], isError: false },
19 json,
20 ];
21 const calls = outputs.map((output) =>
22 completed(
23 {
24 toolName: "t3_thread_send",
25 args: { threadId: "input-thread", message: '{"threadId":"fake"}' },
26 },
27 output,
28 ),
29 );
30 expect(summarizeT3ToolCalls("thread-send", calls).label).toBe("Sent 1 message to 1 thread");
31 expect(
32 summarizeT3ToolCalls("thread-send", [
33 completed({ toolName: "t3_thread_send", args: { threadId: "input-thread" } }),
34 completed({ threadId: "input-thread" }),
35 ]).label,
36 ).toBe("Sent 2 messages to 1 thread");
37 });
38
39 it("counts the pull requests batch link and unlink calls changed", () => {
40 const urls = ["https://github.com/o/r/pull/1", "https://github.com/o/r/pull/1", "not-a-pr"];
41 const links = completed({ urls }, { linkedCount: 1 });
42 expect(summarizeT3ToolCalls("link-pr", [links, completed({ number: 3 })]).label).toBe(
43 "Linked 2 pull requests",
44 );
45 expect(summarizeT3ToolCalls("link-pr", [completed({ urls }, {})]).label).toBe(
46 "Linked pull requests",
47 );
48 const batch = completed({ states: ["merged"] }, { unlinkedCount: 2 });
49 expect(summarizeT3ToolCalls("unlink-pr", [batch, completed({ number: 3 })]).label).toBe(
50 "Unlinked 3 pull requests",
51 );
52 const uncounted = completed({ states: ["merged"] }, { isError: false });
53 expect(summarizeT3ToolCalls("unlink-pr", [uncounted]).label).toBe("Unlinked pull requests");
54 });
55
56 it("tells opened and merged pull requests from found ones and auto-merges", () => {
57 const create = (created: boolean) => completed({ title: "t" }, { number: 1, created });
58 expect(summarizeT3ToolCalls("create-pr", [create(true)]).label).toBe("Opened 1 pull request");
59 expect(summarizeT3ToolCalls("create-pr", [create(false)]).label).toBe(
60 "Found 1 open pull request",
61 );
62 expect(summarizeT3ToolCalls("create-pr", [create(true), create(false)]).label).toBe(
63 "Opened 1 pull request, found 1 open",
64 );
65 const merge = (auto: boolean, state: string) => completed({ auto }, { number: 1, state });
66 expect(summarizeT3ToolCalls("merge-pr", [merge(false, "merged")]).label).toBe(
67 "Merged 1 pull request",
68 );
69 expect(summarizeT3ToolCalls("merge-pr", [merge(false, "open")]).label).toBe(
70 "Queued 1 pull request to merge",
71 );
72 expect(summarizeT3ToolCalls("merge-pr", [merge(true, "open")]).label).toBe(
73 "Queued 1 pull request to merge",
74 );
75 expect(
76 summarizeT3ToolCalls("merge-pr", [merge(true, "merged"), merge(true, "open")]).label,
77 ).toBe("Merged 1 pull request, queued 1");
78 expect(summarizeT3ToolCalls("review-pr", [completed({ verdict: "approve" }, {})]).label).toBe(
79 "Reviewed 1 pull request",
80 );
81 });
82
83 it("counts the checks a CI rerun restarted, and the ones the host refused", () => {
84 const rerun = (...outcomes: boolean[]) =>
85 completed(
86 {},
87 { checks: outcomes.map((outcome, index) => ({ name: `c${index}`, rerun: outcome })) },
88 );
89 expect(summarizeT3ToolCalls("ci-rerun", [rerun(true, true)])).toEqual({
90 label: "Re-ran 2 checks",
91 failedCount: 0,
92 });
93 expect(summarizeT3ToolCalls("ci-rerun", [rerun(true, false)])).toEqual({
94 label: "Re-ran 1 check, 1 refused",
95 failedCount: 1,
96 });
97 expect(summarizeT3ToolCalls("ci-rerun", [rerun(false)])).toEqual({
98 label: "Could not re-run 1 check",
99 failedCount: 1,
100 });
101 expect(summarizeT3ToolCalls("ci-rerun", [rerun()]).label).toBe(
102 "Found no failed checks to re-run",
103 );
104 });
105
106 it("preserves pull request summary outcomes after MCP tool output compaction", () => {
107 const createExisting = completed(
108 { title: "t" },
109 compactT3McpToolOutput("t3-code.pull_request_create", {
110 number: 1,
111 repository: "o/r",
112 url: "https://github.com/o/r/pull/1",
113 created: false,
114 }),
115 );
116 expect(summarizeT3ToolCalls("create-pr", [createExisting]).label).toBe(
117 "Found 1 open pull request",
118 );
119
120 const mergeQueued = completed(
121 { auto: false },
122 compactT3McpToolOutput("t3-code.pull_request_merge", { number: 1, state: "open" }),
123 );
124 const mergeLanded = completed(
125 { auto: true },
126 compactT3McpToolOutput("t3-code.pull_request_merge", { number: 2, state: "merged" }),
127 );
128 expect(summarizeT3ToolCalls("merge-pr", [mergeQueued]).label).toBe(
129 "Queued 1 pull request to merge",
130 );
131 expect(summarizeT3ToolCalls("merge-pr", [mergeLanded]).label).toBe("Merged 1 pull request");
132
133 const guidesMixed = completed(
134 {},
135 compactT3McpToolOutput("t3-code.write_pull_request_guides", {
136 guides: [
137 { number: 1, outcome: "started", detail: null },
138 { number: 2, outcome: "failed", detail: "Guides disabled" },
139 ],
140 }),
141 );
142 expect(summarizeT3ToolCalls("write-pr-guides", [guidesMixed])).toEqual({
143 label: "Started 1 review guide, 1 failed",
144 failedCount: 1,
145 });
146
147 const rerunMixed = completed(
148 {},
149 compactT3McpToolOutput("t3-code.ci_rerun", {
150 checks: [
151 { name: "test", rerun: true, detail: null },
152 { name: "external", rerun: false, detail: "Cannot rerun" },
153 ],
154 }),
155 );
156 const rerunEmpty = completed({}, compactT3McpToolOutput("t3-code.ci_rerun", { checks: [] }));
157 expect(summarizeT3ToolCalls("ci-rerun", [rerunMixed])).toEqual({
158 label: "Re-ran 1 check, 1 refused",
159 failedCount: 1,
160 });
161 expect(summarizeT3ToolCalls("ci-rerun", [rerunEmpty]).label).toBe(
162 "Found no failed checks to re-run",
163 );
164 });
165
166 it("excludes failed and unfinished sends even if the provider reports completed", () => {
167 const calls: T3ToolSummaryCall[] = [
168 completed({ threadId: "success" }, { messageId: "ok", threadId: "success" }),
169 completed(
170 { threadId: "failed" },
171 { isError: true, structuredContent: { threadId: "failed" } },
172 ),
173 completed({ threadId: "failed" }, [
174 { type: "text", text: JSON.stringify({ _tag: "OrchestratorMcpFailure" }) },
175 ]),
176 { input: { threadId: "cancelled" }, output: undefined, outcome: "unfinished" },
177 ];
178 expect(summarizeT3ToolCalls("thread-send", calls)).toEqual({
179 label: "Sent 1 message to 1 thread",
180 failedCount: 2,
181 });
182 expect(summarizeT3ToolCalls("thread-send", [calls[1]!])).toEqual({
183 label: "Tried to send 1 message to 1 thread",
184 failedCount: 1,
185 });
186 });
187
188 it("does not confuse a child's failure or wait timeout with failure of the orchestration call", () => {
189 const failedChild = { taskId: "task-1", status: "failed", summary: "command not found" };
190 expect(
191 summarizeT3ToolCalls("delegate", [completed({}, failedChild), completed({}, failedChild)]),
192 ).toEqual({
193 label: "Delegated 1 task",
194 failedCount: 0,
195 });
196 expect(
197 summarizeT3ToolCalls(
198 "task-status",
199 Array.from({ length: 4 }, () => completed({ taskId: "task-1" }, failedChild)),
200 ).label,
201 ).toBe("Checked task status 4 times");
202 expect(
203 summarizeT3ToolCalls("thread-wait", [
204 completed({ threadId: "thread-1" }, { threadId: "thread-1", timedOut: true }),
205 ]),
206 ).toEqual({
207 label: "Waited on 1 thread",
208 failedCount: 0,
209 });
210 });
211});