// node modules/test/test-world-model-ask.js // Executes the real class from module-world-model.js (no imports, no DOM at class level). const fs = require('fs'); const path = require('path'); const src = fs.readFileSync(path.join(__dirname, '../core/module-world-model.js'), 'utf8'); const WorldModelModule = new Function(src + '\nreturn WorldModelModule;')(); const wm = new WorldModelModule(); const failures = []; const check = (name, cond, detail) => { if (!cond) failures.push(name + (detail ? ' — ' + detail : '')); console.log((cond ? 'PASS' : 'FAIL'), name, detail || ''); }; // no model yet check('empty model digest is empty', wm._modelDigest() === ''); wm._model = { version: 'v4.6.47', synthesized: '2026-10-21T20:00:00.000Z', system: { health: 'ok', memory: '3 MB' }, modules: { loaded: 30, total: 30, modifiedCount: 0, modified: [] }, files: { total: 42, directories: 8, size: '12 KB' }, skills: { count: 23, skills: Array.from({ length: 60 }, (_, i) => 'skill-' + i) }, memory: { nodes: 120, edges: 200, active: true }, agents: { agents: 4, active: 1 }, telemetry: { toolLatencyDataPoints: 9, recentErrors: 0 }, projects: { totalTasks: 3, activeProjects: [] }, recent: ['Skills directory: 23 files'] }; const digest = wm._modelDigest(); check('digest is valid JSON', (() => { try { JSON.parse(digest); return true; } catch (e) { return false; } })()); check('digest is capped at 4000', digest.length <= 4000, digest.length + ' chars'); check('digest keeps counts', /"count":23/.test(digest) && /"loaded":30/.test(digest)); check('digest trims the skill list to 40', (JSON.parse(digest).skills.skills || []).length === 40, String((JSON.parse(digest).skills.skills || []).length)); const prompt = wm._buildAskPrompt(' why did the build break? '); check('prompt carries the question', /QUESTION: why did the build break\?$/.test(prompt), prompt.slice(-60)); check('prompt carries the snapshot', prompt.indexOf('SNAPSHOT: {') > 0); check('prompt forbids invention', /Never invent numbers/.test(prompt)); check('facts path still answers a keyword', /30 modules/.test(wm._answerQuery('how many modules'))); check('facts path still refuses nonsense', /Unknown query/.test(wm._answerQuery('why did it break'))); console.log(failures.length ? '\n' + failures.length + ' FAILED' : '\nall PASS'); process.exit(failures.length ? 1 : 0);