// verify embedded built-in navigation through the extension's vm and wasm hosts. // usage: node tests/drivers/nav_host_test.js vm|wasm path/to/server const assert = require('assert'); const fs = require('fs'); const path = require('path'); const mode = process.argv[2]; const server = path.resolve(process.argv[3]); const client = path.resolve(__dirname, '..', '..', 'editors', 'vscode', 'client'); const processHost = mode === 'vm' ? require(path.join(client, 'vmproc.js')).vmProcess : mode === 'wasm' ? require(path.join(client, 'wasmproc.js')).wasmProcess : null; assert.ok(processHost, 'host must be vm or wasm'); const proc = processHost(server, { log: () => {} }); const replies = new Map(); let inbox = Buffer.alloc(0); proc.stdout.on('data', chunk => { inbox = Buffer.concat([inbox, chunk]); while (true) { const end = inbox.indexOf('\r\n\r\n'); if (end < 0) return; const size = Number(/Content-Length:\s*(\d+)/i.exec(inbox.subarray(0, end).toString())[1]); if (inbox.length < end + 4 + size) return; const message = JSON.parse(inbox.subarray(end + 4, end + 4 + size).toString()); inbox = inbox.subarray(end + 4 + size); if (message.id !== undefined) replies.set(message.id, message); } }); function send(message) { const body = Buffer.from(JSON.stringify({ jsonrpc: '2.0', ...message })); proc.stdin.write(Buffer.concat([Buffer.from(`Content-Length: ${body.length}\r\n\r\n`), body])); } const source = 'include rt; fn main() { return rt.s_to_n(b"42"); }\n'; const needle = 's_to_n'; const offset = source.indexOf(needle); const before = source.slice(0, offset); const position = { line: before.split('\n').length - 1, character: before.length - before.lastIndexOf('\n') - 1 }; send({ id: 1, method: 'initialize', params: { processId: 1, capabilities: {} } }); send({ method: 'initialized', params: {} }); send({ method: 'textDocument/didOpen', params: { textDocument: { uri: 'file:///nav.zc', languageId: 'zc', version: 1, text: source } } }); send({ id: 2, method: 'textDocument/definition', params: { textDocument: { uri: 'file:///nav.zc' }, position } }); send({ id: 3, method: 'zc/source', params: { path: '/rt.zc' } }); send({ method: 'exit' }); const location = replies.get(2)?.result?.[0]; assert.ok(location, 's_to_n has a definition'); assert.strictEqual(location.uri, 'tinyvm:/rt.zc'); const runtime = replies.get(3)?.result; assert.strictEqual(runtime, fs.readFileSync(path.resolve(__dirname, '..', '..', 'src', 'runtime', 'rt.zc'), 'utf8')); const line = runtime.split('\n')[location.range.start.line]; assert.strictEqual(line.slice(location.range.start.character, location.range.end.character), needle); console.log(`ok ${mode} host opens the embedded runtime source for s_to_n`);