| 1 | import { |
| 2 | cleanupTestContext, |
| 3 | createTestContext, |
| 4 | runCLI, |
| 5 | } from "../test-helpers/cli-helpers.js"; |
| 6 | import { |
| 7 | setupMockLLMTest, |
| 8 | cleanupMockLLMServer, |
| 9 | type MockLLMServer, |
| 10 | } from "../test-helpers/mock-llm-server.js"; |
| 11 | |
| 12 | describe("E2E: Headless Mode with JSON Format", () => { |
| 13 | let context: any; |
| 14 | let mockServer: MockLLMServer; |
| 15 | |
| 16 | beforeEach(async () => { |
| 17 | context = await createTestContext(); |
| 18 | }); |
| 19 | |
| 20 | afterEach(async () => { |
| 21 | if (mockServer) { |
| 22 | await cleanupMockLLMServer(mockServer); |
| 23 | } |
| 24 | await cleanupTestContext(context); |
| 25 | }); |
| 26 | |
| 27 | it("should output valid JSON when LLM returns valid JSON", async () => { |
| 28 | const validJsonResponse = { |
| 29 | response: "This is a JSON response", |
| 30 | status: "success", |
| 31 | files: ["test.ts"] |
| 32 | }; |
| 33 | |
| 34 | mockServer = await setupMockLLMTest(context, { |
| 35 | response: JSON.stringify(validJsonResponse), |
| 36 | streaming: false, // Use non-streaming to avoid JSON escaping issues |
| 37 | }); |
| 38 | |
| 39 | const result = await runCLI(context, { |
| 40 | args: ["-p", "--format", "json", "--config", context.configPath, "Test prompt"], |
| 41 | timeout: 15000, |
| 42 | }); |
| 43 | |
| 44 | // Verify the output is valid JSON |
| 45 | expect(() => JSON.parse(result.stdout.trim())).not.toThrow(); |
| 46 | const parsedOutput = JSON.parse(result.stdout.trim()); |
| 47 | expect(parsedOutput).toEqual(validJsonResponse); |
| 48 | expect(result.exitCode).toBe(0); |
| 49 | }, 20000); |
| 50 | |
| 51 | it("should wrap non-JSON response in JSON object", async () => { |
| 52 | const nonJsonResponse = "This is just plain text response"; |
| 53 | |
| 54 | mockServer = await setupMockLLMTest(context, { |
| 55 | response: nonJsonResponse, |
| 56 | }); |
| 57 | |
| 58 | const result = await runCLI(context, { |
| 59 | args: ["-p", "--format", "json", "--config", context.configPath, "Test prompt"], |
| 60 | timeout: 15000, |
| 61 | }); |
| 62 | |
| 63 | // Verify the output is valid JSON |
| 64 | expect(() => JSON.parse(result.stdout.trim())).not.toThrow(); |
| 65 | const parsedOutput = JSON.parse(result.stdout.trim()); |
| 66 | expect(parsedOutput.response).toBe(nonJsonResponse); |
| 67 | expect(parsedOutput.status).toBe("success"); |
| 68 | expect(parsedOutput.note).toBe("Response was not valid JSON, so it was wrapped in a JSON object"); |
| 69 | expect(result.exitCode).toBe(0); |
| 70 | }, 20000); |
| 71 | |
| 72 | it("should include JSON formatting instructions in system message", async () => { |
| 73 | mockServer = await setupMockLLMTest(context, { |
| 74 | response: '{"response": "I understand JSON format"}', |
| 75 | }); |
| 76 | |
| 77 | const result = await runCLI(context, { |
| 78 | args: ["-p", "--format", "json", "--config", context.configPath, "Hello"], |
| 79 | timeout: 15000, |
| 80 | }); |
| 81 | |
| 82 | expect(result.exitCode).toBe(0); |
| 83 | expect(mockServer.requests).toHaveLength(1); |
| 84 | |
| 85 | // Find the system message |
| 86 | const systemMessage = mockServer.requests[0].body.messages.find((m: any) => m.role === "system"); |
| 87 | expect(systemMessage.content).toContain("JSON output mode"); |
| 88 | expect(systemMessage.content).toContain("valid JSON that can be parsed by JSON.parse()"); |
| 89 | }, 20000); |
| 90 | |
| 91 | it("should fail validation when --format is used without -p flag", async () => { |
| 92 | const result = await runCLI(context, { |
| 93 | args: ["--format", "json", "Test prompt"], |
| 94 | timeout: 5000, |
| 95 | expectError: true, |
| 96 | }); |
| 97 | |
| 98 | expect(result.stderr).toContain("--format flag can only be used with -p/--print flag"); |
| 99 | expect(result.exitCode).toBe(1); |
| 100 | }, 10000); |
| 101 | |
| 102 | it("should fail validation with invalid format value", async () => { |
| 103 | const result = await runCLI(context, { |
| 104 | args: ["-p", "--format", "xml", "Test prompt"], |
| 105 | timeout: 5000, |
| 106 | expectError: true, |
| 107 | }); |
| 108 | |
| 109 | expect(result.stderr).toContain("--format currently only supports 'json'"); |
| 110 | expect(result.exitCode).toBe(1); |
| 111 | }, 10000); |
| 112 | }); |