54 lines
2.2 KiB
JavaScript
54 lines
2.2 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import test from 'node:test';
|
|
import { checkRenderedOutput } from '../scripts/lib/rendered-output.mjs';
|
|
|
|
async function withRenderedSite(run) {
|
|
const root = await mkdtemp(join(tmpdir(), 'incognito-rendered-'));
|
|
try {
|
|
await run(root);
|
|
} finally {
|
|
await rm(root, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
test('reports every published HTML page that does not contain exactly one H1', async () => {
|
|
await withRenderedSite(async (root) => {
|
|
await mkdir(join(root, 'about'), { recursive: true });
|
|
await mkdir(join(root, 'empty'), { recursive: true });
|
|
await writeFile(join(root, 'index.html'), '<main><h1>Home</h1></main>');
|
|
await writeFile(join(root, 'about/index.html'), '<main><h1>About</h1><h1>Duplicate</h1></main>');
|
|
await writeFile(join(root, 'empty/index.html'), '<main><p>No heading</p></main>');
|
|
await writeFile(join(root, '404.html'), '<main><p>Generated error page</p></main>');
|
|
|
|
const result = await checkRenderedOutput({ distRoot: root });
|
|
|
|
assert.equal(result.htmlFilesChecked, 3);
|
|
assert.deepEqual(result.headingIssues, [
|
|
{ file: 'about/index.html', h1Count: 2 },
|
|
{ file: 'empty/index.html', h1Count: 0 },
|
|
]);
|
|
});
|
|
});
|
|
|
|
test('reports any to-be-studied path that reaches built output', async () => {
|
|
await withRenderedSite(async (root) => {
|
|
await writeFile(join(root, 'index.html'), '<main><h1>Home</h1></main>');
|
|
await mkdir(join(root, 'to-be-studied'), { recursive: true });
|
|
await writeFile(join(root, 'to-be-studied/leak.txt'), 'unpublished');
|
|
|
|
const result = await checkRenderedOutput({ distRoot: root });
|
|
|
|
assert.deepEqual(result.unpublishedPaths, ['to-be-studied/leak.txt']);
|
|
});
|
|
});
|
|
|
|
test('ignores Astro redirect documents when checking content headings', async () => {
|
|
await withRenderedSite(async (root) => {
|
|
await writeFile(join(root, 'index.html'), '<meta http-equiv="refresh" content="0;url=/new/"><a href="/new/">Redirect</a>');
|
|
const result = await checkRenderedOutput({ distRoot: root });
|
|
assert.deepEqual(result.headingIssues, []);
|
|
});
|
|
});
|