User-reported: a begin inside a case (or any scope the rainbow filters off the wire) painted one level deeper than its siblings — the hidden scope read as an invisible begin/end layer. Depth now counts only enclosing RAINBOW_KEYWORDS frames, one const shared by the counter and the wire filter. Corpus: same 8,146 wire pairs, max depth 13 -> 7; depth-0 bucket goes from near-empty to 2,881 (the palette was rotated). Also closes M4 QA's e2e gap: the exclude test now asserts the republish half of reindex(), and that a no-op settings push publishes nothing. Co-Authored-By: Claude Fable 5 <[email protected]>
180 lines
7.5 KiB
JavaScript
180 lines
7.5 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/vfmt_server/tests/e2e.rs.
|
|
//
|
|
// node smoke.js [path-to-vfmt-server] # default: `vfmt-server` on PATH
|
|
//
|
|
// Exits 0 on success, 1 on any failure.
|
|
|
|
const { spawn } = require('child_process');
|
|
|
|
const serverPath = process.argv[2] || 'vfmt-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');
|
|
|
|
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: 'vfmt/scopePairs', params: { textDocument: { uri } } }),
|
|
// The settings push, in exactly the shape `synchronize.configurationSection:
|
|
// 'vfmt'` produces — the whole section, defaults included.
|
|
frame({
|
|
jsonrpc: '2.0',
|
|
method: 'workspace/didChangeConfiguration',
|
|
params: {
|
|
settings: {
|
|
vfmt: { serverPath: 'vfmt-server', rainbowBeginEnd: 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');
|
|
|
|
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 vfmt/scopePairs response');
|
|
if (scopePairs.error) fail(`vfmt/scopePairs failed: ${JSON.stringify(scopePairs.error)}`);
|
|
const pairs = scopePairs.result && scopePairs.result.pairs;
|
|
if (!Array.isArray(pairs)) fail(`vfmt/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)}`);
|
|
}
|
|
}
|
|
|
|
// 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 vfmt.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)`);
|
|
});
|