|
| 1 | +import fs from "fs"; |
| 2 | +import os from "os"; |
1 | 3 | import path from "path";
|
2 | 4 |
|
3 | 5 | import { codexExecSpy } from "./codexExecSpy";
|
@@ -211,14 +213,79 @@ describe("Codex", () => {
|
211 | 213 |
|
212 | 214 | expectPair(commandArgs, ["--sandbox", "workspace-write"]);
|
213 | 215 | expectPair(commandArgs, ["--model", "gpt-test-1"]);
|
214 |
| - |
215 | 216 | } finally {
|
216 | 217 | restore();
|
217 | 218 | await close();
|
218 | 219 | }
|
219 | 220 | });
|
220 |
| -}); |
221 | 221 |
|
| 222 | + it("runs in provided working directory", async () => { |
| 223 | + const { url, close } = await startResponsesTestProxy({ |
| 224 | + statusCode: 200, |
| 225 | + responseBodies: [ |
| 226 | + sse( |
| 227 | + responseStarted("response_1"), |
| 228 | + assistantMessage("Working directory applied", "item_1"), |
| 229 | + responseCompleted("response_1"), |
| 230 | + ), |
| 231 | + ], |
| 232 | + }); |
| 233 | + |
| 234 | + const { args: spawnArgs, restore } = codexExecSpy(); |
| 235 | + |
| 236 | + try { |
| 237 | + const workingDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "codex-working-dir-")); |
| 238 | + const client = new Codex({ |
| 239 | + codexPathOverride: codexExecPath, |
| 240 | + baseUrl: url, |
| 241 | + apiKey: "test", |
| 242 | + }); |
| 243 | + |
| 244 | + const thread = client.startThread(); |
| 245 | + await thread.run("use custom working directory", { |
| 246 | + workingDirectory, |
| 247 | + skipGitRepoCheck: true, |
| 248 | + }); |
| 249 | + |
| 250 | + const commandArgs = spawnArgs[0]; |
| 251 | + expectPair(commandArgs, ["--cd", workingDirectory]); |
| 252 | + } finally { |
| 253 | + restore(); |
| 254 | + await close(); |
| 255 | + } |
| 256 | + }); |
| 257 | + |
| 258 | + it("throws if working directory is not git and no skipGitRepoCheck is provided", async () => { |
| 259 | + const { url, close } = await startResponsesTestProxy({ |
| 260 | + statusCode: 200, |
| 261 | + responseBodies: [ |
| 262 | + sse( |
| 263 | + responseStarted("response_1"), |
| 264 | + assistantMessage("Working directory applied", "item_1"), |
| 265 | + responseCompleted("response_1"), |
| 266 | + ), |
| 267 | + ], |
| 268 | + }); |
| 269 | + |
| 270 | + try { |
| 271 | + const workingDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "codex-working-dir-")); |
| 272 | + const client = new Codex({ |
| 273 | + codexPathOverride: codexExecPath, |
| 274 | + baseUrl: url, |
| 275 | + apiKey: "test", |
| 276 | + }); |
| 277 | + |
| 278 | + const thread = client.startThread(); |
| 279 | + await expect( |
| 280 | + thread.run("use custom working directory", { |
| 281 | + workingDirectory, |
| 282 | + }), |
| 283 | + ).rejects.toThrow(/Not inside a trusted directory/); |
| 284 | + } finally { |
| 285 | + await close(); |
| 286 | + } |
| 287 | + }); |
| 288 | +}); |
222 | 289 |
|
223 | 290 | function expectPair(args: string[] | undefined, pair: [string, string]) {
|
224 | 291 | if (!args) {
|
|
0 commit comments