check-blog.mjs 6.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import assert from 'node:assert/strict';
  2. import fs from 'node:fs';
  3. import path from 'node:path';
  4. import vm from 'node:vm';
  5. import ts from 'typescript';
  6. const root = process.cwd();
  7. const articles = JSON.parse(fs.readFileSync('src/content/blog/articles.json', 'utf8'));
  8. const locales = ['en', 'de', 'fr', 'es', 'it', 'pt', 'zh'];
  9. const inventory = JSON.parse(fs.readFileSync('src/content/blog/approved-inventory.json', 'utf8'));
  10. assert.deepEqual(articles.map(a => `${a.locale}/${a.id}/${a.slug}`).sort(), inventory.map(a => `${a.locale}/${a.id}/${a.slug}`).sort(), 'Active collection must match the approved topic/locale/slug inventory');
  11. const keys = new Set();
  12. for (const a of articles) {
  13. assert(locales.includes(a.locale), `Invalid locale: ${a.locale}`);
  14. assert(/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(a.slug), `Invalid slug: ${a.slug}`);
  15. assert(!keys.has(`${a.locale}/${a.slug}`), `Duplicate route: ${a.locale}/${a.slug}`);
  16. keys.add(`${a.locale}/${a.slug}`);
  17. assert(typeof a.published === 'boolean');
  18. assert(a.title.length > 10 && a.description.length > 30 && a.body.length > 1000, `Thin content: ${a.id}/${a.locale}`);
  19. assert(/^\d{4}-\d{2}-\d{2}$/.test(a.reviewedAt) && Number.isFinite(Date.parse(a.reviewedAt)));
  20. assert(!/<\/?(?:script|iframe|html|body)\b/i.test(a.body), 'HTML is not allowed');
  21. assert(!/prenotami|italy-visa/i.test(a.body), 'Archived Italy content must not leak into this cohort');
  22. assert(a.sources.length > 0);
  23. assert(a.destinationLabel && a.officialBooking?.label);
  24. assert(new URL(a.officialBooking.url).protocol === 'https:');
  25. assert(a.sources.some(source => source.url === a.officialBooking.url), 'Official CTA must be a verified source');
  26. for (const source of a.sources) assert(new URL(source.url).protocol === 'https:', 'Sources must use HTTPS');
  27. for (const id of a.relatedIds) assert(articles.some(b => b.locale === a.locale && b.id === id), `Missing related guide: ${id}/${a.locale}`);
  28. for (const match of a.body.matchAll(/\]\((\/(?:[a-z]{2}\/)?blogs\/[^)\s]+)\)/g)) {
  29. const target = match[1].replace(/^\//, '').replace(/^blogs\//, 'en/').replace('/blogs/', '/');
  30. assert(articles.some(b => `${b.locale}/${b.slug}` === target), `Broken contextual link: ${match[1]}`);
  31. }
  32. }
  33. // Every translation retains the reviewed English source set and booking destination.
  34. for (const a of articles.filter(a => a.locale !== 'en')) {
  35. const en = articles.find(b => b.id === a.id && b.locale === 'en');
  36. assert(en, `Missing English master: ${a.id}`);
  37. assert.equal(a.reviewedAt, en.reviewedAt);
  38. assert.equal(a.officialBooking.url, en.officialBooking.url);
  39. assert.deepEqual(a.sources.map(s => s.url).sort(), en.sources.map(s => s.url).sort());
  40. assert.notEqual(a.title, en.title, 'Untranslated title');
  41. assert.notEqual(a.body, en.body, 'Untranslated body');
  42. const externalLinks = body => [...body.matchAll(/\]\((https?:[^)\s]+)\)/g)].map(m => m[1]).sort();
  43. assert.deepEqual(externalLinks(a.body), externalLinks(en.body), 'Translation changed official body links');
  44. assert.equal((a.body.match(/^## /gm) || []).length, (en.body.match(/^## /gm) || []).length, 'Translation omitted a section');
  45. }
  46. // Execute our TypeScript helpers with in-memory fixtures; never modify editorial publication flags.
  47. function loadModule(filename, fixture, indexing, cache = new Map()) {
  48. filename = path.resolve(root, filename);
  49. if (cache.has(filename)) return cache.get(filename);
  50. const source = fs.readFileSync(filename, 'utf8');
  51. const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, esModuleInterop: true, target: ts.ScriptTarget.ES2022 } }).outputText;
  52. const module = { exports: {} }; cache.set(filename, module.exports);
  53. const require = spec => {
  54. if (spec.endsWith('articles.json')) return fixture;
  55. const resolved = spec.startsWith('@/') ? path.join(root, 'src', spec.slice(2)) : path.resolve(path.dirname(filename), spec);
  56. return loadModule(`${resolved}.ts`, fixture, indexing, cache);
  57. };
  58. vm.runInNewContext(compiled, { exports: module.exports, module, require, process: { env: { BLOG_INDEXING_ENABLED: indexing ? 'true' : 'false' } }, URL });
  59. return module.exports;
  60. }
  61. assert(articles.length > 0, 'No guides to validate');
  62. const master = articles.find(a => a.locale === 'en');
  63. const fixture = locales.map(locale => ({ ...master, locale, published: true }));
  64. fixture.push({ ...master, id: 'unpublished', slug: 'unpublished', locale: 'en', published: false });
  65. const live = loadModule('src/lib/blog/data.ts', fixture, true);
  66. assert.equal(live.publishedArticles().length, 7);
  67. assert.equal(live.visibleArticles().length, 7);
  68. assert.equal(live.getArticle('en', 'unpublished'), undefined);
  69. const preview = loadModule('src/lib/blog/data.ts', fixture, false);
  70. assert.equal(preview.visibleArticles().length, 8);
  71. const meta = loadModule('src/lib/blog/metadata.ts', fixture, true);
  72. for (const a of fixture.filter(a => a.published)) {
  73. const m = meta.blogMetadata(a.locale, a);
  74. assert.equal(m.alternates.canonical, `https://text.skin${a.locale === 'en' ? '' : `/${a.locale}`}/blogs/${a.slug}`);
  75. assert.equal(Object.keys(m.alternates.languages).length, 8);
  76. assert.equal(m.robots.index, true);
  77. assert.equal(m.openGraph.url, m.alternates.canonical);
  78. }
  79. const partialMeta = loadModule('src/lib/blog/metadata.ts', fixture.slice(0, 2), true);
  80. assert.equal(Object.keys(partialMeta.blogMetadata('en', fixture[0]).alternates.languages).length, 3);
  81. const draftMeta = loadModule('src/lib/blog/metadata.ts', articles.map(a => ({ ...a, published: false })), false);
  82. assert.equal(draftMeta.blogMetadata('en', master).robots.index, false);
  83. assert.equal(Object.keys(draftMeta.blogMetadata('en', master).alternates.languages).length, 0);
  84. const sitemap = loadModule('src/app/sitemap.ts', fixture, true).default();
  85. assert.equal(sitemap.length, 14);
  86. assert(sitemap.every(a => /\/blogs(?:\/|$)/.test(a.url) && !a.url.includes('unpublished')));
  87. assert.equal(loadModule('src/app/sitemap.ts', fixture, false).default().length, 0);
  88. console.log(`PASS: ${articles.length} articles; content references, publication gates, reciprocal SEO alternates, canonical URLs, and sitemap fixtures.`);
  89. if (process.argv.includes('--release')) {
  90. assert.equal(inventory.length, 21, 'Approved multilingual inventory is three guides in seven languages');
  91. for (const a of articles.filter(a => a.locale === 'en')) assert.deepEqual(articles.filter(b => b.id === a.id).map(b => b.locale).sort(), [...locales].sort(), 'Each guide needs all seven reviewed editions');
  92. const expected = inventory.map(a => `${a.locale}/${a.id}`).sort();
  93. const actual = articles.filter(a => a.published).map(a => `${a.locale}/${a.id}`).sort();
  94. assert.deepEqual(actual, expected, 'Release blocked: published records must match the approved inventory exactly');
  95. assert.equal(process.env.BLOG_INDEXING_ENABLED, 'true', 'Release blocked: BLOG_INDEXING_ENABLED must be true at build time');
  96. console.log('PASS: editorial release flags and approved inventory. This does not establish deployment, indexing, traffic, or payment outcomes.');
  97. }