Incognito-Wiki/tests/internal-links.test.mjs
msa46 1d4d832b6c
Some checks failed
Deploy to GitHub Pages / build (push) Has been cancelled
Deploy to GitHub Pages / deploy (push) Has been cancelled
fix: verify programme routes across deployment bases
2026-08-15 21:09:25 +02:00

128 lines
4.8 KiB
JavaScript

import assert from 'node:assert/strict';
import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import test from 'node:test';
import { checkInternalLinks } from '../scripts/lib/internal-links.mjs';
async function withBuiltSite(run) {
const root = await mkdtemp(join(tmpdir(), 'incognito-links-'));
try {
await run(root);
} finally {
await rm(root, { recursive: true, force: true });
}
}
test('reports a missing built internal route', async () => {
await withBuiltSite(async (root) => {
await writeFile(join(root, 'index.html'), '<a href="/missing/">Missing</a>');
const result = await checkInternalLinks({ distRoot: root, base: '/' });
assert.deepEqual(result.broken.map(({ href }) => href), ['/missing/']);
});
});
test('accepts an existing directory index route', async () => {
await withBuiltSite(async (root) => {
await mkdir(join(root, 'about'), { recursive: true });
await writeFile(join(root, 'index.html'), '<a href="/about/">About</a>');
await writeFile(join(root, 'about/index.html'), '<h1>About</h1>');
const result = await checkInternalLinks({ distRoot: root, base: '/' });
assert.equal(result.broken.length, 0);
});
});
test('accepts an existing exact-file link', async () => {
await withBuiltSite(async (root) => {
await writeFile(join(root, 'index.html'), '<a href="/manual.pdf">Manual</a>');
await writeFile(join(root, 'manual.pdf'), 'manual');
const result = await checkInternalLinks({ distRoot: root, base: '/' });
assert.equal(result.broken.length, 0);
});
});
test('maps the root route to the built root index', async () => {
await withBuiltSite(async (root) => {
await writeFile(join(root, 'index.html'), '<a href="/">Home</a>');
const result = await checkInternalLinks({ distRoot: root, base: '/' });
assert.equal(result.broken.length, 0);
});
});
test('removes the configured base prefix before resolving built routes', async () => {
await withBuiltSite(async (root) => {
await mkdir(join(root, 'about'), { recursive: true });
await writeFile(join(root, 'index.html'), [
'<a href="/wiki/about/">About</a>',
'<a href="/about/">Missing base prefix</a>',
].join(''));
await writeFile(join(root, 'about/index.html'), '<h1>About</h1>');
const result = await checkInternalLinks({ distRoot: root, base: '/wiki/' });
assert.deepEqual(result.broken.map(({ href }) => href), ['/about/']);
assert.equal(result.broken[0].resolvedPath, '/about/');
});
});
test('resolves relative routes from the source HTML route', async () => {
await withBuiltSite(async (root) => {
await mkdir(join(root, 'guide', 'install'), { recursive: true });
await mkdir(join(root, 'guide', 'about'), { recursive: true });
await writeFile(join(root, 'guide/install/index.html'), '<a href="../about/">About</a>');
await writeFile(join(root, 'guide/about/index.html'), '<h1>About</h1>');
const result = await checkInternalLinks({ distRoot: root, base: '/' });
assert.equal(result.broken.length, 0);
});
});
test('ignores fragments, remote protocols, and Astro-generated assets', async () => {
await withBuiltSite(async (root) => {
await writeFile(join(root, 'index.html'), [
'<a href="#section">Section</a>',
'<a href="mailto:info@example.com">Email</a>',
'<a href="tel:+31000000000">Call</a>',
'<a href="https://example.com/elsewhere">Remote</a>',
'<link href="/_astro/site.css" rel="stylesheet">',
].join(''));
const result = await checkInternalLinks({ distRoot: root, base: '/' });
assert.equal(result.broken.length, 0);
});
});
test('ignores links inside generated redirect documents', async () => {
await withBuiltSite(async (root) => {
await writeFile(join(root, 'index.html'), '<meta http-equiv="refresh" content="0;url=/target/"><a href="/target/">Redirect</a>');
const result = await checkInternalLinks({ distRoot: root, base: '/wiki/' });
assert.equal(result.broken.length, 0);
});
});
for (const { name, href } of [
{ name: 'encoded slash', href: '/..%2Fmanual.pdf' },
{ name: 'encoded backslash', href: '/..%5Cmanual.pdf' },
]) {
test(`rejects ${name} traversal even when the escaped target exists`, async () => {
const siteRoot = await mkdtemp(join(tmpdir(), 'incognito-links-escape-'));
const distRoot = join(siteRoot, 'dist');
try {
await mkdir(distRoot);
await writeFile(join(siteRoot, 'manual.pdf'), 'outside dist');
await writeFile(join(distRoot, 'index.html'), `<a href="${href}">Escaped</a>`);
const result = await checkInternalLinks({ distRoot, base: '/' });
assert.deepEqual(result.broken.map((broken) => broken.href), [href]);
} finally {
await rm(siteRoot, { recursive: true, force: true });
}
});
}