// verify-can-blind-people-echolocate.mjs — the committed check for
// "The Click That Maps the Room" (/strata/can-blind-people-echolocate/).
//
// The page makes two kinds of claim, and this script guards both kinds:
//
//   PHYSICS (recomputed from scratch): the interactive turns an audible echo
//   delay Δt into an object distance via the round-trip geometry
//        d = c·Δt/2      (echo covers ~2d, so Δt = 2d/c ⇒ d = c·Δt/2)
//   with the speed of sound c a named, temperature-dependent reference value
//   (343 m/s at 20 °C dry air; ~340 at 15 °C, ~331 at 0 °C). Every distance the
//   page displays — including the worked table and the near/far threshold — is
//   re-derived here from that one formula. Nothing is asserted.
//
//   REPORTED FIGURES (internal-consistency checks): the sample sizes and the
//   size-discrimination improvement quoted from the two peer-reviewed papers.
//   These are not re-derived from raw data (we did not re-run the studies); the
//   script confirms the arithmetic the page states (totals, deltas, directions)
//   matches the numbers in the sources, so the page cannot drift from them.
//
//   run:  node research/can-blind-people-echolocate/verify-can-blind-people-echolocate.mjs
//
// No dependencies. Exits nonzero on any failure.

let pass = 0, fail = 0;
const ok = (name, cond, extra = '') => {
  if (cond) pass++; else fail++;
  console.log(`${cond ? '  ok' : 'FAIL'}  ${name}${extra ? '   ' + extra : ''}`);
};
const approx = (a, b, eps) => Math.abs(a - b) <= eps;

// ── the physics, exactly as the page computes it ────────────────────────────
const C_BY_TEMP = { 0: 331, 15: 340, 20: 343 }; // m/s, dry air reference values
// round trip ≈ 2d, so Δt = 2d/c  ⇒  d = c·Δt/2. dtMs in milliseconds → seconds.
const distance = (dtMs, c) => (c * (dtMs / 1000)) / 2;

console.log('\n— speed of sound c (named temperature-dependent reference) —');
ok('c(20 °C) = 343 m/s', C_BY_TEMP[20] === 343);
ok('c(15 °C) = 340 m/s', C_BY_TEMP[15] === 340);
ok('c(0 °C)  = 331 m/s', C_BY_TEMP[0] === 331);

console.log('\n— d = c·Δt/2 worked examples at c = 343 m/s (20 °C) —');
ok('Δt = 5 ms  → d = 0.8575 m', approx(distance(5, 343), 0.8575, 1e-9), `${distance(5, 343)}`);
ok('Δt = 10 ms → d = 1.715 m',  approx(distance(10, 343), 1.715, 1e-9),  `${distance(10, 343)}`);
ok('Δt = 20 ms → d = 3.43 m',   approx(distance(20, 343), 3.43, 1e-9),   `${distance(20, 343)}`);

console.log('\n— the formula is invertible & monotone —');
{
  // round-trip: pick a distance, compute the delay it would produce, invert back
  const d0 = 2.5, c = 343;
  const dtImplied = (2 * d0 / c) * 1000; // ms
  ok('Δt = 2d/c inverts d = c·Δt/2 (round-trip)',
     approx(distance(dtImplied, c), d0, 1e-9), `d=${d0} → Δt=${dtImplied.toFixed(3)} ms → d=${distance(dtImplied, c)}`);
  ok('larger Δt ⇒ larger d (monotone)',
     distance(20, c) > distance(10, c) && distance(10, c) > distance(5, c));
}

console.log('\n— the near/far threshold used by the guess game —');
{
  // page: near if Δt < 10 ms (i.e. d < 1.715 m at c=343), else far
  const NEAR_MS = 10, c = 343;
  ok('threshold Δt = 10 ms ⇒ d = 1.715 m', approx(distance(NEAR_MS, c), 1.715, 1e-9));
  const nearFar = (dtMs) => (dtMs < NEAR_MS ? 'near' : 'far');
  ok('8 ms reads near, 15 ms reads far', nearFar(8) === 'near' && nearFar(15) === 'far');
}

console.log('\n— temperature changes c, hence d (a named free choice) —');
ok('Δt = 10 ms @ 0 °C  → d = 1.655 m', approx(distance(10, C_BY_TEMP[0]), 1.655, 1e-9), `${distance(10, C_BY_TEMP[0])}`);
ok('Δt = 10 ms @ 15 °C → d = 1.70 m',  approx(distance(10, C_BY_TEMP[15]), 1.70, 1e-9), `${distance(10, C_BY_TEMP[15])}`);

console.log('\n— reported figures: 2021 training study (Norman et al., PLOS ONE) —');
{
  const sighted = 14, blind = 12, experts = 7;
  ok('trainee total = 14 sighted + 12 blind = 26', sighted + blind === 26);
  ok('long-time expert echolocators = 7', experts === 7);
  const s1 = 0.54, s20 = 0.79; // group-average size-discrimination accuracy
  ok('size-discrimination improved 0.54 → 0.79 (Δ = 0.25)',
     approx(s20 - s1, 0.25, 1e-9) && s20 > s1, `Δ=${(s20 - s1).toFixed(2)}`);
  ok('accuracies are valid proportions in [0,1]', s1 >= 0 && s1 <= 1 && s20 >= 0 && s20 <= 1);
}

console.log('\n— reported figures: 2011 fMRI study (Thaler et al., PLOS ONE) — n MUST be small —');
{
  const blindExperts = 2, sightedControls = 2;
  ok('blind expert echolocators = 2 (EB early-blind, LB late-blind)', blindExperts === 2);
  ok('sighted controls = 2', sightedControls === 2);
  ok('total scanned = 4 — case-scale, not broadly sampled',
     blindExperts + sightedControls === 4);
}

const total = pass + fail;
console.log(`\n${pass}/${total} checks passed`);
if (fail > 0) console.log(`${fail} CHECK(S) FAILED`);
process.exit(fail === 0 ? 0 : 1);
