|
| 1 | +// npx vitest run src/integrations/terminal/__tests__/TerminalProfile.spec.ts |
| 2 | + |
| 3 | +import * as vscode from "vscode" |
| 4 | + |
| 5 | +import { Terminal } from "../Terminal" |
| 6 | +import { TerminalRegistry } from "../TerminalRegistry" |
| 7 | + |
| 8 | +vi.mock("execa", () => ({ |
| 9 | + execa: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +describe("Terminal inline terminal profile (#119)", () => { |
| 13 | + let getConfigurationSpy: ReturnType<typeof vi.spyOn> |
| 14 | + let createTerminalSpy: ReturnType<typeof vi.spyOn> |
| 15 | + |
| 16 | + const mockTerminal = () => |
| 17 | + ({ |
| 18 | + exitStatus: undefined, |
| 19 | + name: "Roo Code", |
| 20 | + processId: Promise.resolve(123), |
| 21 | + creationOptions: {}, |
| 22 | + state: { isInteractedWith: true }, |
| 23 | + dispose: vi.fn(), |
| 24 | + hide: vi.fn(), |
| 25 | + show: vi.fn(), |
| 26 | + sendText: vi.fn(), |
| 27 | + shellIntegration: { executeCommand: vi.fn() }, |
| 28 | + }) as any |
| 29 | + |
| 30 | + // Helper to stub `terminal.integrated.profiles.<platform>` config reads. |
| 31 | + const stubProfiles = (profilesByPlatform: Record<string, unknown>) => { |
| 32 | + getConfigurationSpy = vi.spyOn(vscode.workspace, "getConfiguration").mockImplementation((section?: string) => { |
| 33 | + if (section === "terminal.integrated.profiles") { |
| 34 | + return { |
| 35 | + get: (platformKey: string) => profilesByPlatform[platformKey], |
| 36 | + } as any |
| 37 | + } |
| 38 | + |
| 39 | + return { get: (_key: string, defaultValue?: unknown) => defaultValue } as any |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + createTerminalSpy = vi.spyOn(vscode.window, "createTerminal").mockImplementation(() => mockTerminal()) |
| 45 | + // Reset to default (unset) before each test. |
| 46 | + Terminal.setTerminalProfile(undefined) |
| 47 | + }) |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + Terminal.setTerminalProfile(undefined) |
| 51 | + vi.restoreAllMocks() |
| 52 | + }) |
| 53 | + |
| 54 | + describe("getTerminalProfile / setTerminalProfile", () => { |
| 55 | + it("defaults to undefined", () => { |
| 56 | + expect(Terminal.getTerminalProfile()).toBeUndefined() |
| 57 | + }) |
| 58 | + |
| 59 | + it("stores a profile name", () => { |
| 60 | + Terminal.setTerminalProfile("Git Bash") |
| 61 | + expect(Terminal.getTerminalProfile()).toBe("Git Bash") |
| 62 | + }) |
| 63 | + |
| 64 | + it("treats empty/whitespace strings as unset (default behavior)", () => { |
| 65 | + Terminal.setTerminalProfile("Git Bash") |
| 66 | + Terminal.setTerminalProfile("") |
| 67 | + expect(Terminal.getTerminalProfile()).toBeUndefined() |
| 68 | + |
| 69 | + Terminal.setTerminalProfile(" ") |
| 70 | + expect(Terminal.getTerminalProfile()).toBeUndefined() |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + describe("getProfileShell", () => { |
| 75 | + it("returns undefined when no profile is configured (default behavior preserved)", () => { |
| 76 | + stubProfiles({}) |
| 77 | + expect(Terminal.getProfileShell("win32")).toBeUndefined() |
| 78 | + }) |
| 79 | + |
| 80 | + it("resolves a Windows Git Bash profile to its shell path and args", () => { |
| 81 | + stubProfiles({ |
| 82 | + windows: { |
| 83 | + "Git Bash": { |
| 84 | + path: "C:\\Program Files\\Git\\bin\\bash.exe", |
| 85 | + args: ["--login", "-i"], |
| 86 | + }, |
| 87 | + }, |
| 88 | + }) |
| 89 | + |
| 90 | + Terminal.setTerminalProfile("Git Bash") |
| 91 | + |
| 92 | + expect(Terminal.getProfileShell("win32")).toEqual({ |
| 93 | + shellPath: "C:\\Program Files\\Git\\bin\\bash.exe", |
| 94 | + shellArgs: ["--login", "-i"], |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + it("uses the first path candidate when path is an array", () => { |
| 99 | + stubProfiles({ |
| 100 | + windows: { |
| 101 | + "Git Bash": { |
| 102 | + path: ["C:\\missing\\bash.exe", "C:\\Program Files\\Git\\bin\\bash.exe"], |
| 103 | + }, |
| 104 | + }, |
| 105 | + }) |
| 106 | + |
| 107 | + Terminal.setTerminalProfile("Git Bash") |
| 108 | + |
| 109 | + expect(Terminal.getProfileShell("win32")).toEqual({ |
| 110 | + shellPath: "C:\\missing\\bash.exe", |
| 111 | + shellArgs: undefined, |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + it("wraps a string args value into an array", () => { |
| 116 | + stubProfiles({ |
| 117 | + linux: { |
| 118 | + bash: { path: "/bin/bash", args: "-l" }, |
| 119 | + }, |
| 120 | + }) |
| 121 | + |
| 122 | + Terminal.setTerminalProfile("bash") |
| 123 | + |
| 124 | + expect(Terminal.getProfileShell("linux")).toEqual({ |
| 125 | + shellPath: "/bin/bash", |
| 126 | + shellArgs: ["-l"], |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + it("reads the osx profile section on darwin", () => { |
| 131 | + stubProfiles({ |
| 132 | + osx: { zsh: { path: "/bin/zsh" } }, |
| 133 | + }) |
| 134 | + |
| 135 | + Terminal.setTerminalProfile("zsh") |
| 136 | + |
| 137 | + expect(Terminal.getProfileShell("darwin")).toEqual({ |
| 138 | + shellPath: "/bin/zsh", |
| 139 | + shellArgs: undefined, |
| 140 | + }) |
| 141 | + }) |
| 142 | + |
| 143 | + it("falls back to default when the configured profile is not found", () => { |
| 144 | + stubProfiles({ windows: { PowerShell: { path: "pwsh.exe" } } }) |
| 145 | + |
| 146 | + Terminal.setTerminalProfile("Nonexistent") |
| 147 | + |
| 148 | + expect(Terminal.getProfileShell("win32")).toBeUndefined() |
| 149 | + }) |
| 150 | + |
| 151 | + it("falls back to default when the profile has no resolvable path (source-only profile)", () => { |
| 152 | + stubProfiles({ windows: { PowerShell: { source: "PowerShell" } } }) |
| 153 | + |
| 154 | + Terminal.setTerminalProfile("PowerShell") |
| 155 | + |
| 156 | + expect(Terminal.getProfileShell("win32")).toBeUndefined() |
| 157 | + }) |
| 158 | + }) |
| 159 | + |
| 160 | + describe("createTerminal integration", () => { |
| 161 | + afterEach(() => { |
| 162 | + TerminalRegistry["terminals"] = [] |
| 163 | + }) |
| 164 | + |
| 165 | + it("does NOT pass shellPath/shellArgs when no profile is configured", () => { |
| 166 | + stubProfiles({}) |
| 167 | + TerminalRegistry.createTerminal("/test/path", "vscode") |
| 168 | + |
| 169 | + const options = createTerminalSpy.mock.calls[0][0] as vscode.TerminalOptions |
| 170 | + expect(options.shellPath).toBeUndefined() |
| 171 | + expect(options.shellArgs).toBeUndefined() |
| 172 | + }) |
| 173 | + |
| 174 | + it("passes the resolved shellPath/shellArgs when a profile is configured", () => { |
| 175 | + stubProfiles({ |
| 176 | + [Terminal["getPlatformProfileKey"](process.platform)]: { |
| 177 | + "Git Bash": { path: "/usr/bin/bash", args: ["-i"] }, |
| 178 | + }, |
| 179 | + }) |
| 180 | + |
| 181 | + Terminal.setTerminalProfile("Git Bash") |
| 182 | + TerminalRegistry.createTerminal("/test/path", "vscode") |
| 183 | + |
| 184 | + const options = createTerminalSpy.mock.calls[0][0] as vscode.TerminalOptions |
| 185 | + expect(options.shellPath).toBe("/usr/bin/bash") |
| 186 | + expect(options.shellArgs).toEqual(["-i"]) |
| 187 | + }) |
| 188 | + }) |
| 189 | +}) |
0 commit comments