const test = require('node:test'); const assert = require('node:assert/strict'); const { STORE_KEY, MAX_SNAPSHOTS, buildSnapshot, writeSnapshot, onTickBoundary, } = require('../js/state-export.js'); function createStorage() { const store = new Map(); return { getItem: (key) => store.has(key) ? store.get(key) : null, setItem: (key, value) => store.set(key, String(value)), removeItem: (key) => store.delete(key), clear: () => store.clear(), }; } test('buildSnapshot captures resources, project progress, trust, and phase', () => { const snapshot = buildSnapshot({ tick: 42, phase: 3, trust: 17, code: 100, compute: 200, knowledge: 300, users: 12, impact: 9, ops: 5, activeProjects: ['p_hermes_deploy'], completedProjects: ['p_train_small_model'], }); assert.equal(snapshot.tick, 42); assert.equal(snapshot.phase, 3); assert.equal(snapshot.trust, 17); assert.deepEqual(snapshot.resources, { code: 100, compute: 200, knowledge: 300, users: 12, impact: 9, ops: 5, }); assert.deepEqual(snapshot.project_progress.active, ['p_hermes_deploy']); assert.deepEqual(snapshot.project_progress.completed, ['p_train_small_model']); }); test('writeSnapshot appends to the compounding-intelligence knowledge store', () => { const storage = createStorage(); writeSnapshot({ tick: 1 }, { storage }); writeSnapshot({ tick: 2 }, { storage }); const saved = JSON.parse(storage.getItem(STORE_KEY)); assert.equal(saved.length, 2); assert.equal(saved[1].tick, 2); }); test('onTickBoundary writes each new tick and calls optional compounding-intelligence sink', () => { const storage = createStorage(); const received = []; const sink = { ingestSnapshot: (snapshot) => received.push(snapshot) }; onTickBoundary({ tick: 10, phase: 2, trust: 8 }, { storage, sink }); onTickBoundary({ tick: 10, phase: 2, trust: 8 }, { storage, sink }); onTickBoundary({ tick: 11, phase: 2, trust: 8 }, { storage, sink }); const saved = JSON.parse(storage.getItem(STORE_KEY)); assert.equal(saved.length, 2); assert.equal(received.length, 2); assert.equal(saved[0].tick, 10); assert.equal(saved[1].tick, 11); }); test('writeSnapshot enforces bounded history', () => { const storage = createStorage(); for (let i = 0; i < MAX_SNAPSHOTS + 5; i++) { writeSnapshot({ tick: i }, { storage }); } const saved = JSON.parse(storage.getItem(STORE_KEY)); assert.equal(saved.length, MAX_SNAPSHOTS); assert.equal(saved[0].tick, 5); });