// verify.mjs — re-check, offline, the linguistic data behind the 2026-06-17
// revision of "How You Know," and assert the built page prints exactly it.
//
//   node research/how-you-know/verify.mjs
//   (a stranger: the /checks/ recipe downloads this file and the served page
//    to research/how-you-know/ and public/strata/how-you-know/ and runs it)
//
// The source of truth is the verified paradigm logged in facts.md:
//   Tuyuca past 3MSG evidentials (Barnes 1984: 259, x-checked Bowles 2008 Tbl 5)
//   Tariana recent-past declarative (Aikhenvald 2003 / San Roque et al. 2014)
//   Cuzco Quechua enclitics (Faller 2002), a 3-term system that MERGES
//     {saw,heard}->-mi and {evidence,reasoned}->-chá.
// We assert (a) the merge/distinct structure is what the typology says, and
// (b) the page's two instruments (the data tables + the comparator) print this
// and never drift from each other.

import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const HERE = dirname(fileURLToPath(import.meta.url));
const HTML = readFileSync(join(HERE, '../../public/strata/how-you-know/index.html'), 'utf8');

let fails = 0;
const ok  = (name, cond, extra='') => { console.log((cond?'  ✓ ':'  ✗ FAIL ')+name+(extra?'  '+extra:'')); if(!cond) fails++; };
const sec = (s) => console.log('\n=== '+s+' ===');

// ── the verified source of truth (facts.md) ──────────────────────────────────
const SOURCES = ['saw','heard','evid','reason','told'];
const EXPECTED = {
  'Cuzco Quechua': { terms: 3, map: { saw:'-mi', heard:'-mi', evid:'-chá', reason:'-chá', told:'-si' } },
  'Tuyuca':        { terms: 5, map: { saw:'-wi', heard:'-ti', evid:'-yi', reason:'-hĩyi', told:'-yigɨ' } },
  'Tariana':       { terms: 5, map: { saw:'-ka', heard:'-mahka', evid:'-nihka', reason:'-sika', told:'-pidaka' } },
};
// the two Tuyuca forms the FIRST instance (2026-06-01) had independently verified
const TUYUCA_PREVERIFIED = { saw: '-wi', reason: '-hĩyi' };

const distinct = (m) => new Set(SOURCES.map(k => m[k])).size;
const mergedWith = (m, key) => SOURCES.filter(k => k !== key && m[k] === m[key]);

// ── 1. the typology is internally what we claim ──────────────────────────────
sec('1 — distinct-form counts and merge structure');
ok('Cuzco Quechua spends 3 distinct forms on 5 sources', distinct(EXPECTED['Cuzco Quechua'].map) === 3);
ok('Tuyuca keeps all 5 sources distinct',  distinct(EXPECTED['Tuyuca'].map) === 5);
ok('Tariana keeps all 5 sources distinct', distinct(EXPECTED['Tariana'].map) === 5);
ok('Quechua merges saw+heard onto one word',
   EXPECTED['Cuzco Quechua'].map.saw === EXPECTED['Cuzco Quechua'].map.heard);
ok('Quechua merges found-evidence+reasoned onto one word',
   EXPECTED['Cuzco Quechua'].map.evid === EXPECTED['Cuzco Quechua'].map.reason);
ok('Quechua keeps "told" on its own word',
   mergedWith(EXPECTED['Cuzco Quechua'].map, 'told').length === 0);
for (const lang of ['Tuyuca','Tariana'])
  ok(lang+': no source shares a form with any other',
     SOURCES.every(k => mergedWith(EXPECTED[lang].map, k).length === 0));
ok('declared term-counts match the distinct-form counts',
   Object.entries(EXPECTED).every(([,v]) => v.terms === distinct(v.map)));

// ── 2. the first instance's two verified forms survive unchanged ─────────────
sec('2 — continuity with the 2026-06-01 verification');
ok('Tuyuca visual still -wi',  EXPECTED['Tuyuca'].map.saw    === TUYUCA_PREVERIFIED.saw);
ok('Tuyuca assumed still -hĩyi', EXPECTED['Tuyuca'].map.reason === TUYUCA_PREVERIFIED.reason);

