import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { readFile, access } from 'node:fs/promises';
import test from 'node:test';
import { Window } from 'happy-dom';
import { readReportIndex, MAX_INDEX_BYTES } from '../src/lib/report-index.ts';
const lock = JSON.parse(await readFile(new URL('../src/data/report-index-lock.json', import.meta.url), 'utf8'));
const digest = bytes => createHash('sha256').update(bytes).digest('hex');
const fixture = steps => new TextEncoder().encode(JSON.stringify({ version: 1, recipient: lock.recipient, steps })).buffer;
const step = { order: 1, tool: 'Example tool', purpose: 'A local test fixture', pages: '1', sheetId: 'tools/example' };
test('only the exact decrypted payload is accepted', async () => {
const bytes = fixture([step]);
assert.deepEqual(await readReportIndex(bytes, digest(Buffer.from(bytes)), lock.recipient), [step]);
await assert.rejects(readReportIndex(bytes, lock.plaintextSha256, lock.recipient), /does not match/);
await assert.rejects(readReportIndex(new ArrayBuffer(MAX_INDEX_BYTES + 1), '', lock.recipient), /under 128 KB/);
});
test('decrypted entries cannot change recipients, skip steps or inject URLs', async () => {
for (const bad of [[], [{ ...step, order: 2 }], [{ ...step, sheetId: 'javascript:alert(1)' }], [{ ...step, sheetId: '../private' }]]) {
const bytes = fixture(bad);
await assert.rejects(readReportIndex(bytes, digest(Buffer.from(bytes)), lock.recipient));
}
const bytes = fixture([step]);
await assert.rejects(readReportIndex(bytes, digest(Buffer.from(bytes)), 'another-key'), /Unsupported/);
});
test('download is the pinned ciphertext and public source has no ordered JSON', async () => {
const bytes = await readFile(new URL('../public/encrypted/report-index.gpg', import.meta.url));
assert.equal(digest(bytes), lock.ciphertextSha256);
assert.equal(digest(await readFile(new URL('../dist/encrypted/report-index.gpg', import.meta.url))), lock.ciphertextSha256);
// GPG emits one old-format RSA session-key packet followed by integrity-protected ciphertext.
assert.equal(bytes[0], 0x85);
assert.equal(bytes[3], 3);
assert.equal(bytes.subarray(4, 12).toString('hex').toUpperCase(), lock.encryptionSubkey.slice(-16));
assert.equal(bytes[12], 1);
const nextPacket = 3 + bytes.readUInt16BE(1);
assert.equal(bytes[nextPacket], 0xd2);
await assert.rejects(access(new URL('../src/data/report-sequence.json', import.meta.url)));
await assert.rejects(access(new URL('../public/report-index.json', import.meta.url)));
await assert.rejects(access(new URL('../dist/report-index.json', import.meta.url)));
});
test('public pages retain unnumbered marks and start locked with an empty list', async () => {
for (const route of ['sheets/pentest-workflow/pentesting-basics-and-attacks', 'sheets/exploitation/sqlmap', 'enumeration']) {
const html = await readFile(new URL(`../dist/${route}/index.html`, import.meta.url), 'utf8');
const window = new Window();
window.document.write(html);
const doc = window.document;
assert.equal(doc.querySelector('[data-report-step]'), null);
assert.equal(doc.querySelector('[id^="report-step-"]'), null);
const panel = doc.querySelector('#report-index-dialog');
assert.ok(panel);
assert.equal(panel.hasAttribute('open'), false);
assert.equal(panel.querySelector('[data-report-index-results]').children.length, 0);
assert.ok(panel.hasAttribute('data-pagefind-ignore'));
assert.ok(doc.querySelector('[data-report-index-open]'));
if (route.endsWith('/sqlmap')) assert.equal(doc.querySelector('h1 [data-report-guide]').textContent, '☧');
window.happyDOM.abort();
}
});