| 1 | import * as http from "http"; |
| 2 | |
| 3 | import { |
| 4 | cleanupTestContext, |
| 5 | createTestContext, |
| 6 | runCLI, |
| 7 | } from "../test-helpers/cli-helpers.js"; |
| 8 | import { |
| 9 | cleanupMockLLMServer, |
| 10 | setupMockLLMTest, |
| 11 | type MockLLMServer, |
| 12 | } from "../test-helpers/mock-llm-server.js"; |
| 13 | |
| 14 | describe("E2E: Headless Mode with Tool Calls", () => { |
| 15 | let context: any; |
| 16 | let mockServer: MockLLMServer; |
| 17 | |
| 18 | beforeEach(async () => { |
| 19 | context = await createTestContext(); |
| 20 | }); |
| 21 | |
| 22 | afterEach(async () => { |
| 23 | if (mockServer) { |
| 24 | await cleanupMockLLMServer(mockServer); |
| 25 | } |
| 26 | await cleanupTestContext(context); |
| 27 | }); |
| 28 | |
| 29 | it("should print nothing when response contains only tool calls in headless mode", async () => { |
| 30 | // In headless mode, the CLI makes only one request and exits |
| 31 | // If that response contains only tool calls, nothing is printed |
| 32 | |
| 33 | mockServer = await setupMockLLMTest(context, { |
| 34 | customHandler: (req: http.IncomingMessage, res: http.ServerResponse) => { |
| 35 | // Collect request body first |
| 36 | let body = ""; |
| 37 | req.on("data", (chunk) => { |
| 38 | body += chunk.toString(); |
| 39 | }); |
| 40 | |
| 41 | req.on("end", () => { |
| 42 | if (req.method === "POST" && req.url === "/chat/completions") { |
| 43 | res.writeHead(200, { |
| 44 | "Content-Type": "text/event-stream", |
| 45 | "Cache-Control": "no-cache", |
| 46 | Connection: "keep-alive", |
| 47 | }); |
| 48 | |
| 49 | // Send only tool call (no text content) |
| 50 | res.write( |
| 51 | `data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_123","type":"function","function":{"name":"search_code"}}]},"index":0}]}\n\n`, |
| 52 | ); |
| 53 | res.write( |
| 54 | `data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\\"pattern\\":\\"createTestContext\\"}"}}]},"index":0}]}\n\n`, |
| 55 | ); |
| 56 | |
| 57 | // Send usage data |
| 58 | res.write( |
| 59 | `data: {"usage":{"prompt_tokens":10,"completion_tokens":20}}\n\n`, |
| 60 | ); |
| 61 | |
| 62 | // End the stream |
| 63 | res.write(`data: [DONE]\n\n`); |
| 64 | res.end(); |
| 65 | } else { |
| 66 | res.writeHead(404); |
| 67 | res.end("Not found"); |
| 68 | } |
| 69 | }); |
| 70 | }, |
| 71 | }); |
| 72 | |
| 73 | // Run the CLI with print mode (-p) |
| 74 | const result = await runCLI(context, { |
| 75 | args: ["-p", "--config", context.configPath, "Test with tool calls"], |
| 76 | timeout: 15000, |
| 77 | }); |
| 78 | |
| 79 | // In headless mode with only tool calls, nothing should be printed |
| 80 | expect(result.stdout).toBe(""); |
| 81 | expect(result.exitCode).toBe(0); |
| 82 | }, 20000); |
| 83 | }); |