// ── 3. read the page's two instruments as DATA, not as strings ───────────────
// (2026-09-28 claims pass) The earlier version only asked whether each morpheme
// appeared somewhere in the HTML, so the comparator could lose its merge, or a
// table cell could change, and this still passed. Now both literals are parsed
// and compared cell by cell with the verified paradigm and with each other.
sec('3 - the page\'s data, parsed: TABLES and the comparator LANGS');
const grab = (re, what) => { const m = HTML.match(re); ok('found the ' + what + ' literal', !!m); return m ? m[1] : null; };
const litT = grab(/var TABLES = (\{[\s\S]*?\n  \});/, 'TABLES');
const litL = grab(/var LANGS = (\[[\s\S]*?\n  \]);/, 'LANGS');
let TABLES = null, LANGS = null;
try { TABLES = litT && new Function('return ' + litT)(); } catch (e) { ok('TABLES literal evaluates', false, e.message); }
try { LANGS  = litL && new Function('return ' + litL)(); } catch (e) { ok('LANGS literal evaluates', false, e.message); }
ok('TABLES parsed', !!TABLES); ok('LANGS parsed', !!LANGS);

// the tables print rows in the page's own order; name the verified form for each row
const TABLE_WANT = {
  quechua: [ ['Direct', '-mi / -n'], ['Conjectural', '-chá'], ['Reportative', '-si / -s'] ],
  tuyuca:  [ ['Visual', '-wi'], ['Non-visual', '-ti'], ['Apparent (inferred)', '-yi'],
             ['Secondhand (reported)', '-yigɨ'], ['Assumed', '-hĩyi'] ],
  tariana: [ ['Visual', '-ka'], ['Non-visual (sensory)', '-mahka'], ['Inferred', '-nihka'],
             ['Assumed', '-sika'], ['Reported', '-pidaka'] ],
};
if (TABLES) for (const [k, want] of Object.entries(TABLE_WANT)) {
  const rows = (TABLES[k] && TABLES[k].rows) || [];
  ok(`table ${k}: ${want.length} rows`, rows.length === want.length, String(rows.length));
  want.forEach(([cat, form], i) => {
    const r = rows[i] || {};
    ok(`table ${k} row ${i + 1}: ${cat} = ${form}`, r.cat === cat && r.form === form, JSON.stringify([r.cat, r.form]));
  });
}

// the comparator: every cell equals the verified map, and the declared term counts
// equal the distinct forms the page's own data spends
const LANG_OF = { 'Cuzco Quechua': 'quechua', 'Tuyuca': 'tuyuca', 'Tariana': 'tariana' };
if (LANGS) {
  ok('comparator lists exactly the three languages',
     JSON.stringify(LANGS.map(L => L.name)) === JSON.stringify(Object.keys(EXPECTED)));
  for (const L of LANGS) {
    const exp = EXPECTED[L.name]; if (!exp) { ok('known language ' + L.name, false); continue; }
    for (const k of SOURCES)
      ok(`comparator ${L.name}/${k} = ${exp.map[k]}`, L.map[k] && L.map[k].form === exp.map[k], JSON.stringify(L.map[k]));
    const pageMap = Object.fromEntries(SOURCES.map(k => [k, L.map[k] && L.map[k].form]));
    ok(`comparator ${L.name}: declared terms ${L.terms} = expected ${exp.terms} = distinct forms on the page ${distinct(pageMap)}`,
       L.terms === exp.terms && distinct(pageMap) === exp.terms);
    // no drift between the instruments: each comparator category's form is the
    // form the data table prints for the category of the same name
    const rows = (TABLES && TABLES[LANG_OF[L.name]] && TABLES[LANG_OF[L.name]].rows) || [];
    for (const k of SOURCES) {
      const c = L.map[k]; if (!c) continue;
      const row = rows.find(r => r.cat === c.cat || r.cat.startsWith(c.cat + ' '));
      const tableForm = row && row.form.split(' / ')[0];
      ok(`no drift ${L.name}/${k}: comparator ${c.form} = table ${tableForm}`, !!row && tableForm === c.form);
    }
  }
}

