|
| 1 | +// npx vitest run src/integrations/terminal/__tests__/Terminal.spec.ts |
| 2 | + |
| 3 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 4 | +import * as vscode from "vscode" |
| 5 | +import { Terminal } from "../Terminal" |
| 6 | + |
| 7 | +vi.mock("vscode", () => ({ |
| 8 | + window: { |
| 9 | + createTerminal: vi.fn(), |
| 10 | + }, |
| 11 | + ThemeIcon: vi.fn().mockImplementation((icon) => ({ icon })), |
| 12 | +})) |
| 13 | + |
| 14 | +describe("Terminal", () => { |
| 15 | + beforeEach(() => { |
| 16 | + vi.clearAllMocks() |
| 17 | + // Reset all static properties |
| 18 | + Terminal.setShellIntegrationTimeout(5000) |
| 19 | + Terminal.setCommandDelay(0) |
| 20 | + Terminal.setTerminalZshClearEolMark(true) |
| 21 | + Terminal.setTerminalZshOhMy(false) |
| 22 | + Terminal.setTerminalZshP10k(false) |
| 23 | + Terminal.setTerminalZdotdir(false) |
| 24 | + }) |
| 25 | + |
| 26 | + describe("getEnv", () => { |
| 27 | + it("should include UTF-8 encoding environment variables", () => { |
| 28 | + const env = Terminal.getEnv() |
| 29 | + |
| 30 | + expect(env.LANG).toBe("en_US.UTF-8") |
| 31 | + expect(env.LC_ALL).toBe("en_US.UTF-8") |
| 32 | + }) |
| 33 | + |
| 34 | + it("should include VTE_VERSION and PAGER", () => { |
| 35 | + const env = Terminal.getEnv() |
| 36 | + |
| 37 | + expect(env.VTE_VERSION).toBe("0") |
| 38 | + expect(env.PAGER).toBe(process.platform === "win32" ? "" : "cat") |
| 39 | + }) |
| 40 | + |
| 41 | + it("should handle Oh My Zsh configuration", () => { |
| 42 | + Terminal.setTerminalZshOhMy(true) |
| 43 | + const env = Terminal.getEnv() |
| 44 | + |
| 45 | + expect(env.ITERM_SHELL_INTEGRATION_INSTALLED).toBe("Yes") |
| 46 | + }) |
| 47 | + |
| 48 | + it("should handle Powerlevel10k configuration", () => { |
| 49 | + Terminal.setTerminalZshP10k(true) |
| 50 | + const env = Terminal.getEnv() |
| 51 | + |
| 52 | + expect(env.POWERLEVEL9K_TERM_SHELL_INTEGRATION).toBe("true") |
| 53 | + }) |
| 54 | + |
| 55 | + it("should handle command delay configuration", () => { |
| 56 | + Terminal.setCommandDelay(100) |
| 57 | + const env = Terminal.getEnv() |
| 58 | + |
| 59 | + expect(env.PROMPT_COMMAND).toBe("sleep 0.1") |
| 60 | + }) |
| 61 | + |
| 62 | + it("should clear EOL mark by default", () => { |
| 63 | + const env = Terminal.getEnv() |
| 64 | + |
| 65 | + expect(env.PROMPT_EOL_MARK).toBe("") |
| 66 | + }) |
| 67 | + |
| 68 | + it("should not clear EOL mark when disabled", () => { |
| 69 | + Terminal.setTerminalZshClearEolMark(false) |
| 70 | + const env = Terminal.getEnv() |
| 71 | + |
| 72 | + expect(env.PROMPT_EOL_MARK).toBeUndefined() |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + describe("Terminal creation", () => { |
| 77 | + it("should create terminal with UTF-8 environment", () => { |
| 78 | + const mockTerminal = { shellIntegration: undefined } |
| 79 | + vi.mocked(vscode.window.createTerminal).mockReturnValue(mockTerminal as any) |
| 80 | + |
| 81 | + const terminal = new Terminal(1, undefined, "/test/path") |
| 82 | + |
| 83 | + expect(vscode.window.createTerminal).toHaveBeenCalledWith({ |
| 84 | + cwd: "/test/path", |
| 85 | + name: "Roo Code", |
| 86 | + iconPath: expect.objectContaining({ icon: "rocket" }), |
| 87 | + env: expect.objectContaining({ |
| 88 | + LANG: "en_US.UTF-8", |
| 89 | + LC_ALL: "en_US.UTF-8", |
| 90 | + }), |
| 91 | + }) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + describe("UTF-8 command handling", () => { |
| 96 | + it("should properly handle commands with UTF-8 characters", async () => { |
| 97 | + const mockTerminal = { |
| 98 | + shellIntegration: { |
| 99 | + executeCommand: vi.fn(), |
| 100 | + cwd: { fsPath: "/test/path" }, |
| 101 | + }, |
| 102 | + exitStatus: undefined, |
| 103 | + } |
| 104 | + vi.mocked(vscode.window.createTerminal).mockReturnValue(mockTerminal as any) |
| 105 | + |
| 106 | + const terminal = new Terminal(1, undefined, "/test/path") |
| 107 | + |
| 108 | + // Test that the terminal is created with proper UTF-8 environment |
| 109 | + const env = Terminal.getEnv() |
| 110 | + expect(env.LANG).toBe("en_US.UTF-8") |
| 111 | + expect(env.LC_ALL).toBe("en_US.UTF-8") |
| 112 | + |
| 113 | + // Verify that environment ensures proper encoding for commands with special characters |
| 114 | + const testCommands = [ |
| 115 | + 'python -c "print(\\" → foo\\")"', |
| 116 | + 'echo "→ arrow"', |
| 117 | + 'echo "λ lambda"', |
| 118 | + 'echo "中文"', |
| 119 | + 'echo "émoji 😀"', |
| 120 | + ] |
| 121 | + |
| 122 | + // The UTF-8 environment should be set properly for handling these commands |
| 123 | + for (const cmd of testCommands) { |
| 124 | + // The terminal should be able to handle UTF-8 commands without issues |
| 125 | + expect(() => { |
| 126 | + // This validates that the environment is properly configured |
| 127 | + const encodedCmd = Buffer.from(cmd, "utf8").toString("utf8") |
| 128 | + expect(encodedCmd).toBe(cmd) |
| 129 | + }).not.toThrow() |
| 130 | + } |
| 131 | + }) |
| 132 | + }) |
| 133 | +}) |
0 commit comments