|
| 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 } from '../types/diagnostics'; |
| 5 | +import type { LJContext } from '../types/context'; |
| 6 | +import type { LiquidJavaTestApi } from '../types/test-api'; |
| 7 | + |
| 8 | +suite('Bundled LiquidJava webview and lifecycle', () => { |
| 9 | + test('sends verification messages and verifies again after Stop, Start, and Restart', 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 uri = vscode.Uri.joinPath(workspace.uri, 'src/main/java/FailingRefinement.java'); |
| 18 | + const sameFile = (file: string) => vscode.Uri.file(path.resolve(file)).fsPath === uri.fsPath; |
| 19 | + const subscriptions: vscode.Disposable[] = []; |
| 20 | + const nextEvent = <T>(event: vscode.Event<T>, matches: (value: T) => boolean) => |
| 21 | + new Promise<T>(resolve => { |
| 22 | + const subscription = event(value => { |
| 23 | + if (matches(value)) { |
| 24 | + subscription.dispose(); |
| 25 | + resolve(value); |
| 26 | + } |
| 27 | + }); |
| 28 | + subscriptions.push(subscription); |
| 29 | + }); |
| 30 | + const isFixtureDiagnostic = (diagnostics: LJDiagnostic[]) => diagnostics.some(diagnostic => |
| 31 | + diagnostic.type === 'refinement-error' && sameFile(diagnostic.file)); |
| 32 | + const nextDiagnostics = () => nextEvent(api.onDiagnostics, isFixtureDiagnostic); |
| 33 | + const assertServerRunning = () => { |
| 34 | + const pid = api.getState().serverPid; |
| 35 | + assert.ok(pid, 'the bundled extension must expose its running Java server'); |
| 36 | + assert.doesNotThrow(() => process.kill(pid, 0)); |
| 37 | + return pid; |
| 38 | + }; |
| 39 | + const assertServerStopped = (pid: number) => { |
| 40 | + assert.equal(api.getState().serverPid, undefined); |
| 41 | + assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }, 'Stop must terminate the Java process'); |
| 42 | + assert.equal(api.getState().status, 'stopped'); |
| 43 | + assert.deepEqual(api.getState().diagnostics, []); |
| 44 | + }; |
| 45 | + |
| 46 | + try { |
| 47 | + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); |
| 48 | + await vscode.commands.executeCommand('liquidjava.stop'); |
| 49 | + await vscode.commands.executeCommand('liquidjava.start'); |
| 50 | + const ready = nextEvent(api.onWebviewMessage, event => |
| 51 | + event.direction === 'fromWebview' && event.message.type === 'ready'); |
| 52 | + await vscode.commands.executeCommand('liquidjava.showView'); |
| 53 | + await ready; |
| 54 | + |
| 55 | + const initialDiagnostics = nextDiagnostics(); |
| 56 | + const document = await vscode.workspace.openTextDocument(uri); |
| 57 | + await vscode.window.showTextDocument(document); |
| 58 | + await initialDiagnostics; |
| 59 | + |
| 60 | + const diagnosticMessage = nextEvent(api.onWebviewMessage, event => |
| 61 | + event.direction === 'toWebview' && event.message.type === 'diagnostics' && |
| 62 | + isFixtureDiagnostic(event.message.diagnostics)); |
| 63 | + const contextMessage = nextEvent(api.onWebviewMessage, event => |
| 64 | + event.direction === 'toWebview' && event.message.type === 'context'); |
| 65 | + const manualDiagnostics = nextDiagnostics(); |
| 66 | + await vscode.commands.executeCommand('liquidjava.verify'); |
| 67 | + assert.equal(api.getState().status, 'loading'); |
| 68 | + const [diagnostics, outboundDiagnostics, outboundContext] = await Promise.all([ |
| 69 | + manualDiagnostics, diagnosticMessage, contextMessage, |
| 70 | + ]); |
| 71 | + assert.deepEqual(outboundDiagnostics.message.diagnostics, diagnostics); |
| 72 | + const context: LJContext = outboundContext.message.context; |
| 73 | + const valid = context.localVars.find(variable => variable.name === 'valid'); |
| 74 | + assert.ok(valid, JSON.stringify(context)); |
| 75 | + assert.ok(valid.position); |
| 76 | + assert.ok(sameFile(valid.position.file)); |
| 77 | + assert.equal(valid.type, 'int'); |
| 78 | + assert.ok(valid.mainRefinement.includes('> 0')); |
| 79 | + assert.equal(api.getState().status, 'failed'); |
| 80 | + |
| 81 | + const originalPid = assertServerRunning(); |
| 82 | + const stopped = nextEvent(api.onWebviewMessage, event => |
| 83 | + event.direction === 'toWebview' && event.message.type === 'status' && event.message.status === 'stopped'); |
| 84 | + await vscode.commands.executeCommand('liquidjava.stop'); |
| 85 | + await stopped; |
| 86 | + assertServerStopped(originalPid); |
| 87 | + |
| 88 | + const startDiagnostics = nextDiagnostics(); |
| 89 | + await vscode.commands.executeCommand('liquidjava.start'); |
| 90 | + await startDiagnostics; |
| 91 | + const startedPid = assertServerRunning(); |
| 92 | + assert.notEqual(startedPid, originalPid); |
| 93 | + assert.equal(api.getState().status, 'failed'); |
| 94 | + |
| 95 | + const restartStopped = nextEvent(api.onWebviewMessage, event => |
| 96 | + event.direction === 'toWebview' && event.message.type === 'status' && event.message.status === 'stopped'); |
| 97 | + const restartDiagnostics = nextDiagnostics(); |
| 98 | + await vscode.commands.executeCommand('liquidjava.restart'); |
| 99 | + await restartStopped; |
| 100 | + await restartDiagnostics; |
| 101 | + const restartedPid = assertServerRunning(); |
| 102 | + assert.notEqual(restartedPid, startedPid); |
| 103 | + assert.throws(() => process.kill(startedPid, 0), { code: 'ESRCH' }); |
| 104 | + |
| 105 | + const finalDiagnostics = nextDiagnostics(); |
| 106 | + await vscode.commands.executeCommand('liquidjava.verify'); |
| 107 | + await finalDiagnostics; |
| 108 | + assert.equal(api.getState().status, 'failed'); |
| 109 | + } finally { |
| 110 | + subscriptions.forEach(subscription => subscription.dispose()); |
| 111 | + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); |
| 112 | + await vscode.commands.executeCommand('liquidjava.stop'); |
| 113 | + await vscode.commands.executeCommand('liquidjava.start'); |
| 114 | + } |
| 115 | + }); |
| 116 | +}); |
0 commit comments