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

toolOutput.test.ts

206 lines7.4 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 compactDynamicToolOutput,
5 MAX_TOOL_OUTPUT_IMAGES,
6 toolOutputImages,
7 toolOutputIndicatesFailure,
8} from "./toolOutput.ts";
9​
10describe("compactDynamicToolOutput", () => {
11 it("extracts IDs through the MCP result envelopes used by T3 summaries", () => {
12 const metadata = { threadId: "thread-1", messageId: "message-1" };
13 const json = JSON.stringify({ ...metadata, response: "Private response body" });
14 for (const value of [
15 { ...metadata, response: "Private response body" },
16 { structuredContent: JSON.parse(json), content: "Ignored alternative body" },
17 [{ type: "text", text: json }],
18 { content: [{ text: { text: json } }], isError: false },
19 json,
20 ]) {
21 expect(compactDynamicToolOutput(value)).toEqual(metadata);
22 }
23 });
24​
25 it("keeps nested thread identity and scalar task IDs without result bodies", () => {
26 const output = {
27 thread: { threadId: "thread-1", messages: ["Private message"] },
28 taskId: "task-1",
29 scheduledTaskId: "scheduled-1",
30 status: "failed",
31 summary: "A child failed; the tool itself succeeded.",
32 unlinked: [{ repository: "acme/app", state: "merged", numbers: [1] }],
33 unlinkedCount: 1,
34 linkedCount: 2,
35 };
36 expect(compactDynamicToolOutput(output)).toEqual({
37 thread: { threadId: "thread-1" },
38 taskId: "task-1",
39 scheduledTaskId: "scheduled-1",
40 linkedCount: 2,
41 unlinkedCount: 1,
42 });
43 expect(output.thread.messages).toEqual(["Private message"]);
44 });
45​
46 it("keeps explicit envelope failure while dropping error text and arbitrary output", () => {
47 for (const failure of [
48 { isError: true },
49 { is_error: true },
50 { _tag: "OrchestratorMcpFailure" },
51 { error: { message: "Private error" } },
52 ]) {
53 expect(
54 compactDynamicToolOutput({
55 ...failure,
56 structuredContent: { threadId: "thread-1", error: "Private details" },
57 }),
58 ).toEqual({ isError: true, threadId: "thread-1" });
59 }
60 expect(compactDynamicToolOutput("Private plain text")).toBeUndefined();
61 expect(compactDynamicToolOutput({ result: { body: "Private result" } })).toBeUndefined();
62 });
63​
64 it("uses the first result data but combines explicit failure flags across content blocks", () => {
65 expect(
66 compactDynamicToolOutput([
67 { text: JSON.stringify({ threadId: "first" }) },
68 { text: JSON.stringify({ threadId: "second", isError: true, error: "Private error" }) },
69 ]),
70 ).toEqual({ threadId: "first", isError: true });
71 expect(
72 compactDynamicToolOutput([{ text: "{}" }, { text: JSON.stringify({ threadId: "second" }) }]),
73 ).toBeUndefined();
74 });
75​
76 it("keeps complete thread creation batches and rollback markers without row bodies", () => {
77 expect(
78 compactDynamicToolOutput({
79 threads: [
80 { threadId: "created", status: "running", messages: ["Private message"] },
81 { threadId: "reverted", status: "rolled_back", error: "Private error" },
82 { status: "rolled_back" },
83 ],
84 }),
85 ).toEqual({
86 threads: [
87 { threadId: "created" },
88 { threadId: "reverted", status: "rolled_back" },
89 { status: "rolled_back" },
90 ],
91 });
92 expect(compactDynamicToolOutput({ threadId: "reverted", status: "rolled_back" })).toEqual({
93 threadId: "reverted",
94 status: "rolled_back",
95 });
96 expect(compactDynamicToolOutput({ threads: [] })).toEqual({ threads: [] });
97 });
98​
99 it("omits incomplete or oversized creation evidence instead of returning a partial count", () => {
100 for (const threads of [
101 [{ threadId: "known" }, { title: "No confirmed ID" }],
102 Array.from({ length: 101 }, (_, index) => ({ threadId: `thread-${index}` })),
103 Array.from({ length: 100 }, (_, index) => ({ threadId: `${index}`.padEnd(256, "x") })),
104 [{ threadId: "x".repeat(257) }],
105 ]) {
106 expect(
107 compactDynamicToolOutput({
108 threadId: "must-not-be-counted-as-one",
109 status: "rolled_back",
110 taskId: "task-1",
111 threads,
112 }),
113 ).toEqual({ taskId: "task-1" });
114 }
115 const normalBatch = {
116 threads: Array.from({ length: 100 }, (_, index) => ({
117 threadId: `00000000-0000-4000-8000-${String(index).padStart(12, "0")}`,
118 })),
119 };
120 expect(compactDynamicToolOutput(normalBatch)).toEqual(normalBatch);
121 expect(
122 new TextEncoder().encode(JSON.stringify(compactDynamicToolOutput(normalBatch))).byteLength,
123 ).toBeLessThanOrEqual(8_192);
124 });
125​
126 it("bounds parsing and envelope scanning while preserving an outer failure marker", () => {
127 const oversizedJson = JSON.stringify({ threadId: "hidden", body: "😄".repeat(5_000) });
128 for (const content of [
129 "x".repeat(1_000_000),
130 oversizedJson,
131 Array.from({ length: 33 }, () => ({ text: JSON.stringify({ threadId: "hidden" }) })),
132 { content: { content: { content: { content: { threadId: "too-deep" } } } } },
133 ]) {
134 expect(compactDynamicToolOutput({ isError: true, content })).toEqual({ isError: true });
135 }
136 expect(compactDynamicToolOutput({ threadId: "known", body: "x".repeat(1_000_000) })).toEqual({
137 threadId: "known",
138 });
139 });
140});
141​
142describe("toolOutputIndicatesFailure", () => {
143 it("preserves the command failure phrases across shells without copying the output", () => {
144 for (const text of [
145 "FILE NOT FOUND",
146 "No files found",
147 "ENOENT",
148 "No such file or directory",
149 "CommandNotFoundException",
150 "command not found",
151 "Cannot find path 'a' because it does not exist",
152 "The term 'example' is not recognized",
153 "is not recognized as the name of a cmdlet",
154 "A parameter cannot be found that matches parameter name",
155 "<exited with exit code 2>",
156 "Exited with exit code 1",
157 "exit code: 127",
158 ])
159 expect(toolOutputIndicatesFailure(text)).toBe(true);
160 });
161​
162 it("does not mark successful exit codes or incomplete failure phrases", () => {
163 for (const text of [
164 "Done",
165 "exit code: 0",
166 "Exited with exit code 0",
167 "cannot find path",
168 "is not recognized",
169 ]) {
170 expect(toolOutputIndicatesFailure(text)).toBe(false);
171 }
172 });
173});
174​
175describe("toolOutputImages", () => {
176 const mcpImage = { type: "image", data: "AAAA", mimeType: "image/png" };
177 const claudeImage = {
178 type: "image",
179 source: { type: "base64", media_type: "image/JPEG", data: "BBBB" },
180 };
181 const text = { type: "text", text: "{}" };
182​
183 it("finds images in each place providers store them, in order", () => {
184 expect(toolOutputImages({ content: [text, claudeImage, mcpImage] })).toEqual([
185 { mimeType: "image/jpeg", data: "BBBB" },
186 { mimeType: "image/png", data: "AAAA" },
187 ]);
188 expect(toolOutputImages([mcpImage])).toEqual([{ mimeType: "image/png", data: "AAAA" }]);
189 expect(toolOutputImages(mcpImage)).toEqual([{ mimeType: "image/png", data: "AAAA" }]);
190 });
191​
192 it("skips types the server must not serve inline", () => {
193 expect(
194 toolOutputImages([
195 { ...mcpImage, mimeType: "image/svg+xml" },
196 { ...mcpImage, mimeType: "text/html" },
197 { type: "image", source: { type: "url", url: "https://example.com/a.png" } },
198 ]),
199 ).toEqual([]);
200 });
201​
202 it("stops after the first few images", () => {
203 const images = toolOutputImages(Array.from({ length: 10_000 }, () => mcpImage));
204 expect(images).toHaveLength(MAX_TOOL_OUTPUT_IMAGES);
205 });
206});