-
Notifications
You must be signed in to change notification settings - Fork 1
Test webview messages and server lifecycle #146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rcosta358
wants to merge
4
commits into
codex/issue-132-passing-min-vscode
Choose a base branch
from
codex/issue-133-webview-lifecycle
base: codex/issue-132-passing-min-vscode
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
67037d6
Test real webview messaging and server lifecycle
rcosta358 d6e718e
Reset the webview before observing readiness
rcosta358 bcca949
Merge updated lifecycle test base
rcosta358 6ded2e6
Assert fresh context and concrete webview diagnostics
rcosta358 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| import * as assert from 'node:assert/strict'; | ||
| import * as path from 'node:path'; | ||
| import { readFile, writeFile } from 'node:fs/promises'; | ||
| import * as vscode from 'vscode'; | ||
| import type { LJDiagnostic } from '../types/diagnostics'; | ||
| import type { LJContext } from '../types/context'; | ||
| import type { LiquidJavaTestApi } from '../types/test-api'; | ||
|
|
||
| suite('Bundled LiquidJava webview and lifecycle', () => { | ||
| test('sends verification messages and verifies again after Stop, Start, and Restart', async () => { | ||
| const installed = vscode.extensions.getExtension<LiquidJavaTestApi>('AlcidesFonseca.liquid-java'); | ||
| assert.ok(installed); | ||
| const api = await installed.activate(); | ||
| await api.ready; | ||
|
|
||
| const workspace = vscode.workspace.workspaceFolders?.[0]; | ||
| assert.ok(workspace); | ||
| const uri = vscode.Uri.joinPath(workspace.uri, 'src/main/java/FailingRefinement.java'); | ||
| const sameFile = (file: string) => vscode.Uri.file(path.resolve(file)).fsPath === uri.fsPath; | ||
| const subscriptions: vscode.Disposable[] = []; | ||
| const nextEvent = <T>(event: vscode.Event<T>, matches: (value: T) => boolean) => | ||
| new Promise<T>((resolve, reject) => { | ||
| const failure = api.onFailure(() => { | ||
| subscription.dispose(); | ||
| failure.dispose(); | ||
| reject(new Error('LiquidJava verifier crashed while waiting for lifecycle verification')); | ||
| }); | ||
| subscriptions.push(failure); | ||
| const subscription = event(value => { | ||
| if (matches(value)) { | ||
| subscription.dispose(); | ||
| failure.dispose(); | ||
| resolve(value); | ||
| } | ||
| }); | ||
| subscriptions.push(subscription); | ||
| }); | ||
| const nextDiagnostics = () => nextEvent(api.onDiagnostics, () => true); | ||
| const assertFixtureDiagnostics = (diagnostics: LJDiagnostic[]) => { | ||
| const error = diagnostics.find(diagnostic => diagnostic.type === 'refinement-error' && sameFile(diagnostic.file)); | ||
| assert.ok(error, JSON.stringify(diagnostics)); | ||
| assert.equal(error.category, 'error'); | ||
| assert.equal(error.title, 'Refinement Error'); | ||
| assert.ok(error.position); | ||
| assert.ok(error.position.file && sameFile(error.position.file)); | ||
| }; | ||
| const originalSource = await readFile(uri.fsPath, 'utf8'); | ||
| const assertServerRunning = () => { | ||
| const pid = api.getState().serverPid; | ||
| assert.ok(pid, 'the bundled extension must expose its running Java server'); | ||
| assert.doesNotThrow(() => process.kill(pid, 0)); | ||
| return pid; | ||
| }; | ||
| const assertServerStopped = (pid: number) => { | ||
| assert.equal(api.getState().serverPid, undefined); | ||
| assert.throws(() => process.kill(pid, 0), { code: 'ESRCH' }, 'Stop must terminate the Java process'); | ||
| assert.equal(api.getState().status, 'stopped'); | ||
| assert.deepEqual(api.getState().diagnostics, []); | ||
| }; | ||
|
|
||
| try { | ||
| await vscode.commands.executeCommand('workbench.action.closeSidebar'); | ||
| await vscode.commands.executeCommand('workbench.action.closeAllEditors'); | ||
| await vscode.commands.executeCommand('liquidjava.stop'); | ||
| await vscode.commands.executeCommand('liquidjava.start'); | ||
| const ready = nextEvent(api.onWebviewMessage, event => | ||
| event.direction === 'fromWebview' && event.message.type === 'ready'); | ||
| await vscode.commands.executeCommand('liquidjava.showView'); | ||
| await ready; | ||
|
|
||
| const initialDiagnostics = nextDiagnostics(); | ||
| const initialContext = nextEvent(api.onWebviewMessage, event => | ||
| event.direction === 'toWebview' && event.message.type === 'context' && | ||
| event.message.context.localVars.some((variable: LJContext['localVars'][number]) => variable.name === 'valid')); | ||
| const document = await vscode.workspace.openTextDocument(uri); | ||
| await vscode.window.showTextDocument(document); | ||
| const [initialResult] = await Promise.all([initialDiagnostics, initialContext]); | ||
| assertFixtureDiagnostics(initialResult); | ||
|
|
||
| // change the context on disk without triggering automatic verification on save | ||
| assert.ok(originalSource.includes('int valid = 1;')); | ||
| await writeFile(uri.fsPath, originalSource.replace('int valid = 1;', 'int verifiedNow = 1;')); | ||
|
|
||
| const diagnosticMessage = nextEvent(api.onWebviewMessage, event => | ||
| event.direction === 'toWebview' && event.message.type === 'diagnostics'); | ||
| const contextMessage = nextEvent(api.onWebviewMessage, event => | ||
| event.direction === 'toWebview' && event.message.type === 'context' && | ||
| event.message.context.localVars.some((variable: LJContext['localVars'][number]) => variable.name === 'verifiedNow')); | ||
| const manualDiagnostics = nextDiagnostics(); | ||
| await vscode.commands.executeCommand('liquidjava.verify'); | ||
| assert.equal(api.getState().status, 'loading'); | ||
| const [diagnostics, outboundDiagnostics, outboundContext] = await Promise.all([ | ||
| manualDiagnostics, diagnosticMessage, contextMessage, | ||
| ]); | ||
| assertFixtureDiagnostics(diagnostics); | ||
| assertFixtureDiagnostics(outboundDiagnostics.message.diagnostics); | ||
| const context: LJContext = outboundContext.message.context; | ||
| const valid = context.localVars.find(variable => variable.name === 'verifiedNow'); | ||
| assert.ok(valid, JSON.stringify(context)); | ||
| assert.ok(valid.position); | ||
| assert.ok(sameFile(valid.position.file)); | ||
| assert.equal(valid.type, 'int'); | ||
| assert.ok(valid.mainRefinement.includes('> 0')); | ||
| assert.equal(api.getState().status, 'failed'); | ||
|
|
||
| const originalPid = assertServerRunning(); | ||
| const stopped = nextEvent(api.onWebviewMessage, event => | ||
| event.direction === 'toWebview' && event.message.type === 'status' && event.message.status === 'stopped'); | ||
| await vscode.commands.executeCommand('liquidjava.stop'); | ||
| await stopped; | ||
| assertServerStopped(originalPid); | ||
|
|
||
| const startDiagnostics = nextDiagnostics(); | ||
| await vscode.commands.executeCommand('liquidjava.start'); | ||
| assertFixtureDiagnostics(await startDiagnostics); | ||
| const startedPid = assertServerRunning(); | ||
| assert.notEqual(startedPid, originalPid); | ||
| assert.equal(api.getState().status, 'failed'); | ||
|
|
||
| const restartStopped = nextEvent(api.onWebviewMessage, event => | ||
| event.direction === 'toWebview' && event.message.type === 'status' && event.message.status === 'stopped'); | ||
| const restartDiagnostics = nextDiagnostics(); | ||
| await vscode.commands.executeCommand('liquidjava.restart'); | ||
| await restartStopped; | ||
| assertFixtureDiagnostics(await restartDiagnostics); | ||
| const restartedPid = assertServerRunning(); | ||
| assert.notEqual(restartedPid, startedPid); | ||
| assert.throws(() => process.kill(startedPid, 0), { code: 'ESRCH' }); | ||
|
|
||
| const finalDiagnostics = nextDiagnostics(); | ||
| await vscode.commands.executeCommand('liquidjava.verify'); | ||
| assertFixtureDiagnostics(await finalDiagnostics); | ||
| assert.equal(api.getState().status, 'failed'); | ||
| } finally { | ||
| subscriptions.forEach(subscription => subscription.dispose()); | ||
| await writeFile(uri.fsPath, originalSource); | ||
| await vscode.commands.executeCommand('workbench.action.closeAllEditors'); | ||
| await vscode.commands.executeCommand('liquidjava.stop'); | ||
| await vscode.commands.executeCommand('liquidjava.start'); | ||
| } | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
client/test-fixtures/failing/src/main/java/FailingRefinement.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
| import liquidjava.specification.Refinement; | ||
|
|
||
| public class FailingRefinement { | ||
| @Refinement("_ > 0") | ||
| int positive = -1; | ||
| void check() { | ||
| @Refinement("_ > 0") | ||
| int valid = 1; | ||
|
|
||
| @Refinement("_ > 0") | ||
| int positive = -1; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can match the old context.
handleLJDiagnosticssends acontextmessage with the cachedextension.contextfrom the previous verification, so this listener can resolve on that message, not on the context Verify produces. The fixture doesn't change between runs, so the old and new contexts look the same, and the assertions onvalidpass even if Verify stopped sending a context.Suggest waiting for the
contextmessage that follows theliquidjava/contextnotification for this run, for example by clearing the cached context before Verify, or by matching on something that changes per run.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 6ded2e6: changed the fixture on disk between runs and require the renamed variable in the outbound context, so cached context cannot satisfy the manual Verify assertion. Lifecycle tests passed on stable and minimum VS Code.