|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { BaseCliAgent } from "../src/agents/BaseCliAgent"; |
| 3 | + |
| 4 | +type StdoutHandling = { |
| 5 | + stdoutBannerPatterns?: RegExp[]; |
| 6 | + stdoutErrorPatterns?: RegExp[]; |
| 7 | + errorOnBannerOnly?: boolean; |
| 8 | +}; |
| 9 | + |
| 10 | +/** |
| 11 | + * Test agent that writes a fixed string to stdout and exits 0. |
| 12 | + * Optional stdout handling patterns emulate agent-specific parsing behavior. |
| 13 | + */ |
| 14 | +class StdoutAgent extends BaseCliAgent { |
| 15 | + constructor( |
| 16 | + private readonly stdoutText: string, |
| 17 | + private readonly handling: StdoutHandling = {}, |
| 18 | + ) { |
| 19 | + super({ id: "stdout-test-agent" }); |
| 20 | + } |
| 21 | + |
| 22 | + protected async buildCommand(_params: { |
| 23 | + prompt: string; |
| 24 | + systemPrompt?: string; |
| 25 | + cwd: string; |
| 26 | + options: any; |
| 27 | + }) { |
| 28 | + return { |
| 29 | + command: "printf", |
| 30 | + args: ["%s", this.stdoutText], |
| 31 | + ...this.handling, |
| 32 | + }; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +describe("BaseCliAgent stdout handling defaults", () => { |
| 37 | + test("does not treat generic 'Error:' text as CLI failure by default", async () => { |
| 38 | + const agent = new StdoutAgent("Error: this is model-authored text"); |
| 39 | + const result = await agent.generate({ prompt: "test" }); |
| 40 | + expect(result.text).toBe("Error: this is model-authored text"); |
| 41 | + }); |
| 42 | + |
| 43 | + test("does not strip YOLO banner by default", async () => { |
| 44 | + const agent = new StdoutAgent( |
| 45 | + "YOLO mode is enabled. All tool calls will be automatically approved.", |
| 46 | + ); |
| 47 | + const result = await agent.generate({ prompt: "test" }); |
| 48 | + expect(result.text).toContain("YOLO mode is enabled"); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe("BaseCliAgent stdout handling (opt-in)", () => { |
| 53 | + const kimiErrorPatterns = [ |
| 54 | + /^LLM not set/i, |
| 55 | + /^LLM not supported/i, |
| 56 | + /^Max steps reached/i, |
| 57 | + /^Interrupted by user$/i, |
| 58 | + /^Unknown error:/i, |
| 59 | + /^Error:/i, |
| 60 | + ]; |
| 61 | + |
| 62 | + const errorCases = [ |
| 63 | + "LLM not set", |
| 64 | + "LLM not supported", |
| 65 | + "Max steps reached: 50", |
| 66 | + "Interrupted by user", |
| 67 | + "Unknown error: connection refused", |
| 68 | + "Error: something went wrong", |
| 69 | + "error: lowercase variant", |
| 70 | + ]; |
| 71 | + |
| 72 | + for (const errorText of errorCases) { |
| 73 | + test(`throws for stdout error pattern: "${errorText}"`, async () => { |
| 74 | + const agent = new StdoutAgent(errorText, { |
| 75 | + stdoutErrorPatterns: kimiErrorPatterns, |
| 76 | + }); |
| 77 | + await expect(agent.generate({ prompt: "test" })).rejects.toThrow( |
| 78 | + "CLI agent error (stdout):", |
| 79 | + ); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + test("does not throw for valid JSON output", async () => { |
| 84 | + const agent = new StdoutAgent('{"result": "ok"}', { |
| 85 | + stdoutErrorPatterns: kimiErrorPatterns, |
| 86 | + }); |
| 87 | + const result = await agent.generate({ prompt: "test" }); |
| 88 | + expect(result.text).toContain("ok"); |
| 89 | + }); |
| 90 | + |
| 91 | + test("does not throw for JSON array output", async () => { |
| 92 | + const agent = new StdoutAgent('[{"id": 1}]', { |
| 93 | + stdoutErrorPatterns: kimiErrorPatterns, |
| 94 | + }); |
| 95 | + const result = await agent.generate({ prompt: "test" }); |
| 96 | + expect(result.text).toContain("id"); |
| 97 | + }); |
| 98 | + |
| 99 | + test("does not throw for normal text output", async () => { |
| 100 | + const agent = new StdoutAgent("Hello, this is a normal response", { |
| 101 | + stdoutErrorPatterns: kimiErrorPatterns, |
| 102 | + }); |
| 103 | + const result = await agent.generate({ prompt: "test" }); |
| 104 | + expect(result.text).toBe("Hello, this is a normal response"); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe("BaseCliAgent banner handling (opt-in)", () => { |
| 109 | + const yoloBanner = /^YOLO mode is enabled\b[^\n]*/gm; |
| 110 | + |
| 111 | + test("throws when stdout is only the banner", async () => { |
| 112 | + const agent = new StdoutAgent( |
| 113 | + "YOLO mode is enabled. All tool calls will be automatically approved.", |
| 114 | + { |
| 115 | + stdoutBannerPatterns: [yoloBanner], |
| 116 | + errorOnBannerOnly: true, |
| 117 | + }, |
| 118 | + ); |
| 119 | + await expect(agent.generate({ prompt: "test" })).rejects.toThrow( |
| 120 | + "CLI agent error (stdout):", |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + test("strips banner and returns remaining JSON", async () => { |
| 125 | + const content = [ |
| 126 | + "YOLO mode is enabled. All tool calls will be automatically approved.", |
| 127 | + '{"result": "actual model output"}', |
| 128 | + ].join("\n"); |
| 129 | + const agent = new StdoutAgent(content, { |
| 130 | + stdoutBannerPatterns: [yoloBanner], |
| 131 | + errorOnBannerOnly: true, |
| 132 | + }); |
| 133 | + const result = await agent.generate({ prompt: "test" }); |
| 134 | + expect(result.text).toContain("actual model output"); |
| 135 | + }); |
| 136 | + |
| 137 | + test("strips banner when followed by plain text", async () => { |
| 138 | + const content = [ |
| 139 | + "YOLO mode is enabled. All tool calls will be automatically approved.", |
| 140 | + "Here is the actual response from the model.", |
| 141 | + ].join("\n"); |
| 142 | + const agent = new StdoutAgent(content, { |
| 143 | + stdoutBannerPatterns: [yoloBanner], |
| 144 | + errorOnBannerOnly: true, |
| 145 | + }); |
| 146 | + const result = await agent.generate({ prompt: "test" }); |
| 147 | + expect(result.text).toBe("Here is the actual response from the model."); |
| 148 | + }); |
| 149 | +}); |
0 commit comments