|
| 1 | +/** |
| 2 | + * Tests for the terminal shell selection settings and message contracts. |
| 3 | + * |
| 4 | + * Validates: |
| 5 | + * - `terminalShellSelection` is optional and older settings import unchanged |
| 6 | + * - Discriminated shape validation (auto, profile, path) |
| 7 | + * - Legacy `execaShellPath` remains readable |
| 8 | + * - Message payload types compile correctly |
| 9 | + */ |
| 10 | +import { describe, it, expect } from "vitest" |
| 11 | + |
| 12 | +import { |
| 13 | + globalSettingsSchema, |
| 14 | + terminalShellSelectionSchema, |
| 15 | + type GlobalSettings, |
| 16 | + type TerminalShellSelection, |
| 17 | +} from "../global-settings.js" |
| 18 | + |
| 19 | +import type { |
| 20 | + ExtensionMessage, |
| 21 | + WebviewMessage, |
| 22 | + TerminalShellOption, |
| 23 | + TerminalShellOptionsPayload, |
| 24 | +} from "../vscode-extension-host.js" |
| 25 | + |
| 26 | +describe("terminalShellSelectionSchema", () => { |
| 27 | + // ── Discriminated union validation ────────────────────────────────── |
| 28 | + |
| 29 | + describe("auto mode", () => { |
| 30 | + it("should parse { kind: 'auto' }", () => { |
| 31 | + const result = terminalShellSelectionSchema.parse({ kind: "auto" }) |
| 32 | + expect(result).toEqual({ kind: "auto" }) |
| 33 | + }) |
| 34 | + |
| 35 | + it("should strip extra fields on auto variant", () => { |
| 36 | + // Zod discriminated union objects are non-strict by default; |
| 37 | + // extra keys are stripped rather than rejected. |
| 38 | + const result = terminalShellSelectionSchema.parse({ |
| 39 | + kind: "auto", |
| 40 | + path: "/bin/sh", |
| 41 | + }) |
| 42 | + expect(result).toEqual({ kind: "auto" }) |
| 43 | + expect(result).not.toHaveProperty("path") |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + describe("profile mode", () => { |
| 48 | + it("should parse { kind: 'profile', profileName: 'PowerShell' }", () => { |
| 49 | + const result = terminalShellSelectionSchema.parse({ |
| 50 | + kind: "profile", |
| 51 | + profileName: "PowerShell", |
| 52 | + }) |
| 53 | + expect(result).toEqual({ kind: "profile", profileName: "PowerShell" }) |
| 54 | + }) |
| 55 | + |
| 56 | + it("should reject profile without profileName", () => { |
| 57 | + expect(() => terminalShellSelectionSchema.parse({ kind: "profile" })).toThrow() |
| 58 | + }) |
| 59 | + |
| 60 | + it("should reject profile with empty profileName", () => { |
| 61 | + expect(() => terminalShellSelectionSchema.parse({ kind: "profile", profileName: "" })).not.toThrow() // z.string() accepts empty; validation is extension-host responsibility |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + describe("path mode", () => { |
| 66 | + it("should parse { kind: 'path', path: 'C:\\\\System32\\\\WindowsPowerShell\\\\v1.0\\\\powershell.exe' }", () => { |
| 67 | + const result = terminalShellSelectionSchema.parse({ |
| 68 | + kind: "path", |
| 69 | + path: "C:\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", |
| 70 | + }) |
| 71 | + expect(result.kind).toBe("path") |
| 72 | + if (result.kind === "path") { |
| 73 | + expect(result.path).toContain("powershell.exe") |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + it("should reject path without path field", () => { |
| 78 | + expect(() => terminalShellSelectionSchema.parse({ kind: "path" })).toThrow() |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + describe("invalid discriminated shapes", () => { |
| 83 | + it("should reject unknown kind", () => { |
| 84 | + expect(() => terminalShellSelectionSchema.parse({ kind: "unknown" })).toThrow() |
| 85 | + }) |
| 86 | + |
| 87 | + it("should reject missing kind", () => { |
| 88 | + expect(() => terminalShellSelectionSchema.parse({})).toThrow() |
| 89 | + }) |
| 90 | + |
| 91 | + it("should reject null", () => { |
| 92 | + expect(() => terminalShellSelectionSchema.parse(null)).toThrow() |
| 93 | + }) |
| 94 | + |
| 95 | + it("should reject non-object", () => { |
| 96 | + expect(() => terminalShellSelectionSchema.parse("auto")).toThrow() |
| 97 | + }) |
| 98 | + }) |
| 99 | +}) |
| 100 | + |
| 101 | +describe("globalSettingsSchema — terminalShellSelection", () => { |
| 102 | + // ── Optionality and backward compatibility ────────────────────────── |
| 103 | + |
| 104 | + it("should accept settings without terminalShellSelection (backward compat)", () => { |
| 105 | + const legacySettings = { |
| 106 | + terminalProfile: "PowerShell", |
| 107 | + execaShellPath: "/bin/bash", |
| 108 | + } |
| 109 | + const result = globalSettingsSchema.parse(legacySettings) |
| 110 | + expect(result.terminalShellSelection).toBeUndefined() |
| 111 | + expect(result.execaShellPath).toBe("/bin/bash") |
| 112 | + expect(result.terminalProfile).toBe("PowerShell") |
| 113 | + }) |
| 114 | + |
| 115 | + it("should accept settings with terminalShellSelection auto", () => { |
| 116 | + const result = globalSettingsSchema.parse({ |
| 117 | + terminalShellSelection: { kind: "auto" }, |
| 118 | + }) |
| 119 | + expect(result.terminalShellSelection).toEqual({ kind: "auto" }) |
| 120 | + }) |
| 121 | + |
| 122 | + it("should accept settings with terminalShellSelection profile", () => { |
| 123 | + const result = globalSettingsSchema.parse({ |
| 124 | + terminalShellSelection: { kind: "profile", profileName: "Git Bash" }, |
| 125 | + }) |
| 126 | + expect(result.terminalShellSelection).toEqual({ |
| 127 | + kind: "profile", |
| 128 | + profileName: "Git Bash", |
| 129 | + }) |
| 130 | + }) |
| 131 | + |
| 132 | + it("should accept settings with terminalShellSelection path", () => { |
| 133 | + const result = globalSettingsSchema.parse({ |
| 134 | + terminalShellSelection: { kind: "path", path: "/usr/bin/fish" }, |
| 135 | + }) |
| 136 | + expect(result.terminalShellSelection).toEqual({ |
| 137 | + kind: "path", |
| 138 | + path: "/usr/bin/fish", |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + it("should reject settings with invalid terminalShellSelection shape", () => { |
| 143 | + expect(() => |
| 144 | + globalSettingsSchema.parse({ |
| 145 | + terminalShellSelection: { kind: "invalid" }, |
| 146 | + }), |
| 147 | + ).toThrow() |
| 148 | + }) |
| 149 | + |
| 150 | + it("should allow both terminalShellSelection and legacy execaShellPath", () => { |
| 151 | + const result = globalSettingsSchema.parse({ |
| 152 | + terminalShellSelection: { kind: "auto" }, |
| 153 | + execaShellPath: "/bin/zsh", |
| 154 | + }) |
| 155 | + expect(result.terminalShellSelection).toEqual({ kind: "auto" }) |
| 156 | + expect(result.execaShellPath).toBe("/bin/zsh") |
| 157 | + }) |
| 158 | + |
| 159 | + // ── Legacy field readability ──────────────────────────────────────── |
| 160 | + |
| 161 | + it("should keep execaShellPath readable when present", () => { |
| 162 | + const result = globalSettingsSchema.parse({ |
| 163 | + execaShellPath: "C:\\Program Files\\PowerShell\\7\\pwsh.exe", |
| 164 | + }) |
| 165 | + expect(result.execaShellPath).toBe("C:\\Program Files\\PowerShell\\7\\pwsh.exe") |
| 166 | + }) |
| 167 | + |
| 168 | + it("should keep execaShellPath undefined when absent", () => { |
| 169 | + const result = globalSettingsSchema.parse({}) |
| 170 | + expect(result.execaShellPath).toBeUndefined() |
| 171 | + }) |
| 172 | +}) |
| 173 | + |
| 174 | +describe("message payload type compilation", () => { |
| 175 | + // ── Type-level compile checks (runtime no-ops) ────────────────────── |
| 176 | + // These tests verify that the message payload types are correctly |
| 177 | + // typed and can carry the expected data shapes. |
| 178 | + |
| 179 | + it("TerminalShellOption should have all required fields", () => { |
| 180 | + const option: TerminalShellOption = { |
| 181 | + id: "auto", |
| 182 | + label: "Auto (follow default profile)", |
| 183 | + family: "powershell", |
| 184 | + source: "os-default", |
| 185 | + available: true, |
| 186 | + } |
| 187 | + expect(option.id).toBe("auto") |
| 188 | + expect(option.label).toBe("Auto (follow default profile)") |
| 189 | + expect(option.family).toBe("powershell") |
| 190 | + expect(option.source).toBe("os-default") |
| 191 | + expect(option.available).toBe(true) |
| 192 | + }) |
| 193 | + |
| 194 | + it("TerminalShellOption family should accept all valid families", () => { |
| 195 | + const families: TerminalShellOption["family"][] = ["powershell", "cmd", "posix", "fish", "wsl"] |
| 196 | + families.forEach((family) => { |
| 197 | + const option: TerminalShellOption = { |
| 198 | + id: `test-${family}`, |
| 199 | + label: family, |
| 200 | + family, |
| 201 | + source: "test", |
| 202 | + available: true, |
| 203 | + } |
| 204 | + expect(option.family).toBe(family) |
| 205 | + }) |
| 206 | + }) |
| 207 | + |
| 208 | + it("TerminalShellOptionsPayload should carry options and effectiveShell", () => { |
| 209 | + const payload: TerminalShellOptionsPayload = { |
| 210 | + options: [ |
| 211 | + { |
| 212 | + id: "auto", |
| 213 | + label: "Auto", |
| 214 | + family: "powershell", |
| 215 | + source: "os-default", |
| 216 | + available: true, |
| 217 | + }, |
| 218 | + { |
| 219 | + id: "profile:PowerShell", |
| 220 | + label: "PowerShell", |
| 221 | + family: "powershell", |
| 222 | + source: "vscode-default", |
| 223 | + available: true, |
| 224 | + }, |
| 225 | + ], |
| 226 | + effectiveShell: { |
| 227 | + label: "PowerShell 7 (pwsh.exe)", |
| 228 | + family: "powershell", |
| 229 | + source: "vscode-default", |
| 230 | + }, |
| 231 | + } |
| 232 | + expect(payload.options).toHaveLength(2) |
| 233 | + expect(payload.effectiveShell?.family).toBe("powershell") |
| 234 | + }) |
| 235 | + |
| 236 | + it("TerminalShellOptionsPayload should allow error without effectiveShell", () => { |
| 237 | + const payload: TerminalShellOptionsPayload = { |
| 238 | + options: [], |
| 239 | + error: "SHELL/terminalShellOptions/001: profile discovery failed", |
| 240 | + } |
| 241 | + expect(payload.options).toHaveLength(0) |
| 242 | + expect(payload.error).toBeDefined() |
| 243 | + }) |
| 244 | + |
| 245 | + it("WebviewMessage should carry terminalShellSelection for setTerminalShellSelection", () => { |
| 246 | + const msg: WebviewMessage = { |
| 247 | + type: "setTerminalShellSelection", |
| 248 | + terminalShellSelection: { kind: "profile", profileName: "PowerShell" }, |
| 249 | + } |
| 250 | + expect(msg.type).toBe("setTerminalShellSelection") |
| 251 | + expect(msg.terminalShellSelection?.kind).toBe("profile") |
| 252 | + }) |
| 253 | + |
| 254 | + it("WebviewMessage should carry requestTerminalShellOptions without payload", () => { |
| 255 | + const msg: WebviewMessage = { |
| 256 | + type: "requestTerminalShellOptions", |
| 257 | + } |
| 258 | + expect(msg.type).toBe("requestTerminalShellOptions") |
| 259 | + expect(msg.terminalShellSelection).toBeUndefined() |
| 260 | + }) |
| 261 | + |
| 262 | + it("ExtensionMessage should carry terminalShellOptions response", () => { |
| 263 | + const msg: ExtensionMessage = { |
| 264 | + type: "terminalShellOptions", |
| 265 | + terminalShellOptions: { |
| 266 | + options: [ |
| 267 | + { |
| 268 | + id: "auto", |
| 269 | + label: "Auto", |
| 270 | + family: "posix", |
| 271 | + source: "os-default", |
| 272 | + available: true, |
| 273 | + }, |
| 274 | + ], |
| 275 | + effectiveShell: { |
| 276 | + label: "/bin/bash", |
| 277 | + family: "posix", |
| 278 | + source: "os-default", |
| 279 | + }, |
| 280 | + }, |
| 281 | + } |
| 282 | + expect(msg.type).toBe("terminalShellOptions") |
| 283 | + expect(msg.terminalShellOptions?.options).toHaveLength(1) |
| 284 | + }) |
| 285 | + |
| 286 | + it("TerminalShellSelection type should narrow correctly", () => { |
| 287 | + const pathSelection: TerminalShellSelection = { kind: "path", path: "/bin/zsh" } |
| 288 | + if (pathSelection.kind === "path") { |
| 289 | + // TypeScript narrows to the path variant |
| 290 | + expect(pathSelection.path).toBe("/bin/zsh") |
| 291 | + } |
| 292 | + |
| 293 | + const profileSelection: TerminalShellSelection = { |
| 294 | + kind: "profile", |
| 295 | + profileName: "PowerShell", |
| 296 | + } |
| 297 | + if (profileSelection.kind === "profile") { |
| 298 | + expect(profileSelection.profileName).toBe("PowerShell") |
| 299 | + } |
| 300 | + |
| 301 | + const autoSelection: TerminalShellSelection = { kind: "auto" } |
| 302 | + if (autoSelection.kind === "auto") { |
| 303 | + expect(autoSelection.kind).toBe("auto") |
| 304 | + } |
| 305 | + }) |
| 306 | + |
| 307 | + it("GlobalSettings should include terminalShellSelection as optional", () => { |
| 308 | + const settings: GlobalSettings = {} |
| 309 | + expect(settings.terminalShellSelection).toBeUndefined() |
| 310 | + |
| 311 | + const settingsWithSelection: GlobalSettings = { |
| 312 | + terminalShellSelection: { kind: "auto" }, |
| 313 | + } |
| 314 | + expect(settingsWithSelection.terminalShellSelection).toEqual({ kind: "auto" }) |
| 315 | + }) |
| 316 | +}) |
0 commit comments