38 lines
2.6 KiB
JavaScript
38 lines
2.6 KiB
JavaScript
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
|
|
import { join, posix } from 'node:path';
|
|
import { gzipSync } from 'node:zlib';
|
|
import assert from 'node:assert/strict';
|
|
|
|
const dist = 'dist';
|
|
const routes = ['', 'download/', 'start/', 'docs/', 'ecosystem/', 'research/', 'releases/', 'feedback/', 'about/', 'privacy/'];
|
|
const paths = ['', 'en/'].flatMap(locale => routes.map(route => locale + route + 'index.html')).concat('404.html');
|
|
const documents = new Map(paths.map(path => [path, readFileSync(join(dist, path), 'utf8')]));
|
|
const ids = new Map([...documents].map(([path, html]) =>
|
|
[path, new Set([...html.matchAll(/\sid="([^"]+)"/g)].map(match => match[1]))]));
|
|
const production = process.env.MARQDO_RELEASE_BUILD === '1';
|
|
for (const [path, html] of documents) {
|
|
assert.equal([...html.matchAll(/<h1[\s>]/g)].length, 1, `Expected one h1 in ${path}`);
|
|
assert.match(html, /豫ICP备2025120352号-6/);
|
|
assert.match(html, /mailto:pingan@cflmy\.com/);
|
|
if (!production) assert.match(html, /name="robots" content="noindex"/);
|
|
assert.doesNotMatch(html, /<script(?:\s[^>]*?)?>\s*[^<\s]/, `Inline script in ${path}`);
|
|
assert.doesNotMatch(html, /(?:https?:\/\/(?:localhost|127\.0\.0\.1)|fixture_kind|MARQDO_WOODPECKER_DISPATCH_TOKEN|BEGIN (?:OPENSSH |RSA |EC )?PRIVATE KEY)/);
|
|
for (const [, ref] of html.matchAll(/\s(?:href|src)="(\/[^/"][^"]*|\/|#[^"]+)"/g)) {
|
|
const [link, anchor] = ref.split('#');
|
|
const dest = link ? decodeURIComponent(link.slice(1)) : path;
|
|
const file = link && (!dest || dest.endsWith('/')) ? posix.join(dest, 'index.html') : dest;
|
|
assert.ok(existsSync(join(dist, file)) && statSync(join(dist, file)).isFile(), `Missing ${ref}, referenced by ${path}`);
|
|
if (anchor && ids.has(file)) assert.ok(ids.get(file).has(decodeURIComponent(anchor)), `Missing anchor ${ref}, referenced by ${path}`);
|
|
}
|
|
}
|
|
for (const file of ['assets/logo.svg', 'assets/cflmy.jpg', 'assets/social.png', 'assets/geometry.mq.md', 'pagefind/pagefind.js', 'robots.txt', 'sitemap.xml']) {
|
|
assert.ok(existsSync(join(dist, file)), `Missing ${file}`);
|
|
}
|
|
|
|
const home = documents.get('index.html');
|
|
const homeScripts = [...home.matchAll(/<script[^>]* src="(\/[^"]+)"/g)].map(hit => hit[1]);
|
|
const compressed = homeScripts.reduce((size, file) => size + gzipSync(readFileSync(dist + file)).length, 0);
|
|
assert.ok(compressed <= 150 * 1024, `Home JS exceeds 150 KiB: ${compressed}`);
|
|
assert.ok(!readdirSync(dist).some(name => name.startsWith('.') || name === 'data'), 'Unexpected output root entries');
|
|
console.log(`Verified ${paths.length} HTML pages, local references and anchors; homepage JS ${compressed} bytes gzip.`);
|