|
1 | | -import { describe, test, expect, vi, beforeEach } from "vitest" |
| 1 | +import { describe, test, expect, vi, beforeEach, afterEach } from "vitest" |
| 2 | + |
| 3 | +// Mock os module |
| 4 | +vi.mock("os", () => ({ |
| 5 | + platform: vi.fn(() => "darwin"), // Default to non-Windows |
| 6 | +})) |
2 | 7 |
|
3 | 8 | // Mock vscode workspace |
4 | 9 | vi.mock("vscode", () => ({ |
@@ -118,56 +123,53 @@ describe("runClaudeCode", () => { |
118 | 123 | expect(typeof result[Symbol.asyncIterator]).toBe("function") |
119 | 124 | }) |
120 | 125 |
|
121 | | - test("should use stdin instead of command line arguments for messages", async () => { |
| 126 | + test("should handle platform-specific stdin behavior", async () => { |
122 | 127 | const { runClaudeCode } = await import("../run") |
123 | 128 | const messages = [{ role: "user" as const, content: "Hello world!" }] |
| 129 | + const systemPrompt = "You are a helpful assistant" |
124 | 130 | const options = { |
125 | | - systemPrompt: "You are a helpful assistant", |
| 131 | + systemPrompt, |
126 | 132 | messages, |
127 | 133 | } |
128 | 134 |
|
129 | | - const generator = runClaudeCode(options) |
| 135 | + // Test on Windows |
| 136 | + const os = await import("os") |
| 137 | + vi.mocked(os.platform).mockReturnValue("win32") |
130 | 138 |
|
131 | | - // Consume the generator to completion |
| 139 | + const generator = runClaudeCode(options) |
132 | 140 | const results = [] |
133 | 141 | for await (const chunk of generator) { |
134 | 142 | results.push(chunk) |
135 | 143 | } |
136 | 144 |
|
137 | | - // Verify execa was called with correct arguments (no JSON.stringify(messages) in args) |
138 | | - expect(mockExeca).toHaveBeenCalledWith( |
139 | | - "claude", |
140 | | - expect.arrayContaining([ |
141 | | - "-p", |
142 | | - "--system-prompt", |
143 | | - "You are a helpful assistant", |
144 | | - "--verbose", |
145 | | - "--output-format", |
146 | | - "stream-json", |
147 | | - "--disallowedTools", |
148 | | - expect.any(String), |
149 | | - "--max-turns", |
150 | | - "1", |
151 | | - ]), |
152 | | - expect.objectContaining({ |
153 | | - stdin: "pipe", |
154 | | - stdout: "pipe", |
155 | | - stderr: "pipe", |
156 | | - }), |
157 | | - ) |
158 | | - |
159 | | - // Verify the arguments do NOT contain the stringified messages |
| 145 | + // On Windows, should NOT have --system-prompt in args |
160 | 146 | const [, args] = mockExeca.mock.calls[0] |
161 | | - expect(args).not.toContain(JSON.stringify(messages)) |
| 147 | + expect(args).not.toContain("--system-prompt") |
162 | 148 |
|
163 | | - // Verify messages were written to stdin with callback |
164 | | - expect(mockStdin.write).toHaveBeenCalledWith(JSON.stringify(messages), "utf8", expect.any(Function)) |
165 | | - expect(mockStdin.end).toHaveBeenCalled() |
| 149 | + // Should pass both system prompt and messages via stdin |
| 150 | + const expectedStdinData = JSON.stringify({ systemPrompt, messages }) |
| 151 | + expect(mockStdin.write).toHaveBeenCalledWith(expectedStdinData, "utf8", expect.any(Function)) |
166 | 152 |
|
167 | | - // Verify we got the expected mock output |
168 | | - expect(results).toHaveLength(2) |
169 | | - expect(results[0]).toEqual({ type: "text", text: "Hello" }) |
170 | | - expect(results[1]).toEqual({ type: "text", text: " world" }) |
| 153 | + // Reset mocks for non-Windows test |
| 154 | + vi.clearAllMocks() |
| 155 | + mockExeca.mockReturnValue(createMockProcess()) |
| 156 | + |
| 157 | + // Test on non-Windows |
| 158 | + vi.mocked(os.platform).mockReturnValue("darwin") |
| 159 | + |
| 160 | + const generator2 = runClaudeCode(options) |
| 161 | + const results2 = [] |
| 162 | + for await (const chunk of generator2) { |
| 163 | + results2.push(chunk) |
| 164 | + } |
| 165 | + |
| 166 | + // On non-Windows, should have --system-prompt in args |
| 167 | + const [, args2] = mockExeca.mock.calls[0] |
| 168 | + expect(args2).toContain("--system-prompt") |
| 169 | + expect(args2).toContain(systemPrompt) |
| 170 | + |
| 171 | + // Should only pass messages via stdin |
| 172 | + expect(mockStdin.write).toHaveBeenCalledWith(JSON.stringify(messages), "utf8", expect.any(Function)) |
171 | 173 | }) |
172 | 174 |
|
173 | 175 | test("should include model parameter when provided", async () => { |
|
0 commit comments