import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import sharp from 'sharp';
import { renderNotFound } from '../src/seo.js';

const template = fs.readFileSync(new URL('../public/404.html', import.meta.url), 'utf8');
const page = renderNotFound({
  template,
  baseUrl: 'https://memes.example',
  pathname: '/missing-node'
});
const styles = fs.readFileSync(new URL('../public/assets/styles.css', import.meta.url), 'utf8');
const artworkPaths = [
  new URL('../public/assets/homo-reseticus-404-276.0fde8f3e.webp', import.meta.url),
  new URL('../public/assets/homo-reseticus-404-552.f829379a.webp', import.meta.url),
  new URL('../public/assets/homo-reseticus-404-828.68be2119.webp', import.meta.url)
];

test('404 page is noindexed and links visitors back to the live feed', () => {
  assert.match(page, /<title>404 \| The Meme Protocol<\/title>/);
  assert.match(page, /<meta name="robots" content="noindex,nofollow,noarchive">/);
  assert.match(page, /class="primary-action not-found-action" href="\/"/);
  assert.doesNotMatch(page, /PAGE_NOT_FOUND/);
  assert.doesNotMatch(page, /THE_REQUESTED_NODE_DOES_NOT_EXIST/);
});

test('404 page carries adjusted site discovery and social metadata', () => {
  assert.match(page, /<meta name="application-name" content="The Meme Protocol">/);
  assert.match(page, /<link rel="manifest" href="\/site\.webmanifest\?v=20260814">/);
  assert.match(page, /<link rel="service-desc"[^>]+href="\/openapi\.json">/);
  assert.match(page, /<link rel="help"[^>]+href="\/meme-api\.skill\.md">/);
  assert.match(page, /<meta property="og:url" content="https:\/\/memes\.example\/missing-node">/);
  assert.match(page, /<meta property="og:image" content="https:\/\/memes\.example\/assets\/homo-reseticus-404-828\.68be2119\.webp">/);
  assert.match(page, /<meta property="og:image:type" content="image\/webp">/);
  assert.match(page, /<meta name="twitter:card" content="summary_large_image">/);
  assert.doesNotMatch(page, /<link rel="canonical"/);
});

test('404 page keeps the homepage protocol menu and replaces its live status', () => {
  assert.match(page, /class="status-line"/);
  assert.match(page, />DOWNLOAD PET<\/a>/);
  assert.match(page, />READ LORE<\/a>/);
  assert.match(page, /ERROR_404 \/\/ NODE_UNREACHABLE/);
});

test('404 page uses responsive artwork with a diffused green backdrop', () => {
  assert.match(page, /src="\/assets\/homo-reseticus-404-552\.f829379a\.webp"/);
  assert.match(page, /srcset="[^"]+276\.0fde8f3e\.webp 276w,[^"]+552\.f829379a\.webp 552w,[^"]+828\.68be2119\.webp 828w"/);
  assert.match(page, /fetchpriority="high"/);
  assert.match(page, /alt="Homo Reseticus surrounded by an unreachable node pattern"/);
  assert.match(styles, /\.not-found-visual::before/);
  assert.match(styles, /rgb\(0 255 65 \/ 48%\)/);
  assert.match(styles, /filter: blur\(36px\)/);
  assert.match(styles, /transform: scale\(1\.24\)/);
  assert.match(styles, /width: clamp\(127px, 26vmin, 276px\)/);
  assert.match(styles, /\.not-found-page \.pulse/);
});

test('404 artwork variants retain lossless WebP transparency', async () => {
  const expectedSizes = [[276, 271], [552, 542], [828, 813]];

  for (const [index, artworkPath] of artworkPaths.entries()) {
    const artwork = fs.readFileSync(artworkPath);
    const metadata = await sharp(artwork).metadata();
    assert.equal(artwork.subarray(0, 4).toString('ascii'), 'RIFF');
    assert.equal(artwork.subarray(8, 12).toString('ascii'), 'WEBP');
    assert.equal(artwork.subarray(12, 16).toString('ascii'), 'VP8L');
    assert.equal(metadata.format, 'webp');
    assert.equal(metadata.hasAlpha, true);
    assert.deepEqual([metadata.width, metadata.height], expectedSizes[index]);
  }
});
