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

toolOutput.test.ts

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