|
1 | 1 | import { JSONRPCMessage } from "../types.js"; |
2 | | -import { StdioClientTransport, StdioServerParameters, DEFAULT_INHERITED_ENV_VARS, getDefaultEnvironment } from "./stdio.js"; |
3 | | -import { AsyncLocalStorage } from "node:async_hooks"; |
| 2 | +import { StdioClientTransport, StdioServerParameters } from "./stdio.js"; |
4 | 3 |
|
5 | 4 | const serverParameters: StdioServerParameters = { |
6 | 5 | command: "/usr/bin/tee", |
7 | 6 | }; |
8 | 7 |
|
9 | | -const envAsyncLocalStorage = new AsyncLocalStorage<{ env: Record<string, string> }>(); |
10 | | - |
11 | | -jest.mock('cross-spawn', () => { |
12 | | - const originalSpawn = jest.requireActual('cross-spawn'); |
13 | | - return jest.fn((command, args, options) => { |
14 | | - const env = envAsyncLocalStorage.getStore(); |
15 | | - if (env) { |
16 | | - env.env = options.env; |
17 | | - } |
18 | | - return originalSpawn(command, args, options); |
19 | | - }); |
20 | | -}); |
21 | | - |
22 | 8 | test("should start then close cleanly", async () => { |
23 | 9 | const client = new StdioClientTransport(serverParameters); |
24 | 10 | client.onerror = (error) => { |
@@ -74,68 +60,11 @@ test("should read messages", async () => { |
74 | 60 | await client.close(); |
75 | 61 | }); |
76 | 62 |
|
77 | | - |
78 | | -test("should properly set default environment variables in spawned process", async () => { |
79 | | - await envAsyncLocalStorage.run({ env: {} }, async () => { |
| 63 | +test("should return child process pid", async () => { |
80 | 64 | const client = new StdioClientTransport(serverParameters); |
81 | 65 |
|
82 | 66 | await client.start(); |
| 67 | + expect(client.pid).not.toBeNull(); |
83 | 68 | await client.close(); |
84 | | - |
85 | | - // Get the default environment variables |
86 | | - const defaultEnv = getDefaultEnvironment(); |
87 | | - const spawnEnv = envAsyncLocalStorage.getStore()?.env; |
88 | | - expect(spawnEnv).toBeDefined(); |
89 | | - // Verify that all default environment variables are present |
90 | | - for (const key of DEFAULT_INHERITED_ENV_VARS) { |
91 | | - if (process.env[key] && !process.env[key].startsWith("()")) { |
92 | | - expect(spawnEnv).toHaveProperty(key); |
93 | | - expect(spawnEnv![key]).toBe(process.env[key]); |
94 | | - expect(spawnEnv![key]).toBe(defaultEnv[key]); |
95 | | - } |
96 | | - } |
97 | | - }); |
98 | | -}); |
99 | | - |
100 | | -test("should override default environment variables with custom ones", async () => { |
101 | | - await envAsyncLocalStorage.run({ env: {} }, async () => { |
102 | | - const customEnv = { |
103 | | - HOME: "/custom/home", |
104 | | - PATH: "/custom/path", |
105 | | - USER: "custom_user" |
106 | | - }; |
107 | | - |
108 | | - const client = new StdioClientTransport({ |
109 | | - ...serverParameters, |
110 | | - env: customEnv |
111 | | - }); |
112 | | - |
113 | | - await client.start(); |
114 | | - await client.close(); |
115 | | - |
116 | | - const spawnEnv = envAsyncLocalStorage.getStore()?.env; |
117 | | - expect(spawnEnv).toBeDefined(); |
118 | | - // Verify that custom environment variables override default ones |
119 | | - for (const [key, value] of Object.entries(customEnv)) { |
120 | | - expect(spawnEnv).toHaveProperty(key); |
121 | | - expect(spawnEnv![key]).toBe(value); |
122 | | - } |
123 | | - |
124 | | - // Verify that other default environment variables are still present |
125 | | - for (const key of DEFAULT_INHERITED_ENV_VARS) { |
126 | | - if (!(key in customEnv) && process.env[key] && !process.env[key].startsWith("()")) { |
127 | | - expect(spawnEnv).toHaveProperty(key); |
128 | | - expect(spawnEnv![key]).toBe(process.env[key]); |
129 | | - } |
130 | | - } |
131 | | - }); |
132 | | - |
133 | | - test("should return child process pid", async () => { |
134 | | - const client = new StdioClientTransport(serverParameters); |
135 | | - |
136 | | - await client.start(); |
137 | | - expect(client.pid).not.toBeNull(); |
138 | | - await client.close(); |
139 | | - expect(client.pid).toBeNull(); |
140 | | - }); |
| 69 | + expect(client.pid).toBeNull(); |
141 | 70 | }); |
0 commit comments