// 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`; 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: 'module top;\n wire a;\nendmodule\n' } }, }), 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}`); } const diagnostics = messages.find((m) => m.method === 'textDocument/publishDiagnostics'); if (!diagnostics) fail('the server pushed no diagnostics for the opened document'); 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)`); });