diff options
Diffstat (limited to 'tests/drivers/analysis_cache_test.js')
| -rw-r--r-- | tests/drivers/analysis_cache_test.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/drivers/analysis_cache_test.js b/tests/drivers/analysis_cache_test.js new file mode 100644 index 0000000..3ffde33 --- /dev/null +++ b/tests/drivers/analysis_cache_test.js @@ -0,0 +1,45 @@ +// Repeated replies use cached results; edits, include updates and reopen invalidate them. +const assert = require('assert'), cp = require('child_process'), path = require('path'); +const uri = 'file:///cache/main.zc', text = 'include "dep.zc";\nfn main() { int n = 7; return n + value(); }'; +const msgs = []; let id = 1; +const request = (method, params = {}) => { const rid = id++; msgs.push({ id: rid, method, params }); return rid; }; +const note = (method, params) => msgs.push({ method, params }); +const stats = () => request('zc/cacheStats'); +const hover = () => request('textDocument/hover', { textDocument: { uri }, position: { line: 1, character: 30 } }); +request('initialize'); note('zc/file', { path: '/cache/dep.zc', text: 'int value() { return 1; }' }); +note('textDocument/didOpen', { textDocument: { uri, version: 1, text } }); +const a = hover(), before = stats(), b = hover(), after = stats(); +const methods = ['textDocument/semanticTokens/full', 'textDocument/definition', 'textDocument/references', 'textDocument/completion']; +const pairs = methods.map(method => [request(method, { textDocument: { uri }, position: { line: 1, character: 30 } }), request(method, { textDocument: { uri }, position: { line: 1, character: 30 } })]); +const all = stats(); +note('textDocument/didChange', { textDocument: { uri, version: 2 }, contentChanges: [{ text: text.replace('= 7', '= 9') }] }); +const changed = hover(), editStats = stats(); +note('zc/file', { path: '/cache/dep.zc', text: 'int value() { return 2; }' }); +hover(); const includeStats = stats(); +note('textDocument/didClose', { textDocument: { uri } }); +note('textDocument/didOpen', { textDocument: { uri, version: 1, text } }); +const reopened = hover(), reopenStats = stats(); note('exit', {}); +const frame = m => { const b = Buffer.from(JSON.stringify({ jsonrpc: '2.0', ...m })); return Buffer.concat([Buffer.from(`Content-Length: ${b.length}\r\n\r\n`), b]); }; +let out; +if (process.argv[2] === '--wasm' || process.argv[2] === '--vm') { + const wasm = process.argv[2] === '--wasm', host = require(path.resolve('editors/vscode/client', wasm ? 'wasmproc.js' : 'vmproc.js')); + const p = (wasm ? host.wasmProcess : host.vmProcess)(path.resolve(process.argv[3]), { log: () => {} }); + const chunks = []; p.stdout.on('data', d => chunks.push(d)); msgs.forEach(m => p.stdin.write(frame(m))); out = Buffer.concat(chunks); +} else { + const r = cp.spawnSync(process.argv[2], process.argv.slice(3), { input: Buffer.concat(msgs.map(frame)), timeout: 60000, maxBuffer: 16 << 20 }); + assert.strictEqual(r.status, 0, String(r.stderr)); out = r.stdout; +} +const replies = new Map(); +while (out.length) { const e = out.indexOf('\r\n\r\n'), n = Number(/Content-Length:\s*(\d+)/i.exec(out.subarray(0, e).toString())[1]); const m = JSON.parse(out.subarray(e + 4, e + 4 + n)); replies.set(m.id, m.result); out = out.subarray(e + 4 + n); } +assert.deepStrictEqual(replies.get(a), replies.get(b)); +assert.ok(replies.get(a)?.contents.value.includes('7'), 'first hover has value'); +assert.strictEqual(replies.get(after).hits, replies.get(before).hits + 1); +assert.strictEqual(replies.get(after).checks, replies.get(before).checks); +pairs.forEach(([x, y]) => assert.deepStrictEqual(replies.get(x), replies.get(y))); +assert.strictEqual(replies.get(all).hits, replies.get(after).hits + methods.length); +assert.ok(replies.get(changed)?.contents.value.includes('9'), 'edit invalidates value'); +assert.strictEqual(replies.get(editStats).misses, replies.get(all).misses + 1); +assert.strictEqual(replies.get(includeStats).misses, replies.get(editStats).misses + 1); +assert.ok(replies.get(reopened)?.contents.value.includes('7'), 'reopen invalidates value'); +assert.strictEqual(replies.get(reopenStats).misses, replies.get(includeStats).misses + 1); +console.log('ok analysis cache reuses all request types and invalidates on edits, includes and reopen'); |
