import { test } from 'node:test'; import assert from 'node:assert/strict'; import { execFileSync, spawnSync } from 'node:child_process'; import { mkdtempSync, writeFileSync, readFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { decrypt, passphraseProblem } from '../src/crypto.js'; import { validate } from '../src/model.js'; import { backupName } from '../src/ops.js'; import { assemble } from '../src/template.js'; const SECRET = 'hunter2-SENTINEL-9431'; const doc = { schemaVersion: 1, meta: { owner: 'Test Owner SENTINEL', title: 'T', lastUpdated: '2022-04-14' }, sections: [{ id: 's', title: 'S', kind: 'records', records: [ { id: 'r', title: 'Acct', status: 'ok', updated: '2022-04-14', fields: [{ label: 'PW', value: SECRET, secret: true }] }] }], }; const PASS = 'a long test passphrase'; function build(extraEnv = {}, args) { const dir = mkdtempSync(join(tmpdir(), 'fw-')); const data = join(dir, 'doc.json'); const out = join(dir, 'out.html'); writeFileSync(data, JSON.stringify(doc)); const r = spawnSync('node', ['build.js', '--data', data, '--out', out, ...(args || [])], { env: { ...process.env, FW_PASSPHRASE: PASS, ...extraEnv }, encoding: 'utf8' }); return { r, out }; } test('built file embeds ciphertext only: no plaintext anywhere', () => { const { r, out } = build(); assert.equal(r.status, 0, r.stderr); const html = readFileSync(out, 'utf8'); assert.ok(!html.includes(SECRET), 'secret leaked'); assert.ok(!html.includes('Test Owner SENTINEL'), 'owner leaked'); assert.ok(!html.includes(PASS), 'passphrase leaked'); assert.doesNotMatch(html, /https?:\/\//, 'external URL present'); }); test('embedded payload decrypts back to the document', async () => { const { out } = build(); const html = readFileSync(out, 'utf8'); const payload = html.match(/