aboutsummaryrefslogtreecommitdiff
path: root/tests/drivers/fuzz_lsp.js
blob: f8a1d3bbf9f177e5575d685cbf8fb19a88e57c3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Deterministic source/protocol fuzzing for diagnostics, caching, and recovery.
// Usage: node tests/drivers/fuzz_lsp.js path/to/zclsp
// interpreted hosts (no jit) are far slower; ZC_TEST_TIMEOUT_SCALE stretches every deadline
const TIMEOUT_SCALE = Number(process.env.ZC_TEST_TIMEOUT_SCALE) || 1;
const { spawnSync } = require('child_process');
const server = process.argv[2] || 'build/bin/zclsp';
let state = 0x5a17;
function next() { state = (Math.imul(state, 1664525) + 1013904223) >>> 0; return state; }
function frame(m) {
	const body = Buffer.from(JSON.stringify(m), 'utf8');
	return Buffer.concat([Buffer.from('Content-Length: ' + body.length + '\r\n\r\n'), body]);
}
function parse(buf) {
	const out = []; let at = 0;
	while (at < buf.length) {
		const end = buf.indexOf('\r\n\r\n', at);
		if (end < 0) throw new Error('truncated LSP header');
		const match = /Content-Length: (\d+)/.exec(buf.slice(at, end).toString());
		if (!match) throw new Error('missing Content-Length');
		const size = Number(match[1]), start = end + 4;
		if (start + size > buf.length) throw new Error('truncated LSP body');
		out.push(JSON.parse(buf.slice(start, start + size).toString('utf8')));
		at = start + size;
	}
	return out;
}
const msgs = [{ jsonrpc: '2.0', id: 1, method: 'initialize', params: {} }];
const docs = [];
for (let i = 0; i < 32; i++) {
	const uri = `file:///fuzz-${i}.zc`, n = next() % 1000;
	const broken = i % 3 === 0;
	const initial = `fn calc_${i}(x) { return (x + ${n}) * 2; }\nfn main() { let y = calc_${i}(${n})${broken ? '' : ';'} return y; }\n`;
	const fixed = `fn calc_${i}(x) { return (x + ${n}) * 2; }\nfn main() { let y = calc_${i}(${n}); return y; }\n`;
	docs.push({ uri, broken });
	msgs.push({ jsonrpc: '2.0', method: 'textDocument/didOpen', params: { textDocument: { uri, languageId: 'zc', version: 1, text: initial } } });
	msgs.push({ jsonrpc: '2.0', id: 100 + i, method: 'textDocument/semanticTokens/full', params: { textDocument: { uri } } });
	msgs.push({ jsonrpc: '2.0', method: 'textDocument/didChange', params: { textDocument: { uri, version: 2 }, contentChanges: [{ text: fixed }] } });
}
const stdUri = 'file:///fuzz-std.zc';
msgs.push({ jsonrpc: '2.0', method: 'textDocument/didOpen', params: { textDocument: { uri: stdUri, languageId: 'zc', version: 1, text: 'include std;\nfn main() { return std.Text.starts("alpha", "al"); }\n' } } });

// Keep one server alive while documents are repeatedly opened, made malformed,
// repaired, closed, and reopened at a new version. This exercises cache
// invalidation and stale-diagnostic cleanup across a long-lived editor session.
const churn = new Map();
for (let i = 0; i < 96; i++) {
	const slot = i % 4, uri = `file:///churn-${slot}.zc`, n = next() % 10000;
	const malformed = `fn churn_${slot}(x) { let y = (x + ${n}); return y; }\nfn main() { return churn_${slot}(${n}) }\n`;
	const valid = `fn churn_${slot}(x) { let y = (x + ${n}); return y; }\nfn main() { return churn_${slot}(${n}); }\n`;
	const version = Math.floor(i / 4) * 3 + 1;
	msgs.push({ jsonrpc: '2.0', method: 'textDocument/didOpen', params: { textDocument: { uri, languageId: 'zc', version, text: malformed } } });
	msgs.push({ jsonrpc: '2.0', id: 1000 + i, method: 'textDocument/semanticTokens/full', params: { textDocument: { uri } } });
	msgs.push({ jsonrpc: '2.0', method: 'textDocument/didChange', params: { textDocument: { uri, version: version + 1 }, contentChanges: [{ text: valid }] } });
	msgs.push({ jsonrpc: '2.0', method: 'textDocument/didClose', params: { textDocument: { uri } } });
	churn.set(uri, valid);
}
let churnVersion = 100;
for (const [uri, text] of churn) {
	msgs.push({ jsonrpc: '2.0', method: 'textDocument/didOpen', params: { textDocument: { uri, languageId: 'zc', version: churnVersion++, text } } });
}
msgs.push({ jsonrpc: '2.0', method: 'exit' });
const run = spawnSync(server, [], { input: Buffer.concat(msgs.map(frame)), timeout: 60000 * TIMEOUT_SCALE, maxBuffer: 1 << 28 });
if (run.error || run.status !== 0) throw run.error || new Error(`server exit ${run.status}: ${run.stderr}`);
const out = parse(run.stdout);
const diagnostics = new Map();
for (const m of out) {
	if (m.method === 'textDocument/publishDiagnostics') {
		const items = diagnostics.get(m.params.uri) || [];
		items.push(m.params.diagnostics);
		diagnostics.set(m.params.uri, items);
	}
}
for (let i = 0; i < docs.length; i++) {
	const { uri, broken } = docs[i], batches = diagnostics.get(uri) || [];
	if (batches.length < 2) throw new Error(`${uri}: expected diagnostics for open and change`);
	if (broken && !batches[0].some(d => d.severity === 1)) throw new Error(`${uri}: malformed source was not diagnosed`);
	if (batches[batches.length - 1].some(d => d.severity === 1)) throw new Error(`${uri}: valid edit retained an error`);
	const response = out.find(m => m.id === 100 + i);
	if (!response || response.error || !response.result || response.result.data.length % 5) throw new Error(`${uri}: semantic tokens response was malformed`);
}
for (let i = 0; i < 96; i++) {
	const response = out.find(m => m.id === 1000 + i);
	if (!response || response.error || !response.result || response.result.data.length % 5) throw new Error(`churn request ${i}: malformed semantic tokens response`);
}
const stdDiag = diagnostics.get(stdUri) || [];
if (!stdDiag.length || stdDiag[stdDiag.length - 1].some(d => d.severity === 1)) throw new Error('stdlib include failed LSP source analysis');
for (const uri of churn.keys()) {
	const batches = diagnostics.get(uri) || [];
	if (!batches.length || batches[batches.length - 1].some(d => d.severity === 1)) throw new Error(`${uri}: reopened document retained stale diagnostics`);
}
console.log(`ok   lsp fuzz: ${docs.length} deterministic open/change sequences, 96 long-session cache churns, semantic tokens, and include std`);