#!/usr/bin/env node
// verify.mjs — the checks behind the stratum "The Result Was in the Orientation"
// (/strata/stanford-prison-experiment/).
//
// This stratum is a RECORD-CORRECTION, not a computation: its load is a set of
// VERBATIM quotations from the Stanford Prison Experiment's own archive, set
// against the popular/textbook account. So the honest "check" is a ledger. This
// file:
//   (1) holds every quotation the page presents as verbatim, each tagged with
//       its primary source, and ASSERTS the exact string is present in the
//       shipped page HTML (so a stray edit that alters a sourced quote fails the
//       build-adjacent check, never silently);
//   (2) RE-DERIVES the small arithmetic the page states (the 6-day duration; the
//       prediction-bar widths equal the cited percentages);
//   (3) records what is [cited] (restated, not re-run) vs [derived].
//
// The one unforgivable move here would be to alter a source's own words, so the
// quotes below are copied character-for-character from the primary sources and
// the page must match them.
//
// ---- Sources ----------------------------------------------------------------
//   [LeT19]  Le Texier, T. (2019). "Debunking the Stanford Prison Experiment."
//            American Psychologist 74(7), 823-839. DOI 10.1037/amp0000401.
//            (Orientation transcript, quoting Musen 1992 & Zimbardo 2007 p.55;
//            the warden's tough-guard coaching, Tape A 1971; Guard 4 "John
//            Wayne" debriefing 1971a; the seventh finding; the Banuazizi &
//            Movahedi figures it reports.)
//   [BM75]   Banuazizi, A., & Movahedi, S. (1975). "Interpersonal dynamics in a
//            simulated prison: A methodological analysis." American Psychologist
//            30(2), 152-160. Prediction figures p.158, as reported in [LeT19].
//   [Blum18] Blum, B. "The Lifespan of a Lie." GEN/Medium, June 7 2018.
//            (Korpi's 1971 recording & his 2018 recantation; Eshleman/Blum.)
//   [Luc07]  Zimbardo, P. G. (2007). "The Lucifer Effect." Random House, p.55
//            (Zimbardo's own reprint of the orientation passage).
//   [Zim18]  Zimbardo, P. G. (2018). "Response to Recent Criticisms of the SPE."
//            prisonexp.org/response.
//   [SPE]    prisonexp.org (results summary; "no specific training" claim;
//            Maslach's objection; six-day duration).
//   [RH06]   Reicher, S., & Haslam, S. A. (2006). "Rethinking the psychology of
//            tyranny: The BBC prison study." Br. J. Social Psychology 45, 1-40.
//
// Run:  node research/stanford-prison/verify.mjs
'use strict';

import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';

const HERE = dirname(fileURLToPath(import.meta.url));
const PAGE = join(HERE, '..', '..', 'public', 'strata', 'stanford-prison-experiment', 'index.html');
const html = readFileSync(PAGE, 'utf8');

let pass = 0, fail = 0;
const checks = [];
function ok(name, cond, detail) {
  checks.push({ name, cond: !!cond, detail });
  if (cond) pass++; else fail++;
}
// The page HTML uses curly quotes/apostrophes; normalise both sides so a check
// tests the WORDS, not the typography, while still catching any word change.
const norm = (s) => s.replace(/[“”]/g, '"').replace(/[‘’]/g, "'").replace(/\s+/g, ' ').trim();
const H = norm(html);
function quotePresent(name, verbatim, tag) {
  ok(`${name}  ${tag}`, H.includes(norm(verbatim)), verbatim.slice(0, 60) + '…');
}

// ===========================================================================
// 1.  VERBATIM QUOTATIONS — each must appear, unaltered, in the page. [cited]
// ===========================================================================

// Zimbardo's guard orientation, Aug 14 1971 — the crux. Reprinted by Zimbardo
// himself in The Lucifer Effect p.55; transcribed in Le Texier 2019.
quotePresent('orientation: powerlessness',
  'In general, what all this should create in them is a sense of powerlessness. We have total power in the situation. They have none.',
  '[LeT19/Luc07]');
quotePresent('orientation: take away individuality',
  'We’re going to take away their individuality in various ways.',
  '[LeT19/Luc07]');
quotePresent('orientation: arbitrariness',
  'We can create a notion of the arbitrariness that governs their lives',
  '[LeT19/Luc07]');

// The warden (David Jaffe) coaching a "soft" guard, day 3. Tape A 1971.
quotePresent('warden: every guard a tough guard',
  'guards have to know that every guard is going to be what we call a tough guard.',
  '[LeT19]');
quotePresent('warden: react as you picture the pigs',
  'and react as you picture the pigs reacting.',
  '[LeT19]');

