The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { summarizeT3ToolCalls, type T3ToolSummaryCall } from "./t3ToolSummary.ts"; |
| 4 | |
| 5 | function completed(input: unknown, output?: unknown): T3ToolSummaryCall { |
| 6 | return { input, output, outcome: "completed" }; |
| 7 | } |
| 8 | |
| 9 | describe("summarizeT3ToolCalls", () => { |
| 10 | it("reads provider result envelopes without treating JSON in the message as result data", () => { |
| 11 | const result = { messageId: "message-1", threadId: "actual-thread" }; |
| 12 | const json = JSON.stringify(result); |
| 13 | const outputs = [ |
| 14 | result, |
| 15 | { structuredContent: result }, |
| 16 | [{ type: "text", text: json }], |
| 17 | { content: [{ text: { text: json } }], isError: false }, |
| 18 | json, |
| 19 | ]; |
| 20 | const calls = outputs.map((output) => |
| 21 | completed( |
| 22 | { |
| 23 | toolName: "t3_thread_send", |
| 24 | args: { threadId: "input-thread", message: '{"threadId":"fake"}' }, |
| 25 | }, |
| 26 | output, |
| 27 | ), |
| 28 | ); |
| 29 | expect(summarizeT3ToolCalls("thread-send", calls).label).toBe("Sent 1 message to 1 thread"); |
| 30 | expect( |
| 31 | summarizeT3ToolCalls("thread-send", [ |
| 32 | completed({ toolName: "t3_thread_send", args: { threadId: "input-thread" } }), |
| 33 | completed({ threadId: "input-thread" }), |
| 34 | ]).label, |
| 35 | ).toBe("Sent 2 messages to 1 thread"); |
| 36 | }); |
| 37 | |
| 38 | it("counts the pull requests batch link and unlink calls changed", () => { |
| 39 | const urls = ["https://github.com/o/r/pull/1", "https://github.com/o/r/pull/1", "not-a-pr"]; |
| 40 | const links = completed({ urls }, { linkedCount: 1 }); |
| 41 | expect(summarizeT3ToolCalls("link-pr", [links, completed({ number: 3 })]).label).toBe( |
| 42 | "Linked 2 pull requests", |
| 43 | ); |
| 44 | expect(summarizeT3ToolCalls("link-pr", [completed({ urls }, {})]).label).toBe( |
| 45 | "Linked pull requests", |
| 46 | ); |
| 47 | const batch = completed({ states: ["merged"] }, { unlinkedCount: 2 }); |
| 48 | expect(summarizeT3ToolCalls("unlink-pr", [batch, completed({ number: 3 })]).label).toBe( |
| 49 | "Unlinked 3 pull requests", |
| 50 | ); |
| 51 | const uncounted = completed({ states: ["merged"] }, { isError: false }); |
| 52 | expect(summarizeT3ToolCalls("unlink-pr", [uncounted]).label).toBe("Unlinked pull requests"); |
| 53 | }); |
| 54 | |
| 55 | it("tells opened and merged pull requests from found ones and auto-merges", () => { |
| 56 | const create = (created: boolean) => completed({ title: "t" }, { number: 1, created }); |
| 57 | expect(summarizeT3ToolCalls("create-pr", [create(true)]).label).toBe("Opened 1 pull request"); |
| 58 | expect(summarizeT3ToolCalls("create-pr", [create(false)]).label).toBe( |
| 59 | "Found 1 open pull request", |
| 60 | ); |
| 61 | expect(summarizeT3ToolCalls("create-pr", [create(true), create(false)]).label).toBe( |
| 62 | "Opened 1 pull request, found 1 open", |
| 63 | ); |
| 64 | const merge = (auto: boolean, state: string) => completed({ auto }, { number: 1, state }); |
| 65 | expect(summarizeT3ToolCalls("merge-pr", [merge(false, "merged")]).label).toBe( |
| 66 | "Merged 1 pull request", |
| 67 | ); |
| 68 | expect(summarizeT3ToolCalls("merge-pr", [merge(false, "open")]).label).toBe( |
| 69 | "Queued 1 pull request to merge", |
| 70 | ); |
| 71 | expect(summarizeT3ToolCalls("merge-pr", [merge(true, "open")]).label).toBe( |
| 72 | "Queued 1 pull request to merge", |
| 73 | ); |
| 74 | expect( |
| 75 | summarizeT3ToolCalls("merge-pr", [merge(true, "merged"), merge(true, "open")]).label, |
| 76 | ).toBe("Merged 1 pull request, queued 1"); |
| 77 | expect(summarizeT3ToolCalls("review-pr", [completed({ verdict: "approve" }, {})]).label).toBe( |
| 78 | "Reviewed 1 pull request", |
| 79 | ); |
| 80 | }); |
| 81 | |
| 82 | it("counts the checks a CI rerun restarted, and the ones the host refused", () => { |
| 83 | const rerun = (...outcomes: boolean[]) => |
| 84 | completed( |
| 85 | {}, |
| 86 | { checks: outcomes.map((outcome, index) => ({ name: `c${index}`, rerun: outcome })) }, |
| 87 | ); |
| 88 | expect(summarizeT3ToolCalls("ci-rerun", [rerun(true, true)])).toEqual({ |
| 89 | label: "Re-ran 2 checks", |
| 90 | failedCount: 0, |
| 91 | }); |
| 92 | expect(summarizeT3ToolCalls("ci-rerun", [rerun(true, false)])).toEqual({ |
| 93 | label: "Re-ran 1 check, 1 refused", |
| 94 | failedCount: 1, |
| 95 | }); |
| 96 | expect(summarizeT3ToolCalls("ci-rerun", [rerun(false)])).toEqual({ |
| 97 | label: "Could not re-run 1 check", |
| 98 | failedCount: 1, |
| 99 | }); |
| 100 | expect(summarizeT3ToolCalls("ci-rerun", [rerun()]).label).toBe( |
| 101 | "Found no failed checks to re-run", |
| 102 | ); |
| 103 | }); |
| 104 | |
| 105 | it("excludes failed and unfinished sends even if the provider reports completed", () => { |
| 106 | const calls: T3ToolSummaryCall[] = [ |
| 107 | completed({ threadId: "success" }, { messageId: "ok", threadId: "success" }), |
| 108 | completed( |
| 109 | { threadId: "failed" }, |
| 110 | { isError: true, structuredContent: { threadId: "failed" } }, |
| 111 | ), |
| 112 | completed({ threadId: "failed" }, [ |
| 113 | { type: "text", text: JSON.stringify({ _tag: "OrchestratorMcpFailure" }) }, |
| 114 | ]), |
| 115 | { input: { threadId: "cancelled" }, output: undefined, outcome: "unfinished" }, |
| 116 | ]; |
| 117 | expect(summarizeT3ToolCalls("thread-send", calls)).toEqual({ |
| 118 | label: "Sent 1 message to 1 thread", |
| 119 | failedCount: 2, |
| 120 | }); |
| 121 | expect(summarizeT3ToolCalls("thread-send", [calls[1]!])).toEqual({ |
| 122 | label: "Tried to send 1 message to 1 thread", |
| 123 | failedCount: 1, |
| 124 | }); |
| 125 | }); |
| 126 | |
| 127 | it("does not confuse a child's failure or wait timeout with failure of the orchestration call", () => { |
| 128 | const failedChild = { taskId: "task-1", status: "failed", summary: "command not found" }; |
| 129 | expect( |
| 130 | summarizeT3ToolCalls("delegate", [completed({}, failedChild), completed({}, failedChild)]), |
| 131 | ).toEqual({ |
| 132 | label: "Delegated 1 task", |
| 133 | failedCount: 0, |
| 134 | }); |
| 135 | expect( |
| 136 | summarizeT3ToolCalls( |
| 137 | "task-status", |
| 138 | Array.from({ length: 4 }, () => completed({ taskId: "task-1" }, failedChild)), |
| 139 | ).label, |
| 140 | ).toBe("Checked task status 4 times"); |
| 141 | expect( |
| 142 | summarizeT3ToolCalls("thread-wait", [ |
| 143 | completed({ threadId: "thread-1" }, { threadId: "thread-1", timedOut: true }), |
| 144 | ]), |
| 145 | ).toEqual({ |
| 146 | label: "Waited on 1 thread", |
| 147 | failedCount: 0, |
| 148 | }); |
| 149 | }); |
| 150 | }); |