-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathconfig.test.ts
More file actions
265 lines (224 loc) · 7.5 KB
/
config.test.ts
File metadata and controls
265 lines (224 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* Unit tests for config module
*/
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import {
loadConfig,
getServerConfig,
listServerNames,
isHttpServer,
isStdioServer,
} from '../src/config';
describe('config', () => {
let tempDir: string;
beforeEach(async () => {
tempDir = await mkdtemp(join(tmpdir(), 'mcp-cli-test-'));
});
afterEach(async () => {
await rm(tempDir, { recursive: true, force: true });
});
describe('loadConfig', () => {
test('loads valid config from explicit path', async () => {
const configPath = join(tempDir, 'mcp_servers.json');
await writeFile(
configPath,
JSON.stringify({
mcpServers: {
test: { command: 'echo', args: ['hello'] },
},
})
);
const config = await loadConfig(configPath);
expect(config.mcpServers.test).toBeDefined();
expect((config.mcpServers.test as any).command).toBe('echo');
});
test('throws on missing config file', async () => {
const configPath = join(tempDir, 'nonexistent.json');
await expect(loadConfig(configPath)).rejects.toThrow('not found');
});
test('throws on invalid JSON', async () => {
const configPath = join(tempDir, 'invalid.json');
await writeFile(configPath, 'not valid json');
await expect(loadConfig(configPath)).rejects.toThrow('Invalid JSON');
});
test('throws on missing mcpServers key', async () => {
const configPath = join(tempDir, 'bad_structure.json');
await writeFile(configPath, JSON.stringify({ servers: {} }));
await expect(loadConfig(configPath)).rejects.toThrow('mcpServers');
});
test('substitutes environment variables', async () => {
process.env.TEST_MCP_TOKEN = 'secret123';
const configPath = join(tempDir, 'env_config.json');
await writeFile(
configPath,
JSON.stringify({
mcpServers: {
test: {
url: 'https://example.com',
headers: { Authorization: 'Bearer ${TEST_MCP_TOKEN}' },
},
},
})
);
const config = await loadConfig(configPath);
const server = config.mcpServers.test as any;
expect(server.headers.Authorization).toBe('Bearer secret123');
delete process.env.TEST_MCP_TOKEN;
});
test('handles missing env vars gracefully with MCP_STRICT_ENV=false', async () => {
// Set non-strict mode to allow missing env vars with warning
process.env.MCP_STRICT_ENV = 'false';
const configPath = join(tempDir, 'missing_env.json');
await writeFile(
configPath,
JSON.stringify({
mcpServers: {
test: {
command: 'echo',
env: { TOKEN: '${NONEXISTENT_VAR}' },
},
},
})
);
const config = await loadConfig(configPath);
const server = config.mcpServers.test as any;
expect(server.env.TOKEN).toBe('');
delete process.env.MCP_STRICT_ENV;
});
test('treats MCP_STRICT_ENV with surrounding-CRLF leading-space uppercase true as strict mode', async () => {
process.env.MCP_STRICT_ENV = '\r\n TRUE\r\n';
const configPath = join(tempDir, 'missing_env_leading_space_uppercase_true_surrounded_crlf.json');
await writeFile(
configPath,
JSON.stringify({
mcpServers: {
test: {
command: 'echo',
env: { TOKEN: '${NONEXISTENT_VAR}' },
},
},
})
);
await expect(loadConfig(configPath)).rejects.toThrow('Missing environment variable');
delete process.env.MCP_STRICT_ENV;
});
test('throws error on missing env vars in strict mode (default)', async () => {
// Ensure strict mode is enabled (default)
delete process.env.MCP_STRICT_ENV;
const configPath = join(tempDir, 'missing_env_strict.json');
await writeFile(
configPath,
JSON.stringify({
mcpServers: {
test: {
command: 'echo',
env: { TOKEN: '${ANOTHER_NONEXISTENT_VAR}' },
},
},
})
);
await expect(loadConfig(configPath)).rejects.toThrow('MISSING_ENV_VAR');
});
test('throws error on empty server config', async () => {
const configPath = join(tempDir, 'empty_server.json');
await writeFile(
configPath,
JSON.stringify({
mcpServers: {
badserver: {},
},
})
);
await expect(loadConfig(configPath)).rejects.toThrow('missing required field');
});
test('throws error on server with both command and url', async () => {
const configPath = join(tempDir, 'both_types.json');
await writeFile(
configPath,
JSON.stringify({
mcpServers: {
mixed: {
command: 'echo',
url: 'https://example.com',
},
},
})
);
await expect(loadConfig(configPath)).rejects.toThrow('both "command" and "url"');
});
test('throws error on null server config', async () => {
const configPath = join(tempDir, 'null_server.json');
await writeFile(
configPath,
JSON.stringify({
mcpServers: {
nullserver: null,
},
})
);
await expect(loadConfig(configPath)).rejects.toThrow('Invalid server configuration');
});
});
describe('getServerConfig', () => {
test('returns server config by name', async () => {
const configPath = join(tempDir, 'config.json');
await writeFile(
configPath,
JSON.stringify({
mcpServers: {
server1: { command: 'cmd1' },
server2: { command: 'cmd2' },
},
})
);
const config = await loadConfig(configPath);
const server = getServerConfig(config, 'server1');
expect((server as any).command).toBe('cmd1');
});
test('throws on unknown server', async () => {
const configPath = join(tempDir, 'config.json');
await writeFile(
configPath,
JSON.stringify({
mcpServers: { known: { command: 'cmd' } },
})
);
const config = await loadConfig(configPath);
expect(() => getServerConfig(config, 'unknown')).toThrow('not found');
});
});
describe('listServerNames', () => {
test('returns all server names', async () => {
const configPath = join(tempDir, 'config.json');
await writeFile(
configPath,
JSON.stringify({
mcpServers: {
alpha: { command: 'a' },
beta: { command: 'b' },
gamma: { url: 'https://example.com' },
},
})
);
const config = await loadConfig(configPath);
const names = listServerNames(config);
expect(names).toContain('alpha');
expect(names).toContain('beta');
expect(names).toContain('gamma');
expect(names.length).toBe(3);
});
});
describe('type guards', () => {
test('isHttpServer identifies HTTP config', () => {
expect(isHttpServer({ url: 'https://example.com' })).toBe(true);
expect(isHttpServer({ command: 'echo' })).toBe(false);
});
test('isStdioServer identifies stdio config', () => {
expect(isStdioServer({ command: 'echo' })).toBe(true);
expect(isStdioServer({ url: 'https://example.com' })).toBe(false);
});
});
});