|
| 1 | +import * as childProcess from "node:child_process"; |
| 2 | +import * as fs from "node:fs"; |
| 3 | +import * as os from "node:os"; |
| 4 | +import * as path from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
1 | 6 | import { |
| 7 | + afterAll, |
| 8 | + afterEach, |
| 9 | + beforeEach, |
2 | 10 | describe, |
3 | | - it, |
4 | 11 | expect, |
5 | | - beforeEach, |
6 | | - afterEach, |
7 | | - afterAll, |
| 12 | + it, |
8 | 13 | vi, |
9 | 14 | } from "vitest"; |
10 | | -import * as childProcess from "node:child_process"; |
11 | | -import * as fs from "node:fs"; |
12 | | -import * as os from "node:os"; |
13 | | -import * as path from "node:path"; |
14 | 15 | import { IDE, ToolExtras } from "../.."; |
15 | 16 | import * as processTerminalStates from "../../util/processTerminalStates"; |
16 | | -import { runTerminalCommandImpl } from "./runTerminalCommand"; |
17 | 17 | import { runTerminalCommandTool } from "../definitions/runTerminalCommand"; |
| 18 | +import { runTerminalCommandImpl } from "./runTerminalCommand"; |
18 | 19 |
|
19 | 20 | // We're using real child processes, so ensure these aren't mocked |
20 | 21 | vi.unmock("node:child_process"); |
@@ -476,6 +477,206 @@ describe("runTerminalCommandImpl", () => { |
476 | 477 | process.cwd = originalCwd; |
477 | 478 | } |
478 | 479 | }); |
| 480 | + |
| 481 | + describe("cwd handling", () => { |
| 482 | + describe("workspace directory handling", () => { |
| 483 | + it("should use file:// URI when available", async () => { |
| 484 | + const fileUri = "file:///home/user/workspace"; |
| 485 | + mockGetWorkspaceDirs.mockResolvedValue([fileUri]); |
| 486 | + |
| 487 | + // We can't easily test the internal cwd without mocking child_process, |
| 488 | + // but we can verify the function doesn't throw with file URIs |
| 489 | + await expect( |
| 490 | + runTerminalCommandImpl( |
| 491 | + { command: "echo test", waitForCompletion: false }, |
| 492 | + createMockExtras(), |
| 493 | + ), |
| 494 | + ).resolves.toBeDefined(); |
| 495 | + }); |
| 496 | + |
| 497 | + it("should skip non-file URIs and use the first file:// URI", async () => { |
| 498 | + const workspaceDirs = [ |
| 499 | + "vscode-vfs://github/user/repo", |
| 500 | + "untitled:/Untitled-1", |
| 501 | + "file:///home/user/workspace", |
| 502 | + "file:///home/user/other-workspace", |
| 503 | + ]; |
| 504 | + mockGetWorkspaceDirs.mockResolvedValue(workspaceDirs); |
| 505 | + |
| 506 | + await expect( |
| 507 | + runTerminalCommandImpl( |
| 508 | + { command: "echo test", waitForCompletion: false }, |
| 509 | + createMockExtras(), |
| 510 | + ), |
| 511 | + ).resolves.toBeDefined(); |
| 512 | + }); |
| 513 | + |
| 514 | + it("should handle workspace with only non-file URIs", async () => { |
| 515 | + const workspaceDirs = [ |
| 516 | + "vscode-vfs://github/user/repo", |
| 517 | + "untitled:/Untitled-1", |
| 518 | + ]; |
| 519 | + mockGetWorkspaceDirs.mockResolvedValue(workspaceDirs); |
| 520 | + |
| 521 | + // Should fall back to HOME/USERPROFILE or process.cwd() without throwing |
| 522 | + await expect( |
| 523 | + runTerminalCommandImpl( |
| 524 | + { command: "echo test", waitForCompletion: false }, |
| 525 | + createMockExtras(), |
| 526 | + ), |
| 527 | + ).resolves.toBeDefined(); |
| 528 | + }); |
| 529 | + |
| 530 | + it("should handle empty workspace directories", async () => { |
| 531 | + mockGetWorkspaceDirs.mockResolvedValue([]); |
| 532 | + |
| 533 | + // Should fall back to HOME/USERPROFILE or process.cwd() without throwing |
| 534 | + await expect( |
| 535 | + runTerminalCommandImpl( |
| 536 | + { command: "echo test", waitForCompletion: false }, |
| 537 | + createMockExtras(), |
| 538 | + ), |
| 539 | + ).resolves.toBeDefined(); |
| 540 | + }); |
| 541 | + |
| 542 | + it("should properly convert file:// URIs to paths", () => { |
| 543 | + const fileUri = "file:///home/user/workspace"; |
| 544 | + const expectedPath = "/home/user/workspace"; |
| 545 | + |
| 546 | + // Test that fileURLToPath works correctly with file:// URIs |
| 547 | + expect(fileURLToPath(fileUri)).toBe(expectedPath); |
| 548 | + }); |
| 549 | + |
| 550 | + it("should throw error when trying to convert non-file URI", () => { |
| 551 | + const nonFileUri = "vscode-vfs://github/user/repo"; |
| 552 | + |
| 553 | + // This demonstrates why the fix is needed - fileURLToPath throws on non-file URIs |
| 554 | + expect(() => fileURLToPath(nonFileUri)).toThrow(); |
| 555 | + }); |
| 556 | + }); |
| 557 | + |
| 558 | + describe("remote environment handling", () => { |
| 559 | + it("should use ide.runCommand for non-enabled remote environments", async () => { |
| 560 | + const extras = createMockExtras({ |
| 561 | + remoteName: "some-unsupported-remote", |
| 562 | + }); |
| 563 | + |
| 564 | + const result = await runTerminalCommandImpl( |
| 565 | + { command: "echo test" }, |
| 566 | + extras, |
| 567 | + ); |
| 568 | + |
| 569 | + expect(mockRunCommand).toHaveBeenCalledWith("echo test"); |
| 570 | + expect(result[0].content).toContain("Terminal output not available"); |
| 571 | + }); |
| 572 | + |
| 573 | + it("should handle local environment with file URIs", async () => { |
| 574 | + mockGetWorkspaceDirs.mockResolvedValue(["file:///home/user/workspace"]); |
| 575 | + |
| 576 | + await expect( |
| 577 | + runTerminalCommandImpl( |
| 578 | + { command: "echo test", waitForCompletion: false }, |
| 579 | + createMockExtras({ remoteName: "local" }), |
| 580 | + ), |
| 581 | + ).resolves.toBeDefined(); |
| 582 | + }); |
| 583 | + |
| 584 | + it("should handle WSL environment", async () => { |
| 585 | + mockGetWorkspaceDirs.mockResolvedValue(["file:///home/user/workspace"]); |
| 586 | + |
| 587 | + await expect( |
| 588 | + runTerminalCommandImpl( |
| 589 | + { command: "echo test", waitForCompletion: false }, |
| 590 | + createMockExtras({ remoteName: "wsl" }), |
| 591 | + ), |
| 592 | + ).resolves.toBeDefined(); |
| 593 | + }); |
| 594 | + |
| 595 | + it("should handle dev-container environment", async () => { |
| 596 | + mockGetWorkspaceDirs.mockResolvedValue(["file:///workspace"]); |
| 597 | + |
| 598 | + await expect( |
| 599 | + runTerminalCommandImpl( |
| 600 | + { command: "echo test", waitForCompletion: false }, |
| 601 | + createMockExtras({ remoteName: "dev-container" }), |
| 602 | + ), |
| 603 | + ).resolves.toBeDefined(); |
| 604 | + }); |
| 605 | + }); |
| 606 | + |
| 607 | + describe("fallback behavior", () => { |
| 608 | + it("should use HOME environment variable as fallback", async () => { |
| 609 | + const originalHome = process.env.HOME; |
| 610 | + process.env.HOME = "/home/testuser"; |
| 611 | + |
| 612 | + mockGetWorkspaceDirs.mockResolvedValue([ |
| 613 | + "vscode-vfs://github/user/repo", |
| 614 | + ]); |
| 615 | + |
| 616 | + try { |
| 617 | + await expect( |
| 618 | + runTerminalCommandImpl( |
| 619 | + { command: "echo test", waitForCompletion: false }, |
| 620 | + createMockExtras(), |
| 621 | + ), |
| 622 | + ).resolves.toBeDefined(); |
| 623 | + } finally { |
| 624 | + process.env.HOME = originalHome; |
| 625 | + } |
| 626 | + }); |
| 627 | + |
| 628 | + it("should use USERPROFILE on Windows as fallback", async () => { |
| 629 | + const originalHome = process.env.HOME; |
| 630 | + const originalUserProfile = process.env.USERPROFILE; |
| 631 | + |
| 632 | + delete process.env.HOME; |
| 633 | + process.env.USERPROFILE = "C:\\Users\\TestUser"; |
| 634 | + |
| 635 | + mockGetWorkspaceDirs.mockResolvedValue([]); |
| 636 | + |
| 637 | + try { |
| 638 | + await expect( |
| 639 | + runTerminalCommandImpl( |
| 640 | + { command: "echo test", waitForCompletion: false }, |
| 641 | + createMockExtras(), |
| 642 | + ), |
| 643 | + ).resolves.toBeDefined(); |
| 644 | + } finally { |
| 645 | + process.env.HOME = originalHome; |
| 646 | + process.env.USERPROFILE = originalUserProfile; |
| 647 | + } |
| 648 | + }); |
| 649 | + |
| 650 | + it("should use os.tmpdir() as final fallback", async () => { |
| 651 | + const originalHome = process.env.HOME; |
| 652 | + const originalUserProfile = process.env.USERPROFILE; |
| 653 | + const originalCwd = process.cwd; |
| 654 | + |
| 655 | + delete process.env.HOME; |
| 656 | + delete process.env.USERPROFILE; |
| 657 | + // Mock process.cwd to throw an error |
| 658 | + process.cwd = vi.fn().mockImplementation(() => { |
| 659 | + throw new Error("No cwd available"); |
| 660 | + }) as typeof process.cwd; |
| 661 | + |
| 662 | + mockGetWorkspaceDirs.mockResolvedValue([]); |
| 663 | + |
| 664 | + try { |
| 665 | + // Should fall back to os.tmpdir() without throwing |
| 666 | + await expect( |
| 667 | + runTerminalCommandImpl( |
| 668 | + { command: "echo test", waitForCompletion: false }, |
| 669 | + createMockExtras(), |
| 670 | + ), |
| 671 | + ).resolves.toBeDefined(); |
| 672 | + } finally { |
| 673 | + process.env.HOME = originalHome; |
| 674 | + process.env.USERPROFILE = originalUserProfile; |
| 675 | + process.cwd = originalCwd; |
| 676 | + } |
| 677 | + }); |
| 678 | + }); |
| 679 | + }); |
479 | 680 | }); |
480 | 681 |
|
481 | 682 | describe("runTerminalCommandTool.evaluateToolCallPolicy", () => { |
|
0 commit comments