import { test } from 'node:test'; import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import { mkdtempSync, readFileSync, existsSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { encrypt, decrypt, DecryptError } from '../src/crypto.js'; import { newWalkthrough, sealBundle, openBundle, validateBundle, destinations } from '../src/capture.js'; import { emptyDocument, validate } from '../src/model.js'; import { addSection, addSubsection, importWalkthrough } from '../src/ops.js'; const FAST = 1000; const PNG = 'data:image/png;base64,iVBORw0KGgo='; const walk = (title = 'Log in to Chase', n = 2) => ({ ...newWalkthrough(title, '2026-11-24'), frames: Array.from({ length: n }, () => ({ image: PNG })) }); test('bundle round trip keeps walkthroughs and drops empty ones', async () => { const blob = await sealBundle([walk('A'), walk('Empty', 0), walk('B', 1)], 'bundle passphrase', FAST); const b = await openBundle(blob, 'bundle passphrase'); assert.deepEqual(b.walkthroughs.map((w) => w.title), ['A', 'B']); assert.equal(b.walkthroughs[0].frames[0].image, PNG); assert.deepEqual(validateBundle(b), []); }); test('wrong passphrase and tampering are rejected', async () => { const blob = await sealBundle([walk()], 'right passphrase', FAST); await assert.rejects(openBundle(blob, 'wrong passphrase'), DecryptError); const bytes = Buffer.from(blob, 'base64'); bytes[bytes.length - 3] ^= 1; await assert.rejects(openBundle(bytes.toString('base64'), 'right passphrase'), DecryptError); const hdr = Buffer.from(blob, 'base64'); hdr[5] ^= 1; // iteration count is authenticated header data await assert.rejects(openBundle(hdr.toString('base64'), 'right passphrase'), DecryptError); }); test('a bundle and a document cannot be mistaken for each other', async () => { const bundle = await sealBundle([walk()], 'same passphrase!', FAST); await assert.rejects(decrypt(bundle, 'same passphrase!'), DecryptError); const docBlob = await encrypt('{}', 'same passphrase!', FAST); await assert.rejects(openBundle(docBlob, 'same passphrase!'), DecryptError); }); test('validateBundle rejects bad contents', () => { const good = { format: 'fw-capture', version: 1, created: '2026-11-24', walkthroughs: [walk()] }; assert.deepEqual(validateBundle(good), []); assert.ok(validateBundle({ ...good, format: 'x' }).length); assert.ok(validateBundle({ ...good, version: 2 }).length); assert.ok(validateBundle({ ...good, walkthroughs: [{ ...walk(), frames: [{ image: 'http://evil/x.png' }] }] }).length); assert.ok(validateBundle({ ...good, walkthroughs: [{ ...walk(), captured: 'yesterday' }] }).length); assert.ok(validateBundle(null).length); }); test('importWalkthrough adds a valid sequence with unique ids, in a tab if asked', () => { const doc = emptyDocument('Me'); const banks = addSection(doc, 'records', 'Banks'); const tab = addSubsection(doc, banks, 'Checking'); const text = addSection(doc, 'text', 'Intro'); const steps = addSection(doc, 'steps', 'Steps'); const a = importWalkthrough(doc, banks, walk('Chase'), tab.id); const b = importWalkthrough(doc, banks, walk('Schwab', 3)); const c = importWalkthrough(doc, text, walk('Text one')); const d = importWalkthrough(doc, steps, walk(' ', 1)); assert.deepEqual(validate(doc), []); assert.equal(a.subsection, tab.id); assert.equal(b.subsection, undefined); assert.equal(a.captured, '2026-11-24'); assert.equal(b.frames.length, 3); assert.equal(d.title, 'Captured walkthrough'); assert.equal(new Set([a.id, b.id, c.id, d.id]).size, 4); assert.deepEqual(text.sequences.map((q) => q.title), ['Text one']); }); test('destinations lists every section and each sub-tab', () => { const doc = emptyDocument('Me'); const banks = addSection(doc, 'records', 'Banks'); addSection(doc, 'steps', 'Steps'); const tab = addSubsection(doc, banks, 'Checking'); const d = destinations(doc); assert.deepEqual(d.map((x) => x.label), ['Banks', 'Banks › Checking', 'Steps']); assert.equal(d[1].tab, tab.id); }); test('build writes capture.html: no document, no payload, no network, no app code', () => { const dir = mkdtempSync(join(tmpdir(), 'fw-cap-')); const out = join(dir, 'final-wishes.html'); const r = spawnSync('node', ['build.js', '--out', out], { encoding: 'utf8' }); assert.equal(r.status, 0, r.stderr); const capPath = join(dir, 'capture.html'); assert.ok(existsSync(capPath)); const html = readFileSync(capPath, 'utf8'); assert.doesNotMatch(html, /id="payload"/); assert.doesNotMatch(html, /https?:\/\/(?!www\.w3\.org)/); assert.doesNotMatch(html, /localStorage|indexedDB|sessionStorage/); assert.doesNotMatch(html, /assertValid|showSaveFilePicker|clearSecrets/); // nothing of the main app assert.match(html, /sealBundle/); }); test('both built pages have scripts that parse (no name clashes across the shared scope)', () => { const dir = mkdtempSync(join(tmpdir(), 'fw-parse-')); const r = spawnSync('node', ['build.js', '--out', join(dir, 'final-wishes.html')], { encoding: 'utf8' }); assert.equal(r.status, 0, r.stderr); for (const f of ['final-wishes.html', 'capture.html']) { const js = readFileSync(join(dir, f), 'utf8').match(/