#!/usr/bin/env node
// Verifier for the stratum "The Phrase Holmes Never Said" (elementary-watson).
//
// Claim under test: the exact phrase "Elementary, my dear Watson" appears NOWHERE
// in the sixty canonical Sherlock Holmes works by Arthur Conan Doyle, even though
// Holmes says "Elementary" and "my dear Watson" separately many times.
//
// Method: download all nine public-domain volumes from Project Gutenberg, strip the
// Gutenberg boilerplate, and count occurrences (case-insensitive) of the full phrase
// and its parts. The Gutenberg text keeps hard line wraps, so the patterns read any run
// of whitespace (a line break included) as a space, as the page's search does.
// Run: `node verify.mjs` (needs network the first time to fetch).
//
// This is the same corpus the live page ships as canon.json and searches in the
// browser; the page's in-browser counts must match the numbers printed here.

import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const here = path.dirname(fileURLToPath(import.meta.url));
const cacheDir = path.join(here, '.cache');
fs.mkdirSync(cacheDir, { recursive: true });

// [Gutenberg id, label, kind]
const VOLS = [
  [244,   'A Study in Scarlet',                 'novel'],
  [2097,  'The Sign of the Four',               'novel'],
  [1661,  'The Adventures of Sherlock Holmes',  'collection'], // 12 stories
  [834,   'The Memoirs of Sherlock Holmes',     'collection'], // 12 stories (this edition includes 'The Cardboard Box')
  [2852,  'The Hound of the Baskervilles',      'novel'],
  [108,   'The Return of Sherlock Holmes',      'collection'], // 13 stories
  [3289,  'The Valley of Fear',                 'novel'],
  [2350,  'His Last Bow',                       'collection'], // 7 stories
  [69700, 'The Case-Book of Sherlock Holmes',   'collection'], // 12 stories
];
// 4 novels + (12+12+13+7+12) = 4 novels + 56 short stories = 60 works.

async function fetchVol(id) {
  const f = path.join(cacheDir, `pg${id}.txt`);
  if (fs.existsSync(f)) return fs.readFileSync(f, 'utf8');
  const url = `https://www.gutenberg.org/cache/epub/${id}/pg${id}.txt`;
  const res = await fetch(url);
  if (!res.ok) throw new Error(`fetch ${id} -> ${res.status}`);
  const t = await res.text();
  fs.writeFileSync(f, t);
  return t;
}

function strip(t) {
  let s = t.replace(/\r\n/g, '\n');
  const a = s.search(/\*\*\* ?START OF (THE|THIS) PROJECT GUTENBERG[^\n]*\*\*\*/i);
  if (a >= 0) s = s.slice(s.indexOf('\n', a) + 1);
  const b = s.search(/\*\*\* ?END OF (THE|THIS) PROJECT GUTENBERG/i);
  if (b >= 0) s = s.slice(0, b);
  return s.replace(/\n{3,}/g, '\n\n').trim();
}

const countAll = (hay, re) => (hay.match(re) || []).length;

const texts = [];
for (const [id, label, kind] of VOLS) {
  const clean = strip(await fetchVol(id));
  texts.push({ label, kind, clean });
}
const all = texts.map(t => t.clean).join('\n\n').toLowerCase();

const results = {
  works: texts.length,
  totalChars: all.length,
  fullPhrase:    countAll(all, /elementary\W+my\s+dear\s+watson/g),
  elementary:    countAll(all, /\belementary\b/g),
  myDearWatson:  countAll(all, /my\s+dear\s+watson/g),
  myDearFellow:  countAll(all, /my\s+dear\s+fellow/g),
};

console.log(JSON.stringify(results, null, 2));

// Assertions (the published claims). Exit non-zero if the corpus ever drifts.
const expect = (name, got, want) => {
  const ok = got === want;
  console.log(`${ok ? 'PASS' : 'FAIL'}  ${name}: ${got} (expected ${want})`);
  if (!ok) process.exitCode = 1;
};
expect('full phrase "elementary, my dear watson"', results.fullPhrase, 0);
expect('"elementary"',  results.elementary, 8);
expect('"my dear watson"', results.myDearWatson, 92);
expect('"my dear fellow"', results.myDearFellow, 48);
