| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- import assert from 'node:assert/strict';
- import fs from 'node:fs';
- import path from 'node:path';
- import vm from 'node:vm';
- import ts from 'typescript';
- const root = process.cwd();
- const articles = JSON.parse(fs.readFileSync('src/content/blog/articles.json', 'utf8'));
- const locales = ['en', 'de', 'fr', 'es', 'it', 'pt', 'zh'];
- const inventory = JSON.parse(fs.readFileSync('src/content/blog/approved-inventory.json', 'utf8'));
- 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');
- const keys = new Set();
- for (const a of articles) {
- assert(locales.includes(a.locale), `Invalid locale: ${a.locale}`);
- assert(/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(a.slug), `Invalid slug: ${a.slug}`);
- assert(!keys.has(`${a.locale}/${a.slug}`), `Duplicate route: ${a.locale}/${a.slug}`);
- keys.add(`${a.locale}/${a.slug}`);
- assert(typeof a.published === 'boolean');
- assert(a.title.length > 10 && a.description.length > 30 && a.body.length > 1000, `Thin content: ${a.id}/${a.locale}`);
- assert(/^\d{4}-\d{2}-\d{2}$/.test(a.reviewedAt) && Number.isFinite(Date.parse(a.reviewedAt)));
- assert(!/<\/?(?:script|iframe|html|body)\b/i.test(a.body), 'HTML is not allowed');
- assert(!/prenotami|italy-visa/i.test(a.body), 'Archived Italy content must not leak into this cohort');
- assert(a.sources.length > 0);
- assert(a.destinationLabel && a.officialBooking?.label);
- assert(new URL(a.officialBooking.url).protocol === 'https:');
- assert(a.sources.some(source => source.url === a.officialBooking.url), 'Official CTA must be a verified source');
- for (const source of a.sources) assert(new URL(source.url).protocol === 'https:', 'Sources must use HTTPS');
- for (const id of a.relatedIds) assert(articles.some(b => b.locale === a.locale && b.id === id), `Missing related guide: ${id}/${a.locale}`);
- for (const match of a.body.matchAll(/\]\((\/(?:[a-z]{2}\/)?blogs\/[^)\s]+)\)/g)) {
- const target = match[1].replace(/^\//, '').replace(/^blogs\//, 'en/').replace('/blogs/', '/');
- assert(articles.some(b => `${b.locale}/${b.slug}` === target), `Broken contextual link: ${match[1]}`);
- }
- }
- // Every translation retains the reviewed English source set and booking destination.
- for (const a of articles.filter(a => a.locale !== 'en')) {
- const en = articles.find(b => b.id === a.id && b.locale === 'en');
- assert(en, `Missing English master: ${a.id}`);
- assert.equal(a.reviewedAt, en.reviewedAt);
- assert.equal(a.officialBooking.url, en.officialBooking.url);
- assert.deepEqual(a.sources.map(s => s.url).sort(), en.sources.map(s => s.url).sort());
- assert.notEqual(a.title, en.title, 'Untranslated title');
- assert.notEqual(a.body, en.body, 'Untranslated body');
- const externalLinks = body => [...body.matchAll(/\]\((https?:[^)\s]+)\)/g)].map(m => m[1]).sort();
- assert.deepEqual(externalLinks(a.body), externalLinks(en.body), 'Translation changed official body links');
- assert.equal((a.body.match(/^## /gm) || []).length, (en.body.match(/^## /gm) || []).length, 'Translation omitted a section');
- }
- // Execute our TypeScript helpers with in-memory fixtures; never modify editorial publication flags.
- function loadModule(filename, fixture, indexing, cache = new Map()) {
- filename = path.resolve(root, filename);
- if (cache.has(filename)) return cache.get(filename);
- const source = fs.readFileSync(filename, 'utf8');
- const compiled = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, esModuleInterop: true, target: ts.ScriptTarget.ES2022 } }).outputText;
- const module = { exports: {} }; cache.set(filename, module.exports);
- const require = spec => {
- if (spec.endsWith('articles.json')) return fixture;
- const resolved = spec.startsWith('@/') ? path.join(root, 'src', spec.slice(2)) : path.resolve(path.dirname(filename), spec);
- return loadModule(`${resolved}.ts`, fixture, indexing, cache);
- };
- vm.runInNewContext(compiled, { exports: module.exports, module, require, process: { env: { BLOG_INDEXING_ENABLED: indexing ? 'true' : 'false' } }, URL });
- return module.exports;
- }
- assert(articles.length > 0, 'No guides to validate');
- const master = articles.find(a => a.locale === 'en');
- const fixture = locales.map(locale => ({ ...master, locale, published: true }));
- fixture.push({ ...master, id: 'unpublished', slug: 'unpublished', locale: 'en', published: false });
- const live = loadModule('src/lib/blog/data.ts', fixture, true);
- assert.equal(live.publishedArticles().length, 7);
- assert.equal(live.visibleArticles().length, 7);
- assert.equal(live.getArticle('en', 'unpublished'), undefined);
- const preview = loadModule('src/lib/blog/data.ts', fixture, false);
- assert.equal(preview.visibleArticles().length, 8);
- const meta = loadModule('src/lib/blog/metadata.ts', fixture, true);
- for (const a of fixture.filter(a => a.published)) {
- const m = meta.blogMetadata(a.locale, a);
- assert.equal(m.alternates.canonical, `https://text.skin${a.locale === 'en' ? '' : `/${a.locale}`}/blogs/${a.slug}`);
- assert.equal(Object.keys(m.alternates.languages).length, 8);
- assert.equal(m.robots.index, true);
- assert.equal(m.openGraph.url, m.alternates.canonical);
- }
- const partialMeta = loadModule('src/lib/blog/metadata.ts', fixture.slice(0, 2), true);
- assert.equal(Object.keys(partialMeta.blogMetadata('en', fixture[0]).alternates.languages).length, 3);
- const draftMeta = loadModule('src/lib/blog/metadata.ts', articles.map(a => ({ ...a, published: false })), false);
- assert.equal(draftMeta.blogMetadata('en', master).robots.index, false);
- assert.equal(Object.keys(draftMeta.blogMetadata('en', master).alternates.languages).length, 0);
- const sitemap = loadModule('src/app/sitemap.ts', fixture, true).default();
- assert.equal(sitemap.length, 14);
- assert(sitemap.every(a => /\/blogs(?:\/|$)/.test(a.url) && !a.url.includes('unpublished')));
- assert.equal(loadModule('src/app/sitemap.ts', fixture, false).default().length, 0);
- console.log(`PASS: ${articles.length} articles; content references, publication gates, reciprocal SEO alternates, canonical URLs, and sitemap fixtures.`);
- if (process.argv.includes('--release')) {
- assert.equal(inventory.length, 21, 'Approved multilingual inventory is three guides in seven languages');
- 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');
- const expected = inventory.map(a => `${a.locale}/${a.id}`).sort();
- const actual = articles.filter(a => a.published).map(a => `${a.locale}/${a.id}`).sort();
- assert.deepEqual(actual, expected, 'Release blocked: published records must match the approved inventory exactly');
- assert.equal(process.env.BLOG_INDEXING_ENABLED, 'true', 'Release blocked: BLOG_INDEXING_ENABLED must be true at build time');
- console.log('PASS: editorial release flags and approved inventory. This does not establish deployment, indexing, traffic, or payment outcomes.');
- }
|