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