check-blog-http.mjs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import assert from 'node:assert/strict';
  2. import fs from 'node:fs';
  3. const base = process.env.BLOG_TEST_URL || 'http://127.0.0.1:3108';
  4. const indexing = process.env.BLOG_INDEXING_ENABLED === 'true';
  5. const approvedInventory = JSON.parse(fs.readFileSync('src/content/blog/approved-inventory.json', 'utf8'));
  6. const all = JSON.parse(fs.readFileSync('src/content/blog/articles.json', 'utf8'));
  7. const visible = all.filter(a => !indexing || a.published);
  8. const published = all.filter(a => a.published);
  9. const pathFor = a => `${a.locale === 'en' ? '' : `/${a.locale}`}/blogs/${a.slug}`;
  10. const indexFor = locale => `${locale === 'en' ? '' : `/${locale}`}/blogs`;
  11. const locales = ['en', 'de', 'fr', 'es', 'it', 'pt', 'zh'];
  12. const publicPaths = ['/', '/services', '/slots', '/terms', '/refund-policy', '/privacy', '/cookie-policy', '/contact'];
  13. const localize = (path, locale) => locale === 'en' ? path : '/' + locale + (path === '/' ? '' : path);
  14. const publicCopy = JSON.parse(fs.readFileSync('src/lib/i18n/public-pages.json', 'utf8'));
  15. const serviceIds = { 'netherlands-appointment-booking': 35, 'greece-appointment-booking': 34, 'denmark-appointment-booking': 36 };
  16. const receipt = [];
  17. const escape = value => value.replaceAll('&', '&amp;').replaceAll('"', '&quot;').replaceAll("'", '&#x27;').replaceAll('<', '&lt;').replaceAll('>', '&gt;');
  18. let checked = 0;
  19. for (const locale of new Set(visible.map(a => a.locale))) {
  20. const records = [{ locale, path: indexFor(locale) }, ...visible.filter(a => a.locale === locale).map(a => ({ ...a, path: pathFor(a) }))];
  21. for (const a of records) {
  22. const response = await fetch(`${base}${a.path}`);
  23. assert.equal(response.status, 200, a.path);
  24. assert.equal(response.headers.get('x-robots-tag'), null, `Private route header leaked onto blog: ${a.path}`);
  25. const html = await response.text();
  26. assert(html.includes(`<html lang="${locale}"`), `Wrong HTML language: ${a.path}`);
  27. assert.equal((html.match(/<html\b/g) || []).length, 1, 'Nested HTML root');
  28. assert(html.includes(`rel="canonical" href="https://text.skin${a.path}"`), `Wrong canonical: ${a.path}`);
  29. assert(html.includes(`name="robots" content="${indexing ? 'index' : 'noindex'}, follow"`), `Wrong robots: ${a.path}`);
  30. assert(/<title>[^<]+STRATA<\/title>/.test(html), `Missing title: ${a.path}`);
  31. assert(/name="description" content="[^"]+"/.test(html));
  32. assert(html.includes(`property="og:url" content="https://text.skin${a.path}"`));
  33. const ogLocales = { en:'en_IE', de:'de_DE', fr:'fr_FR', es:'es_ES', it:'it_IT', pt:'pt_PT', zh:'zh_CN' };
  34. assert(html.includes(`property="og:locale" content="${ogLocales[locale]}"`));
  35. assert(html.includes('rel="icon" href="/blog-icon.svg"'), `Missing blog icon: ${a.path}`);
  36. const variants = a.id ? published.filter(b => b.id === a.id) : [...new Set(published.map(b => b.locale))].map(locale => ({ locale }));
  37. for (const b of variants) assert(html.includes(`hrefLang="${b.locale}" href="https://text.skin${b.id ? pathFor(b) : indexFor(b.locale)}"`), `Missing SEO alternate ${a.path} → ${b.locale}`);
  38. const alternateCount = (html.match(/rel="alternate" hrefLang=/g) || []).length;
  39. assert.equal(alternateCount, variants.length + (variants.some(b => b.locale === 'en') ? 1 : 0), `Unexpected alternate: ${a.path}`);
  40. assert(!html.replace(/<script\b[^>]*>.*?<\/script>/gs, '').includes('nav.blogs'), 'Untranslated key in visible markup');
  41. assert(html.includes('data-blog-navigation'), 'Missing shared site navigation wrapper');
  42. assert(!html.includes('class="blog-languages"'), 'Duplicate blog language row must be absent');
  43. for (const target of ['/services', '/slots', '/login'].map(path => localize(path, locale))) assert(html.includes(`href="${target}"`), `Missing shared navigation action: ${target}`);
  44. assert(html.includes('aria-expanded="false"'), 'Mobile menu must SSR closed');
  45. assert(html.includes('<select') && html.includes('lang="'+locale+'"'), 'Missing locale navigation selector');
  46. const options = [...html.matchAll(/<option[^>]*value="([^"]+)"/g)].map(m => m[1]);
  47. const expectedOptions = a.id ? visible.filter(b => b.id === a.id).map(pathFor) : [...new Set(visible.map(b => b.locale))].map(indexFor);
  48. assert.deepEqual([...new Set(options)].sort(), expectedOptions.sort(), 'Navigation must link actual counterparts');
  49. assert([...html.matchAll(/<option[^>]+>/g)].some(([tag]) => tag.includes(`value="${a.path}"`) && tag.includes('selected=""')), 'Current locale counterpart must be selected');
  50. receipt.push({ path: a.path, status: response.status, language: locale, canonical: 'https://text.skin' + a.path, robots: indexing ? 'index, follow' : 'noindex, follow', alternates: alternateCount });
  51. if (a.id) {
  52. assert(html.includes(`data-service-id="${serviceIds[a.id]}"`), 'Missing destination-specific conversion section');
  53. assert(html.includes(`class="blog-service-button" href="${localize('/create-order/' + serviceIds[a.id], locale)}"`), 'Wrong service CTA');
  54. assert(/<h1>.+?<\/h1>/.test(html), 'Missing server-rendered heading');
  55. assert(/<h2\b[^>]*>.+?<\/h2>/.test(html), 'Missing server-rendered article body');
  56. const schema = JSON.parse(html.match(/<script type="application\/ld\+json">(.*?)<\/script>/s)[1]);
  57. assert.equal(schema[0]['@type'], 'Article');
  58. assert.equal(schema[0].inLanguage, locale);
  59. assert.equal(schema[1]['@type'], 'BreadcrumbList');
  60. assert(!('datePublished' in schema[0]), 'Invented publish date');
  61. for (const source of a.sources) assert(html.includes(source.url.replace(/&/g, '&amp;')), `Missing source: ${source.url}`);
  62. assert(html.includes(a.officialBooking.url.replace(/&/g, '&amp;')), 'Missing destination-specific official CTA');
  63. assert(!html.includes('prenotami.esteri.it'), 'Stale Italy CTA');
  64. }
  65. checked++;
  66. }
  67. }
  68. for (const route of [...new Set(visible.map(a => `${indexFor(a.locale)}/nonexistent-guide`)), '/xx/blogs', '/en/blogs', '/blogs/nonexistent-guide', '/de/blogs/nonexistent-guide', '/blogs/italy-visa-appointments-dublin-prenotami', '/blogs/no-italy-visa-appointments-dublin', '/blogs/italy-tourist-visa-documents-ireland', '/nonexistent-route']) {
  69. const response = await fetch(`${base}${route}`);
  70. assert.equal(response.status, 404, `Should 404: ${route}`);
  71. assert(/name="robots" content="noindex(?:,|"|\s)/.test(await response.text()), `Missing error-page noindex: ${route}`);
  72. }
  73. let publicChecked = 0, privateChecked = 0;
  74. for (const locale of locales) {
  75. for (const path of publicPaths) {
  76. const route = localize(path, locale);
  77. const response = await fetch(base + route);
  78. assert.equal(response.status, 200, route);
  79. assert.equal(response.headers.get('x-robots-tag'), null, route);
  80. const html = await response.text();
  81. const canonical = 'https://text.skin' + route;
  82. const robots = indexing || (locale === 'en' && path === '/') ? 'index, follow' : 'noindex, follow';
  83. assert(html.includes(`<html lang="${locale === 'zh' ? 'zh-CN' : locale}"`), route);
  84. assert.equal((html.match(/<html\b/g) || []).length, 1, route);
  85. assert(html.includes(`<title>${escape(publicCopy[locale][path][0])} | STRATA</title>`), route);
  86. assert(html.includes(`name="description" content="${escape(publicCopy[locale][path][1])}"`), route);
  87. assert(html.includes(`name="robots" content="${robots}"`), route);
  88. assert(html.includes(`rel="canonical" href="${canonical}"`), route);
  89. assert.equal((html.match(/rel="canonical"/g) || []).length, 1, route);
  90. assert(html.includes(`property="og:url" content="${canonical}"`), route);
  91. assert.equal((html.match(/rel="alternate" hrefLang=/g) || []).length, 8, route);
  92. for (const other of locales) assert(html.includes(`hrefLang="${other}" href="https://text.skin${localize(path, other)}"`), route);
  93. assert(html.includes(`hrefLang="x-default" href="https://text.skin${path}"`), route);
  94. assert.equal((html.match(/<h1\b/g) || []).length, 1, route);
  95. for (const target of ['/services', '/slots', '/login', '/blogs']) assert(html.includes(`href="${localize(target, locale)}"`), route);
  96. receipt.push({ path: route, status: 200, language: locale, canonical, robots, alternates: 8 });
  97. publicChecked++;
  98. }
  99. for (const path of ['/login', '/dashboard', '/dashboard/settings', '/payment/test', '/payment/confirm', '/create-order/35']) {
  100. const route = localize(path, locale);
  101. const response = await fetch(base + route);
  102. assert.equal(response.status, 200, route);
  103. assert.equal(response.headers.get('x-robots-tag'), 'noindex, nofollow', route);
  104. const html = await response.text();
  105. assert(html.includes(`<html lang="${locale === 'zh' ? 'zh-CN' : locale}"`), route);
  106. assert(html.includes('name="robots" content="noindex, nofollow"'), route);
  107. assert(html.includes(`rel="canonical" href="https://text.skin${route}"`), route);
  108. assert.equal((html.match(/rel="alternate" hrefLang=/g) || []).length, 0, route);
  109. receipt.push({ path: route, status: 200, language: locale, robots: 'noindex, nofollow', private: true });
  110. privateChecked++;
  111. }
  112. const query = await fetch(base + localize('/', locale) + '?utm_source=qa');
  113. assert.equal(query.status, 200);
  114. assert((await query.text()).includes(`rel="canonical" href="https://text.skin${localize('/', locale)}"`), 'Query leaked into canonical');
  115. }
  116. const admin = await fetch(base + '/admin');
  117. assert.equal(admin.status, 200);
  118. assert.equal(admin.headers.get('x-robots-tag'), 'noindex, nofollow');
  119. for (const route of ['/en', '/en/services', '/xx', '/fr/admin', '/de/not-a-page', '/fr/services/missing']) {
  120. const response = await fetch(base + route); assert.equal(response.status, 404, route);
  121. assert((await response.text()).includes('noindex'), '404 must be non-indexable: ' + route);
  122. }
  123. const spoof = await fetch(base + '/fr/services', { headers: { 'x-strata-locale': 'zh', 'x-strata-path': '/' } });
  124. assert((await spoof.text()).includes('<html lang="fr"'), 'Client headers must not override URL language');
  125. const sitemapResponse = await fetch(`${base}/sitemap.xml`);
  126. assert.equal(sitemapResponse.status, 200, 'Sitemap response');
  127. const sitemap = await sitemapResponse.text();
  128. const urls = [...sitemap.matchAll(/<loc>(.*?)<\/loc>/g)].map(m => m[1]);
  129. assert.equal(urls.length, indexing ? publicPaths.length * locales.length + published.length + new Set(published.map(a => a.locale)).size : 0);
  130. if (indexing) {
  131. const expectedUrls = [...locales.flatMap(locale => publicPaths.map(path => 'https://text.skin' + localize(path, locale))), ...new Set(approvedInventory.map(a => `https://text.skin${indexFor(a.locale)}`)), ...approvedInventory.map(a => `https://text.skin${pathFor(a)}`)].sort();
  132. assert.equal(expectedUrls.length, 84, 'Local indexing-enabled scope is 56 public pages and 28 blog pages; not production approval');
  133. assert.deepEqual([...urls].sort(), expectedUrls, 'Sitemap must exactly match approved multilingual cohort');
  134. }
  135. for (const url of urls) assert(url.startsWith('https://text.skin/') && !/\/(?:admin|login|dashboard|payment|create-order)(?:\/|$)/.test(url));
  136. const robotsResponse = await fetch(`${base}/robots.txt`);
  137. assert.equal(robotsResponse.status, 200, 'Robots response');
  138. const robots = await robotsResponse.text();
  139. const iconResponse = await fetch(`${base}/blog-icon.svg`);
  140. assert.equal(iconResponse.status, 200, 'Blog icon response');
  141. assert(iconResponse.headers.get('content-type')?.includes('image/svg+xml'), 'Blog icon MIME type');
  142. assert(robots.includes('Disallow: /api/'));
  143. assert(!/Disallow: \/(?:blogs|admin|dashboard|login|payment|create-order)/.test(robots));
  144. if (process.env.BLOG_HTTP_RECEIPT) fs.writeFileSync(process.env.BLOG_HTTP_RECEIPT, JSON.stringify({ base, indexing, pages: receipt, sitemapUrls: urls, publication: 'Local build verification only; no production approval or deployment' }, null, 2) + '\n');
  145. console.log(`PASS: ${publicChecked} public and ${privateChecked} private localized pages, admin exclusions and ${checked} server-rendered blog pages; language, metadata, sources, structured data; excluded/unknown routes; query canonicals; private headers; sitemap (${urls.length} URLs) and robots.`);