|
| 1 | +/** |
| 2 | + * Integration tests for workspace folder change handling. |
| 3 | + * |
| 4 | + * These run inside the Extension Development Host with the REAL VS Code API. |
| 5 | + * They verify that the MCP server definition version changes when workspace |
| 6 | + * folders are added or removed, which signals VS Code to restart the server |
| 7 | + * with the updated environment rather than simply stopping it. |
| 8 | + * |
| 9 | + * Bug: Prior to the fix, fireDidChange() notified VS Code that the MCP server |
| 10 | + * definitions changed, causing it to stop the running server. However, because |
| 11 | + * the returned McpStdioServerDefinition had the same version as before, VS Code |
| 12 | + * did not restart the server — it only stopped it. |
| 13 | + */ |
| 14 | + |
| 15 | +import * as assert from 'assert'; |
| 16 | +import * as fs from 'fs'; |
| 17 | +import * as os from 'os'; |
| 18 | +import * as path from 'path'; |
| 19 | +import * as vscode from 'vscode'; |
| 20 | + |
| 21 | +const EXTENSION_ID = 'advanced-security.vscode-codeql-development-mcp-server'; |
| 22 | + |
| 23 | +suite('Workspace Folder Change Tests', () => { |
| 24 | + let api: any; |
| 25 | + |
| 26 | + suiteSetup(async () => { |
| 27 | + const ext = vscode.extensions.getExtension(EXTENSION_ID); |
| 28 | + assert.ok(ext, `Extension ${EXTENSION_ID} not found`); |
| 29 | + api = ext.isActive ? ext.exports : await ext.activate(); |
| 30 | + }); |
| 31 | + |
| 32 | + test('MCP definition version should change after workspace folder is added', async function () { |
| 33 | + const provider = api.mcpProvider; |
| 34 | + if (!provider) { |
| 35 | + this.skip(); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const token: vscode.CancellationToken = { |
| 40 | + isCancellationRequested: false, |
| 41 | + onCancellationRequested: () => ({ dispose: () => {} }), |
| 42 | + }; |
| 43 | + |
| 44 | + // Get initial definitions |
| 45 | + const beforeDefs = await provider.provideMcpServerDefinitions(token); |
| 46 | + assert.ok(beforeDefs && beforeDefs.length >= 1, 'Should provide at least one definition'); |
| 47 | + const versionBefore = beforeDefs[0].version; |
| 48 | + |
| 49 | + // Create a real temporary directory to add as a workspace folder |
| 50 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-mcp-test-')); |
| 51 | + |
| 52 | + // Listen for the onDidChangeMcpServerDefinitions event. |
| 53 | + // Mocha's test timeout (60 s) handles the failure case. |
| 54 | + const changePromise = new Promise<void>((resolve) => { |
| 55 | + const disposable = provider.onDidChangeMcpServerDefinitions(() => { |
| 56 | + disposable.dispose(); |
| 57 | + resolve(); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + // Add the temporary folder to the workspace |
| 62 | + const folders = vscode.workspace.workspaceFolders ?? []; |
| 63 | + const added = vscode.workspace.updateWorkspaceFolders( |
| 64 | + folders.length, |
| 65 | + 0, |
| 66 | + { uri: vscode.Uri.file(tempDir) }, |
| 67 | + ); |
| 68 | + |
| 69 | + if (!added) { |
| 70 | + // Clean up and skip if updateWorkspaceFolders is not supported in this profile |
| 71 | + fs.rmdirSync(tempDir); |
| 72 | + this.skip(); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + // Wait for the MCP definition change event |
| 78 | + await changePromise; |
| 79 | + |
| 80 | + // Get definitions after the workspace folder change |
| 81 | + const afterDefs = await provider.provideMcpServerDefinitions(token); |
| 82 | + assert.ok(afterDefs && afterDefs.length >= 1, 'Should still provide definitions after folder change'); |
| 83 | + const versionAfter = afterDefs[0].version; |
| 84 | + |
| 85 | + // The version MUST be different so VS Code knows to restart the server |
| 86 | + assert.notStrictEqual( |
| 87 | + versionAfter, |
| 88 | + versionBefore, |
| 89 | + 'MCP server definition version must change after workspace folder update ' + |
| 90 | + 'to signal VS Code to restart the server instead of only stopping it', |
| 91 | + ); |
| 92 | + } finally { |
| 93 | + // Cleanup: remove the added folder and temp directory |
| 94 | + const updatedFolders = vscode.workspace.workspaceFolders ?? []; |
| 95 | + const idx = updatedFolders.findIndex((f) => f.uri.fsPath === tempDir); |
| 96 | + if (idx >= 0) { |
| 97 | + vscode.workspace.updateWorkspaceFolders(idx, 1); |
| 98 | + } |
| 99 | + try { |
| 100 | + fs.rmdirSync(tempDir); |
| 101 | + } catch { |
| 102 | + // Best-effort cleanup |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
| 106 | + |
| 107 | + test('MCP definition version should change after workspace folder is removed', async function () { |
| 108 | + const provider = api.mcpProvider; |
| 109 | + const folders = vscode.workspace.workspaceFolders; |
| 110 | + if (!provider || !folders || folders.length < 2) { |
| 111 | + // Need at least 2 folders to remove one without closing the workspace |
| 112 | + this.skip(); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + const token: vscode.CancellationToken = { |
| 117 | + isCancellationRequested: false, |
| 118 | + onCancellationRequested: () => ({ dispose: () => {} }), |
| 119 | + }; |
| 120 | + |
| 121 | + // Get initial definitions |
| 122 | + const beforeDefs = await provider.provideMcpServerDefinitions(token); |
| 123 | + assert.ok(beforeDefs && beforeDefs.length >= 1, 'Should provide at least one definition'); |
| 124 | + const versionBefore = beforeDefs[0].version; |
| 125 | + |
| 126 | + // Remember the last folder so we can re-add it after removal |
| 127 | + const lastFolder = folders[folders.length - 1]; |
| 128 | + const removedUri = lastFolder.uri; |
| 129 | + |
| 130 | + // Listen for the onDidChangeMcpServerDefinitions event. |
| 131 | + // Mocha's test timeout (60 s) handles the failure case. |
| 132 | + const changePromise = new Promise<void>((resolve) => { |
| 133 | + const disposable = provider.onDidChangeMcpServerDefinitions(() => { |
| 134 | + disposable.dispose(); |
| 135 | + resolve(); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + // Remove the last workspace folder |
| 140 | + const removed = vscode.workspace.updateWorkspaceFolders(folders.length - 1, 1); |
| 141 | + if (!removed) { |
| 142 | + this.skip(); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + try { |
| 147 | + // Wait for the MCP definition change event |
| 148 | + await changePromise; |
| 149 | + |
| 150 | + // Get definitions after the workspace folder change |
| 151 | + const afterDefs = await provider.provideMcpServerDefinitions(token); |
| 152 | + assert.ok(afterDefs && afterDefs.length >= 1, 'Should still provide definitions after folder removal'); |
| 153 | + const versionAfter = afterDefs[0].version; |
| 154 | + |
| 155 | + // The version MUST be different so VS Code knows to restart the server |
| 156 | + assert.notStrictEqual( |
| 157 | + versionAfter, |
| 158 | + versionBefore, |
| 159 | + 'MCP server definition version must change after workspace folder removal ' + |
| 160 | + 'to signal VS Code to restart the server instead of only stopping it', |
| 161 | + ); |
| 162 | + } finally { |
| 163 | + // Cleanup: re-add the removed folder |
| 164 | + const currentFolders = vscode.workspace.workspaceFolders ?? []; |
| 165 | + vscode.workspace.updateWorkspaceFolders( |
| 166 | + currentFolders.length, |
| 167 | + 0, |
| 168 | + { uri: removedUri }, |
| 169 | + ); |
| 170 | + } |
| 171 | + }); |
| 172 | + |
| 173 | + test('Environment should reflect updated workspace folders after change', async function () { |
| 174 | + const envBuilder = api.environmentBuilder; |
| 175 | + const folders = vscode.workspace.workspaceFolders; |
| 176 | + if (!envBuilder || !folders || folders.length === 0) { |
| 177 | + this.skip(); |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + // Create a real temporary directory |
| 182 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ql-mcp-env-test-')); |
| 183 | + |
| 184 | + // Listen for workspace folder changes through the VS Code API |
| 185 | + const changePromise = new Promise<void>((resolve) => { |
| 186 | + const disposable = vscode.workspace.onDidChangeWorkspaceFolders(() => { |
| 187 | + disposable.dispose(); |
| 188 | + resolve(); |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + // Add the temporary folder |
| 193 | + const added = vscode.workspace.updateWorkspaceFolders( |
| 194 | + folders.length, |
| 195 | + 0, |
| 196 | + { uri: vscode.Uri.file(tempDir) }, |
| 197 | + ); |
| 198 | + |
| 199 | + if (!added) { |
| 200 | + fs.rmdirSync(tempDir); |
| 201 | + this.skip(); |
| 202 | + return; |
| 203 | + } |
| 204 | + |
| 205 | + try { |
| 206 | + await changePromise; |
| 207 | + |
| 208 | + // invalidate() was called by the event handler, so build() returns fresh env |
| 209 | + const env = await envBuilder.build(); |
| 210 | + assert.ok(env.CODEQL_MCP_WORKSPACE_FOLDERS, 'CODEQL_MCP_WORKSPACE_FOLDERS should be set'); |
| 211 | + assert.ok( |
| 212 | + env.CODEQL_MCP_WORKSPACE_FOLDERS.includes(tempDir), |
| 213 | + `CODEQL_MCP_WORKSPACE_FOLDERS should include the newly added folder: ${env.CODEQL_MCP_WORKSPACE_FOLDERS}`, |
| 214 | + ); |
| 215 | + } finally { |
| 216 | + // Cleanup |
| 217 | + const updatedFolders = vscode.workspace.workspaceFolders ?? []; |
| 218 | + const idx = updatedFolders.findIndex((f) => f.uri.fsPath === tempDir); |
| 219 | + if (idx >= 0) { |
| 220 | + vscode.workspace.updateWorkspaceFolders(idx, 1); |
| 221 | + } |
| 222 | + try { |
| 223 | + fs.rmdirSync(tempDir); |
| 224 | + } catch { |
| 225 | + // Best-effort cleanup |
| 226 | + } |
| 227 | + } |
| 228 | + }); |
| 229 | +}); |
0 commit comments