Files
Emberfall/tests/integration/link-checker.test.js
T
2026-10-22 23:10:45 +11:00

57 lines
1.8 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const PUBLIC_DIR = path.resolve(__dirname, '../../public');
function extractLocalRefs(filePath) {
const html = fs.readFileSync(filePath, 'utf-8');
const refs = [];
// Extract href/src values from <a>, <link>, <script>, <img> tags
const refRegex = /(?:href|src)\s*=\s*["']([^"']+)["']/gi;
let match;
while ((match = refRegex.exec(html)) !== null) {
const value = match[1];
// Skip external URLs
if (/^https?:\/\//i.test(value) || value.startsWith('//')) continue;
// Skip data URIs, javascript:, mailto:
if (/^(data:|javascript:|mailto:)/i.test(value)) continue;
// Skip hash-only links
if (value.startsWith('#')) continue;
// Skip API endpoints (not static files)
if (value.startsWith('/api/')) continue;
// All Emberfall HTML uses absolute paths (starting with /)
if (value.startsWith('/')) {
refs.push(value); // keep leading / for PUBLIC_DIR resolution
}
}
return refs;
}
test('link-checker: all local href/src references in public/*.html resolve to existing files', async () => {
const htmlFiles = fs.readdirSync(PUBLIC_DIR)
.filter((f) => f.endsWith('.html'))
.map((f) => path.join(PUBLIC_DIR, f));
const broken = [];
for (const file of htmlFiles) {
const refs = extractLocalRefs(file);
const relFile = path.relative(PUBLIC_DIR, file);
for (const ref of refs) {
// ref starts with / → resolve relative to PUBLIC_DIR
const target = path.join(PUBLIC_DIR, ref.slice(1));
const exists = fs.existsSync(target);
if (!exists) {
broken.push(`${relFile} → ${ref} (expected ${target})`);
}
}
}
assert.equal(broken.length, 0, `Broken links found:\n${broken.join('\n')}`);
});