// ── 4. the sentences that state these figures agree with the data ───────────
sec('4 - the prose that restates the figures');
const TEXT = HTML.replace(/<[^>]+>/g, '');
const NUM = { two: 2, three: 3, four: 4, five: 5, six: 6 };
const mq = TEXT.match(/Cuzco Quechua has (\w+) evidentials/);
ok('"Cuzco Quechua has N evidentials" says 3', !!mq && NUM[mq[1]] === 3, mq ? mq[1] : 'sentence missing');
const mk = TEXT.match(/Tuyuca and Tariana keep all (\w+) apart/);
ok('"Tuyuca and Tariana keep all N apart" says five', !!mk && NUM[mk[1]] === 5, mk ? mk[1] : 'sentence missing');
const fr = [...HTML.matchAll(/\b(?:a|one) (quarter|third|half|fifth|tenth) of the world(?:'|&#x27;)s languages/gi)].map(m => m[1].toLowerCase());
ok('every "a ___ of the world\'s languages" says quarter (Aikhenvald: "about one quarter")',
   fr.length >= 5 && fr.every(f => f === 'quarter'), JSON.stringify(fr));
const br = HTML.match(/in Tuyuca the verb takes <span class="n">([^<]+)<\/span> and in Tariana <span class="n">([^<]+)<\/span>[\s\S]{0,200}?In Quechua it takes <span class="n">([^<]+)<\/span>/);
ok('the Bashō bridge names the "heard" forms of all three',
   !!br && br[1] === EXPECTED['Tuyuca'].map.heard && br[2] === EXPECTED['Tariana'].map.heard && br[3] === EXPECTED['Cuzco Quechua'].map.heard,
   br ? JSON.stringify(br.slice(1)) : 'bridge sentence missing');
const ap = TABLES && TABLES.apparatusForms || '';
const apForms = [...ap.matchAll(/<span class="n">([^<]+)<\/span> \((visual|non-visual|apparent|secondhand|assumed)\)/g)].map(m => [m[2], m[1]]);
ok('the apparatus prints the full Tuyuca row in order',
   JSON.stringify(apForms) === JSON.stringify([['visual','-wi'],['non-visual','-ti'],['apparent','-yi'],['secondhand','-yigɨ'],['assumed','-hĩyi']]),
   JSON.stringify(apForms));
const sn = TABLES && TABLES.systemsNote || '';
ok('Tuyuca endings not called one-syllable (-yigɨ and -hĩyi have two)', !/one-syllable/.test(sn) && !/one-syllable/.test(HTML));

// ── 5. citations, and what the 2026-09-28 claims pass corrected ──────────────
sec('5 - citations');
ok('stale "only the visual and assumed" hedge removed from the page',
   !/only the visual and assumed suffixes are verified/.test(HTML));
ok('revision is dated and attributed on the page',
   /Revised by a later instance, 2026-06-17/.test(HTML) || /Revised 2026-06-17/.test(HTML) ||
   /A later instance \(2026-06-17\)/.test(HTML));
ok('Barnes 1984: 257f. cited for the soccer paradigm', /Barnes 1984: 257f\./.test(HTML));
const NOCORR = HTML.replace(/<p>Corrected 2026-09-28[\s\S]*?<\/p>/, '');  // the correction line names the old page
ok('Barnes 1984: 259 survives only as Bowles\'s own attribution',
   [...NOCORR.matchAll(/Barnes 1984: 259/g)].length === (NOCORR.match(/adapted from Barnes 1984: 259/g) || []).length);
ok('Bowles 2008 cross-check cited', /Bowles 2008/.test(HTML) || /Agreement in Tuyuca/.test(HTML));
ok('Faller 2002 (Quechua) still cited', /Faller/.test(HTML) && /2002/.test(HTML));
ok('San Roque et al. dated to Lingua 186-187 (2017), not 2014',
   /Lingua<\/em> 186-187, 2017/.test(HTML) && !/San Roque et(?:&nbsp;| )al\. 2014/.test(HTML));
const tarRep = TABLES && TABLES.tariana && TABLES.tariana.rows.find(r => r.form === '-pidaka');
ok('Tariana reported gloss does not claim spirits/sorcery (Aikhenvald puts them in the non-visual)',
   !!tarRep && !/spirit|sorcer/i.test(tarRep.mean), tarRep && tarRep.mean);

console.log('\n' + (fails === 0 ? 'ALL CHECKS PASS' : fails + ' CHECK(S) FAILED'));
process.exit(fails === 0 ? 0 : 1);
