All files / scripts/sitemap-xml validator.ts

100% Statements 13/13
100% Branches 10/10
100% Functions 1/1
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                                              11x   11x 2x     9x 2x     7x 11x   11x 2x     5x 2x     3x 3x    
/**
 * @module Infrastructure/SitemapXml/Validator
 * @category Intelligence Operations / Supporting Infrastructure
 * @name sitemap.xml structural validator
 *
 * @description
 * Cheap structural checks on the generated XML: declaration present,
 * correct sitemap namespace, at least one `<url>` block, at least one
 * `<loc>` tag. Throws a descriptive error on failure so the CLI exits
 * with status 1 in CI.
 *
 * Round-6 split: extracted from `scripts/generate-sitemap.ts`.
 *
 * @author Hack23 AB (Infrastructure Team)
 * @license Apache-2.0
 */
 
/**
 * Validate the structural integrity of a sitemap XML string. Returns
 * `true` on success and throws on the first failed check so callers
 * can rely on a clear stack trace.
 */
export function validateSitemap(xml: string): boolean {
  console.log('✅ Validating sitemap...');
 
  if (!xml.includes('<?xml version="1.0"')) {
    throw new Error('Invalid XML declaration');
  }
 
  if (!xml.includes('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"')) {
    throw new Error('Invalid sitemap namespace');
  }
 
  const urlCount = (xml.match(/<url>/g) || []).length;
  console.log(`  Found ${urlCount} URLs in sitemap`);
 
  if (urlCount === 0) {
    throw new Error('No URLs in sitemap');
  }
 
  if (!xml.includes('<loc>')) {
    throw new Error('Missing <loc> tags');
  }
 
  console.log('  ✅ Sitemap validation passed');
  return true;
}