| 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: [ |
| 41 | "-p", |
| 42 | "--format", |
| 43 | "json", |
| 44 | "--config", |
| 45 | context.configPath, |
| 46 | "Test prompt", |
| 47 | ], |
| 48 | timeout: 15000, |
| 49 | }); |
| 50 | |
| 51 | // Verify the output is valid JSON |
| 52 | expect(() => JSON.parse(result.stdout.trim())).not.toThrow(); |
| 53 | const parsedOutput = JSON.parse(result.stdout.trim()); |
| 54 | expect(parsedOutput).toEqual(validJsonResponse); |
| 55 | expect(result.exitCode).toBe(0); |
| 56 | }, 20000); |
| 57 | |
| 58 | it("should wrap non-JSON response in JSON object", async () => { |
| 59 | const nonJsonResponse = "This is just plain text response"; |
| 60 | |
| 61 | mockServer = await setupMockLLMTest(context, { |
| 62 | response: nonJsonResponse, |
| 63 | }); |
| 64 | |
| 65 | const result = await runCLI(context, { |
| 66 | args: [ |
| 67 | "-p", |
| 68 | "--format", |
| 69 | "json", |
| 70 | "--config", |
| 71 | context.configPath, |
| 72 | "Test prompt", |
| 73 | ], |
| 74 | timeout: 15000, |
| 75 | }); |
| 76 | |
| 77 | // Verify the output is valid JSON |
| 78 | expect(() => JSON.parse(result.stdout.trim())).not.toThrow(); |
| 79 | const parsedOutput = JSON.parse(result.stdout.trim()); |
| 80 | expect(parsedOutput.response).toBe(nonJsonResponse); |
| 81 | expect(parsedOutput.status).toBe("success"); |
| 82 | expect(parsedOutput.note).toBe( |
| 83 | "Response was not valid JSON, so it was wrapped in a JSON object", |
| 84 | ); |
| 85 | expect(result.exitCode).toBe(0); |
| 86 | }, 20000); |
| 87 | |
| 88 | it("should include JSON formatting instructions in system message", async () => { |
| 89 | mockServer = await setupMockLLMTest(context, { |
| 90 | response: '{"response": "I understand JSON format"}', |
| 91 | }); |
| 92 | |
| 93 | const result = await runCLI(context, { |
| 94 | args: ["-p", "--format", "json", "--config", context.configPath, "Hello"], |
| 95 | timeout: 15000, |
| 96 | }); |
| 97 | |
| 98 | expect(result.exitCode).toBe(0); |
| 99 | expect(mockServer.requests).toHaveLength(1); |
| 100 | |
| 101 | // Find the system message |
| 102 | const systemMessage = mockServer.requests[0].body.messages.find( |
| 103 | (m: any) => m.role === "system", |
| 104 | ); |
| 105 | expect(systemMessage.content).toContain("JSON output mode"); |
| 106 | expect(systemMessage.content).toContain( |
| 107 | "valid JSON that can be parsed by JSON.parse()", |
| 108 | ); |
| 109 | }, 20000); |
| 110 | |
| 111 | it("should fail validation when --format is used without -p flag", async () => { |
| 112 | const result = await runCLI(context, { |
| 113 | args: ["--format", "json", "Test prompt"], |
| 114 | timeout: 5000, |
| 115 | expectError: true, |
| 116 | }); |
| 117 | |
| 118 | expect(result.stderr).toContain( |
| 119 | "--format flag can only be used with -p/--print flag", |
| 120 | ); |
| 121 | expect(result.exitCode).toBe(1); |
| 122 | }, 10000); |
| 123 | |
| 124 | it("should fail validation with invalid format value", async () => { |
| 125 | const result = await runCLI(context, { |
| 126 | args: ["-p", "--format", "xml", "Test prompt"], |
| 127 | timeout: 5000, |
| 128 | expectError: true, |
| 129 | }); |
| 130 | |
| 131 | expect(result.stderr).toContain("--format currently only supports 'json'"); |
| 132 | expect(result.exitCode).toBe(1); |
| 133 | }, 10000); |
| 134 | }); |