|
| 1 | +/** |
| 2 | + * Rename Symbol Integration Tests |
| 3 | + * |
| 4 | + * These tests verify the prepareRename and rename LSP functionality |
| 5 | + * using actual TypeScript language server. |
| 6 | + * |
| 7 | + * Based on serena reference: ref/serena/src/solidlsp/ls.py:1834-1859 |
| 8 | + */ |
| 9 | + |
| 10 | +import path from 'node:path' |
| 11 | +import { afterAll, beforeAll, describe, expect, test } from 'bun:test' |
| 12 | +import { LSPManager } from '../../src/index' |
| 13 | + |
| 14 | +const VUE_PROJECT_PATH = path.join(import.meta.dir, '../fixtures/vue-project') |
| 15 | +const MATH_TS_PATH = path.join(VUE_PROJECT_PATH, 'src/utils/math.ts') |
| 16 | + |
| 17 | +// Check if npm is available for auto-installing LSP dependencies |
| 18 | +const isNpmAvailable = Bun.which('npm') !== null |
| 19 | + |
| 20 | +describe.skipIf(!isNpmAvailable)('Rename Symbol Integration', () => { |
| 21 | + let manager: LSPManager |
| 22 | + |
| 23 | + beforeAll(async () => { |
| 24 | + manager = new LSPManager(VUE_PROJECT_PATH) |
| 25 | + // Touch the TypeScript file to initialize LSP and wait for diagnostics |
| 26 | + await manager.touchFile(MATH_TS_PATH, true) |
| 27 | + }, 120000) // 120s timeout for server startup |
| 28 | + |
| 29 | + afterAll(async () => { |
| 30 | + await manager.shutdown() |
| 31 | + }) |
| 32 | + |
| 33 | + test('prepareRename returns result for function name', async () => { |
| 34 | + // Test prepareRename on 'add' function name (line 4, character 16: "export function add") |
| 35 | + const result = await manager.prepareRename({ |
| 36 | + file: MATH_TS_PATH, |
| 37 | + line: 3, // 0-indexed: line 4 in editor |
| 38 | + character: 16, // position of 'add' in "export function add" |
| 39 | + }) |
| 40 | + |
| 41 | + // TypeScript server should return a valid prepare rename result |
| 42 | + expect(result).toBeDefined() |
| 43 | + if (result) { |
| 44 | + // Should be one of the PrepareRenameResult formats |
| 45 | + // Range format has start/end |
| 46 | + // Placeholder format has range/placeholder |
| 47 | + // DefaultBehavior format has defaultBehavior |
| 48 | + const hasRange = 'start' in result && 'end' in result |
| 49 | + const hasPlaceholder = 'range' in result && 'placeholder' in result |
| 50 | + const hasDefaultBehavior = 'defaultBehavior' in result |
| 51 | + |
| 52 | + expect(hasRange || hasPlaceholder || hasDefaultBehavior).toBe(true) |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + test('prepareRename returns null for non-renameable position', async () => { |
| 57 | + // Test prepareRename on a keyword position (line 1, character 0: "/**") |
| 58 | + const result = await manager.prepareRename({ |
| 59 | + file: MATH_TS_PATH, |
| 60 | + line: 0, // Comment line |
| 61 | + character: 0, |
| 62 | + }) |
| 63 | + |
| 64 | + // Should return null for non-renameable positions |
| 65 | + expect(result).toBeNull() |
| 66 | + }) |
| 67 | + |
| 68 | + test('rename returns WorkspaceEdit for valid symbol', async () => { |
| 69 | + // Test rename on 'power' function (line 35: "export function power") |
| 70 | + // Using 'power' since it's not used in other files, safer for testing |
| 71 | + const result = await manager.rename({ |
| 72 | + file: MATH_TS_PATH, |
| 73 | + line: 34, // 0-indexed: line 35 in editor |
| 74 | + character: 16, // position of 'power' |
| 75 | + newName: 'pow', |
| 76 | + }) |
| 77 | + |
| 78 | + // TypeScript server should return a WorkspaceEdit |
| 79 | + expect(result).toBeDefined() |
| 80 | + if (result) { |
| 81 | + expect(result.changes).toBeDefined() |
| 82 | + expect(typeof result.changes).toBe('object') |
| 83 | + |
| 84 | + // Should have at least one file with edits |
| 85 | + const files = Object.keys(result.changes!) |
| 86 | + expect(files.length).toBeGreaterThan(0) |
| 87 | + |
| 88 | + // Each file should have at least one edit |
| 89 | + for (const file of files) { |
| 90 | + const edits = result.changes![file] |
| 91 | + expect(edits.length).toBeGreaterThan(0) |
| 92 | + |
| 93 | + // Each edit should have range and newText |
| 94 | + for (const edit of edits) { |
| 95 | + expect(edit.range).toBeDefined() |
| 96 | + expect(edit.newText).toBe('pow') |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + test('rename returns null for invalid position', async () => { |
| 103 | + // Test rename on a whitespace/comment position |
| 104 | + const result = await manager.rename({ |
| 105 | + file: MATH_TS_PATH, |
| 106 | + line: 0, // Comment line |
| 107 | + character: 0, |
| 108 | + newName: 'newName', |
| 109 | + }) |
| 110 | + |
| 111 | + // Should return null for non-renameable positions |
| 112 | + expect(result).toBeNull() |
| 113 | + }) |
| 114 | + |
| 115 | + test('rename on parameter returns edits', async () => { |
| 116 | + // Test rename on parameter 'a' in add function (line 4: "add(a: number, b: number)") |
| 117 | + const result = await manager.rename({ |
| 118 | + file: MATH_TS_PATH, |
| 119 | + line: 3, // 0-indexed |
| 120 | + character: 20, // position of 'a' parameter |
| 121 | + newName: 'num1', |
| 122 | + }) |
| 123 | + |
| 124 | + // Should return WorkspaceEdit with edits for parameter usages |
| 125 | + expect(result).toBeDefined() |
| 126 | + if (result) { |
| 127 | + expect(result.changes).toBeDefined() |
| 128 | + |
| 129 | + // Parameter rename should affect at least the definition and usage in return statement |
| 130 | + const mathTsUri = Object.keys(result.changes!).find(uri => uri.includes('math.ts')) |
| 131 | + expect(mathTsUri).toBeDefined() |
| 132 | + |
| 133 | + if (mathTsUri) { |
| 134 | + const edits = result.changes![mathTsUri] |
| 135 | + // Should have at least 2 edits: parameter definition and usage in "return a + b" |
| 136 | + expect(edits.length).toBeGreaterThanOrEqual(2) |
| 137 | + } |
| 138 | + } |
| 139 | + }) |
| 140 | +}) |
| 141 | + |
| 142 | +// Tests that don't require npm/LSP server |
| 143 | +describe('Rename Symbol Integration (no npm required)', () => { |
| 144 | + test('fixture file exists', async () => { |
| 145 | + const mathTs = Bun.file(MATH_TS_PATH) |
| 146 | + expect(await mathTs.exists()).toBe(true) |
| 147 | + }) |
| 148 | + |
| 149 | + test('manager exposes prepareRename method', () => { |
| 150 | + const manager = new LSPManager(VUE_PROJECT_PATH) |
| 151 | + expect(typeof manager.prepareRename).toBe('function') |
| 152 | + }) |
| 153 | + |
| 154 | + test('manager exposes rename method', () => { |
| 155 | + const manager = new LSPManager(VUE_PROJECT_PATH) |
| 156 | + expect(typeof manager.rename).toBe('function') |
| 157 | + }) |
| 158 | + |
| 159 | + test('prepareRename returns null when disabled', async () => { |
| 160 | + const manager = new LSPManager(VUE_PROJECT_PATH, { enabled: false }) |
| 161 | + const result = await manager.prepareRename({ |
| 162 | + file: MATH_TS_PATH, |
| 163 | + line: 3, |
| 164 | + character: 16, |
| 165 | + }) |
| 166 | + expect(result).toBeNull() |
| 167 | + }) |
| 168 | + |
| 169 | + test('rename returns null when disabled', async () => { |
| 170 | + const manager = new LSPManager(VUE_PROJECT_PATH, { enabled: false }) |
| 171 | + const result = await manager.rename({ |
| 172 | + file: MATH_TS_PATH, |
| 173 | + line: 3, |
| 174 | + character: 16, |
| 175 | + newName: 'newAdd', |
| 176 | + }) |
| 177 | + expect(result).toBeNull() |
| 178 | + }) |
| 179 | +}) |
0 commit comments