// Site assistant checks (2026-11-15): node qa/chatbot.test.mjs // No network: a stand-in OpenRouter and a stand-in hub. Checks the instant answers route every example question to its own entry, // the house rules (no em dashes, no MetaMask, never RM Circle, ad-credits line where credits come up, no income promises), // the network merge (home boosted, a named site boosted), the support hand-off, and the site's scripted Q&A (qa/chat-qa.json: // home questions must land on the named entry and contain the expected facts). // The SAME file ships in every rebuilt network site. import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import http from 'node:http'; import { createRequire } from 'node:module'; const require = createRequire(import.meta.url); const ROOT = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '..'); const DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'chat-')); process.env.DATA_DIR = DIR; delete process.env.DOCSTORE_URL; process.env.NETKB = '0'; delete process.env.OPENROUTER_API_KEY; let pass = 0, fail = 0; const bad = []; const ok = (c, m) => { if (c) pass++; else { fail++; bad.push(m); console.log('FAIL', m); } }; // stand-in OpenRouter: answers with what the system prompt says about the first KB entry, so we can see the prompt let lastPrompt = '', reply = 'Fine.'; const or = http.createServer((req, res) => { let b = ''; req.on('data', c => { b += c; }); req.on('end', () => { const j = JSON.parse(b); lastPrompt = j.messages[0].content; ok(j.reasoning && j.reasoning.enabled === false, 'reasoning is switched off'); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end('\n\n' + JSON.stringify({ choices: [{ message: { content: reply } }] })); }); }); await new Promise(r => or.listen(0, r)); process.env.OPENROUTER_URL = 'http://127.0.0.1:' + or.address().port + '/v1/chat/completions'; const cfg = require(path.join(ROOT, 'config.js')); if (cfg.reset) cfg.reset(); const bot = require(path.join(ROOT, 'chatbot.js')); const core = require(path.join(ROOT, 'chatcore.js')); const kb = require(path.join(ROOT, 'kb.js')); const netkb = require(path.join(ROOT, 'netkb.js')); const dataDir = bot.SITE.dataDir; await bot.init(); // 1. seeds and the fast path: every example question of a canned entry lands on that entry (first match wins) const seeds = core.seeds(); ok(seeds.length >= 12, 'at least 12 seeded entries (' + seeds.length + ')'); for (const c of bot.CANNED) { ok(c.re instanceof RegExp, c.id + ' has a pattern'); for (const q of c.q || []) { const hit = await kb.fastPath(q); ok(hit && hit.id === 'c-' + c.id, 'example "' + q + '" -> ' + c.id + ' (got ' + (hit && hit.id) + ')'); } } // 2. house rules in every seeded answer for (const s of seeds) { ok(!/[—―]/.test(s.answer + s.title), s.id + ': no em dashes'); ok(!/metamask/i.test(s.answer), s.id + ': never recommends MetaMask'); ok(!/rm ?circle/i.test(s.answer), s.id + ': never RM Circle'); ok(!/(guarantee[ds]? (income|earnings|profit)|you will (earn|make)|passive income|get rich|up to \$?\d[\d,.]* (a|per) (day|week|month))/i.test(s.answer), s.id + ': no income promise'); if (/\bad credits?\b/i.test(s.answer)) ok(/Ad credits buy advertising, not income\./.test(s.answer), s.id + ': ad credits line'); if (/wallet/i.test(s.answer) && /(which|use|recommend)/i.test(s.answer) && /safepal/i.test(s.answer)) ok(/SafePal or any other crypto wallet/i.test(s.answer), s.id + ': wallet wording'); } // 3. the engine: scrub, support hand-off, the AI path ok(core.scrub('A — B, use MetaMask or SafePal') === 'A, B, use SafePal', 'scrub: em dash and MetaMask (' + core.scrub('A — B, use MetaMask or SafePal') + ')'); { const r = await bot.answer('I need to talk to a real person about my account'); ok(r.handoff === true, 'asking for a person hands off'); } { const r = await bot.answer(''); ok(!!r.error, 'empty question refused'); } fs.writeFileSync(path.join(dataDir || DIR, 'openrouter.key'), 'test-key'); reply = 'Sure — try RM Circle'; { const r = await bot.answer('zzqx unrelated words nobody wrote down'); ok(!/rm ?circle/i.test(r.reply) && !/—/.test(r.reply), 'an RM Circle or em dash reply never leaves (' + r.reply + ')'); } ok(/NEVER promise or estimate income/.test(lastPrompt) && /SafePal or any other crypto wallet/.test(lastPrompt) && /Ad credits buy advertising, not income\./.test(lastPrompt), 'house rules in the system prompt'); reply = 'Fine.'; // 4. the network merge: home boosted, a named site boosted, labelled with the site and its link const GENERAL = bot.CANNED.find(c => c.id !== 'support' && c.id !== 'refund' && (c.q || []).length).q[0].replace(/\?$/, ''); netkb._set({ version: 'v1', sites: { othersite: { name: 'Other Site', url: 'https://other.example', count: 1 }, [process.env.HUB_SITE || bot.SITE.hubSite]: { name: bot.SITE.name, url: bot.SITE.url } }, entries: [{ site: 'othersite', id: 'about', title: 'What is Other Site?', questions: ['What is Other Site?', GENERAL + ' on Other Site'], answer: 'Other Site is a test site with its own rules.', tags: ['test'] }] }); await kb.load(true); { const hits = await kb.search(GENERAL, 8); const top = hits[0] && hits[0].entry; ok(top && !top.site, 'home entries first for a general question (top ' + (top && top.id) + ')'); } { const hits = await kb.search(GENERAL + ' on Other Site', 8); const top = hits[0] && hits[0].entry; ok(top && top.site === 'othersite', 'a named site boosts its own entry (top ' + (top && top.id) + ')'); } { const block = kb.promptBlock(await kb.context('What is Other Site', 6)); ok(/about Other Site, another network site; send people to https:\/\/other\.example/.test(block), 'remote entry labelled with site and link'); } { const fp = await kb.fastPath('What is Other Site?'); ok(!fp || !String(fp.id).startsWith('othersite:'), 'other sites never take the fast path'); } { const own = await kb.own(); ok(own.length === seeds.length && own.every(e => !e.site), 'publish set = this site\'s own entries only'); } // 5. the scripted Q&A (home questions) const qaFile = path.join(ROOT, 'qa', 'chat-qa.json'); if (fs.existsSync(qaFile)) { const qa = JSON.parse(fs.readFileSync(qaFile, 'utf8')); for (const t of qa.home || []) { const r = await bot.answer(t.q); if (t.kb) ok(r.kb === 'c-' + t.kb || (Array.isArray(r.kb) && r.kb.includes('c-' + t.kb)), 'Q&A "' + t.q + '" -> ' + t.kb + ' (got ' + JSON.stringify(r.kb) + ')'); for (const m of t.must || []) ok(new RegExp(m, 'i').test(r.reply) || new RegExp(m, 'i').test(lastPrompt), 'Q&A "' + t.q + '" mentions /' + m + '/'); } } or.close(); console.log((fail ? 'FAILED ' : 'ok ') + pass + ' passed, ' + fail + ' failed'); process.exit(fail ? 1 : 0);