|
| 1 | +/** |
| 2 | + * Integration tests for file watcher stability. |
| 3 | + * |
| 4 | + * Verifies that workspace file changes (database discovery, query result |
| 5 | + * creation) do NOT trigger redundant MCP server definition change events. |
| 6 | + * |
| 7 | + * The MCP server definition should only change when: |
| 8 | + * - The extension itself changes (update / reinstall) |
| 9 | + * - Workspace folder registration changes (folders added / removed) |
| 10 | + * - Configuration changes that affect the server |
| 11 | + * |
| 12 | + * File content changes are NOT a reason to re-provide the definition: |
| 13 | + * the running server discovers files on its own through filesystem |
| 14 | + * scanning at tool invocation time. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as assert from 'assert'; |
| 18 | +import * as fs from 'fs'; |
| 19 | +import * as path from 'path'; |
| 20 | +import * as vscode from 'vscode'; |
| 21 | + |
| 22 | +const EXTENSION_ID = 'advanced-security.vscode-codeql-development-mcp-server'; |
| 23 | + |
| 24 | +/** Milliseconds to wait past the 1 s debounce to confirm no event fires. */ |
| 25 | +const SETTLE_MS = 2_500; |
| 26 | + |
| 27 | +suite('File Watcher Stability Tests', () => { |
| 28 | + let api: any; |
| 29 | + const cleanupPaths: string[] = []; |
| 30 | + |
| 31 | + suiteSetup(async function () { |
| 32 | + this.timeout(10_000); |
| 33 | + const ext = vscode.extensions.getExtension(EXTENSION_ID); |
| 34 | + assert.ok(ext, `Extension ${EXTENSION_ID} not found`); |
| 35 | + api = ext.isActive ? ext.exports : await ext.activate(); |
| 36 | + |
| 37 | + if (!vscode.workspace.workspaceFolders?.length) { |
| 38 | + console.log( |
| 39 | + '[file-watcher-stability] Skipping — requires workspace with at least one folder', |
| 40 | + ); |
| 41 | + this.skip(); |
| 42 | + } |
| 43 | + |
| 44 | + // Let any startup-related events settle before running tests. |
| 45 | + await new Promise((r) => globalThis.setTimeout(r, SETTLE_MS)); |
| 46 | + }); |
| 47 | + |
| 48 | + suiteTeardown(() => { |
| 49 | + for (const p of cleanupPaths) { |
| 50 | + try { |
| 51 | + fs.rmSync(p, { recursive: true, force: true }); |
| 52 | + } catch { |
| 53 | + /* best-effort */ |
| 54 | + } |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + // ----------------------------------------------------------------- |
| 59 | + // Test 1: BQRS file creation must NOT fire definition change event |
| 60 | + // ----------------------------------------------------------------- |
| 61 | + test('Creating a BQRS file should NOT trigger onDidChangeMcpServerDefinitions', async function () { |
| 62 | + this.timeout(10_000); |
| 63 | + |
| 64 | + const mcpProvider = api.mcpProvider; |
| 65 | + assert.ok(mcpProvider, 'API missing mcpProvider'); |
| 66 | + assert.ok( |
| 67 | + typeof mcpProvider.onDidChangeMcpServerDefinitions === 'function', |
| 68 | + 'mcpProvider missing onDidChangeMcpServerDefinitions', |
| 69 | + ); |
| 70 | + |
| 71 | + let changeCount = 0; |
| 72 | + const disposable = mcpProvider.onDidChangeMcpServerDefinitions(() => { |
| 73 | + changeCount++; |
| 74 | + }); |
| 75 | + |
| 76 | + try { |
| 77 | + const workspaceRoot = |
| 78 | + vscode.workspace.workspaceFolders![0].uri.fsPath; |
| 79 | + const bqrsPath = path.join( |
| 80 | + workspaceRoot, |
| 81 | + `stability-test-${Date.now()}.bqrs`, |
| 82 | + ); |
| 83 | + cleanupPaths.push(bqrsPath); |
| 84 | + |
| 85 | + fs.writeFileSync(bqrsPath, Buffer.from([0x00])); |
| 86 | + |
| 87 | + // Wait past the debounce period (1 s) plus buffer |
| 88 | + await new Promise((r) => globalThis.setTimeout(r, SETTLE_MS)); |
| 89 | + |
| 90 | + assert.strictEqual( |
| 91 | + changeCount, |
| 92 | + 0, |
| 93 | + `onDidChangeMcpServerDefinitions fired ${changeCount} time(s) after BQRS file creation; ` + |
| 94 | + 'file content changes should NOT trigger MCP server definition updates', |
| 95 | + ); |
| 96 | + } finally { |
| 97 | + disposable.dispose(); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + // ----------------------------------------------------------------- |
| 102 | + // Test 2: codeql-database.yml creation must NOT fire change event |
| 103 | + // ----------------------------------------------------------------- |
| 104 | + test('Creating a codeql-database.yml should NOT trigger onDidChangeMcpServerDefinitions', async function () { |
| 105 | + this.timeout(10_000); |
| 106 | + |
| 107 | + const mcpProvider = api.mcpProvider; |
| 108 | + assert.ok(mcpProvider, 'API missing mcpProvider'); |
| 109 | + |
| 110 | + let changeCount = 0; |
| 111 | + const disposable = mcpProvider.onDidChangeMcpServerDefinitions(() => { |
| 112 | + changeCount++; |
| 113 | + }); |
| 114 | + |
| 115 | + try { |
| 116 | + const workspaceRoot = |
| 117 | + vscode.workspace.workspaceFolders![0].uri.fsPath; |
| 118 | + const dbDir = path.join( |
| 119 | + workspaceRoot, |
| 120 | + `stability-test-db-${Date.now()}`, |
| 121 | + ); |
| 122 | + fs.mkdirSync(dbDir, { recursive: true }); |
| 123 | + cleanupPaths.push(dbDir); |
| 124 | + |
| 125 | + const ymlPath = path.join(dbDir, 'codeql-database.yml'); |
| 126 | + fs.writeFileSync(ymlPath, 'primaryLanguage: javascript\n'); |
| 127 | + |
| 128 | + await new Promise((r) => globalThis.setTimeout(r, SETTLE_MS)); |
| 129 | + |
| 130 | + assert.strictEqual( |
| 131 | + changeCount, |
| 132 | + 0, |
| 133 | + `onDidChangeMcpServerDefinitions fired ${changeCount} time(s) after codeql-database.yml creation; ` + |
| 134 | + 'file content changes should NOT trigger MCP server definition updates', |
| 135 | + ); |
| 136 | + } finally { |
| 137 | + disposable.dispose(); |
| 138 | + } |
| 139 | + }); |
| 140 | +}); |
0 commit comments