#!/usr/bin/env node
// verify-showing.mjs — drive the showing in a real browser and read what a reader would see.
//
// The page computes its verdicts live from embedded data. Grepping the HTML tells you nothing
// about that, so this loads the file in Chromium, reads the rendered text, and then DOCTORS
// the inputs to prove the demonstration can fail. A demonstration that cannot fail is a
// decoration.
//
//   node research/au-coins/verify-showing.mjs

import { chromium } from 'playwright'
import { pathToFileURL } from 'node:url'
import { resolve } from 'node:path'

const PAGE = pathToFileURL(resolve('public/tools/australian-coins/what-a-mintage-counts/index.html')).href
let pass = 0, fail = 0
const ok = (c, m) => { if (c) { pass++; console.log('  ok   ' + m) } else { fail++; console.log('  FAIL ' + m) } }

const browser = await chromium.launch()
const page = await browser.newPage()
const errors = []
page.on('pageerror', e => errors.push(String(e)))
page.on('console', m => { if (m.type() === 'error') errors.push(m.text()) })
// Loaded over file://, the page's two site-absolute assets (/fonts/fonts.css and
// /media/home-banner.js) cannot resolve. That is an artefact of testing a static file, not a
// fault in the page, so it is excused BY NAME. Any other missing resource still fails, and
// the request log below proves those two were the only ones.
const requestFailures = []
page.on('requestfailed', r => requestFailures.push(r.url()))
const EXCUSED = /\/(fonts\/fonts\.css|media\/home-banner\.js)$/
await page.goto(PAGE, { waitUntil: 'networkidle' })

console.log('the reader sees the 1920 closure')
let closure = await page.textContent('#closure')
ok(closure.includes('7,112,160'), 'the printed total appears in the rendered verdict')
ok(/closes/i.test(closure), 'the verdict says the arithmetic closes')
ok(closure.includes('522,000') && closure.includes('5,280,000'),
  'the converted piece counts are shown')
ok(closure.includes('146,160'), 'the OCR-damaged pence figure is reconstructed on screen')

console.log('\nthe closure can FAIL, which is the whole point')
await page.fill('#u-shilling', '12')
await page.waitForTimeout(120)
closure = await page.textContent('#closure')
ok(/no longer closes/i.test(closure), 'a wrong shillings-per-pound breaks the closure on screen')
ok(closure.includes('report says 522,000'), 'and the page says what the report actually printed')
await page.fill('#u-shilling', '20')
await page.waitForTimeout(120)
ok(/closes/i.test(await page.textContent('#closure')), 'restoring the true unit restores the closure')

console.log('\nthe pound test responds to the reader')
await page.fill('#test', '1639200')
await page.waitForTimeout(120)
let v = await page.textContent('#verdict')
ok(v.includes('£6,830') && /exactly/.test(v), '1,639,200 pennies reads as £6,830 exactly')
await page.fill('#test', '117289')
await page.waitForTimeout(120)
v = await page.textContent('#verdict')
ok(/Not a whole number of pounds/i.test(v), '117,289 is reported as NOT a whole number of pounds')

console.log('\nthe tables actually rendered')
const pennyRows = await page.locator('#pennytab tbody tr').count()
ok(pennyRows === 51, `penny table has ${pennyRows} rows, expected 51`)
const capRows = await page.locator('#captab tbody tr').count()
ok(capRows === 81, `collector-cap table has ${capRows} rows, expected 81`)
const tally = await page.textContent('#tally')
ok(tally.includes('30 of 51'), `tally reads "${tally.trim()}"`)

console.log('\nno console errors')
const unexcused = requestFailures.filter(u => !EXCUSED.test(u))
ok(unexcused.length === 0, `unexcused failed requests: ${unexcused.slice(0, 3).join(' | ') || 'none'}`)
const realErrors = errors.filter(e => !/ERR_FILE_NOT_FOUND/.test(e))
ok(realErrors.length === 0, `script errors: ${realErrors.slice(0, 2).join(' | ') || 'none'}`)
ok(requestFailures.length === 2, `exactly the two known site-absolute assets failed under file:// (saw ${requestFailures.length})`)

await browser.close()
console.log(`\n${pass} passed, ${fail} failed`)
process.exit(fail ? 1 : 0)
