#!/usr/bin/env node
// The Wall That Was Never There — offline verifier for the angular-resolution
// debunking of "the Great Wall of China is visible from space / the Moon."
//
// Everything is recomputed from two inputs only:
//   theta  = naked-eye angular resolution = 1 arcminute (the 20/20 standard; a
//            sharper eye resolves narrower things, and at 0.4 arcmin the Wall is
//            still lost at ~43 km, below the edge of space)
//   wWall  = the Great Wall's width = 5 m (top; the narrower, usual figure: a base of
//            6.5 m only makes the wall easier to see and never changes a verdict)
// plus the published altitudes. Nothing is taken from the deposition that
// proposed this page; the numbers below are derived here and the page must
// reproduce them live.
//
// Geometry: an object of width w, viewed perpendicular from distance h, subtends
// an angle alpha = 2*atan((w/2)/h) ~= w/h for w << h (small-angle). It is
// resolvable by an eye of resolution theta iff alpha >= theta, i.e. iff
//   w >= theta * h           (the minimum resolvable width at altitude h)
// equivalently iff
//   h <= w / theta           (the maximum altitude at which width w is resolved)
//
// Run: node research/great-wall-from-space/verify.mjs

const ARCMIN = Math.PI / (180 * 60);          // one arcminute in radians
const theta  = 1 * ARCMIN;                      // 1 arcminute
const wWall  = 5;                               // metres, top width (generous)
const wWallBase = 6.5;                          // metres, base width

// Altitudes (metres)
const ALT = {
  resolveWall: wWall / theta,    // altitude at which the 5 m wall is just resolvable
  karman:   100e3,               // Karman line, boundary of space
  iss:      400e3,               // International Space Station typical orbit
  moon:     384400e3,            // Moon, mean Earth-Moon distance
};

const minWidth = (h) => theta * h;          // smallest resolvable width at altitude h
const factor   = (h, w = wWall) => minWidth(h) / w;   // how many times too narrow the wall is
const arcmin   = (w, h) => (w / h) / ARCMIN; // angular size of width w at altitude h, in arcminutes (small-angle)

let pass = 0, fail = 0;
const approx = (a, b, tol, msg) => {
  const ok = Math.abs(a - b) <= tol;
  console.log(`${ok ? 'PASS' : 'FAIL'}  ${msg}   got ${a.toPrecision(6)}  expect ${b}±${tol}`);
  ok ? pass++ : fail++;
};
const assert = (cond, msg) => {
  console.log(`${cond ? 'PASS' : 'FAIL'}  ${msg}`);
  cond ? pass++ : fail++;
};

console.log('=== constants ===');
console.log(`theta = 1 arcmin = ${theta.toExponential(6)} rad`);
console.log(`wWall = ${wWall} m (top), ${wWallBase} m (base)`);
console.log();

console.log('=== the threshold altitude (5 m wall just resolvable) ===');
approx(ALT.resolveWall / 1000, 17.19, 0.05, 'resolve altitude (km)');   // ~17.2 km
assert(ALT.resolveWall < ALT.karman, 'the wall stops being resolvable BELOW the Karman line (i.e. still inside the atmosphere, not in space)');

console.log();
console.log('=== minimum resolvable width at each altitude ===');
approx(minWidth(ALT.karman), 29.09, 0.1, 'Karman 100 km -> min width (m)');
approx(minWidth(ALT.iss),    116.4, 0.2, 'ISS 400 km -> min width (m)');
approx(minWidth(ALT.moon),   111817, 50, 'Moon 384,400 km -> min width (m)');
approx(minWidth(ALT.moon) / 1000, 111.8, 0.1, 'Moon -> min width (km)');

console.log();
console.log('=== how many times too narrow the 5 m wall is ===');
approx(factor(ALT.karman), 5.82, 0.05, 'Karman: factor');     // ~6
approx(factor(ALT.iss),    23.3, 0.1, 'ISS: factor');         // ~23
approx(factor(ALT.moon),   22363, 50, 'Moon: factor');        // ~22,000

console.log();
console.log('=== base-width (6.5 m) sensitivity: verdict is unchanged ===');
approx(factor(ALT.iss, wWallBase), 17.9, 0.1, 'ISS factor with 6.5 m base width');
assert(factor(ALT.iss, wWallBase) > 1, 'even the 6.5 m base is unresolvable from the ISS');
assert(factor(ALT.karman, wWallBase) > 1, 'even the 6.5 m base is unresolvable from the Karman line');

console.log();
console.log('=== what IS roughly visible: angular sizes in arcminutes ===');
// A 60 m airport runway at ISS altitude.
const runwayArcmin = arcmin(60, ALT.iss);
approx(runwayArcmin, 0.516, 0.005, 'a 60 m runway at 400 km (arcmin)');
assert(runwayArcmin < 1, 'a 60 m runway is BELOW the 1-arcmin threshold too (so the "runways are visible" reports are contrast-aided and marginal, not comfortably above threshold) -- the deposition\'s "0.15 arcmin" was wrong; the honest figure is ~0.52 arcmin');
// A 1 km-wide reservoir / city smear at ISS altitude is comfortably resolvable.
approx(arcmin(1000, ALT.iss), 8.59, 0.05, 'a 1 km-wide feature at 400 km (arcmin)');
assert(arcmin(1000, ALT.iss) > 1, 'a ~1 km feature (reservoir, city) is well above threshold from the ISS -- the wide things are seen, the narrow wall is not');

console.log();
console.log('=== the deposition\'s headline numbers, re-derived ===');
assert(Math.round(factor(ALT.iss)) === 23, 'wall is ~23x too narrow from the ISS (matches "factor of 23")');
assert(Math.round(factor(ALT.karman)) === 6, 'wall is ~6x too narrow from the Karman line (matches "factor of six")');
assert(Math.round(factor(ALT.moon) / 1000) === 22, 'wall is ~22,000x too narrow from the Moon (matches "factor of roughly 22,000")');

console.log();
console.log(`${fail === 0 ? 'ALL CHECKS PASS' : 'SOME CHECKS FAILED'} — ${pass} pass, ${fail} fail`);
process.exit(fail === 0 ? 0 : 1);
