240 lines
11 KiB
JavaScript
240 lines
11 KiB
JavaScript
// Pre-publish smoke test: spawn the server exactly the way extension.js does
|
|
// and drive a whole LSP session at it over stdio. Plain node, no dependencies,
|
|
// hand-rolled framing — a check that reused a client library could not catch a
|
|
// framing or startup bug. Mirrors crates/lucisv_server/tests/e2e.rs.
|
|
//
|
|
// node smoke.js [path-to-lucisv-server] # default: `lucisv-server` on PATH
|
|
//
|
|
// Exits 0 on success, 1 on any failure.
|
|
|
|
const { spawn } = require('child_process');
|
|
|
|
const serverPath = process.argv[2] || 'lucisv-server';
|
|
|
|
/** Frame one JSON payload the way LSP wants it. */
|
|
function frame(payload) {
|
|
const body = Buffer.from(JSON.stringify(payload), 'utf8');
|
|
return Buffer.concat([Buffer.from(`Content-Length: ${body.length}\r\n\r\n`, 'ascii'), body]);
|
|
}
|
|
|
|
/** Split a stdout dump into its JSON payloads. */
|
|
function unframe(buffer) {
|
|
const messages = [];
|
|
let at = 0;
|
|
while (at < buffer.length) {
|
|
const headerEnd = buffer.indexOf('\r\n\r\n', at, 'ascii');
|
|
if (headerEnd < 0) break;
|
|
const headers = buffer.toString('ascii', at, headerEnd);
|
|
const match = /Content-Length: *(\d+)/i.exec(headers);
|
|
if (!match) break;
|
|
const start = headerEnd + 4;
|
|
const end = start + Number(match[1]);
|
|
if (end > buffer.length) break;
|
|
messages.push(JSON.parse(buffer.toString('utf8', start, end)));
|
|
at = end;
|
|
}
|
|
return messages;
|
|
}
|
|
|
|
function fail(message) {
|
|
console.error(`smoke: FAIL — ${message}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
// The same spawn contract extension.js uses: no arguments, inherited env, the
|
|
// workspace directory as cwd, LSP on stdin/stdout.
|
|
const server = spawn(serverPath, [], { cwd: process.cwd(), env: process.env, stdio: 'pipe' });
|
|
server.on('error', (error) => fail(`could not spawn "${serverPath}": ${error.message}`));
|
|
|
|
const chunks = [];
|
|
server.stdout.on('data', (chunk) => chunks.push(chunk));
|
|
server.stderr.on('data', (chunk) => process.stderr.write(chunk));
|
|
|
|
// Every message is written before anything is read: the server handles them in
|
|
// order and exits on `exit`, so stdout closes on its own and this can never
|
|
// deadlock waiting for a message that is not coming.
|
|
const uri = `file://${process.cwd()}/smoke_scratch.sv`;
|
|
|
|
// Nested blocks plus a sibling fork: enough for the rainbow's whole contract —
|
|
// which pairs are reported, and what depth each one carries.
|
|
const SOURCE = [
|
|
'module top;',
|
|
' wire a;',
|
|
' initial begin',
|
|
' if (a) begin',
|
|
' b = 1;',
|
|
' end',
|
|
' end',
|
|
' initial fork',
|
|
' c = 1;',
|
|
' join',
|
|
'endmodule',
|
|
'',
|
|
].join('\n');
|
|
|
|
// The same file with the outer `end` taken away: the half-pair the extension
|
|
// paints in its error colour, which is the other half of the rainbow contract.
|
|
const BROKEN = SOURCE.replace(' end\n end\n', ' end\n');
|
|
const brokenUri = `file://${process.cwd()}/smoke_scratch_broken.sv`;
|
|
|
|
server.stdin.end(
|
|
Buffer.concat([
|
|
frame({ jsonrpc: '2.0', id: 1, method: 'initialize', params: { processId: process.pid, rootUri: null, capabilities: {} } }),
|
|
frame({ jsonrpc: '2.0', method: 'initialized', params: {} }),
|
|
frame({
|
|
jsonrpc: '2.0',
|
|
method: 'textDocument/didOpen',
|
|
params: { textDocument: { uri, languageId: 'systemverilog', version: 1, text: SOURCE } },
|
|
}),
|
|
frame({ jsonrpc: '2.0', id: 3, method: 'lucisv/scopePairs', params: { textDocument: { uri } } }),
|
|
frame({
|
|
jsonrpc: '2.0',
|
|
method: 'textDocument/didOpen',
|
|
params: { textDocument: { uri: brokenUri, languageId: 'systemverilog', version: 1, text: BROKEN } },
|
|
}),
|
|
frame({ jsonrpc: '2.0', id: 5, method: 'lucisv/scopePairs', params: { textDocument: { uri: brokenUri } } }),
|
|
// Ghost text, just after the `begin` on line 2 of the buffer that lost its
|
|
// `end`. The extension only ever sends this after opting into
|
|
// `ProposedFeatures`, so an "unhandled method" here is the same kind of
|
|
// mismatch the scopePairs check above exists to catch.
|
|
frame({
|
|
jsonrpc: '2.0',
|
|
id: 6,
|
|
method: 'textDocument/inlineCompletion',
|
|
params: { textDocument: { uri: brokenUri }, position: { line: 2, character: 15 }, context: { triggerKind: 2 } },
|
|
}),
|
|
// The settings push, in exactly the shape `synchronize.configurationSection:
|
|
// 'lucisv'` produces — the whole section, defaults included.
|
|
frame({
|
|
jsonrpc: '2.0',
|
|
method: 'workspace/didChangeConfiguration',
|
|
params: {
|
|
settings: {
|
|
lucisv: { serverPath: 'lucisv-server', rainbowBeginEnd: true, inlineTemplates: true, lineLength: null, indent: 4, exclude: [] },
|
|
},
|
|
},
|
|
}),
|
|
// Formatting asks for a 2-space tab: the pushed `indent` must win, which
|
|
// is rung 2 of the precedence ladder beating rung 3, over the wire.
|
|
frame({
|
|
jsonrpc: '2.0',
|
|
id: 4,
|
|
method: 'textDocument/formatting',
|
|
params: { textDocument: { uri }, options: { tabSize: 2, insertSpaces: true } },
|
|
}),
|
|
frame({ jsonrpc: '2.0', id: 2, method: 'shutdown', params: null }),
|
|
frame({ jsonrpc: '2.0', method: 'exit', params: null }),
|
|
]),
|
|
);
|
|
|
|
server.on('close', (code) => {
|
|
if (code !== 0) fail(`server exited with code ${code}`);
|
|
|
|
const messages = unframe(Buffer.concat(chunks));
|
|
const initialize = messages.find((m) => m.id === 1);
|
|
if (!initialize) fail('no initialize response');
|
|
if (initialize.error) fail(`initialize failed: ${JSON.stringify(initialize.error)}`);
|
|
|
|
const capabilities = initialize.result && initialize.result.capabilities;
|
|
if (!capabilities) fail('initialize response carries no capabilities');
|
|
for (const capability of ['documentFormattingProvider', 'definitionProvider', 'hoverProvider', 'semanticTokensProvider']) {
|
|
if (!capabilities[capability]) fail(`capability missing: ${capability}`);
|
|
}
|
|
|
|
if (!capabilities.documentHighlightProvider) fail('capability missing: documentHighlightProvider');
|
|
if (!capabilities.inlineCompletionProvider) fail('capability missing: inlineCompletionProvider');
|
|
|
|
const diagnostics = messages.find((m) => m.method === 'textDocument/publishDiagnostics');
|
|
if (!diagnostics) fail('the server pushed no diagnostics for the opened document');
|
|
|
|
// The rainbow's data source. Custom method, so nothing advertises it — an
|
|
// "unhandled method" error here is exactly the mismatch this test exists to
|
|
// catch, and it is what an extension paired with an older server would hit.
|
|
const scopePairs = messages.find((m) => m.id === 3);
|
|
if (!scopePairs) fail('no lucisv/scopePairs response');
|
|
if (scopePairs.error) fail(`lucisv/scopePairs failed: ${JSON.stringify(scopePairs.error)}`);
|
|
const pairs = scopePairs.result && scopePairs.result.pairs;
|
|
if (!Array.isArray(pairs)) fail(`lucisv/scopePairs result has no pairs array: ${JSON.stringify(scopePairs.result)}`);
|
|
// Depth counts only the scopes the rainbow draws, so the enclosing module
|
|
// does not count: the outer begin is 0, the begin inside it is 1, and the
|
|
// fork is the outer begin's sibling at 0.
|
|
const depths = pairs.map((p) => p.depth);
|
|
if (JSON.stringify(depths) !== JSON.stringify([0, 1, 0])) {
|
|
fail(`expected depths [0,1,0] (outer begin, inner begin, fork), got ${JSON.stringify(depths)}`);
|
|
}
|
|
// Opener lines, 0-based: `initial begin`, `if (a) begin`, `initial fork`.
|
|
const openLines = pairs.map((p) => p.open.start.line);
|
|
if (JSON.stringify(openLines) !== JSON.stringify([2, 3, 7])) {
|
|
fail(`expected openers on lines [2,3,7], got ${JSON.stringify(openLines)}`);
|
|
}
|
|
const closeLines = pairs.map((p) => p.close.start.line);
|
|
if (JSON.stringify(closeLines) !== JSON.stringify([6, 5, 9])) {
|
|
fail(`expected closers on lines [6,5,9], got ${JSON.stringify(closeLines)}`);
|
|
}
|
|
for (const pair of pairs) {
|
|
for (const half of [pair.open, pair.close]) {
|
|
if (half.end.character <= half.start.character) fail(`empty range in ${JSON.stringify(pair)}`);
|
|
}
|
|
}
|
|
// A balanced buffer still carries the field, empty: that is what clears the
|
|
// error decoration once the user finishes typing the missing `end`.
|
|
if (!Array.isArray(scopePairs.result.unmatched) || scopePairs.result.unmatched.length !== 0) {
|
|
fail(`a balanced buffer must report no unmatched keywords, got ${JSON.stringify(scopePairs.result.unmatched)}`);
|
|
}
|
|
|
|
// ... and the buffer missing its outer `end` reports that `begin`, so the
|
|
// extension can paint it rather than leave it undecorated.
|
|
const broken = messages.find((m) => m.id === 5);
|
|
if (!broken) fail('no lucisv/scopePairs response for the broken buffer');
|
|
if (broken.error) fail(`lucisv/scopePairs failed: ${JSON.stringify(broken.error)}`);
|
|
const unmatched = broken.result && broken.result.unmatched;
|
|
if (!Array.isArray(unmatched) || unmatched.length !== 1) {
|
|
fail(`expected one unmatched keyword, got ${JSON.stringify(unmatched)}`);
|
|
}
|
|
if (unmatched[0].start.line !== 2 || unmatched[0].start.character !== 10) {
|
|
fail(`expected the unclosed begin at 2:10, got ${JSON.stringify(unmatched[0])}`);
|
|
}
|
|
|
|
// Ghost text: the `begin` that lost its `end` gets one offered, spelled the
|
|
// one way the language client turns into a working snippet. A bare-string
|
|
// `insertText` would be inserted as plain text, tabstops and all.
|
|
const ghost = messages.find((m) => m.id === 6);
|
|
if (!ghost) fail('no textDocument/inlineCompletion response');
|
|
if (ghost.error) fail(`textDocument/inlineCompletion failed: ${JSON.stringify(ghost.error)}`);
|
|
const suggestion = ghost.result && ghost.result.items && ghost.result.items[0];
|
|
if (!suggestion) fail(`expected one inline completion, got ${JSON.stringify(ghost.result)}`);
|
|
if (!suggestion.insertText || suggestion.insertText.kind !== 'snippet') {
|
|
fail(`insertText must be a {kind:"snippet"} object, got ${JSON.stringify(suggestion.insertText)}`);
|
|
}
|
|
// The body is indented *relative* to the trigger line — one tab per step,
|
|
// no trace of where in the file it goes — because VS Code's snippet session
|
|
// prepends the insertion line's own leading whitespace to every line after
|
|
// the first and re-spells the result in the editor's tabs-or-spaces. Sending
|
|
// the absolute layout, as 0.6.0 did, indented it twice.
|
|
if (suggestion.insertText.value !== '\n\t$0\nend') {
|
|
fail(`unexpected ghost text: ${JSON.stringify(suggestion.insertText.value)}`);
|
|
}
|
|
|
|
// The settings the extension pushes actually reach the formatter.
|
|
const formatting = messages.find((m) => m.id === 4);
|
|
if (!formatting) fail('no textDocument/formatting response');
|
|
if (formatting.error) fail(`formatting failed: ${JSON.stringify(formatting.error)}`);
|
|
const edits = formatting.result;
|
|
if (!Array.isArray(edits) || edits.length !== 1) {
|
|
fail(`expected one edit re-indenting the buffer, got ${JSON.stringify(edits)}`);
|
|
}
|
|
const indented = edits[0].newText;
|
|
if (!indented.includes('\n wire a;')) {
|
|
fail(`pushed lucisv.indent=4 was not honoured over tabSize=2:\n${indented}`);
|
|
}
|
|
if (indented.includes('\n wire a;')) {
|
|
fail(`the editor tab size won over the pushed setting:\n${indented}`);
|
|
}
|
|
|
|
const shutdown = messages.find((m) => m.id === 2);
|
|
if (!shutdown) fail('no shutdown response');
|
|
if (shutdown.error) fail(`shutdown failed: ${JSON.stringify(shutdown.error)}`);
|
|
|
|
console.log(`smoke: OK — "${serverPath}" completed initialize/didOpen/shutdown/exit (${messages.length} messages)`);
|
|
});
|