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'), 'Home
');
await writeFile(join(root, 'about/index.html'), 'About
Duplicate
');
await writeFile(join(root, 'empty/index.html'), 'No heading
');
await writeFile(join(root, '404.html'), 'Generated error page
');
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'), 'Home
');
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'), 'Redirect');
const result = await checkRenderedOutput({ distRoot: root });
assert.deepEqual(result.headingIssues, []);
});
});