Files
t3code/packages/shared/src/toolOutput.test.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

176 lines
6.4 KiB
TypeScript

import { describe, expect, it } from "vite-plus/test";
import { compactDynamicToolOutput, toolOutputIndicatesFailure } from "./toolOutput.ts";
describe("compactDynamicToolOutput", () => {
it("extracts IDs through the MCP result envelopes used by T3 summaries", () => {
const metadata = { threadId: "thread-1", messageId: "message-1" };
const json = JSON.stringify({ ...metadata, response: "Private response body" });
for (const value of [
{ ...metadata, response: "Private response body" },
{ structuredContent: JSON.parse(json), content: "Ignored alternative body" },
[{ type: "text", text: json }],
{ content: [{ text: { text: json } }], isError: false },
json,
]) {
expect(compactDynamicToolOutput(value)).toEqual(metadata);
}
});
it("keeps nested thread identity and scalar task IDs without result bodies", () => {
const output = {
thread: { threadId: "thread-1", messages: ["Private message"] },
taskId: "task-1",
scheduledTaskId: "scheduled-1",
status: "failed",
summary: "A child failed; the tool itself succeeded.",
};
expect(compactDynamicToolOutput(output)).toEqual({
thread: { threadId: "thread-1" },
taskId: "task-1",
scheduledTaskId: "scheduled-1",
});
expect(output.thread.messages).toEqual(["Private message"]);
});
it("keeps explicit envelope failure while dropping error text and arbitrary output", () => {
for (const failure of [
{ isError: true },
{ is_error: true },
{ _tag: "OrchestratorMcpFailure" },
{ error: { message: "Private error" } },
]) {
expect(
compactDynamicToolOutput({
...failure,
structuredContent: { threadId: "thread-1", error: "Private details" },
}),
).toEqual({ isError: true, threadId: "thread-1" });
}
expect(compactDynamicToolOutput("Private plain text")).toBeUndefined();
expect(compactDynamicToolOutput({ result: { body: "Private result" } })).toBeUndefined();
});
it("uses the first result data but combines explicit failure flags across content blocks", () => {
expect(
compactDynamicToolOutput([
{ text: JSON.stringify({ threadId: "first" }) },
{ text: JSON.stringify({ threadId: "second", isError: true, error: "Private error" }) },
]),
).toEqual({ threadId: "first", isError: true });
expect(
compactDynamicToolOutput([{ text: "{}" }, { text: JSON.stringify({ threadId: "second" }) }]),
).toBeUndefined();
});
it("keeps complete thread creation batches and rollback markers without row bodies", () => {
expect(
compactDynamicToolOutput({
threads: [
{ threadId: "created", status: "running", messages: ["Private message"] },
{ threadId: "reverted", status: "rolled_back", error: "Private error" },
{ status: "rolled_back" },
],
}),
).toEqual({
threads: [
{ threadId: "created" },
{ threadId: "reverted", status: "rolled_back" },
{ status: "rolled_back" },
],
});
expect(compactDynamicToolOutput({ threadId: "reverted", status: "rolled_back" })).toEqual({
threadId: "reverted",
status: "rolled_back",
});
expect(compactDynamicToolOutput({ threads: [] })).toEqual({ threads: [] });
});
it("omits incomplete or oversized creation evidence instead of returning a partial count", () => {
for (const threads of [
[{ threadId: "known" }, { title: "No confirmed ID" }],
Array.from({ length: 101 }, (_, index) => ({ threadId: `thread-${index}` })),
Array.from({ length: 100 }, (_, index) => ({ threadId: `${index}`.padEnd(256, "x") })),
[{ threadId: "x".repeat(257) }],
]) {
expect(
compactDynamicToolOutput({
threadId: "must-not-be-counted-as-one",
status: "rolled_back",
taskId: "task-1",
threads,
}),
).toEqual({ taskId: "task-1" });
}
const normalBatch = {
threads: Array.from({ length: 100 }, (_, index) => ({
threadId: `00000000-0000-4000-8000-${String(index).padStart(12, "0")}`,
})),
};
expect(compactDynamicToolOutput(normalBatch)).toEqual(normalBatch);
expect(
new TextEncoder().encode(JSON.stringify(compactDynamicToolOutput(normalBatch))).byteLength,
).toBeLessThanOrEqual(8_192);
});
it("bounds parsing and envelope scanning while preserving an outer failure marker", () => {
const oversizedJson = JSON.stringify({ threadId: "hidden", body: "😄".repeat(5_000) });
for (const content of [
"x".repeat(1_000_000),
oversizedJson,
Array.from({ length: 33 }, () => ({ text: JSON.stringify({ threadId: "hidden" }) })),
{ content: { content: { content: { content: { threadId: "too-deep" } } } } },
]) {
expect(compactDynamicToolOutput({ isError: true, content })).toEqual({ isError: true });
}
expect(compactDynamicToolOutput({ threadId: "known", body: "x".repeat(1_000_000) })).toEqual({
threadId: "known",
});
});
it("is idempotent for compact metadata", () => {
const compact = compactDynamicToolOutput({
isError: true,
structuredContent: {
threadId: "thread-1",
threads: [{ threadId: "thread-1" }, { status: "rolled_back" }],
output: "Private output",
},
});
expect(compactDynamicToolOutput(compact)).toEqual(compact);
});
});
describe("toolOutputIndicatesFailure", () => {
it("preserves the command failure phrases across shells without copying the output", () => {
for (const text of [
"FILE NOT FOUND",
"No files found",
"ENOENT",
"No such file or directory",
"CommandNotFoundException",
"command not found",
"Cannot find path 'a' because it does not exist",
"The term 'example' is not recognized",
"is not recognized as the name of a cmdlet",
"A parameter cannot be found that matches parameter name",
"<exited with exit code 2>",
"Exited with exit code 1",
"exit code: 127",
])
expect(toolOutputIndicatesFailure(text)).toBe(true);
});
it("does not mark successful exit codes or incomplete failure phrases", () => {
for (const text of [
"Done",
"exit code: 0",
"Exited with exit code 0",
"cannot find path",
"is not recognized",
]) {
expect(toolOutputIndicatesFailure(text)).toBe(false);
}
});
});