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