// SEO Tech List controlled test site. Run with Node.js 20+: node fixture.mjs
// Binds only to loopback; no external services or dependencies.
import http from 'node:http';

const origin = 'http://127.0.0.1:4319';
const paths = ['/', '/healthy', '/missing-title', '/duplicate-a', '/duplicate-b', '/noindex', '/missing-description', '/final'];
const page = (path, title, description, extra = '', body = '') => `<!doctype html><html lang="en"><head><meta charset="utf-8">${title ? `<title>${title}</title>` : ''}${description ? `<meta name="description" content="${description}">` : ''}<link rel="canonical" href="${origin}${path}">${extra}</head><body><main><h1>${title || 'Page intentionally missing a title'}</h1><p>This is a controlled SEO audit fixture, created by SEO Tech List. Its defects are intentional and are not observations about any customer website.</p>${body}<p><a href="/">Back to fixture index</a></p></main></body></html>`;
const sitemap = `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${['/', '/healthy', '/duplicate-a', '/duplicate-a', '/noindex', '/gone'].map((path, i) => `<url><loc>${origin}${path}</loc><lastmod>${i === 2 ? 'not-a-date' : '2026-09-29'}</lastmod></url>`).join('')}</urlset>`;
const server = http.createServer((req, res) => {
  const path = new URL(req.url, origin).pathname;
  if (path === '/robots.txt') {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    return res.end('User-agent: *\nAllow: /\n\nUser-agent: GPTBot\nUser-agent: ClaudeBot\nDisallow: /\n');
  }
  if (path === '/sitemap.xml') { res.writeHead(200, { 'Content-Type': 'application/xml' }); return res.end(sitemap); }
  if (path === '/old-url') { res.writeHead(301, { Location: '/final' }); return res.end(); }
  if (!paths.includes(path)) { res.writeHead(404, { 'Content-Type': 'text/plain' }); return res.end('Intentionally missing fixture resource'); }
  const title = path === '/missing-title' ? '' : path.startsWith('/duplicate-') ? 'Shared title for duplicate detection' : `SEO fixture ${path}`;
  const description = path === '/missing-description' ? '' : 'A deliberately small test page for checking how different SEO tools report known defects.';
  let body = '';
  if (path === '/') body = `<ul>${paths.slice(1).map(p => `<li><a href="${p}">${p}</a></li>`).join('')}<li><a href="/old-url">Redirect</a></li><li><a href="/gone">Missing page</a></li><li><a href="/healthy#absent">Missing anchor</a></li></ul>`;
  if (path === '/healthy') body = '<h2 id="present">Existing anchor</h2><img src="/missing-image.svg" alt="Intentionally missing test image">';
  const extra = path === '/noindex' ? '<meta name="robots" content="noindex,follow">' : '';
  res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
  res.end(page(path, title, description, extra, body));
});
server.listen(4319, '127.0.0.1', () => console.log(`Fixture ready at ${origin}; eight HTML pages, one redirect, two missing resources, one missing anchor, metadata and sitemap defects.`));
