// The correctness record (src/telemetry.js), without a browser: off by default, // and while it is off nothing is recorded, nothing is written to storage, and the // engine is not even asked; turned on, each answer is one record, the reader's // decision is kept, and the failures go to the corpus call. // // node test/telemetry.test.mjs import { telemetryStore, KEY } from '../src/telemetry.js'; let fails = 0; const check = (ok, what) => { console.log(`${ok ? 'PASS' : 'FAIL'} ${what}`); if (!ok) fails += 1; }; // A storage that remembers every write. function storage() { const m = new Map(); const writes = []; return { getItem: (k) => (m.has(k) ? m.get(k) : null), setItem: (k, v) => { writes.push(k); m.set(k, String(v)); }, removeItem: (k) => m.delete(k), writes, m }; } // An engine that counts what it is asked, and answers as the real one does. function engine() { const asked = []; return { asked, async telemetry(name, args) { asked.push(name); if (name === 'record') return { format: 'wai.quantum.telemetry/1', decision: 'pending', codes: [], repairs: [], ...args.fields }; return { format: 'wai.quantum-intent-conformance-bundle/1', cases: args.records.filter((r) => r.decision === 'rejected').map((r, i) => ({ name: `failure-${i + 1}` })) }; }, }; } const s = storage(); const e = engine(); const t = telemetryStore({ engine: e, storage: s, now: () => 1 }); check(!t.on(), 'it is off by default'); for (let i = 0; i < 5; i++) check((await t.record({ program: 'OPENQASM 3.0;', codes: ['Q103'], verdict: { level: 'refused' } })) === null, `while off, an answer is not recorded (${i + 1})`); t.decide(0, 'accepted'); check(t.records().length === 0, 'while off, the record is empty'); check(s.getItem(KEY) === null && s.writes.length === 0, 'while off, nothing is written to storage, not even a key'); check(e.asked.length === 0, 'while off, the engine is not asked to make a record'); t.set(true); const id = await t.record({ program: 'OPENQASM 3.0;', verdict: { level: 'proof', holds: true } }); check(id === 0 && t.records().length === 1 && t.records()[0].at === 1 && t.records()[0].surface === 'studio', 'turned on, an answer is one record, with its time and surface'); t.decide(id, 'rejected'); check(t.records()[0].decision === 'rejected', 'the reader’s decision is kept'); check(JSON.parse(s.getItem(KEY)).records.length === 1, 'kept in this browser’s storage'); check((await t.corpus()).cases.length === 1 && e.asked.at(-1) === 'corpus', 'the failures go to the engine’s corpus'); t.set(false); const writes = s.writes.length; check((await t.record({ program: 'x' })) === null && t.records().length === 1 && e.asked.filter((n) => n === 'record').length === 1, 'turned off again, nothing more is recorded'); t.decide(0, 'accepted'); check(t.records()[0].decision === 'rejected' && s.writes.length === writes, 'and no decision is changed'); t.clear(); check(t.records().length === 0, 'the record can be deleted'); // A store read back from storage keeps its state. const again = telemetryStore({ engine: e, storage: s }); check(!again.on() && again.records().length === 0, 'read back, it is as it was left'); console.log(fails ? `${fails} check(s) failed` : 'every check passed'); process.exit(fails ? 1 : 0);