#!/usr/bin/env node
// Rendered-text snapshot of one route. This is the migration's regression net:
// token matching in verify.mjs cannot catch a dropped paragraph, this can.
//
// Usage:
//   node .agents/scripts/snapshot-route.mjs http://localhost:4173/models/
//   node .agents/scripts/snapshot-route.mjs dist/models/index.html
//
// Take a snapshot from the vanilla site BEFORE migrating, then diff the built
// output against it. An empty diff is the proof that nothing was lost.

import { readFileSync } from 'node:fs';

const target = process.argv[2];
if (!target) {
  console.error('usage: snapshot-route.mjs <url|path>');
  process.exit(2);
}

const html = target.startsWith('http')
  ? await (await fetch(target)).text()
  : readFileSync(target, 'utf8');

const text = html
  // Drop anything that is not user-visible prose.
  .replace(/<script[\s\S]*?<\/script>/gi, '')
  .replace(/<style[\s\S]*?<\/style>/gi, '')
  .replace(/<!--[\s\S]*?-->/g, '')
  .replace(/<[^>]+>/g, '\n')
  .replace(/&amp;/g, '&')
  .replace(/&lt;/g, '<')
  .replace(/&gt;/g, '>')
  .replace(/&quot;/g, '"')
  .replace(/&#0?39;/g, "'")
  .replace(/&nbsp;/g, ' ')
  .split('\n')
  .map((line) => line.trim())
  .filter(Boolean)
  .join('\n');

console.log(text);
