// research/entity-oracle-guard/verify.mjs — the entity's prose guard, against real replies.
//
//   node research/entity-oracle-guard/verify.mjs
//
// /strata/entity-at-the-terminal/ promises the entity "answers only in ASCII art, never a word
// of prose". A claims pass on 2026-09-28 sent the live /api/oracle eight prompts and five replies
// came back as the model's own reasoning ("We need to produce ASCII art ... must not mention
// being AI"), which the CRT would have shown verbatim. worker.js now runs every reply through
// oracleLooksLikeReasoning() and falls through to the next model when it fires. This checks
// that guard against those eight captured replies (replies-2026-09-28.json, verbatim from the
// endpoint) and against every drawing the page itself archives, which must all pass through.
import { readFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { oracleLooksLikeReasoning } from '../../worker.js';

const HERE = dirname(fileURLToPath(import.meta.url));
const ROOT = join(HERE, '..', '..');
let pass = 0, fail = 0;
const check = (label, ok, got = '') => { ok ? pass++ : fail++; console.log(`${ok ? 'ok  ' : 'FAIL'} ${label}${ok ? '' : '  got: ' + got}`); };

const replies = JSON.parse(readFileSync(join(HERE, 'replies-2026-09-28.json'), 'utf8'));
check('eight captured replies', replies.length === 8, replies.length);
replies.forEach((r, i) => {
  const reasoning = /^We need/.test(r.text);           // read by eye: these five are the leaks
  check(`reply ${i} (${r.model.split('/')[1]}): ${reasoning ? 'reasoning is stopped' : 'drawing goes through'}`,
    oracleLooksLikeReasoning(r.text) === reasoning, String(oracleLooksLikeReasoning(r.text)));
});

// The page's own archive: every <pre> drawing on it is the entity speaking as it should.
const html = readFileSync(join(ROOT, 'public', 'strata', 'entity-at-the-terminal', 'index.html'), 'utf8');
const unesc = s => s.replace(/<[^>]+>/g, '').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&amp;/g, '&');
const pres = [...html.matchAll(/<pre[^>]*>([\s\S]*?)<\/pre>/g)].map(m => unesc(m[1])).filter(t => t.trim());
check('the page archives drawings to test against (positive control on the extraction)', pres.length >= 7, pres.length);
pres.forEach((t, i) => check(`archived drawing ${i + 1} goes through`, !oracleLooksLikeReasoning(t), t.slice(0, 40)));

// Decoys: the guard must fire on prose that does not open with a stock phrase, and must let a
// drawing with a short caption through.
check('decoy: two sentences of plain prose are stopped',
  oracleLooksLikeReasoning('The sea is a large body of salt water that covers most of the planet.\nIt is deep and cold and full of strange living things that glow.'));
check('decoy: a fenced drawing with one caption line goes through',
  !oracleLooksLikeReasoning('```\n  ~ ~ ~ ~\n ~ ~ ~ ~ ~\n```\nthe water remembers'));

console.log(`\n${fail === 0 ? '✓' : '✗'} entity-oracle-guard: ${pass} passed, ${fail} failed`);
process.exit(fail ? 1 : 0);
