Adds src/version.js, bump-version.js and a pre-commit hook that bumps the version in commits that change the app. Closes #15 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
102 lines
4.4 KiB
JavaScript
102 lines
4.4 KiB
JavaScript
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(/<script id="payload"[^>]*>([^<]*)<\/script>/)[1];
|
|
const back = JSON.parse(await decrypt(payload, PASS));
|
|
assert.deepEqual(back, doc);
|
|
assert.deepEqual(validate(back), []);
|
|
await assert.rejects(decrypt(payload, 'wrong passphrase!!'));
|
|
});
|
|
|
|
test('build refuses a weak passphrase and missing data', () => {
|
|
const weak = build({ FW_PASSPHRASE: 'short' });
|
|
assert.notEqual(weak.r.status, 0);
|
|
assert.match(weak.r.stderr, /at least 12/);
|
|
const bad = spawnSync('node', ['build.js', '--data', '/nonexistent.json'], { env: { ...process.env, FW_PASSPHRASE: PASS }, encoding: 'utf8' });
|
|
assert.notEqual(bad.status, 0);
|
|
});
|
|
|
|
test('assemble is reproducible from the pieces of a built file (what Save relies on)', () => {
|
|
const { out } = build();
|
|
const html = readFileSync(out, 'utf8');
|
|
const css = html.match(/<style id="fw-css">([\s\S]*?)<\/style>/)[1];
|
|
const js = html.match(/<script id="fw-js">([\s\S]*?)<\/script>\n<\/body>/)[1];
|
|
const payload = html.match(/<script id="payload"[^>]*>([^<]*)<\/script>/)[1];
|
|
assert.equal(assemble({ css, js, payload }), html);
|
|
assert.doesNotMatch(js, /<\/script/i, 'inline script would end early');
|
|
});
|
|
|
|
test('backupName and passphraseProblem', () => {
|
|
assert.equal(backupName(new Date(2026, 10, 9)), 'final-wishes-20261109.html');
|
|
assert.equal(backupName(new Date(2026, 0, 5)), 'final-wishes-20260105.html');
|
|
assert.ok(passphraseProblem(''));
|
|
assert.ok(passphraseProblem('elevenchars'));
|
|
assert.equal(passphraseProblem('twelve chars!'), '');
|
|
});
|
|
|
|
// ---- version ----
|
|
import { APP_VERSION, isVersion, nextVersion, versionDate } from '../src/version.js';
|
|
|
|
test('APP_VERSION is a valid Ver YYMMDDX', () => {
|
|
assert.equal(isVersion(APP_VERSION), true);
|
|
assert.equal(isVersion('261109B'), true);
|
|
for (const bad of ['', '26110B', '2611091', '261109b', '261109AA', null, 5]) assert.equal(isVersion(bad), false, String(bad));
|
|
});
|
|
|
|
test('nextVersion: next letter same day, A on a new day, error past Z', () => {
|
|
const d = new Date(2026, 10, 9, 15, 0); // 9 Nov 2026 local
|
|
assert.equal(versionDate(d), '261109');
|
|
assert.equal(nextVersion('261109A', d), '261109B');
|
|
assert.equal(nextVersion('261109Y', d), '261109Z');
|
|
assert.throws(() => nextVersion('261109Z', d), /26 versions/);
|
|
assert.equal(nextVersion('261108D', d), '261109A');
|
|
assert.equal(nextVersion('261109C', new Date(2027, 0, 2)), '270102A');
|
|
assert.throws(() => nextVersion('nope', d), /Not a version/);
|
|
});
|
|
|
|
test('the built page carries the version', async () => {
|
|
const { bundleJs } = await import('../build.js'); // importing must not run a build
|
|
assert.ok(bundleJs().includes(`APP_VERSION = '${APP_VERSION}'`));
|
|
});
|