| 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 Mock Server", () => { |
| 13 | let context: any; |
| 14 | let mockServer: MockLLMServer; |
| 15 | |
| 16 | beforeEach(async () => { |
| 17 | context = await createTestContext(); |
| 18 | mockServer = await setupMockLLMTest(context, { |
| 19 | response: "Hello World!", |
| 20 | }); |
| 21 | }); |
| 22 | |
| 23 | afterEach(async () => { |
| 24 | await cleanupMockLLMServer(mockServer); |
| 25 | await cleanupTestContext(context); |
| 26 | }); |
| 27 | |
| 28 | it('should output "Hello World!" when using cn -p "Hi"', async () => { |
| 29 | const result = await runCLI(context, { |
| 30 | args: ["-p", "--config", context.configPath, "Hi"], |
| 31 | timeout: 15000, |
| 32 | }); |
| 33 | |
| 34 | // Verify stdout contains "Hello World!" |
| 35 | expect(result.stdout).toContain("Hello World!"); |
| 36 | expect(result.exitCode).toBe(0); |
| 37 | }, 20000); |
| 38 | |
| 39 | it("should handle streaming responses correctly", async () => { |
| 40 | const result = await runCLI(context, { |
| 41 | args: ["-p", "--config", context.configPath, "Say hello"], |
| 42 | timeout: 15000, |
| 43 | }); |
| 44 | |
| 45 | expect(result.stdout).toContain("Hello World!"); |
| 46 | expect(result.exitCode).toBe(0); |
| 47 | expect(mockServer.requests).toHaveLength(1); |
| 48 | // Find the user message (system message is first) |
| 49 | const userMessage = mockServer.requests[0].body.messages.find( |
| 50 | (m: any) => m.role === "user", |
| 51 | ); |
| 52 | expect(userMessage.content).toBe("Say hello"); |
| 53 | }, 20000); |
| 54 | |
| 55 | it("should work with longer prompts", async () => { |
| 56 | const result = await runCLI(context, { |
| 57 | args: [ |
| 58 | "-p", |
| 59 | "--config", |
| 60 | context.configPath, |
| 61 | "Please respond with exactly 'Hello World!' and nothing else", |
| 62 | ], |
| 63 | timeout: 15000, |
| 64 | }); |
| 65 | |
| 66 | expect(result.stdout).toContain("Hello World!"); |
| 67 | expect(result.exitCode).toBe(0); |
| 68 | expect(mockServer.requests).toHaveLength(1); |
| 69 | // Find the user message (system message is first) |
| 70 | const userMessage = mockServer.requests[0].body.messages.find( |
| 71 | (m: any) => m.role === "user", |
| 72 | ); |
| 73 | expect(userMessage.content).toBe( |
| 74 | "Please respond with exactly 'Hello World!' and nothing else", |
| 75 | ); |
| 76 | }, 20000); |
| 77 | }); |