|
| 1 | +import { ok, strictEqual } from 'node:assert'; |
| 2 | +import * as path from 'node:path'; |
| 3 | +import * as fs from 'node:fs'; |
| 4 | +import * as os from 'node:os'; |
| 5 | + |
| 6 | +import { TypeScriptCompletionService } from '../../services/typescriptCompletionService'; |
| 7 | + |
| 8 | +suite('TypeScriptCompletionService', () => { |
| 9 | + test('provides completions for simple JavaScript', () => { |
| 10 | + const service = new TypeScriptCompletionService(); |
| 11 | + const jsContent = 'const answer = 42; answer.'; |
| 12 | + service.updateContent(jsContent); |
| 13 | + |
| 14 | + const completions = service.getCompletions(jsContent.length); |
| 15 | + ok(completions, 'Expected completions to be defined'); |
| 16 | + ok(completions.entries.length > 0, 'Expected at least one completion'); |
| 17 | + |
| 18 | + // Should have Number methods |
| 19 | + const hasToFixed = completions.entries.some((e) => e.name === 'toFixed'); |
| 20 | + ok(hasToFixed, 'Expected to find Number.toFixed method'); |
| 21 | + }); |
| 22 | + |
| 23 | + test('provides hover information for variables', () => { |
| 24 | + const service = new TypeScriptCompletionService(); |
| 25 | + const jsContent = 'const answer = 42;'; |
| 26 | + service.updateContent(jsContent); |
| 27 | + |
| 28 | + const offset = jsContent.indexOf('answer'); |
| 29 | + const info = service.getQuickInfo(offset); |
| 30 | + ok(info, 'Expected quick info to be defined'); |
| 31 | + ok(info.displayParts, 'Expected display parts to be defined'); |
| 32 | + |
| 33 | + const display = info.displayParts.map((part) => part.text).join(''); |
| 34 | + ok(display.includes('answer'), 'Expected hover to include variable name'); |
| 35 | + }); |
| 36 | + |
| 37 | + test('resolves module imports with JSDoc types', () => { |
| 38 | + // Create a temporary directory with a type definition file |
| 39 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'erb-test-')); |
| 40 | + const typeDefPath = path.join(tmpDir, 'types.js'); |
| 41 | + const testFilePath = path.join(tmpDir, 'test.js'); |
| 42 | + |
| 43 | + try { |
| 44 | + // Create a type definition file |
| 45 | + fs.writeFileSync( |
| 46 | + typeDefPath, |
| 47 | + ` |
| 48 | +/** |
| 49 | + * @typedef {Object} User |
| 50 | + * @property {string} name |
| 51 | + * @property {number} age |
| 52 | + */ |
| 53 | +
|
| 54 | +/** |
| 55 | + * @typedef {Object} Address |
| 56 | + * @property {string} street |
| 57 | + * @property {string} city |
| 58 | + */ |
| 59 | +` |
| 60 | + ); |
| 61 | + |
| 62 | + // Create a test file that imports and uses intersection types |
| 63 | + const jsContent = ` |
| 64 | +/** |
| 65 | + * @typedef {import('./types').User} User |
| 66 | + * @typedef {import('./types').Address} Address |
| 67 | + * @typedef {User & Address} UserWithAddress |
| 68 | + */ |
| 69 | +
|
| 70 | +/** @type {UserWithAddress} */ |
| 71 | +const person = { name: 'John', age: 30, street: '123 Main', city: 'NYC' }; |
| 72 | +person. |
| 73 | +`; |
| 74 | + |
| 75 | + const service = new TypeScriptCompletionService(); |
| 76 | + // Update with the full path as the containing file |
| 77 | + service.updateContent(jsContent.trim(), testFilePath); |
| 78 | + |
| 79 | + const completions = service.getCompletions(jsContent.trim().length); |
| 80 | + ok(completions, 'Expected completions to be defined'); |
| 81 | + ok(completions.entries.length > 0, 'Expected at least one completion'); |
| 82 | + |
| 83 | + // Should have properties from both User and Address types |
| 84 | + const hasName = completions.entries.some((e) => e.name === 'name'); |
| 85 | + const hasAge = completions.entries.some((e) => e.name === 'age'); |
| 86 | + const hasStreet = completions.entries.some((e) => e.name === 'street'); |
| 87 | + const hasCity = completions.entries.some((e) => e.name === 'city'); |
| 88 | + |
| 89 | + ok(hasName, 'Expected to find name property from User type'); |
| 90 | + ok(hasAge, 'Expected to find age property from User type'); |
| 91 | + ok(hasStreet, 'Expected to find street property from Address type'); |
| 92 | + ok(hasCity, 'Expected to find city property from Address type'); |
| 93 | + } finally { |
| 94 | + // Cleanup |
| 95 | + try { |
| 96 | + fs.unlinkSync(typeDefPath); |
| 97 | + fs.rmdirSync(tmpDir); |
| 98 | + } catch (e) { |
| 99 | + // Ignore cleanup errors |
| 100 | + } |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + test('updates version when content changes', () => { |
| 105 | + const service = new TypeScriptCompletionService(); |
| 106 | + const version1 = service.getVirtualFileName(); |
| 107 | + |
| 108 | + service.updateContent('const x = 1;'); |
| 109 | + const completions1 = service.getCompletions(15); |
| 110 | + ok(completions1, 'Expected first completions to be defined'); |
| 111 | + |
| 112 | + service.updateContent('const y = 2;'); |
| 113 | + const completions2 = service.getCompletions(15); |
| 114 | + ok(completions2, 'Expected second completions to be defined'); |
| 115 | + |
| 116 | + // Version tracking is internal, but we can verify service still works |
| 117 | + ok(true, 'Service handled multiple content updates'); |
| 118 | + }); |
| 119 | +}); |
0 commit comments