|
| 1 | +/** |
| 2 | + * E2E test for headless mode permission errors |
| 3 | + * Verifies that when a tool requires permission in headless mode, |
| 4 | + * an appropriate error message is displayed suggesting the --auto flag |
| 5 | + */ |
| 6 | +import * as http from "http"; |
| 7 | + |
| 8 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 9 | + |
| 10 | +import { |
| 11 | + cleanupTestContext, |
| 12 | + createTestContext, |
| 13 | + runCLI, |
| 14 | +} from "../test-helpers/cli-helpers.js"; |
| 15 | +import { |
| 16 | + cleanupMockLLMServer, |
| 17 | + setupMockLLMTest, |
| 18 | + type MockLLMServer, |
| 19 | +} from "../test-helpers/mock-llm-server.js"; |
| 20 | + |
| 21 | +describe("E2E: Headless Mode Permission Errors", () => { |
| 22 | + let context: any; |
| 23 | + let mockServer: MockLLMServer; |
| 24 | + |
| 25 | + beforeEach(async () => { |
| 26 | + context = await createTestContext(); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(async () => { |
| 30 | + if (mockServer) { |
| 31 | + await cleanupMockLLMServer(mockServer); |
| 32 | + } |
| 33 | + await cleanupTestContext(context); |
| 34 | + }); |
| 35 | + it("should display error message with --auto suggestion when tool requires permission", async () => { |
| 36 | + // Set up mock LLM to return a tool call that requires permission (Write tool) |
| 37 | + mockServer = await setupMockLLMTest(context, { |
| 38 | + customHandler: (req: http.IncomingMessage, res: http.ServerResponse) => { |
| 39 | + let body = ""; |
| 40 | + req.on("data", (chunk) => { |
| 41 | + body += chunk.toString(); |
| 42 | + }); |
| 43 | + |
| 44 | + req.on("end", () => { |
| 45 | + if (req.method === "POST" && req.url === "/chat/completions") { |
| 46 | + res.writeHead(200, { |
| 47 | + "Content-Type": "text/event-stream", |
| 48 | + "Cache-Control": "no-cache", |
| 49 | + Connection: "keep-alive", |
| 50 | + }); |
| 51 | + |
| 52 | + // Send a Write tool call (requires permission by default) |
| 53 | + res.write( |
| 54 | + `data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_write","type":"function","function":{"name":"Write"}}]},"index":0}]}\n\n`, |
| 55 | + ); |
| 56 | + res.write( |
| 57 | + `data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\\"filepath\\":\\"test.txt\\",\\"content\\":\\"hello\\"}"}}]},"index":0}]}\n\n`, |
| 58 | + ); |
| 59 | + |
| 60 | + // Send usage data |
| 61 | + res.write( |
| 62 | + `data: {"usage":{"prompt_tokens":10,"completion_tokens":20}}\n\n`, |
| 63 | + ); |
| 64 | + |
| 65 | + // End the stream |
| 66 | + res.write(`data: [DONE]\n\n`); |
| 67 | + res.end(); |
| 68 | + } else { |
| 69 | + res.writeHead(404); |
| 70 | + res.end("Not found"); |
| 71 | + } |
| 72 | + }); |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + // Run CLI in headless mode without permission flags |
| 77 | + const result = await runCLI(context, { |
| 78 | + args: ["-p", "--config", context.configPath, "Create a file test.txt"], |
| 79 | + timeout: 15000, |
| 80 | + }); |
| 81 | + |
| 82 | + // Expect non-zero exit code |
| 83 | + expect(result.exitCode).toBe(1); |
| 84 | + |
| 85 | + // Verify the error message contains the expected information |
| 86 | + expect(result.stderr).toContain("requires permission"); |
| 87 | + expect(result.stderr).toContain("headless mode"); |
| 88 | + expect(result.stderr).toContain("--auto"); |
| 89 | + expect(result.stderr).toContain("--allow"); |
| 90 | + expect(result.stderr).toContain("--exclude"); |
| 91 | + }, 20000); |
| 92 | + |
| 93 | + it("should succeed with --auto flag when tool requires permission", async () => { |
| 94 | + // Set up mock LLM to return a Write tool call |
| 95 | + mockServer = await setupMockLLMTest(context, { |
| 96 | + customHandler: (req: http.IncomingMessage, res: http.ServerResponse) => { |
| 97 | + let body = ""; |
| 98 | + req.on("data", (chunk) => { |
| 99 | + body += chunk.toString(); |
| 100 | + }); |
| 101 | + |
| 102 | + req.on("end", () => { |
| 103 | + if (req.method === "POST" && req.url === "/chat/completions") { |
| 104 | + res.writeHead(200, { |
| 105 | + "Content-Type": "text/event-stream", |
| 106 | + "Cache-Control": "no-cache", |
| 107 | + Connection: "keep-alive", |
| 108 | + }); |
| 109 | + |
| 110 | + // Send a Write tool call |
| 111 | + res.write( |
| 112 | + `data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_write","type":"function","function":{"name":"Write"}}]},"index":0}]}\n\n`, |
| 113 | + ); |
| 114 | + res.write( |
| 115 | + `data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\\"filepath\\":\\"test.txt\\",\\"content\\":\\"hello\\"}"}}]},"index":0}]}\n\n`, |
| 116 | + ); |
| 117 | + |
| 118 | + // Send usage data |
| 119 | + res.write( |
| 120 | + `data: {"usage":{"prompt_tokens":10,"completion_tokens":20}}\n\n`, |
| 121 | + ); |
| 122 | + |
| 123 | + // End the stream |
| 124 | + res.write(`data: [DONE]\n\n`); |
| 125 | + res.end(); |
| 126 | + } else { |
| 127 | + res.writeHead(404); |
| 128 | + res.end("Not found"); |
| 129 | + } |
| 130 | + }); |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + // Run CLI with --auto flag |
| 135 | + const result = await runCLI(context, { |
| 136 | + args: [ |
| 137 | + "-p", |
| 138 | + "--auto", |
| 139 | + "--config", |
| 140 | + context.configPath, |
| 141 | + "Create a file test.txt", |
| 142 | + ], |
| 143 | + timeout: 15000, |
| 144 | + }); |
| 145 | + |
| 146 | + // Should succeed |
| 147 | + expect(result.exitCode).toBe(0); |
| 148 | + expect(result.stderr).not.toContain("requires permission"); |
| 149 | + }, 20000); |
| 150 | + |
| 151 | + it("should succeed with --allow flag for specific tool", async () => { |
| 152 | + // Set up mock LLM to return a Write tool call |
| 153 | + mockServer = await setupMockLLMTest(context, { |
| 154 | + customHandler: (req: http.IncomingMessage, res: http.ServerResponse) => { |
| 155 | + let body = ""; |
| 156 | + req.on("data", (chunk) => { |
| 157 | + body += chunk.toString(); |
| 158 | + }); |
| 159 | + |
| 160 | + req.on("end", () => { |
| 161 | + if (req.method === "POST" && req.url === "/chat/completions") { |
| 162 | + res.writeHead(200, { |
| 163 | + "Content-Type": "text/event-stream", |
| 164 | + "Cache-Control": "no-cache", |
| 165 | + Connection: "keep-alive", |
| 166 | + }); |
| 167 | + |
| 168 | + // Send a Write tool call |
| 169 | + res.write( |
| 170 | + `data: {"choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_write","type":"function","function":{"name":"Write"}}]},"index":0}]}\n\n`, |
| 171 | + ); |
| 172 | + res.write( |
| 173 | + `data: {"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\\"filepath\\":\\"test.txt\\",\\"content\\":\\"hello\\"}"}}]},"index":0}]}\n\n`, |
| 174 | + ); |
| 175 | + |
| 176 | + // Send usage data |
| 177 | + res.write( |
| 178 | + `data: {"usage":{"prompt_tokens":10,"completion_tokens":20}}\n\n`, |
| 179 | + ); |
| 180 | + |
| 181 | + // End the stream |
| 182 | + res.write(`data: [DONE]\n\n`); |
| 183 | + res.end(); |
| 184 | + } else { |
| 185 | + res.writeHead(404); |
| 186 | + res.end("Not found"); |
| 187 | + } |
| 188 | + }); |
| 189 | + }, |
| 190 | + }); |
| 191 | + |
| 192 | + // Run CLI with --allow Write flag |
| 193 | + const result = await runCLI(context, { |
| 194 | + args: [ |
| 195 | + "-p", |
| 196 | + "--allow", |
| 197 | + "Write", |
| 198 | + "--config", |
| 199 | + context.configPath, |
| 200 | + "Create a file test.txt", |
| 201 | + ], |
| 202 | + timeout: 15000, |
| 203 | + }); |
| 204 | + |
| 205 | + // Should succeed |
| 206 | + expect(result.exitCode).toBe(0); |
| 207 | + expect(result.stderr).not.toContain("requires permission"); |
| 208 | + }, 20000); |
| 209 | +}); |
0 commit comments