aboutsummaryrefslogtreecommitdiff
path: root/tests/drivers/fuzz_lsp.js
diff options
context:
space:
mode:
Diffstat (limited to 'tests/drivers/fuzz_lsp.js')
-rw-r--r--tests/drivers/fuzz_lsp.js89
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/drivers/fuzz_lsp.js b/tests/drivers/fuzz_lsp.js
new file mode 100644
index 0000000..e22a44b
--- /dev/null
+++ b/tests/drivers/fuzz_lsp.js
@@ -0,0 +1,89 @@
+// Deterministic source/protocol fuzzing for diagnostics, caching, and recovery.
+// Usage: node tests/drivers/fuzz_lsp.js path/to/zclsp
+const { spawnSync } = require('child_process');
+const server = process.argv[2] || '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 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, 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`);