|
| 1 | +import * as assert from 'node:assert/strict'; |
| 2 | +import * as path from 'node:path'; |
| 3 | +import * as vscode from 'vscode'; |
| 4 | +import type { LJDiagnostic, StateRefinementError } from '../types/diagnostics'; |
| 5 | +import type { LJStateMachine } from '../types/fsm'; |
| 6 | +import type { LiquidJavaTestApi, WebviewMessage } from '../types/test-api'; |
| 7 | + |
| 8 | +suite('Bundled LiquidJava state machine errors', () => { |
| 9 | + test('sends the webview state errors with their machine and shows them on it from the editor', async () => { |
| 10 | + const installed = vscode.extensions.getExtension<LiquidJavaTestApi>('AlcidesFonseca.liquid-java'); |
| 11 | + assert.ok(installed); |
| 12 | + const api = await installed.activate(); |
| 13 | + await api.ready; |
| 14 | + |
| 15 | + const workspace = vscode.workspace.workspaceFolders?.[0]; |
| 16 | + assert.ok(workspace); |
| 17 | + const refinementsUri = vscode.Uri.joinPath(workspace.uri, 'src/main/java/TreeNodeRefinements.java'); |
| 18 | + const callerUri = vscode.Uri.joinPath(workspace.uri, 'src/main/java/FailingTreeNodeState.java'); |
| 19 | + const sameFile = (file: string, uri: vscode.Uri) => vscode.Uri.file(path.resolve(file)).fsPath === uri.fsPath; |
| 20 | + const isStateError = (diagnostic: LJDiagnostic): diagnostic is StateRefinementError => |
| 21 | + diagnostic.type === 'state-refinement-error' && sameFile(diagnostic.file, callerUri); |
| 22 | + const subscriptions: vscode.Disposable[] = []; |
| 23 | + const nextWebviewMessage = <T>(description: string, matches: (event: WebviewMessage) => T | undefined) => |
| 24 | + new Promise<T>((resolve, reject) => { |
| 25 | + const timeout = setTimeout(() => reject(new Error(`Timed out waiting for ${description}`)), 90_000); |
| 26 | + const done = (callback: () => void) => { |
| 27 | + clearTimeout(timeout); |
| 28 | + subscription.dispose(); |
| 29 | + failure.dispose(); |
| 30 | + callback(); |
| 31 | + }; |
| 32 | + const subscription = api.onWebviewMessage(event => { |
| 33 | + const value = matches(event); |
| 34 | + if (value !== undefined) done(() => resolve(value)); |
| 35 | + }); |
| 36 | + const failure = api.onFailure(() => |
| 37 | + done(() => reject(new Error(`LiquidJava verifier crashed while waiting for ${description}`)))); |
| 38 | + subscriptions.push(subscription, failure, { dispose: () => clearTimeout(timeout) }); |
| 39 | + }); |
| 40 | + |
| 41 | + try { |
| 42 | + // hide the view first so showing it always loads a fresh webview |
| 43 | + await vscode.commands.executeCommand('workbench.action.closeSidebar'); |
| 44 | + const ready = nextWebviewMessage('the webview to be ready', event => |
| 45 | + event.direction === 'fromWebview' && event.message.type === 'ready' ? true : undefined); |
| 46 | + await vscode.commands.executeCommand('liquidjava.showView'); |
| 47 | + await ready; |
| 48 | + |
| 49 | + const machine = nextWebviewMessage('the refinements file state machine', event => |
| 50 | + event.direction === 'toWebview' && event.message.type === 'fsm' && event.message.sm |
| 51 | + ? event.message.sm as LJStateMachine : undefined); |
| 52 | + const stateError = nextWebviewMessage('the state error diagnostics', event => |
| 53 | + event.direction === 'toWebview' && event.message.type === 'diagnostics' |
| 54 | + ? (event.message.diagnostics as LJDiagnostic[]).find(isStateError) : undefined); |
| 55 | + const document = await vscode.workspace.openTextDocument(refinementsUri); |
| 56 | + await vscode.window.showTextDocument(document); |
| 57 | + await vscode.commands.executeCommand('liquidjava.verify'); |
| 58 | + |
| 59 | + const [sm, error] = await Promise.all([machine, stateError]); |
| 60 | + assert.equal(sm.className, 'javax.swing.tree.DefaultMutableTreeNode'); |
| 61 | + assert.ok(error.stateMachine, 'the state error must carry its state machine'); |
| 62 | + assert.equal(error.stateMachine.className, sm.className, 'errors are matched to the machine by class'); |
| 63 | + assert.ok(error.stateMachine.errorContext, 'the state error must carry its error context'); |
| 64 | + assert.ok(error.position && sameFile(error.position.file ?? error.file, callerUri)); |
| 65 | + assert.ok(error.declarationPosition?.file && sameFile(error.declarationPosition.file, refinementsUri)); |
| 66 | + |
| 67 | + // the editor offers to show the state error on its state machine |
| 68 | + await vscode.window.showTextDocument(await vscode.workspace.openTextDocument(callerUri)); |
| 69 | + const codeLenses = await vscode.commands.executeCommand<vscode.CodeLens[]>('vscode.executeCodeLensProvider', callerUri); |
| 70 | + const stateMachineLens = codeLenses.find(codeLens => codeLens.command?.title === 'Show in state machine'); |
| 71 | + assert.ok(stateMachineLens?.command, 'state errors must have a CodeLens to show them on the state machine'); |
| 72 | + assert.equal(stateMachineLens.command.command, 'liquidjava.showView'); |
| 73 | + const [target, tab] = stateMachineLens.command.arguments ?? []; |
| 74 | + assert.equal(tab, 'fsm'); |
| 75 | + assert.deepEqual(target.position, error.position); |
| 76 | + |
| 77 | + const reveal = nextWebviewMessage('the state error to be shown on the state machine', event => |
| 78 | + event.direction === 'toWebview' && event.message.type === 'revealDiagnostic' ? event.message : undefined); |
| 79 | + await vscode.commands.executeCommand(stateMachineLens.command.command, ...stateMachineLens.command.arguments!); |
| 80 | + const message = await reveal; |
| 81 | + assert.equal(message.tab, 'fsm'); |
| 82 | + assert.deepEqual((message.diagnostic as { position: unknown }).position, error.position); |
| 83 | + } finally { |
| 84 | + subscriptions.forEach(subscription => subscription.dispose()); |
| 85 | + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); |
| 86 | + } |
| 87 | + }); |
| 88 | +}); |
0 commit comments