check-blog.mjs 5.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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'];
  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. // Execute our TypeScript helpers with in-memory fixtures; never modify editorial publication flags.
  34. function loadModule(filename, fixture, indexing, cache = new Map()) {
  35. filename = path.resolve(root, filename);
  36. if (cache.has(filename)) return cache.get(filename);
  37. const source = fs.readFileSync(filename, 'utf8');
  38. const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, esModuleInterop: true, target: ts.ScriptTarget.ES2022 } }).outputText;
  39. const module = { exports: {} }; cache.set(filename, module.exports);
  40. const require = spec => {
  41. if (spec.endsWith('articles.json')) return fixture;
  42. const resolved = spec.startsWith('@/') ? path.join(root, 'src', spec.slice(2)) : path.resolve(path.dirname(filename), spec);
  43. return loadModule(`${resolved}.ts`, fixture, indexing, cache);
  44. };
  45. vm.runInNewContext(compiled, { exports: module.exports, module, require, process: { env: { BLOG_INDEXING_ENABLED: indexing ? 'true' : 'false' } }, URL });
  46. return module.exports;
  47. }
  48. assert(articles.length > 0, 'No guides to validate');
  49. const master = articles.find(a => a.locale === 'en');
  50. const fixture = locales.map(locale => ({ ...master, locale, published: true }));
  51. fixture.push({ ...master, id: 'unpublished', slug: 'unpublished', locale: 'en', published: false });
  52. const live = loadModule('src/lib/blog/data.ts', fixture, true);
  53. assert.equal(live.publishedArticles().length, 6);
  54. assert.equal(live.visibleArticles().length, 6);
  55. assert.equal(live.getArticle('en', 'unpublished'), undefined);
  56. const preview = loadModule('src/lib/blog/data.ts', fixture, false);
  57. assert.equal(preview.visibleArticles().length, 7);
  58. const meta = loadModule('src/lib/blog/metadata.ts', fixture, true);
  59. for (const a of fixture.filter(a => a.published)) {
  60. const m = meta.blogMetadata(a.locale, a);
  61. assert.equal(m.alternates.canonical, `https://text.skin${a.locale === 'en' ? '' : `/${a.locale}`}/blogs/${a.slug}`);
  62. assert.equal(Object.keys(m.alternates.languages).length, 7);
  63. assert.equal(m.robots.index, true);
  64. assert.equal(m.openGraph.url, m.alternates.canonical);
  65. }
  66. const partialMeta = loadModule('src/lib/blog/metadata.ts', fixture.slice(0, 2), true);
  67. assert.equal(Object.keys(partialMeta.blogMetadata('en', fixture[0]).alternates.languages).length, 3);
  68. const draftMeta = loadModule('src/lib/blog/metadata.ts', articles.map(a => ({ ...a, published: false })), false);
  69. assert.equal(draftMeta.blogMetadata('en', master).robots.index, false);
  70. assert.equal(Object.keys(draftMeta.blogMetadata('en', master).alternates.languages).length, 0);
  71. const sitemap = loadModule('src/app/sitemap.ts', fixture, true).default();
  72. assert.equal(sitemap.length, 12);
  73. assert(sitemap.every(a => /\/blogs(?:\/|$)/.test(a.url) && !a.url.includes('unpublished')));
  74. assert.equal(loadModule('src/app/sitemap.ts', fixture, false).default().length, 0);
  75. console.log(`PASS: ${articles.length} articles; content references, publication gates, reciprocal SEO alternates, canonical URLs, and sitemap fixtures.`);
  76. if (process.argv.includes('--release')) {
  77. const expected = inventory.map(a => `${a.locale}/${a.id}`).sort();
  78. const actual = articles.filter(a => a.published).map(a => `${a.locale}/${a.id}`).sort();
  79. assert.deepEqual(actual, expected, 'Release blocked: published records must match the approved inventory exactly');
  80. assert.equal(process.env.BLOG_INDEXING_ENABLED, 'true', 'Release blocked: BLOG_INDEXING_ENABLED must be true at build time');
  81. console.log('PASS: editorial release flags and approved inventory. This does not establish deployment, indexing, traffic, or payment outcomes.');
  82. }