import { test } from 'node:test'; import assert from 'node:assert/strict'; import { existsSync, readFileSync } from 'node:fs'; import { validate, emptyDocument, assertValid } from '../src/model.js'; const good = () => ({ schemaVersion: 1, meta: { owner: 'A', title: 'T', lastUpdated: '2022-04-14' }, sections: [ { id: 's-text', title: 'Intro', kind: 'text', body: 'hi' }, { id: 's-rec', title: 'Bank', kind: 'records', records: [ { id: 'r1', title: 'Acct', status: 'ok', updated: '2022-04-14', fields: [{ label: 'PIN', value: '', secret: true }], notes: 'n' } ] }, { id: 's-steps', title: 'Steps', kind: 'steps', items: [ { id: 'i1', text: 'Do it', status: 'open', detail: 'how' } ] }, { id: 's-list', title: 'Add', kind: 'checklist', items: [ { id: 'i2', text: 'More', status: 'tbd' } ] }, ], }); test('valid document passes; emptyDocument is valid', () => { assert.deepEqual(validate(good()), []); assert.deepEqual(validate(emptyDocument('Me')), []); assert.equal(assertValid(good()).meta.owner, 'A'); }); test('rejects structural problems', () => { const cases = [ [(d) => { d.schemaVersion = 2; }, /schemaVersion/], [(d) => { d.meta.lastUpdated = 'yesterday'; }, /lastUpdated/], [(d) => { d.sections[0].kind = 'weird'; }, /unknown kind/], [(d) => { d.sections[1].records[0].status = 'maybe'; }, /bad status/], [(d) => { d.sections[1].records[0].fields[0].secret = 'yes'; }, /secret must be a boolean/], [(d) => { d.sections[1].records[0].fields[0].value = 5; }, /value must be a string/], [(d) => { d.sections[2].items[0].status = 'x'; }, /bad status/], [(d) => { d.sections[1].records[0].id = 's-text'; }, /duplicate id/], [(d) => { delete d.sections[3].items; }, /items must be an array/], [(d) => { delete d.sections[0].body; }, /body/], ]; for (const [mutate, re] of cases) { const d = good(); mutate(d); assert.match(validate(d).join('\n'), re); } assert.throws(() => assertValid({}), /Invalid document/); assert.deepEqual(validate(null), ['document must be an object']); }); test('header validation: kinds, aligns, and one left / one right at most', () => { const withHeader = (fields) => ({ ...good(), header: { fields } }); const ok = [{ kind: 'text', text: 'T', align: 'left' }, { kind: 'text', text: 'm' }, { kind: 'lastUpdated', text: '', align: 'right' }]; assert.deepEqual(validate(withHeader(ok)), []); assert.deepEqual(validate(withHeader([])), []); assert.deepEqual(validate(good()), []); // header is optional const bad = [ [[{ kind: 'x', text: 'a' }], /unknown kind/], [[{ kind: 'text', text: 5 }], /text must be a string/], [[{ kind: 'text', text: 'a', align: 'center' }], /bad align/], [[{ kind: 'text', text: 'a', align: 'left' }, { kind: 'text', text: 'b', align: 'left' }], /only one field can be left/], [[{ kind: 'text', text: 'a', align: 'right' }, { kind: 'text', text: 'b', align: 'right' }], /only one field can be right/], ]; for (const [fields, re] of bad) assert.match(validate(withHeader(fields)).join('\n'), re); assert.match(validate({ ...good(), header: {} }).join('\n'), /header.fields must be an array/); }); // data/ is gitignored (private), so this only runs on a machine that has the seed. test('data/seed.json, when present, is valid', { skip: !existsSync('data/seed.json') }, () => { const seed = JSON.parse(readFileSync('data/seed.json', 'utf8')); assert.deepEqual(validate(seed), []); }); // ---- visual sequences ---- import { addSequence, addFrame, allSequences, allIds, sequenceList, addSection, addRecord, addItem } from '../src/ops.js'; import { isSequence } from '../src/model.js'; import { frameLayout, frameView, countSecrets } from '../src/print.js'; const PNG = 'data:image/png;base64,iVBORw0KGgo='; function docWithSequences() { const d = emptyDocument('o'); const rs = addSection(d, 'records', 'Banks'); addRecord(d, rs); const st = addSection(d, 'steps', 'Wind-down'); addItem(d, st); const ck = addSection(d, 'checklist', 'Cancel'); addItem(d, ck); const tx = addSection(d, 'text', 'Start'); for (const s of [rs, st, ck, tx]) { const q = addSequence(d, s); addFrame(q, PNG, 'right').text = 'hello'; addFrame(q, PNG, 'none'); } return { d, rs, st, ck, tx }; } test('sequences are valid in every section kind, interleaved with records and items', () => { const { d, rs, st, ck, tx } = docWithSequences(); assert.deepEqual(validate(d), []); assert.equal(isSequence(rs.records[1]) && isSequence(st.items[1]) && isSequence(ck.items[1]) && isSequence(tx.sequences[0]), true); assert.equal(allSequences(d).length, 4); assert.equal(new Set(allIds(d)).size, allIds(d).size); assert.equal(allIds(d).has(tx.sequences[0].id), true); assert.equal(sequenceList(tx), tx.sequences); }); test('sequence validation: layout, image, text, secret, id, title', () => { const { d, rs } = docWithSequences(); const q = rs.records[1]; const bad = (mut, re) => { const c = structuredClone(d); const cq = c.sections[0].records[1]; mut(cq); assert.match(validate(c).join('|'), re); }; bad((x) => { x.frames[0].layout = 'diagonal'; }, /bad layout/); bad((x) => { x.frames[0].image = 'data:image/gif;base64,AAAA'; }, /image must be a PNG or JPEG/); bad((x) => { x.frames[0].image = 5; }, /image must be a string/); bad((x) => { x.frames[0].text = null; }, /text must be a string/); bad((x) => { x.frames[0].secret = 'yes'; }, /secret must be a boolean/); bad((x) => { x.title = ''; }, /missing title/); bad((x) => { x.id = d.sections[0].records[0].id; }, /duplicate id/); bad((x) => { delete x.frames; }, /frames must be an array/); assert.equal(q.frames[1].image, PNG); const e = structuredClone(d); e.sections[0].records[1].frames[0].image = ''; assert.deepEqual(validate(e), []); // empty frame is fine }); test('frameLayout hides empty text; frameView withholds secret frames only when masked', () => { assert.equal(frameLayout({ layout: 'right', text: ' ' }), 'none'); assert.equal(frameLayout({ layout: 'right', text: 'x' }), 'right'); assert.equal(frameLayout({ layout: 'none', text: 'x' }), 'none'); const f = { image: PNG, text: 'pw is hunter2', layout: 'under', secret: true }; assert.deepEqual(frameView(f, false), { withheld: true, image: '', text: '', layout: 'none' }); assert.equal(frameView(f, true).text, 'pw is hunter2'); assert.equal(frameView({ image: '', text: '', layout: 'under', secret: true }, false).withheld, false); }); test('countSecrets counts secret frames in all places and ignores sequences as records', () => { const { d, rs, st, tx } = docWithSequences(); assert.equal(countSecrets(d), 0); rs.records[1].frames[0].secret = true; st.items[1].frames[1].secret = true; tx.sequences[0].frames[0].secret = true; assert.equal(countSecrets(d), 3); });