-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver-manager.test.ts
More file actions
287 lines (242 loc) · 9.47 KB
/
server-manager.test.ts
File metadata and controls
287 lines (242 loc) · 9.47 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { ServerManager } from '../../src/server/server-manager';
vi.mock('child_process', () => ({
execFile: vi.fn(),
}));
vi.mock('fs/promises', () => ({
access: vi.fn(),
readFile: vi.fn(),
mkdir: vi.fn(),
}));
vi.mock('fs', () => ({
accessSync: vi.fn(),
readFileSync: vi.fn(),
constants: { R_OK: 4 },
}));
import { execFile } from 'child_process';
import { access, readFile } from 'fs/promises';
import { accessSync, readFileSync } from 'fs';
function createMockContext(extensionVersion = '2.24.2') {
return {
extensionUri: { fsPath: '/mock/extension' },
globalStorageUri: { fsPath: '/mock/global-storage' },
extension: {
packageJSON: { version: extensionVersion },
},
globalState: {
get: vi.fn(),
update: vi.fn().mockResolvedValue(undefined),
keys: vi.fn().mockReturnValue([]),
setKeysForSync: vi.fn(),
},
subscriptions: [],
} as any;
}
function createMockLogger() {
return {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
show: vi.fn(),
dispose: vi.fn(),
} as any;
}
describe('ServerManager', () => {
let manager: ServerManager;
let ctx: any;
let logger: any;
beforeEach(() => {
vi.resetAllMocks();
ctx = createMockContext();
logger = createMockLogger();
manager = new ServerManager(ctx, logger);
});
it('should be instantiable', () => {
expect(manager).toBeDefined();
});
it('should compute install directory under globalStorageUri', () => {
const dir = manager.getInstallDir();
expect(dir).toBe('/mock/global-storage/mcp-server');
});
it('should compute package root under install dir', () => {
const root = manager.getPackageRoot();
expect(root).toBe(
'/mock/global-storage/mcp-server/node_modules/codeql-development-mcp-server',
);
});
it('should detect when package is not installed', async () => {
vi.mocked(access).mockRejectedValue(new Error('ENOENT'));
const installed = await manager.isInstalled();
expect(installed).toBe(false);
});
it('should detect when package is installed', async () => {
vi.mocked(access).mockResolvedValue(undefined);
vi.mocked(readFile).mockResolvedValue(JSON.stringify({ version: '2.24.1' }));
const installed = await manager.isInstalled();
expect(installed).toBe(true);
});
it('should default command to npx when bundle is missing', () => {
vi.mocked(accessSync).mockImplementation(() => { throw new Error('ENOENT'); });
expect(manager.getCommand()).toBe('npx');
});
it('should default args to npx -y codeql-development-mcp-server when bundle is missing', () => {
vi.mocked(accessSync).mockImplementation(() => { throw new Error('ENOENT'); });
const args = manager.getArgs();
expect(args).toEqual(['-y', 'codeql-development-mcp-server']);
});
it('should provide a human-readable description', () => {
vi.mocked(accessSync).mockImplementation(() => { throw new Error('ENOENT'); });
const desc = manager.getDescription();
expect(desc).toBe('npx -y codeql-development-mcp-server');
});
it('should use node with bundled server path when bundle exists', () => {
vi.mocked(accessSync).mockImplementation(() => undefined);
expect(manager.getCommand()).toBe('node');
expect(manager.getArgs()).toEqual(['/mock/extension/server/dist/codeql-development-mcp-server.js']);
});
it('should fall back to monorepo server path when VSIX bundle is missing', () => {
let callCount = 0;
vi.mocked(accessSync).mockImplementation(() => {
callCount++;
if (callCount === 1) throw new Error('ENOENT'); // VSIX path missing
return undefined; // monorepo path exists
});
expect(manager.getCommand()).toBe('node');
// monorepo path: /mock/extension/../../server/dist/...
const args = manager.getArgs();
expect(args[0]).toContain('server/dist/codeql-development-mcp-server.js');
});
it('should run npm install when installing', async () => {
vi.mocked(execFile).mockImplementation(
(_cmd: any, _args: any, _opts: any, callback: any) => {
if (typeof _opts === 'function') {
_opts(null, '', '');
} else if (callback) {
callback(null, '', '');
}
return {} as any;
},
);
vi.mocked(access).mockRejectedValue(new Error('ENOENT'));
await manager.install();
expect(execFile).toHaveBeenCalled();
// Should call npm with install --prefix <dir> <package>
const [cmd, args] = vi.mocked(execFile).mock.calls[0];
expect(cmd).toMatch(/npm/);
expect(args).toContain('install');
expect(args).toContain('--prefix');
});
it('should reject when npm install fails', async () => {
vi.mocked(execFile).mockImplementation(
(_cmd: any, _args: any, _opts: any, callback: any) => {
const cb = typeof _opts === 'function' ? _opts : callback;
cb(new Error('npm ERR! network'), '', 'npm ERR! network');
return {} as any;
},
);
await expect(manager.install()).rejects.toThrow(/npm install failed/);
expect(logger.error).toHaveBeenCalled();
});
it('should read installed version from package.json', async () => {
vi.mocked(readFile).mockResolvedValue(JSON.stringify({ version: '2.24.1' }));
const version = await manager.getInstalledVersion();
expect(version).toBe('2.24.1');
});
it('should return undefined for version when not installed', async () => {
vi.mocked(readFile).mockRejectedValue(new Error('ENOENT'));
const version = await manager.getInstalledVersion();
expect(version).toBeUndefined();
});
it('should return extension version from context', () => {
vi.mocked(readFileSync).mockReturnValue(JSON.stringify({ version: '2.24.2' }));
expect(manager.getExtensionVersion()).toBe('2.24.2');
});
describe('getBundledQlRoot', () => {
it('should return VSIX server/ path when bundle exists', () => {
vi.mocked(accessSync).mockImplementation(() => undefined);
expect(manager.getBundledQlRoot()).toBe('/mock/extension/server');
});
it('should fall back to monorepo server/ when VSIX bundle is missing', () => {
let callCount = 0;
vi.mocked(accessSync).mockImplementation(() => {
callCount++;
if (callCount === 1) throw new Error('ENOENT');
return undefined;
});
const root = manager.getBundledQlRoot();
expect(root).toContain('server');
});
it('should return undefined when no bundle exists', () => {
vi.mocked(accessSync).mockImplementation(() => { throw new Error('ENOENT'); });
expect(manager.getBundledQlRoot()).toBeUndefined();
});
});
describe('ensureInstalled', () => {
it('should skip npm install when VSIX bundle is present', async () => {
// Bundle exists
vi.mocked(accessSync).mockImplementation(() => undefined);
const installed = await manager.ensureInstalled();
expect(installed).toBe(false);
expect(execFile).not.toHaveBeenCalled();
expect(logger.info).toHaveBeenCalledWith(
expect.stringContaining('bundled server'),
);
});
it('should npm install when bundle is missing and nothing installed', async () => {
// No bundle
vi.mocked(accessSync).mockImplementation(() => { throw new Error('ENOENT'); });
// Not installed
vi.mocked(access).mockRejectedValue(new Error('ENOENT'));
// npm succeeds
vi.mocked(execFile).mockImplementation(
(_cmd: any, _args: any, _opts: any, callback: any) => {
const cb = typeof _opts === 'function' ? _opts : callback;
cb(null, '', '');
return {} as any;
},
);
const installed = await manager.ensureInstalled();
expect(installed).toBe(true);
expect(execFile).toHaveBeenCalled();
});
it('should skip npm install when bundle is missing but matching version installed', async () => {
// No bundle
vi.mocked(accessSync).mockImplementation(() => { throw new Error('ENOENT'); });
// Extension version from package.json
vi.mocked(readFileSync).mockReturnValue(JSON.stringify({ version: '2.24.2' }));
// Already installed with matching version
vi.mocked(access).mockResolvedValue(undefined);
vi.mocked(readFile).mockResolvedValue(JSON.stringify({ version: '2.24.2' }));
const installed = await manager.ensureInstalled();
expect(installed).toBe(false);
expect(execFile).not.toHaveBeenCalled();
});
it('should upgrade npm install when bundle is missing and version differs', async () => {
// No bundle
vi.mocked(accessSync).mockImplementation(() => { throw new Error('ENOENT'); });
// Old version installed
vi.mocked(access).mockResolvedValue(undefined);
vi.mocked(readFile).mockResolvedValue(JSON.stringify({ version: '2.24.1' }));
// npm succeeds
vi.mocked(execFile).mockImplementation(
(_cmd: any, _args: any, _opts: any, callback: any) => {
const cb = typeof _opts === 'function' ? _opts : callback;
cb(null, '', '');
return {} as any;
},
);
const installed = await manager.ensureInstalled();
expect(installed).toBe(true);
expect(logger.info).toHaveBeenCalledWith(
expect.stringContaining('differs from target'),
);
});
});
it('should return undefined version when using latest', () => {
expect(manager.getVersion()).toBeUndefined();
});
it('should be disposable', () => {
expect(() => manager.dispose()).not.toThrow();
});
});