|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 7 | +import { TestLogService } from '../../../../../platform/testing/common/testLogService'; |
| 8 | +import { MockMcpServer } from './testHelpers'; |
| 9 | + |
| 10 | +const { mockExecuteCommand, mockWriteFile } = vi.hoisted(() => ({ |
| 11 | + mockExecuteCommand: vi.fn(), |
| 12 | + mockWriteFile: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock('vscode', () => { |
| 16 | + class MockPosition { |
| 17 | + constructor(public line: number, public character: number) { } |
| 18 | + } |
| 19 | + class MockUri { |
| 20 | + public fsPath: string; |
| 21 | + public scheme: string; |
| 22 | + constructor(public _str: string) { |
| 23 | + this.fsPath = _str.replace('file://', ''); |
| 24 | + this.scheme = _str.startsWith('file:') ? 'file' : 'unknown'; |
| 25 | + } |
| 26 | + toString() { |
| 27 | + return this._str; |
| 28 | + } |
| 29 | + } |
| 30 | + return { |
| 31 | + Position: MockPosition, |
| 32 | + Uri: { |
| 33 | + parse: (str: string) => new MockUri(str), |
| 34 | + file: (path: string) => new MockUri(`file://${path}`), |
| 35 | + }, |
| 36 | + commands: { |
| 37 | + executeCommand: mockExecuteCommand, |
| 38 | + }, |
| 39 | + }; |
| 40 | +}); |
| 41 | + |
| 42 | +vi.mock('fs/promises', () => ({ |
| 43 | + writeFile: mockWriteFile, |
| 44 | +})); |
| 45 | + |
| 46 | +vi.mock('os', () => ({ |
| 47 | + tmpdir: () => '/mock/tmpdir', |
| 48 | +})); |
| 49 | + |
| 50 | +import { registerRunLspQueryTool } from '../tools/runLspQuery'; |
| 51 | + |
| 52 | +describe('runLspQuery tool', () => { |
| 53 | + const logger = new TestLogService(); |
| 54 | + let server: MockMcpServer; |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + vi.clearAllMocks(); |
| 58 | + server = new MockMcpServer(); |
| 59 | + registerRunLspQueryTool(server as unknown as import('@modelcontextprotocol/sdk/server/mcp.js').McpServer, logger); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should register the run_lsp_query tool', () => { |
| 63 | + expect(server.hasToolRegistered('run_lsp_query')).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should execute a generic LSP query correctly without position', async () => { |
| 67 | + mockExecuteCommand.mockResolvedValue([{ name: 'symbol1' }]); |
| 68 | + const handler = server.getToolHandler('run_lsp_query')!; |
| 69 | + |
| 70 | + const result: any = await handler({ |
| 71 | + command: 'vscode.executeDocumentSymbolProvider', |
| 72 | + uri: 'file:///test/file.ts', |
| 73 | + }); |
| 74 | + |
| 75 | + expect(mockExecuteCommand).toHaveBeenCalledWith('vscode.executeDocumentSymbolProvider', expect.objectContaining({ _str: 'file:///test/file.ts' })); |
| 76 | + expect(result.content[0].text).toContain('symbol1'); |
| 77 | + expect(result.content[0].text).toContain('These are the results of executing vscode.executeDocumentSymbolProvider on file:///test/file.ts'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should execute a positional LSP query correctly', async () => { |
| 81 | + mockExecuteCommand.mockResolvedValue({ start: { line: 0, character: 0 }, end: { line: 0, character: 10 } }); |
| 82 | + const handler = server.getToolHandler('run_lsp_query')!; |
| 83 | + |
| 84 | + const result: any = await handler({ |
| 85 | + command: 'vscode.executeHoverProvider', |
| 86 | + uri: 'file:///test/file.ts', |
| 87 | + position: { line: 5, character: 10 } |
| 88 | + }); |
| 89 | + |
| 90 | + // Check the position argument |
| 91 | + const posArg = mockExecuteCommand.mock.calls[0][2]; |
| 92 | + expect(posArg.line).toBe(5); |
| 93 | + expect(posArg.character).toBe(10); |
| 94 | + expect(result.content[0].text).toContain('These are the results of executing vscode.executeHoverProvider on file:///test/file.ts at line 5, character 10'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should execute a workspace symbol query with query argument', async () => { |
| 98 | + mockExecuteCommand.mockResolvedValue([]); |
| 99 | + const handler = server.getToolHandler('run_lsp_query')!; |
| 100 | + |
| 101 | + const result: any = await handler({ |
| 102 | + command: 'vscode.executeWorkspaceSymbolProvider', |
| 103 | + uri: 'file:///test/file.ts', |
| 104 | + query: 'test_query' |
| 105 | + }); |
| 106 | + |
| 107 | + expect(mockExecuteCommand).toHaveBeenCalledWith('vscode.executeWorkspaceSymbolProvider', 'test_query'); |
| 108 | + expect(result.content[0].text).toContain('with query "test_query"'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should compact results by grouping them if array is returned with uri elements', async () => { |
| 112 | + const mockResult = [ |
| 113 | + { name: 'test1', uri: { fsPath: '/test/a.ts', toString: () => 'file:///test/a.ts' } }, |
| 114 | + { name: 'test2', location: { uri: { fsPath: '/test/a.ts', toString: () => 'file:///test/a.ts' } } }, |
| 115 | + { name: 'test3', targetUri: { fsPath: '/test/b.ts', toString: () => 'file:///test/b.ts' } } |
| 116 | + ]; |
| 117 | + mockExecuteCommand.mockResolvedValue(mockResult); |
| 118 | + |
| 119 | + const handler = server.getToolHandler('run_lsp_query')!; |
| 120 | + const result: any = await handler({ |
| 121 | + command: 'vscode.executeDefinitionProvider', |
| 122 | + uri: 'file:///test/file.ts' |
| 123 | + }); |
| 124 | + |
| 125 | + const text = result.content[0].text; |
| 126 | + expect(text).toContain('"/test/a.ts"'); |
| 127 | + expect(text).toContain('"/test/b.ts"'); |
| 128 | + expect(text).not.toContain('"uri"'); // Assert stripped URI objects inside compact struct |
| 129 | + }); |
| 130 | + |
| 131 | + it('should write to temporary file if result is very long', async () => { |
| 132 | + // Create a very large array > 50 chars to test limit arrays as well, but each object itself very long |
| 133 | + const mockResult = Array.from({ length: 60 }, () => ({ massive_field: 'a'.repeat(2000) })); |
| 134 | + mockExecuteCommand.mockResolvedValue(mockResult); |
| 135 | + |
| 136 | + const handler = server.getToolHandler('run_lsp_query')!; |
| 137 | + const result: any = await handler({ |
| 138 | + command: 'vscode.executeDefinitionProvider', |
| 139 | + uri: 'file:///test/file.ts' |
| 140 | + }); |
| 141 | + |
| 142 | + expect(mockWriteFile).toHaveBeenCalled(); |
| 143 | + expect(result.content[0].text).toContain('The result is very long and has been saved to:'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should gracefully handle circular references', async () => { |
| 147 | + const circularObj: any = { prop: 'value' }; |
| 148 | + circularObj.self = circularObj; |
| 149 | + |
| 150 | + mockExecuteCommand.mockResolvedValue([circularObj]); |
| 151 | + |
| 152 | + const handler = server.getToolHandler('run_lsp_query')!; |
| 153 | + const result: any = await handler({ |
| 154 | + command: 'vscode.executeDefinitionProvider', |
| 155 | + uri: 'file:///test/file.ts' |
| 156 | + }); |
| 157 | + |
| 158 | + expect(result.content[0].text).toContain('"[Circular]"'); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should catch errors and return them cleanly', async () => { |
| 162 | + mockExecuteCommand.mockRejectedValue(new Error('LSP Error!')); |
| 163 | + const handler = server.getToolHandler('run_lsp_query')!; |
| 164 | + |
| 165 | + const result: any = await handler({ |
| 166 | + command: 'vscode.executeDefinitionProvider', |
| 167 | + uri: 'file:///test/file.ts' |
| 168 | + }); |
| 169 | + |
| 170 | + expect(result.content[0].text).toContain('Error executing vscode.executeDefinitionProvider: LSP Error!'); |
| 171 | + }); |
| 172 | +}); |
0 commit comments