diff --git a/src/client/cross-spawn.test.ts b/src/client/cross-spawn.test.ts index 724ec706..8480d94f 100644 --- a/src/client/cross-spawn.test.ts +++ b/src/client/cross-spawn.test.ts @@ -1,4 +1,4 @@ -import { getDefaultEnvironment, StdioClientTransport } from "./stdio.js"; +import { StdioClientTransport, getDefaultEnvironment } from "./stdio.js"; import spawn from "cross-spawn"; import { JSONRPCMessage } from "../types.js"; import { ChildProcess } from "node:child_process"; @@ -67,19 +67,37 @@ describe("StdioClientTransport using cross-spawn", () => { await transport.start(); - // verify environment variables are passed correctly + // verify environment variables are merged correctly expect(mockSpawn).toHaveBeenCalledWith( "test-command", [], expect.objectContaining({ env: { ...getDefaultEnvironment(), - ...customEnv, + ...customEnv } }) ); }); + test("should use default environment when env is undefined", async () => { + const transport = new StdioClientTransport({ + command: "test-command", + env: undefined + }); + + await transport.start(); + + // verify default environment is used + expect(mockSpawn).toHaveBeenCalledWith( + "test-command", + [], + expect.objectContaining({ + env: getDefaultEnvironment() + }) + ); + }); + test("should send messages correctly", async () => { const transport = new StdioClientTransport({ command: "test-command" diff --git a/src/client/stdio.test.ts b/src/client/stdio.test.ts index 3d76a819..b2132446 100644 --- a/src/client/stdio.test.ts +++ b/src/client/stdio.test.ts @@ -1,24 +1,10 @@ import { JSONRPCMessage } from "../types.js"; -import { StdioClientTransport, StdioServerParameters, DEFAULT_INHERITED_ENV_VARS, getDefaultEnvironment } from "./stdio.js"; -import { AsyncLocalStorage } from "node:async_hooks"; +import { StdioClientTransport, StdioServerParameters } from "./stdio.js"; const serverParameters: StdioServerParameters = { command: "/usr/bin/tee", }; -const envAsyncLocalStorage = new AsyncLocalStorage<{ env: Record }>(); - -jest.mock('cross-spawn', () => { - const originalSpawn = jest.requireActual('cross-spawn'); - return jest.fn((command, args, options) => { - const env = envAsyncLocalStorage.getStore(); - if (env) { - env.env = options.env; - } - return originalSpawn(command, args, options); - }); -}); - test("should start then close cleanly", async () => { const client = new StdioClientTransport(serverParameters); client.onerror = (error) => { @@ -74,68 +60,11 @@ test("should read messages", async () => { await client.close(); }); - -test("should properly set default environment variables in spawned process", async () => { - await envAsyncLocalStorage.run({ env: {} }, async () => { +test("should return child process pid", async () => { const client = new StdioClientTransport(serverParameters); await client.start(); + expect(client.pid).not.toBeNull(); await client.close(); - - // Get the default environment variables - const defaultEnv = getDefaultEnvironment(); - const spawnEnv = envAsyncLocalStorage.getStore()?.env; - expect(spawnEnv).toBeDefined(); - // Verify that all default environment variables are present - for (const key of DEFAULT_INHERITED_ENV_VARS) { - if (process.env[key] && !process.env[key].startsWith("()")) { - expect(spawnEnv).toHaveProperty(key); - expect(spawnEnv![key]).toBe(process.env[key]); - expect(spawnEnv![key]).toBe(defaultEnv[key]); - } - } - }); -}); - -test("should override default environment variables with custom ones", async () => { - await envAsyncLocalStorage.run({ env: {} }, async () => { - const customEnv = { - HOME: "/custom/home", - PATH: "/custom/path", - USER: "custom_user" - }; - - const client = new StdioClientTransport({ - ...serverParameters, - env: customEnv - }); - - await client.start(); - await client.close(); - - const spawnEnv = envAsyncLocalStorage.getStore()?.env; - expect(spawnEnv).toBeDefined(); - // Verify that custom environment variables override default ones - for (const [key, value] of Object.entries(customEnv)) { - expect(spawnEnv).toHaveProperty(key); - expect(spawnEnv![key]).toBe(value); - } - - // Verify that other default environment variables are still present - for (const key of DEFAULT_INHERITED_ENV_VARS) { - if (!(key in customEnv) && process.env[key] && !process.env[key].startsWith("()")) { - expect(spawnEnv).toHaveProperty(key); - expect(spawnEnv![key]).toBe(process.env[key]); - } - } - }); - - test("should return child process pid", async () => { - const client = new StdioClientTransport(serverParameters); - - await client.start(); - expect(client.pid).not.toBeNull(); - await client.close(); - expect(client.pid).toBeNull(); - }); + expect(client.pid).toBeNull(); });