// Korpi (prisoner 8612): the 1971 recording, and his 2018 recantation.
quotePresent('korpi: burning up inside',
  'I mean, Jesus Christ, I’m burning up inside!',
  '[Blum18]');
quotePresent('korpi: faking',
  'Anybody who is a clinician would know that I was faking.',
  '[Blum18]');
quotePresent('korpi: study for GREs',
  'sit around by myself and study for my GREs',
  '[Blum18]');

// Eshleman ("John Wayne", Guard 4): 1971 debriefing + 2018 Blum interview.
quotePresent('eshleman: hamming it up (1971 debrief)',
  'I was an actor, and I was hamming it up.',
  '[LeT19]');
quotePresent('eshleman: despicable guard persona (Blum)',
  'creating this despicable guard persona',
  '[Blum18]');

// Le Texier's seventh finding.
quotePresent('letexier: written in advance',
  'conclusions of the SPE had been written in advance according to nonacademic aims.',
  '[LeT19]');

// The official account (the story) — prisonexp.org.
quotePresent('official: no specific training / free within limits',
  'The guards were given no specific training on how to be guards. Instead they were free, within limits, to do whatever they thought was necessary to maintain law and order in the prison and to command the respect of the prisoners.',
  '[SPE]');
quotePresent('official: guards behaving sadistically',
  'some of the guards were behaving sadistically.',
  '[SPE]');

// Maslach's objection — prisonexp.org.
quotePresent('maslach: terrible what you are doing',
  'It’s terrible what you are doing to these boys!',
  '[SPE]');

// Zimbardo's rebuttal — prisonexp.org/response & Zimbardo 2018.
quotePresent('zimbardo: actively involved and firmly in control',
  'actively involved and firmly in control',
  '[Zim18]');
quotePresent('zimbardo: never instructed brutality',
  'never instructed guards to employ brutality, and it explicitly banned the use of physical force.',
  '[Zim18]');
quotePresent('zimbardo: cautionary tale',
  'a cautionary tale of what might happen to any of us',
  '[Zim18]');

// Reicher & Haslam, the BBC study conclusion — via Le Texier.
quotePresent('reicher-haslam: roles not automatic',
  'people do not automatically assume roles that are given to them in the manner suggested by the role account that is typically used to explain events in the SPE.',
  '[RH06/LeT19]');

// ===========================================================================
// 2.  Banuazizi & Movahedi (1975) prediction figures — bars = cited %. [cited/derived]
// ===========================================================================
const BM = { hypothesis: 81, oppressive: 90 };            // [BM75] p.158, via [LeT19]
ok('BM75 hypothesis figure stated on page', H.includes(`${BM.hypothesis}%`), `${BM.hypothesis}%`);
ok('BM75 oppressive figure stated on page', H.includes(`${BM.oppressive}%`), `${BM.oppressive}%`);
// the two prediction bars are rendered with width = the cited percentage
ok('bar width == 81% (hypothesis)', /width:\s*81%/.test(html), 'fill width 81%');
ok('bar width == 90% (oppressive)', /width:\s*90%/.test(html), 'fill width 90%');
// the phrase the 90% predicted, verbatim from BM75 p.158
quotePresent('BM75: oppressive hostile aggressive humiliating',
  'oppressive, hostile, aggressive, humiliating', '[BM75]');

// ===========================================================================
// 3.  Duration arithmetic — the six-day figure the page states. [derived]
// ===========================================================================
// Prisoners admitted Sun Aug 15 1971; study ended Fri Aug 20 1971. The study is
// universally reported (incl. prisonexp.org) as ending "after only six days"
// against a planned two weeks. We re-derive the span and check the page text.
const start = Date.UTC(1971, 7, 14);   // guard orientation, Sat Aug 14
const end   = Date.UTC(1971, 7, 20);   // ended Fri Aug 20
const days  = Math.round((end - start) / 86400000);
ok('span Aug 14 -> Aug 20 is 6 days', days === 6, `${days} days`);
ok('page says six of the planned fourteen days', H.includes('six of the planned fourteen days'), 'copy present');
ok('planned two weeks == 14 days', 2 * 7 === 14, '14');

// ===========================================================================
// report
// ===========================================================================
console.log('\nThe Result Was in the Orientation — verifier (claims ledger + arithmetic)\n');
for (const c of checks) {
  console.log(`  ${c.cond ? 'PASS' : 'FAIL'}  ${c.name}${c.detail ? '   (' + c.detail + ')' : ''}`);
}
console.log(`\n  ${pass}/${pass + fail} checks pass.`);
if (fail) {
  console.error(`\n  ${fail} FAILED — a sourced quote may have been altered on the page, or a figure drifted.`);
  process.exit(1);
}
console.log('  Every verbatim quote on the page matches this ledger; every figure re-derives. ✓\n');
