|
| 1 | +import { describe, expect, it, vi, beforeEach } from "vitest"; |
| 2 | + |
| 3 | +const resolveWindowsSpawnProgramMock = vi.hoisted(() => vi.fn()); |
| 4 | +const materializeWindowsSpawnProgramMock = vi.hoisted(() => vi.fn()); |
| 5 | +const sanitizeHostExecEnvMock = vi.hoisted(() => vi.fn()); |
| 6 | +const spawnMock = vi.hoisted(() => vi.fn()); |
| 7 | + |
| 8 | +vi.mock("../plugin-sdk/windows-spawn.js", () => ({ |
| 9 | + resolveWindowsSpawnProgram: resolveWindowsSpawnProgramMock, |
| 10 | + materializeWindowsSpawnProgram: materializeWindowsSpawnProgramMock, |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("../infra/host-env-security.js", () => ({ |
| 14 | + sanitizeHostExecEnv: sanitizeHostExecEnvMock, |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("node:child_process", async () => ({ |
| 18 | + ...(await vi.importActual<typeof import("node:child_process")>("node:child_process")), |
| 19 | + spawn: spawnMock, |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock("../logger.js", () => ({ |
| 23 | + logDebug: vi.fn(), |
| 24 | + logWarn: vi.fn(), |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock("../process/kill-tree.js", () => ({ |
| 28 | + killProcessTree: vi.fn(), |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock("./embedded-pi-lsp.js", () => ({ |
| 32 | + loadEmbeddedPiLspConfig: vi.fn().mockReturnValue({ lspServers: {}, diagnostics: [] }), |
| 33 | +})); |
| 34 | + |
| 35 | +const FAKE_CHILD = { |
| 36 | + stdout: { setEncoding: vi.fn(), on: vi.fn() }, |
| 37 | + stderr: { setEncoding: vi.fn(), on: vi.fn() }, |
| 38 | + on: vi.fn(), |
| 39 | + pid: 1234, |
| 40 | +} as unknown as import("node:child_process").ChildProcess; |
| 41 | + |
| 42 | +describe("spawnLspServerProcess Windows .cmd shim handling", () => { |
| 43 | + beforeEach(() => { |
| 44 | + vi.clearAllMocks(); |
| 45 | + spawnMock.mockReturnValue(FAKE_CHILD); |
| 46 | + }); |
| 47 | + |
| 48 | + it("calls sanitizeHostExecEnv with baseEnv/overrides, not a flat merged object", async () => { |
| 49 | + const configEnv = { MY_TOKEN: "secret", TOOL_PATH: "/custom" }; |
| 50 | + const sanitizedEnv = { PATH: "/usr/bin", MY_TOKEN: "secret", TOOL_PATH: "/custom" }; |
| 51 | + |
| 52 | + sanitizeHostExecEnvMock.mockReturnValue(sanitizedEnv); |
| 53 | + resolveWindowsSpawnProgramMock.mockReturnValue({ resolvedCommand: "tls", isShim: false }); |
| 54 | + materializeWindowsSpawnProgramMock.mockReturnValue({ |
| 55 | + command: "typescript-language-server", |
| 56 | + argv: ["--stdio"], |
| 57 | + shell: false, |
| 58 | + windowsHide: true, |
| 59 | + }); |
| 60 | + |
| 61 | + const { spawnLspServerProcess } = await import("./pi-bundle-lsp-runtime.js"); |
| 62 | + spawnLspServerProcess({ command: "typescript-language-server", args: ["--stdio"], env: configEnv }); |
| 63 | + |
| 64 | + // Must use structured params so config.env entries are not dropped |
| 65 | + expect(sanitizeHostExecEnvMock).toHaveBeenCalledWith( |
| 66 | + expect.objectContaining({ baseEnv: process.env, overrides: configEnv }), |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + it("passes sanitized env to resolveWindowsSpawnProgram", async () => { |
| 71 | + const sanitizedEnv = { PATH: "C:\\Windows;C:\\nodejs", PATHEXT: ".COM;.EXE;.BAT;.CMD" }; |
| 72 | + |
| 73 | + sanitizeHostExecEnvMock.mockReturnValue(sanitizedEnv); |
| 74 | + resolveWindowsSpawnProgramMock.mockReturnValue({ resolvedCommand: "tls", isShim: false }); |
| 75 | + materializeWindowsSpawnProgramMock.mockReturnValue({ |
| 76 | + command: "typescript-language-server", |
| 77 | + argv: ["--stdio"], |
| 78 | + shell: false, |
| 79 | + windowsHide: true, |
| 80 | + }); |
| 81 | + |
| 82 | + const { spawnLspServerProcess } = await import("./pi-bundle-lsp-runtime.js"); |
| 83 | + spawnLspServerProcess({ command: "typescript-language-server", args: ["--stdio"] }); |
| 84 | + |
| 85 | + expect(resolveWindowsSpawnProgramMock).toHaveBeenCalledWith( |
| 86 | + expect.objectContaining({ env: sanitizedEnv, allowShellFallback: true }), |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it("passes materialized invocation to spawn with the sanitized env", async () => { |
| 91 | + const sanitizedEnv = { PATH: "/usr/bin" }; |
| 92 | + |
| 93 | + sanitizeHostExecEnvMock.mockReturnValue(sanitizedEnv); |
| 94 | + resolveWindowsSpawnProgramMock.mockReturnValue({ resolvedCommand: "tls", isShim: true }); |
| 95 | + materializeWindowsSpawnProgramMock.mockReturnValue({ |
| 96 | + command: "cmd.exe", |
| 97 | + argv: ["/c", "typescript-language-server.cmd", "--stdio"], |
| 98 | + shell: true, |
| 99 | + windowsHide: true, |
| 100 | + }); |
| 101 | + |
| 102 | + const { spawnLspServerProcess } = await import("./pi-bundle-lsp-runtime.js"); |
| 103 | + spawnLspServerProcess({ command: "typescript-language-server", args: ["--stdio"] }); |
| 104 | + |
| 105 | + expect(spawnMock).toHaveBeenCalledWith( |
| 106 | + "cmd.exe", |
| 107 | + ["/c", "typescript-language-server.cmd", "--stdio"], |
| 108 | + expect.objectContaining({ env: sanitizedEnv, shell: true, windowsHide: true }), |
| 109 | + ); |
| 110 | + }); |
| 111 | +}); |
0 commit comments