-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmcp-provider.test.ts
More file actions
235 lines (185 loc) · 8.75 KB
/
mcp-provider.test.ts
File metadata and controls
235 lines (185 loc) · 8.75 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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { McpProvider } from '../../src/server/mcp-provider';
function createMockServerManager() {
return {
getCommand: vi.fn().mockReturnValue('npx'),
getArgs: vi.fn().mockReturnValue(['-y', 'codeql-development-mcp-server']),
getExtensionVersion: vi.fn().mockReturnValue('2.25.1'),
getVersion: vi.fn().mockReturnValue(undefined),
getDescription: vi.fn().mockReturnValue('npx -y codeql-development-mcp-server'),
getInstallDir: vi.fn().mockReturnValue('/mock/install'),
getPackageRoot: vi.fn().mockReturnValue('/mock/install/node_modules/codeql-development-mcp-server'),
isInstalled: vi.fn().mockResolvedValue(true),
getInstalledVersion: vi.fn().mockResolvedValue('2.24.1'),
ensureInstalled: vi.fn().mockResolvedValue(false),
install: vi.fn().mockResolvedValue(undefined),
dispose: vi.fn(),
push: vi.fn(),
} as any;
}
function createMockEnvBuilder() {
return {
build: vi.fn().mockResolvedValue({
CODEQL_PATH: '/usr/local/bin/codeql',
CODEQL_MCP_WORKSPACE: '/mock/workspace',
CODEQL_DATABASES_BASE_DIRS: '/mock/databases',
CODEQL_QUERY_RUN_RESULTS_DIRS: '/mock/queries',
CODEQL_MRVA_RUN_RESULTS_DIRS: '/mock/variant-analyses',
TRANSPORT_MODE: 'stdio',
}),
invalidate: vi.fn(),
dispose: vi.fn(),
push: vi.fn(),
} 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('McpProvider', () => {
let provider: McpProvider;
let serverManager: any;
let envBuilder: any;
let logger: any;
beforeEach(() => {
vi.resetAllMocks();
serverManager = createMockServerManager();
envBuilder = createMockEnvBuilder();
logger = createMockLogger();
provider = new McpProvider(serverManager, envBuilder, logger);
});
it('should be instantiable', () => {
expect(provider).toBeDefined();
});
it('should provide server definitions with npx command', async () => {
const token = { isCancellationRequested: false, onCancellationRequested: vi.fn() };
const definitions = await provider.provideMcpServerDefinitions(token as any);
expect(definitions).toBeDefined();
expect(definitions).toHaveLength(1);
expect(definitions![0]).toMatchObject({
label: 'ql-mcp',
command: 'npx',
args: ['-y', 'codeql-development-mcp-server'],
});
});
it('should include environment from envBuilder in the definition', async () => {
const token = { isCancellationRequested: false, onCancellationRequested: vi.fn() };
const definitions = await provider.provideMcpServerDefinitions(token as any);
expect(definitions![0].env).toEqual({
CODEQL_PATH: '/usr/local/bin/codeql',
CODEQL_MCP_WORKSPACE: '/mock/workspace',
CODEQL_DATABASES_BASE_DIRS: '/mock/databases',
CODEQL_QUERY_RUN_RESULTS_DIRS: '/mock/queries',
CODEQL_MRVA_RUN_RESULTS_DIRS: '/mock/variant-analyses',
TRANSPORT_MODE: 'stdio',
});
});
it('should pass discovery env vars through to the MCP server definition', async () => {
const token = { isCancellationRequested: false, onCancellationRequested: vi.fn() };
const definitions = await provider.provideMcpServerDefinitions(token as any);
const env = definitions![0].env;
expect(env.CODEQL_DATABASES_BASE_DIRS).toBe('/mock/databases');
expect(env.CODEQL_QUERY_RUN_RESULTS_DIRS).toBe('/mock/queries');
expect(env.CODEQL_MRVA_RUN_RESULTS_DIRS).toBe('/mock/variant-analyses');
});
it('should always provide a definition (npx handles download)', async () => {
serverManager.isInstalled.mockResolvedValue(false);
const token = { isCancellationRequested: false, onCancellationRequested: vi.fn() };
const definitions = await provider.provideMcpServerDefinitions(token as any);
expect(definitions).toHaveLength(1);
});
it('should always provide a defined version string even when serverVersion is latest', async () => {
// When serverManager.getVersion() returns undefined ("latest" mode),
// the definition must still carry a concrete version string so that
// VS Code has a baseline for version comparison. An undefined initial
// version prevents VS Code from detecting changes after requestRestart().
serverManager.getVersion.mockReturnValue(undefined);
serverManager.getExtensionVersion.mockReturnValue('2.25.1');
const token = { isCancellationRequested: false, onCancellationRequested: vi.fn() };
const definitions = await provider.provideMcpServerDefinitions(token as any);
expect(definitions![0].version).toBeDefined();
expect(typeof definitions![0].version).toBe('string');
expect(definitions![0].version!.length).toBeGreaterThan(0);
});
it('should pass version from serverManager when pinned', async () => {
serverManager.getVersion.mockReturnValue('2.20.0');
const token = { isCancellationRequested: false, onCancellationRequested: vi.fn() };
const definitions = await provider.provideMcpServerDefinitions(token as any);
expect(definitions![0].version).toBe('2.20.0');
});
it('should not change version on fireDidChange (soft signal)', async () => {
const token = { isCancellationRequested: false, onCancellationRequested: vi.fn() };
const before = await provider.provideMcpServerDefinitions(token as any);
const versionBefore = before![0].version;
provider.fireDidChange();
const after = await provider.provideMcpServerDefinitions(token as any);
expect(after![0].version).toBe(versionBefore);
});
it('should produce a different version after requestRestart', async () => {
const token = { isCancellationRequested: false, onCancellationRequested: vi.fn() };
const before = await provider.provideMcpServerDefinitions(token as any);
const versionBefore = before![0].version;
provider.requestRestart();
const after = await provider.provideMcpServerDefinitions(token as any);
const versionAfter = after![0].version;
expect(versionAfter).toBeDefined();
expect(versionAfter).not.toBe(versionBefore);
});
it('should produce distinct versions after successive requestRestart calls', async () => {
const token = { isCancellationRequested: false, onCancellationRequested: vi.fn() };
provider.requestRestart();
const defs1 = await provider.provideMcpServerDefinitions(token as any);
provider.requestRestart();
const defs2 = await provider.provideMcpServerDefinitions(token as any);
expect(defs1![0].version).not.toBe(defs2![0].version);
});
it('should append revision to pinned version after requestRestart', async () => {
serverManager.getVersion.mockReturnValue('2.20.0');
const token = { isCancellationRequested: false, onCancellationRequested: vi.fn() };
provider.requestRestart();
const definitions = await provider.provideMcpServerDefinitions(token as any);
expect(definitions![0].version).toMatch(/^2\.20\.0\+r\d+$/);
});
it('should append revision to extension version after requestRestart when version is latest', async () => {
serverManager.getVersion.mockReturnValue(undefined);
serverManager.getExtensionVersion.mockReturnValue('2.25.1');
const token = { isCancellationRequested: false, onCancellationRequested: vi.fn() };
provider.requestRestart();
const definitions = await provider.provideMcpServerDefinitions(token as any);
expect(definitions![0].version).toMatch(/^2\.25\.1\+r\d+$/);
});
it('should fire change event when requestRestart is called', () => {
const listener = vi.fn();
provider.onDidChangeMcpServerDefinitions(listener);
provider.requestRestart();
expect(listener).toHaveBeenCalledTimes(1);
});
it('should invalidate env cache when requestRestart is called', () => {
provider.requestRestart();
expect(envBuilder.invalidate).toHaveBeenCalledTimes(1);
});
it('should resolve definition by refreshing environment', async () => {
const updatedEnv = { CODEQL_PATH: '/new/path', TRANSPORT_MODE: 'stdio' };
envBuilder.build.mockResolvedValue(updatedEnv);
const serverDef = { label: 'ql-mcp', command: 'npx', args: [], env: {} } as any;
const token = { isCancellationRequested: false, onCancellationRequested: vi.fn() };
const resolved = await provider.resolveMcpServerDefinition(serverDef, token as any);
expect(resolved).toBeDefined();
expect(resolved!.env).toEqual(updatedEnv);
});
it('should expose onDidChangeMcpServerDefinitions event', () => {
expect(provider.onDidChangeMcpServerDefinitions).toBeDefined();
});
it('should fire change event when fireDidChange is called', () => {
expect(() => provider.fireDidChange()).not.toThrow();
});
it('should be disposable', () => {
expect(() => provider.dispose()).not.toThrow();
});
});