check-blog-http.mjs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. let checked = 0;
  12. for (const locale of new Set(visible.map(a => a.locale))) {
  13. const records = [{ locale, path: indexFor(locale) }, ...visible.filter(a => a.locale === locale).map(a => ({ ...a, path: pathFor(a) }))];
  14. for (const a of records) {
  15. const response = await fetch(`${base}${a.path}`);
  16. assert.equal(response.status, 200, a.path);
  17. assert.equal(response.headers.get('x-robots-tag'), null, `Private route header leaked onto blog: ${a.path}`);
  18. const html = await response.text();
  19. assert(html.includes(`<html lang="${locale}"`), `Wrong HTML language: ${a.path}`);
  20. assert.equal((html.match(/<html\b/g) || []).length, 1, 'Nested HTML root');
  21. assert(html.includes(`rel="canonical" href="https://text.skin${a.path}"`), `Wrong canonical: ${a.path}`);
  22. assert(html.includes(`name="robots" content="${indexing ? 'index' : 'noindex'}, follow"`), `Wrong robots: ${a.path}`);
  23. assert(/<title>[^<]+STRATA<\/title>/.test(html), `Missing title: ${a.path}`);
  24. assert(/name="description" content="[^"]+"/.test(html));
  25. assert(html.includes(`property="og:url" content="https://text.skin${a.path}"`));
  26. const variants = a.id ? published.filter(b => b.id === a.id) : [...new Set(published.map(b => b.locale))].map(locale => ({ locale }));
  27. 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}`);
  28. const alternateCount = (html.match(/rel="alternate" hrefLang=/g) || []).length;
  29. assert.equal(alternateCount, variants.length + (variants.some(b => b.locale === 'en') ? 1 : 0), `Unexpected alternate: ${a.path}`);
  30. assert(!html.includes('nav.blogs'), 'Untranslated key');
  31. if (a.id) {
  32. assert(/<h1>.+?<\/h1>/.test(html), 'Missing server-rendered heading');
  33. assert(/<h2>.+?<\/h2>/.test(html), 'Missing server-rendered article body');
  34. const schema = JSON.parse(html.match(/<script type="application\/ld\+json">(.*?)<\/script>/s)[1]);
  35. assert.equal(schema[0]['@type'], 'Article');
  36. assert.equal(schema[0].inLanguage, locale);
  37. assert.equal(schema[1]['@type'], 'BreadcrumbList');
  38. assert(!('datePublished' in schema[0]), 'Invented publish date');
  39. for (const source of a.sources) assert(html.includes(source.url.replace(/&/g, '&amp;')), `Missing source: ${source.url}`);
  40. assert(html.includes(a.officialBooking.url.replace(/&/g, '&amp;')), 'Missing destination-specific official CTA');
  41. assert(!html.includes('prenotami.esteri.it'), 'Stale Italy CTA');
  42. }
  43. checked++;
  44. }
  45. }
  46. for (const route of ['/xx/blogs', '/en/blogs', '/de/blogs', '/fr/blogs', '/es/blogs', '/it/blogs', '/pt/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']) {
  47. assert.equal((await fetch(`${base}${route}`)).status, 404, `Should 404: ${route}`);
  48. }
  49. for (const route of ['/','/services','/slots','/login','/dashboard','/dashboard/settings','/admin','/payment/test','/payment/confirm','/create-order/test']) {
  50. const response = await fetch(`${base}${route}`);
  51. assert.equal(response.status, 200, `Old route regression: ${route}`);
  52. const html = await response.text(); assert(html.includes('<html lang="zh-CN"'), `Old root altered: ${route}`);
  53. if (/^\/(login|dashboard|admin|payment|create-order)(\/|$)/.test(route)) assert.equal(response.headers.get('x-robots-tag'), 'noindex, nofollow', `Missing privacy indexing header: ${route}`);
  54. }
  55. const queryResponse = await fetch(`${base}/blogs?utm_source=qa`);
  56. assert.equal(queryResponse.status, 200);
  57. const queryHtml = await queryResponse.text();
  58. assert(queryHtml.includes('rel="canonical" href="https://text.skin/blogs"'), 'Query-bearing index canonical must remain clean');
  59. assert(!queryResponse.headers.get('link')?.includes('hreflang'), 'Unexpected conflicting HTTP hreflang');
  60. const sitemap = await (await fetch(`${base}/sitemap.xml`)).text();
  61. const urls = [...sitemap.matchAll(/<loc>(.*?)<\/loc>/g)].map(m => m[1]);
  62. assert.equal(urls.length, indexing ? published.length + new Set(published.map(a => a.locale)).size : 0);
  63. if (indexing) {
  64. const expectedUrls = ['https://text.skin/blogs', ...approvedInventory.map(a => `https://text.skin/blogs/${a.slug}`)].sort();
  65. assert.equal(expectedUrls.length, 4, 'Approved launch scope is 3 English guides plus index');
  66. assert.deepEqual([...urls].sort(), expectedUrls, 'Sitemap must exactly match approved English cohort');
  67. }
  68. for (const url of urls) assert(url.startsWith('https://text.skin/') && /\/blogs(?:\/|$)/.test(url));
  69. const robots = await (await fetch(`${base}/robots.txt`)).text();
  70. assert(robots.includes('Disallow: /api/'));
  71. assert(!/Disallow: \/(?:blogs|admin|dashboard|login|payment|create-order)/.test(robots));
  72. console.log(`PASS: ${checked} server-rendered blog pages; language, metadata, sources, structured data; 13 excluded/unknown routes; query canonical; 10 existing routes; private headers; sitemap and robots.`);