|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +const assert = require("node:assert/strict"); |
| 5 | +const { createHash } = require("node:crypto"); |
| 6 | +const fs = require("node:fs/promises"); |
| 7 | +const vscode = require("vscode"); |
| 8 | + |
| 9 | +const COMMAND = "java27.autotest.insertUnresolvedTypeAfterClose"; |
| 10 | +const SOURCE_LEVEL = "org.eclipse.jdt.core.compiler.source"; |
| 11 | +const INSERTION = "MissingJava27Type sentinel;"; |
| 12 | +const INSERTION_LINE = 3; |
| 13 | +const CHANNEL = "Java 27 AutoTest"; |
| 14 | +const COMPLETED = "REOPEN_INSERTION_CONFIRMED"; |
| 15 | + |
| 16 | +const logicalText = text => text.replace(/\r\n/g, "\n"); |
| 17 | +const hash = text => createHash("sha256").update(logicalText(text)).digest("hex"); |
| 18 | + |
| 19 | +async function waitFor(check, description, deadline) { |
| 20 | + while (Date.now() < deadline) { |
| 21 | + if (check()) { |
| 22 | + return; |
| 23 | + } |
| 24 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 25 | + } |
| 26 | + throw new Error(`Timed out waiting for ${description}`); |
| 27 | +} |
| 28 | + |
| 29 | +async function beforeDeadline(promise, description, deadline) { |
| 30 | + let timer; |
| 31 | + try { |
| 32 | + return await Promise.race([ |
| 33 | + promise, |
| 34 | + new Promise((_, reject) => { |
| 35 | + timer = setTimeout(() => reject(new Error(`Timed out waiting for ${description}`)), |
| 36 | + Math.max(0, deadline - Date.now())); |
| 37 | + }), |
| 38 | + ]); |
| 39 | + } finally { |
| 40 | + clearTimeout(timer); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +async function insertAfterClose(output) { |
| 45 | + const deadline = Date.now() + 60_000; |
| 46 | + const record = (phase, fields = {}) => output.info(JSON.stringify({ |
| 47 | + time: new Date().toISOString(), phase, ...fields, |
| 48 | + })); |
| 49 | + const folder = vscode.workspace.workspaceFolders?.[0]; |
| 50 | + assert(folder, "The Java 27 fixture workspace must be open"); |
| 51 | + const uri = vscode.Uri.joinPath(folder.uri, "src", "main", "java", "example", "PrimitivePatterns.java"); |
| 52 | + const isTarget = candidate => candidate.toString() === uri.toString(); |
| 53 | + const document = vscode.workspace.textDocuments.find(candidate => isTarget(candidate.uri)); |
| 54 | + assert(document && !document.isClosed, "PrimitivePatterns.java must be an open text document"); |
| 55 | + assert(!document.isDirty, "Refusing to close a document with unsaved edits"); |
| 56 | + const original = await fs.readFile(uri.fsPath, "utf8"); |
| 57 | + assert.equal(logicalText(document.getText()), logicalText(original), "Editor and disk contents differ"); |
| 58 | + assert(!original.includes(INSERTION), "The unresolved type must not already exist"); |
| 59 | + |
| 60 | + const extension = vscode.extensions.getExtension("redhat.java"); |
| 61 | + assert(extension, "Language Support for Java is not installed"); |
| 62 | + const java = await beforeDeadline(extension.activate(), "Java extension activation", deadline); |
| 63 | + assert(typeof java?.serverReady === "function" && typeof java?.getProjectSettings === "function", |
| 64 | + "Language Support for Java must expose serverReady and getProjectSettings"); |
| 65 | + assert.equal(await beforeDeadline(java.serverReady(), "the standard Java server", deadline), true); |
| 66 | + record("initial-document", { uri: uri.toString(), javaVersion: extension.packageJSON.version, |
| 67 | + documentVersion: document.version, textHash: hash(original) }); |
| 68 | + |
| 69 | + const roundTrip = async phase => { |
| 70 | + const settings = await beforeDeadline(java.getProjectSettings(uri.toString(), [SOURCE_LEVEL]), phase, deadline); |
| 71 | + assert.equal(settings?.[SOURCE_LEVEL], "27", "The Java server must confirm source compliance 27"); |
| 72 | + record(phase, { sourceLevel: settings[SOURCE_LEVEL] }); |
| 73 | + }; |
| 74 | + let closed = false; |
| 75 | + const closeSubscription = vscode.workspace.onDidCloseTextDocument(candidate => { |
| 76 | + if (candidate === document) { |
| 77 | + closed = true; |
| 78 | + record("client-document-closed", { isClosed: candidate.isClosed }); |
| 79 | + } |
| 80 | + }); |
| 81 | + try { |
| 82 | + const tabs = vscode.window.tabGroups.all.flatMap(group => group.tabs).filter(tab => |
| 83 | + tab.input instanceof vscode.TabInputText && isTarget(tab.input.uri)); |
| 84 | + assert(tabs.length > 0, "PrimitivePatterns.java must have a text editor tab"); |
| 85 | + assert.equal(await beforeDeadline(vscode.window.tabGroups.close(tabs), "closing the editor tabs", deadline), true, |
| 86 | + "VS Code refused to close the target editor tabs"); |
| 87 | + await waitFor(() => closed && document.isClosed |
| 88 | + && !vscode.workspace.textDocuments.some(candidate => isTarget(candidate.uri)), |
| 89 | + "the actual text-document close event", deadline); |
| 90 | + } finally { |
| 91 | + closeSubscription.dispose(); |
| 92 | + } |
| 93 | + |
| 94 | + // JDT processes didClose synchronously; a following request on the same connection is the server barrier. |
| 95 | + await roundTrip("server-close-round-trip"); |
| 96 | + assert.equal(await fs.readFile(uri.fsPath, "utf8"), original, "The source changed while closing"); |
| 97 | + const lines = original.split("\n"); |
| 98 | + assert(lines.length >= INSERTION_LINE, "The fixture is missing the insertion line"); |
| 99 | + lines.splice(INSERTION_LINE, 0, INSERTION); |
| 100 | + const updated = lines.join("\n"); |
| 101 | + let diagnosticEvents = 0; |
| 102 | + let contentChanges = 0; |
| 103 | + const errors = () => vscode.languages.getDiagnostics(uri).filter(diagnostic => |
| 104 | + diagnostic.severity === vscode.DiagnosticSeverity.Error); |
| 105 | + const diagnosticsSubscription = vscode.languages.onDidChangeDiagnostics(event => { |
| 106 | + if (event.uris.some(isTarget)) { |
| 107 | + diagnosticEvents++; |
| 108 | + record("target-diagnostics", { errors: errors().map(diagnostic => ({ |
| 109 | + code: diagnostic.code, message: diagnostic.message, range: diagnostic.range, |
| 110 | + })) }); |
| 111 | + } |
| 112 | + }); |
| 113 | + const changeSubscription = vscode.workspace.onDidChangeTextDocument(event => { |
| 114 | + if (isTarget(event.document.uri)) { |
| 115 | + contentChanges += event.contentChanges.length; |
| 116 | + record("client-content-change", { changes: event.contentChanges }); |
| 117 | + } |
| 118 | + }); |
| 119 | + try { |
| 120 | + await fs.writeFile(uri.fsPath, updated); |
| 121 | + record("disk-written", { textHash: hash(updated), insertions: updated.split(INSERTION).length - 1 }); |
| 122 | + const reopened = await beforeDeadline(vscode.workspace.openTextDocument(uri), "a fresh text document", deadline); |
| 123 | + assert.notEqual(reopened, document, "Reopening must create a new text-document lifecycle"); |
| 124 | + assert(!reopened.isClosed, "The replacement text document is closed"); |
| 125 | + assert.equal(logicalText(reopened.getText()), logicalText(updated), "The reopened text differs from disk"); |
| 126 | + await beforeDeadline(vscode.window.showTextDocument(reopened, { preview: false }), "showing the document", deadline); |
| 127 | + await roundTrip("server-open-round-trip"); |
| 128 | + record("full-document-reopened", { documentVersion: reopened.version, textHash: hash(reopened.getText()) }); |
| 129 | + await waitFor(() => { |
| 130 | + const current = errors(); |
| 131 | + assert(!current.some(diagnostic => diagnostic.message.includes("Duplicate field")), |
| 132 | + "The Java server published a duplicate-field diagnostic after full reopen"); |
| 133 | + if (diagnosticEvents === 0 || current.length !== 1) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + const diagnostic = current[0]; |
| 137 | + const code = typeof diagnostic.code === "object" ? diagnostic.code.value : diagnostic.code; |
| 138 | + return String(code) === "16777218" |
| 139 | + && diagnostic.message === "MissingJava27Type cannot be resolved to a type" |
| 140 | + && diagnostic.range.start.line === INSERTION_LINE; |
| 141 | + }, "one fresh target-file unresolved-type diagnostic", deadline); |
| 142 | + assert.equal(contentChanges, 0, "The insertion must not also produce incremental text edits"); |
| 143 | + assert.equal(await fs.readFile(uri.fsPath, "utf8"), updated, "The inserted source changed unexpectedly"); |
| 144 | + record("insertion-verified", { errors: 1, diagnosticEvents, contentChanges, textHash: hash(updated) }); |
| 145 | + output.info(COMPLETED); |
| 146 | + } finally { |
| 147 | + diagnosticsSubscription.dispose(); |
| 148 | + changeSubscription.dispose(); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +function activate(context) { |
| 153 | + const output = vscode.window.createOutputChannel(CHANNEL, { log: true }); |
| 154 | + context.subscriptions.push(output, vscode.commands.registerCommand(COMMAND, async () => { |
| 155 | + output.clear(); |
| 156 | + try { |
| 157 | + await insertAfterClose(output); |
| 158 | + } catch (error) { |
| 159 | + output.error(`REOPEN_FAILED ${error.stack ?? error.message}`); |
| 160 | + vscode.window.showErrorMessage(`Java 27 AutoTest document reopen failed: ${error.message}`); |
| 161 | + throw error; |
| 162 | + } |
| 163 | + })); |
| 164 | +} |
| 165 | + |
| 166 | +module.exports = { activate }; |
0 commit comments