Files
Reims-landing/tests/regression-dist.test.mjs

213 lines
9.3 KiB
JavaScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
import { join, resolve } from 'node:path';
const ROOT = resolve(import.meta.dirname, '..');
const DIST = join(ROOT, 'dist');
function readText(relPath) {
return readFileSync(join(ROOT, relPath), 'utf8');
}
function parseJsonc(raw) {
// Strip single-line // comments (outside strings) and /* */ block comments
const stripped = raw.replace(/\/\*[\s\S]*?\*\/|(?<=^|[^:\\])\/\/.*$/gm, '');
return JSON.parse(stripped);
}
function extractCssKeyframeBlocks(css, prefix) {
const blocks = [];
const re = new RegExp(`@keyframes\\s+(${prefix}[a-z0-9_-]*)\\s*\\{`, 'gi');
let match;
while ((match = re.exec(css)) !== null) {
let depth = 1;
let i = match.index + match[0].length;
while (i < css.length && depth > 0) {
if (css[i] === '{') depth += 1;
else if (css[i] === '}') depth -= 1;
i += 1;
}
blocks.push({ name: match[1], body: css.slice(match.index, i) });
}
return blocks;
}
describe('Configuration Invariants — wrangler.jsonc & public/_headers', () => {
it('wrangler.jsonc configures worker entrypoint, observability, ASSETS binding, /api/* routing, and custom domain', () => {
const config = parseJsonc(readText('wrangler.jsonc'));
assert.equal(config.main, 'worker/index.js');
assert.equal(config.observability?.enabled, true);
assert.equal(config.observability?.traces?.enabled, true);
assert.equal(config.assets?.binding, 'ASSETS');
assert.equal(config.assets?.directory, './dist');
assert.ok(Array.isArray(config.assets?.run_worker_first), 'assets.run_worker_first must be an array');
assert.ok(config.assets.run_worker_first.includes('/api/*'), 'run_worker_first must include "/api/*"');
assert.ok(Array.isArray(config.routes), 'routes must be an array');
assert.ok(
config.routes.some((r) => r?.pattern === 'reims.dev'),
'routes must include pattern "reims.dev"',
);
});
it('public/_headers defines security headers, HTML Cache-Control, and immutable /_astro/* caching', () => {
const headers = readText('public/_headers');
// Global security headers under /*
assert.match(headers, /^\/\*\r?\n[\s\S]*?X-Content-Type-Options:\s*nosniff/m);
assert.match(headers, /^\/\*\r?\n[\s\S]*?X-Frame-Options:\s*SAMEORIGIN/m);
assert.match(headers, /^\/\*\r?\n[\s\S]*?Referrer-Policy:\s*strict-origin-when-cross-origin/m);
// HTML routes have Cache-Control with stale-while-revalidate
for (const route of ['/', '/en', '/docs/*', '/en/docs/*', '/changelog', '/en/changelog', '/plugins', '/compatibility']) {
const escaped = route.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const routeRe = new RegExp(`^${escaped}\\r?\\n\\s+Cache-Control:.*stale-while-revalidate=`, 'm');
assert.match(headers, routeRe, `Route ${route} must have Cache-Control with stale-while-revalidate`);
}
// Immutable hashed Astro assets
assert.match(
headers,
/^\/_astro\/\*\r?\n\s+Cache-Control:\s*public,\s*max-age=31536000,\s*immutable/m,
'/_astro/* must have immutable 1-year Cache-Control',
);
});
});
describe('Built Site Regression Checks — dist/', () => {
it('all required built HTML, SEO, data, and Pagefind artifacts exist and are non-empty', () => {
const requiredFiles = [
'dist/index.html',
'dist/en/index.html',
'dist/changelog/index.html',
'dist/en/changelog/index.html',
'dist/docs/quick-start/index.html',
'dist/en/docs/quick-start/index.html',
'dist/plugins/index.html',
'dist/compatibility/index.html',
'dist/404.html',
'dist/sitemap.xml',
'dist/robots.txt',
'dist/llms.txt',
'dist/llms-full.txt',
'dist/data/releases.json',
'dist/data/releases-bundle.json',
'dist/pagefind/pagefind.js',
];
for (const rel of requiredFiles) {
const full = join(ROOT, rel);
assert.equal(existsSync(full), true, `Missing required built artifact: ${rel}`);
assert.ok(statSync(full).size > 0, `Built artifact must be non-empty: ${rel}`);
}
});
it('dist/_astro/*.css View Transition keyframes have zero blur() filters and include prefers-reduced-motion', () => {
const astroDir = join(DIST, '_astro');
const cssFiles = readdirSync(astroDir).filter((f) => f.endsWith('.css'));
assert.ok(cssFiles.length > 0, 'Expected at least one CSS bundle in dist/_astro');
const combinedCss = cssFiles.map((f) => readFileSync(join(astroDir, f), 'utf8')).join('\n');
const pageKeyframes = extractCssKeyframeBlocks(combinedCss, 'reims-page-');
const rapidKeyframes = extractCssKeyframeBlocks(combinedCss, 'reims-rapid-');
assert.ok(pageKeyframes.length > 0, 'Expected reims-page-* View Transition keyframes');
assert.ok(rapidKeyframes.length > 0, 'Expected reims-rapid-* View Transition keyframes');
for (const kf of [...pageKeyframes, ...rapidKeyframes]) {
assert.equal(
/blur\s*\(/i.test(kf.body),
false,
`Keyframe ${kf.name} must not use blur() filter: ${kf.body}`,
);
}
assert.match(
combinedCss,
/prefers-reduced-motion\s*:\s*reduce/i,
'Built CSS must include prefers-reduced-motion: reduce handling',
);
});
it('dist/data/releases.json and dist/data/releases-bundle.json conform to strict schema invariants', () => {
const releases = JSON.parse(readText('dist/data/releases.json'));
const bundle = JSON.parse(readText('dist/data/releases-bundle.json'));
// Validate releases.json
assert.ok(Date.parse(releases.synced_at) > 0, 'releases.synced_at must be valid ISO date');
assert.equal(typeof releases.stars, 'number');
assert.ok(releases.stars > 0, 'releases.stars must be positive');
assert.match(releases.version, /^v\d+\.\d+\.\d+/i, 'releases.version must be valid semver tag');
assert.ok(Date.parse(releases.published_at) > 0, 'releases.published_at must be valid ISO date');
assert.match(releases.html_url, /^https:\/\//);
for (const assetKey of ['exe', 'zip']) {
const asset = releases[assetKey];
assert.ok(asset && typeof asset === 'object', `releases.${assetKey} must exist`);
assert.ok(asset.name.length > 0);
assert.ok(typeof asset.size === 'number' && asset.size > 0);
assert.match(asset.url, /^https:\/\//);
}
assert.match(releases.sha256_url, /^https:\/\//);
assert.match(releases.sha256_sums, /[a-f0-9]{64}\s+/i);
// Validate releases-bundle.json
assert.ok(['static-fallback', 'edge-api', 'github-direct'].includes(bundle.source));
assert.ok(Date.parse(bundle.synced_at) > 0);
assert.equal(bundle.latest?.version, releases.version, 'bundle.latest.version should match releases.json');
assert.ok(Array.isArray(bundle.entries) && bundle.entries.length > 0, 'bundle.entries must be non-empty');
for (const entry of bundle.entries) {
assert.ok(typeof entry.tag === 'string' && entry.tag.length > 0);
assert.equal(/^runtime-/i.test(entry.tag), false, `runtime tag leaked into bundle: ${entry.tag}`);
assert.ok(typeof entry.name === 'string' && entry.name.length > 0);
assert.match(entry.date, /^\d{4}-\d{2}-\d{2}$/, `Invalid entry.date for ${entry.tag}`);
assert.match(entry.html_url, /^https:\/\//, `Invalid entry.html_url for ${entry.tag}`);
assert.equal(typeof entry.prerelease, 'boolean');
assert.ok(['stable', 'alpha', 'dev'].includes(entry.channel), `Invalid channel for ${entry.tag}`);
if (entry.exe) assert.match(entry.exe.url, /^https:\/\//);
if (entry.zip) assert.match(entry.zip.url, /^https:\/\//);
if (entry.sha256_url) assert.match(entry.sha256_url, /^https:\/\//);
assert.ok(Array.isArray(entry.sections?.['zh-CN']), `Missing zh-CN sections for ${entry.tag}`);
assert.ok(Array.isArray(entry.sections?.['en-US']), `Missing en-US sections for ${entry.tag}`);
for (const locale of ['zh-CN', 'en-US']) {
for (const sec of entry.sections[locale]) {
assert.equal(typeof sec.title, 'string');
assert.ok(Array.isArray(sec.items) && sec.items.length > 0);
}
}
}
});
it('quick-start docs (zh & en) do not contain hardcoded stale reims-go-v0.2.11-windows-amd64.exe', () => {
for (const rel of ['dist/docs/quick-start/index.html', 'dist/en/docs/quick-start/index.html']) {
const html = readText(rel);
assert.equal(
html.includes('reims-go-v0.2.11-windows-amd64.exe'),
false,
`${rel} must not contain stale v0.2.11 binary filename`,
);
}
});
it('dist/index.html contains nav engine guard, persisted site-header, and Hero live release meta', () => {
const html = readText('dist/index.html');
const releases = JSON.parse(readText('dist/data/releases.json'));
assert.ok(html.includes('__reimsNavEngineBound'), 'index.html must include __reimsNavEngineBound');
assert.ok(html.includes('site-header'), 'index.html must include site-header');
assert.ok(
html.includes('data-astro-transition-persist'),
'index.html must include Astro transition:persist marker (data-astro-transition-persist)',
);
assert.ok(html.includes('HeroReleaseMeta'), 'index.html must hydrate HeroReleaseMeta island');
assert.ok(
html.includes(releases.version),
`index.html must render current release version (${releases.version})`,
);
});
});