| 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 --silent Flag", () => { |
| 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 strip <think></think> tags from response with --silent flag", async () => { |
| 28 | const responseWithThinkTags = `<think> |
| 29 | I need to write a shell script. Let me think about this. |
| 30 | The user wants a simple Hello World script. |
| 31 | </think> |
| 32 | |
| 33 | #!/bin/bash |
| 34 | echo "Hello World"`; |
| 35 | |
| 36 | mockServer = await setupMockLLMTest(context, { |
| 37 | response: responseWithThinkTags, |
| 38 | streaming: false, |
| 39 | }); |
| 40 | |
| 41 | const result = await runCLI(context, { |
| 42 | args: ["-p", "--silent", "--config", context.configPath, "Write a shell script"], |
| 43 | timeout: 15000, |
| 44 | }); |
| 45 | |
| 46 | // Verify think tags are stripped |
| 47 | expect(result.stdout).not.toContain("<think>"); |
| 48 | expect(result.stdout).not.toContain("</think>"); |
| 49 | expect(result.stdout).not.toContain("I need to write a shell script"); |
| 50 | |
| 51 | // Verify the actual content remains |
| 52 | expect(result.stdout).toContain("#!/bin/bash"); |
| 53 | expect(result.stdout).toContain('echo "Hello World"'); |
| 54 | expect(result.exitCode).toBe(0); |
| 55 | }, 20000); |
| 56 | |
| 57 | it("should strip multiple <think></think> blocks with --silent flag", async () => { |
| 58 | const responseWithMultipleThinkTags = `<think> |
| 59 | First thought block |
| 60 | </think> |
| 61 | |
| 62 | Some content here |
| 63 | |
| 64 | <think> |
| 65 | Second thought block |
| 66 | More thinking... |
| 67 | </think> |
| 68 | |
| 69 | Final content`; |
| 70 | |
| 71 | mockServer = await setupMockLLMTest(context, { |
| 72 | response: responseWithMultipleThinkTags, |
| 73 | streaming: false, |
| 74 | }); |
| 75 | |
| 76 | const result = await runCLI(context, { |
| 77 | args: ["-p", "--silent", "--config", context.configPath, "Test prompt"], |
| 78 | timeout: 15000, |
| 79 | }); |
| 80 | |
| 81 | // Verify all think tags are stripped |
| 82 | expect(result.stdout).not.toContain("<think>"); |
| 83 | expect(result.stdout).not.toContain("</think>"); |
| 84 | expect(result.stdout).not.toContain("First thought block"); |
| 85 | expect(result.stdout).not.toContain("Second thought block"); |
| 86 | expect(result.stdout).not.toContain("More thinking..."); |
| 87 | |
| 88 | // Verify the actual content remains |
| 89 | expect(result.stdout).toContain("Some content here"); |
| 90 | expect(result.stdout).toContain("Final content"); |
| 91 | expect(result.exitCode).toBe(0); |
| 92 | }, 20000); |
| 93 | |
| 94 | it("should reduce excess whitespace with --silent flag", async () => { |
| 95 | const responseWithExcessWhitespace = `<think> |
| 96 | This will be removed |
| 97 | </think> |
| 98 | |
| 99 | |
| 100 | |
| 101 | Content with lots of whitespace |
| 102 | |
| 103 | |
| 104 | |
| 105 | More content`; |
| 106 | |
| 107 | mockServer = await setupMockLLMTest(context, { |
| 108 | response: responseWithExcessWhitespace, |
| 109 | streaming: false, |
| 110 | }); |
| 111 | |
| 112 | const result = await runCLI(context, { |
| 113 | args: ["-p", "--silent", "--config", context.configPath, "Test prompt"], |
| 114 | timeout: 15000, |
| 115 | }); |
| 116 | |
| 117 | // Verify think tags are stripped and excess whitespace is reduced |
| 118 | expect(result.stdout).not.toContain("<think>"); |
| 119 | expect(result.stdout).not.toContain("This will be removed"); |
| 120 | expect(result.stdout).toContain("Content with lots of whitespace"); |
| 121 | expect(result.stdout).toContain("More content"); |
| 122 | |
| 123 | // Verify no triple newlines exist (excess whitespace reduced) |
| 124 | expect(result.stdout).not.toMatch(/\n\s*\n\s*\n/); |
| 125 | expect(result.exitCode).toBe(0); |
| 126 | }, 20000); |
| 127 | |
| 128 | it("should work normally without --silent flag (baseline test)", async () => { |
| 129 | const responseWithThinkTags = `<think> |
| 130 | I need to write a shell script. Let me think about this. |
| 131 | </think> |
| 132 | |
| 133 | #!/bin/bash |
| 134 | echo "Hello World"`; |
| 135 | |
| 136 | mockServer = await setupMockLLMTest(context, { |
| 137 | response: responseWithThinkTags, |
| 138 | streaming: false, |
| 139 | }); |
| 140 | |
| 141 | const result = await runCLI(context, { |
| 142 | args: ["-p", "--config", context.configPath, "Write a shell script"], |
| 143 | timeout: 15000, |
| 144 | }); |
| 145 | |
| 146 | // Verify think tags are NOT stripped without --silent flag |
| 147 | expect(result.stdout).toContain("<think>"); |
| 148 | expect(result.stdout).toContain("</think>"); |
| 149 | expect(result.stdout).toContain("I need to write a shell script"); |
| 150 | expect(result.stdout).toContain("#!/bin/bash"); |
| 151 | expect(result.stdout).toContain('echo "Hello World"'); |
| 152 | expect(result.exitCode).toBe(0); |
| 153 | }, 20000); |
| 154 | |
| 155 | it("should handle response with no think tags using --silent flag", async () => { |
| 156 | const responseWithoutThinkTags = `#!/bin/bash |
| 157 | echo "Hello World" |
| 158 | |
| 159 | This is a simple script.`; |
| 160 | |
| 161 | mockServer = await setupMockLLMTest(context, { |
| 162 | response: responseWithoutThinkTags, |
| 163 | streaming: false, |
| 164 | }); |
| 165 | |
| 166 | const result = await runCLI(context, { |
| 167 | args: ["-p", "--silent", "--config", context.configPath, "Write a shell script"], |
| 168 | timeout: 15000, |
| 169 | }); |
| 170 | |
| 171 | // Verify content is preserved when no think tags exist |
| 172 | expect(result.stdout).toContain("#!/bin/bash"); |
| 173 | expect(result.stdout).toContain('echo "Hello World"'); |
| 174 | expect(result.stdout).toContain("This is a simple script."); |
| 175 | expect(result.exitCode).toBe(0); |
| 176 | }, 20000); |
| 177 | |
| 178 | it("should fail validation when --silent is used without -p flag", async () => { |
| 179 | const result = await runCLI(context, { |
| 180 | args: ["--silent", "Test prompt"], |
| 181 | timeout: 5000, |
| 182 | expectError: true, |
| 183 | }); |
| 184 | |
| 185 | expect(result.stderr).toContain("--silent flag can only be used with -p/--print flag"); |
| 186 | expect(result.exitCode).toBe(1); |
| 187 | }, 10000); |
| 188 | |
| 189 | it("should work with both --silent and --format json flags", async () => { |
| 190 | const responseWithThinkTags = `<think> |
| 191 | I should respond with JSON |
| 192 | </think> |
| 193 | |
| 194 | {"message": "Hello World", "status": "success"}`; |
| 195 | |
| 196 | mockServer = await setupMockLLMTest(context, { |
| 197 | response: responseWithThinkTags, |
| 198 | streaming: false, |
| 199 | }); |
| 200 | |
| 201 | const result = await runCLI(context, { |
| 202 | args: ["-p", "--silent", "--format", "json", "--config", context.configPath, "Test prompt"], |
| 203 | timeout: 15000, |
| 204 | }); |
| 205 | |
| 206 | // Verify think tags are stripped and JSON is valid |
| 207 | expect(result.stdout).not.toContain("<think>"); |
| 208 | expect(result.stdout).not.toContain("I should respond with JSON"); |
| 209 | |
| 210 | // Verify the output is valid JSON |
| 211 | expect(() => JSON.parse(result.stdout.trim())).not.toThrow(); |
| 212 | const parsedOutput = JSON.parse(result.stdout.trim()); |
| 213 | expect(parsedOutput.message).toBe("Hello World"); |
| 214 | expect(parsedOutput.status).toBe("success"); |
| 215 | expect(result.exitCode).toBe(0); |
| 216 | }, 20000); |
| 217 | }); |