|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | + |
| 4 | +const FIXTURE_RELATIVE_PATH = 'app/views/example.html.erb'; |
| 5 | + |
| 6 | +suite('ERB Inline JS Helper - Happy Path', () => { |
| 7 | + let document: vscode.TextDocument; |
| 8 | + |
| 9 | + suiteSetup(async function () { |
| 10 | + const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath; |
| 11 | + assert.ok(workspaceRoot, 'Workspace is not open'); |
| 12 | + |
| 13 | + document = await vscode.workspace.openTextDocument(vscode.Uri.file(`${workspaceRoot}/${FIXTURE_RELATIVE_PATH}`)); |
| 14 | + |
| 15 | + const extension = vscode.extensions.getExtension('kudoas.erb-inline-js-helper'); |
| 16 | + assert.ok(extension, 'Extension is not found'); |
| 17 | + await extension.activate(); |
| 18 | + assert.ok(extension.isActive, 'Extension did not activate'); |
| 19 | + }); |
| 20 | + |
| 21 | + test('extension activates', async function () { |
| 22 | + const extension = vscode.extensions.getExtension('kudoas.erb-inline-js-helper'); |
| 23 | + |
| 24 | + assert.ok(extension, 'Extension is not found'); |
| 25 | + await extension.activate(); |
| 26 | + assert.ok(extension.isActive, 'Extension did not activate'); |
| 27 | + }); |
| 28 | + |
| 29 | + test('completion provides console.log', async function () { |
| 30 | + const position = positionAtSubstring(document, 'console.', 0, 'completion target', 'after'); |
| 31 | + |
| 32 | + const list = await vscode.commands.executeCommand<vscode.CompletionList>( |
| 33 | + 'vscode.executeCompletionItemProvider', |
| 34 | + document.uri, |
| 35 | + position, |
| 36 | + '.' |
| 37 | + ); |
| 38 | + |
| 39 | + assert.ok(list, 'Completion list is undefined'); |
| 40 | + const labels = list.items.map((item) => (typeof item.label === 'string' ? item.label : item.label.label)); |
| 41 | + assert.ok(labels.includes('log'), 'Expected "log" in completion items'); |
| 42 | + }); |
| 43 | + |
| 44 | + test('hover shows info for message', async function () { |
| 45 | + const position = positionAtSubstring(document, 'message', 0, 'hover target'); |
| 46 | + const hovers = await vscode.commands.executeCommand<vscode.Hover[]>( |
| 47 | + 'vscode.executeHoverProvider', |
| 48 | + document.uri, |
| 49 | + position |
| 50 | + ); |
| 51 | + |
| 52 | + assert.ok(hovers && hovers.length > 0, 'Hover is empty'); |
| 53 | + const text = hovers |
| 54 | + .flatMap((hover) => hover.contents) |
| 55 | + .map((content) => (typeof content === 'string' ? content : content.value)) |
| 56 | + .join(' '); |
| 57 | + |
| 58 | + assert.ok(text.includes('message'), 'Hover does not mention "message"'); |
| 59 | + }); |
| 60 | + |
| 61 | + test('definition points to message declaration', async function () { |
| 62 | + const usageLineIndex = indexOfSubstring(document, 'console.log(message);', 0, 'definition usage line'); |
| 63 | + const usageNameIndex = indexOfSubstring(document, 'message', usageLineIndex, 'definition usage'); |
| 64 | + const usagePosition = document.positionAt(usageNameIndex); |
| 65 | + const definitions = await vscode.commands.executeCommand< |
| 66 | + vscode.Location[] | vscode.Location | vscode.LocationLink[] |
| 67 | + >('vscode.executeDefinitionProvider', document.uri, usagePosition); |
| 68 | + |
| 69 | + const locations = normalizeLocations(definitions); |
| 70 | + assert.ok(locations.length > 0, 'Definition result is empty'); |
| 71 | + |
| 72 | + const declarationLineIndex = indexOfSubstring(document, 'const message', 0, 'definition declaration'); |
| 73 | + const declarationNameIndex = indexOfSubstring( |
| 74 | + document, |
| 75 | + 'message', |
| 76 | + declarationLineIndex, |
| 77 | + 'definition declaration name' |
| 78 | + ); |
| 79 | + const declarationNamePosition = document.positionAt(declarationNameIndex); |
| 80 | + |
| 81 | + const match = locations.find( |
| 82 | + (location) => |
| 83 | + location.uri.toString() === document.uri.toString() && location.range.start.isEqual(declarationNamePosition) |
| 84 | + ); |
| 85 | + |
| 86 | + assert.ok(match, 'Definition did not resolve to the message declaration'); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +function positionAtSubstring( |
| 91 | + document: vscode.TextDocument, |
| 92 | + substring: string, |
| 93 | + fromIndex: number, |
| 94 | + label: string, |
| 95 | + position: 'start' | 'after' = 'start' |
| 96 | +): vscode.Position { |
| 97 | + const index = indexOfSubstring(document, substring, fromIndex, label); |
| 98 | + const offset = position === 'after' ? substring.length : 0; |
| 99 | + return document.positionAt(index + offset); |
| 100 | +} |
| 101 | + |
| 102 | +function indexOfSubstring(document: vscode.TextDocument, substring: string, fromIndex: number, label: string): number { |
| 103 | + const text = document.getText(); |
| 104 | + const index = text.indexOf(substring, fromIndex); |
| 105 | + assert.ok(index !== -1, `Missing substring for ${label}: ${substring}`); |
| 106 | + return index; |
| 107 | +} |
| 108 | + |
| 109 | +function normalizeLocations( |
| 110 | + result: vscode.Location[] | vscode.Location | vscode.LocationLink[] | undefined |
| 111 | +): vscode.Location[] { |
| 112 | + if (!result) { |
| 113 | + return []; |
| 114 | + } |
| 115 | + |
| 116 | + if (Array.isArray(result)) { |
| 117 | + return result.map((entry) => { |
| 118 | + if ('targetUri' in entry) { |
| 119 | + return new vscode.Location(entry.targetUri, entry.targetRange); |
| 120 | + } |
| 121 | + return entry; |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + return [result]; |
| 126 | +} |
0 commit comments