Fields and steps are Normal, Lawyer or Secret. Lawyer is stored as secret: true plus lawyer: true, so older builds treat it as a plain secret. Print offers masked, lawyer and complete views. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
184 lines
9.8 KiB
JavaScript
184 lines
9.8 KiB
JavaScript
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.frames[0].marks = 'no'; }, /marks must be an array/);
|
|
bad((x) => { x.frames[0].marks = [{ type: 'box', x: 0.9, y: 0, w: 0.5, h: 0.5 }]; }, /not a valid marker or box/);
|
|
const okm = structuredClone(d); okm.sections[0].records[1].frames[0].marks = [{ type: 'marker', x: 0.3, y: 0.4 }, { type: 'box', x: 0.1, y: 0.1, w: 0.2, h: 0.2 }];
|
|
assert.deepEqual(validate(okm), []);
|
|
bad((x) => { x.frames[0].ifStuck = 5; }, /ifStuck must be a string/);
|
|
bad((x) => { x.frames[0].stop = 'yes'; }, /stop must be a boolean/);
|
|
bad((x) => { x.captured = 'yesterday'; }, /captured must be YYYY-MM-DD/);
|
|
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: '', ifStuck: '', stop: false, layout: 'none' });
|
|
assert.equal(frameView(f, true).text, 'pw is hunter2');
|
|
const g = { image: PNG, text: '', layout: 'none', ifStuck: ' call 555-0100 ', stop: true };
|
|
assert.deepEqual([frameView(g, false).ifStuck, frameView(g, false).stop], ['call 555-0100', true]);
|
|
assert.deepEqual([frameView({ ...g, secret: true }, false).ifStuck, frameView({ ...g, secret: true }, false).stop], ['', false]);
|
|
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);
|
|
});
|
|
|
|
test('sub-tabs: valid shapes pass, bad ones are reported', () => {
|
|
const withTabs = () => {
|
|
const d = good();
|
|
const s = d.sections[1];
|
|
s.subsections = [{ id: 'sub1', title: 'Online' }];
|
|
s.generalTitle = 'Everyday';
|
|
s.records[0].subsection = 'sub1';
|
|
return d;
|
|
};
|
|
assert.deepEqual(validate(withTabs()), []);
|
|
|
|
let d = withTabs(); d.sections[1].records[0].subsection = 'nope';
|
|
assert.match(validate(d).join('\n'), /not one of this section's sub-tabs/);
|
|
|
|
d = withTabs(); d.sections[1].subsections[0].title = ' ';
|
|
assert.match(validate(d).join('\n'), /missing title/);
|
|
|
|
d = withTabs(); d.sections[1].subsections.push({ id: 'sub1', title: 'Dup' });
|
|
assert.match(validate(d).join('\n'), /duplicate id "sub1"/);
|
|
|
|
d = withTabs(); d.sections[1].subsections = {};
|
|
assert.match(validate(d).join('\n'), /subsections must be an array/);
|
|
|
|
d = withTabs(); d.sections[1].generalTitle = '';
|
|
assert.match(validate(d).join('\n'), /generalTitle/);
|
|
|
|
d = good(); d.sections[2].subsections = [];
|
|
assert.match(validate(d).join('\n'), /only records sections can have sub-tabs/);
|
|
|
|
d = good(); d.sections[1].records[0].subsection = 'x';
|
|
assert.match(validate(d).join('\n'), /not one of this section's sub-tabs/);
|
|
});
|
|
|
|
test('lawyer must be a boolean and needs secret: true, on fields and frames', () => {
|
|
const withField = (f) => { const d = good(); d.sections[1].records[0].fields = [{ label: 'A', value: 'x', ...f }]; return d; };
|
|
assert.deepEqual(validate(withField({ secret: true, lawyer: true })), []);
|
|
assert.deepEqual(validate(withField({ secret: true, lawyer: false })), []);
|
|
assert.match(validate(withField({ lawyer: true })).join('\n'), /lawyer needs secret: true/);
|
|
assert.match(validate(withField({ secret: true, lawyer: 'yes' })).join('\n'), /lawyer must be a boolean/);
|
|
const withFrame = (fr) => { const d = good(); d.sections[0].sequences = [{ id: 'q1', kind: 'sequence', title: 'Q', updated: '2022-04-14', frames: [{ image: '', text: 't', layout: 'none', ...fr }] }]; return d; };
|
|
assert.deepEqual(validate(withFrame({ secret: true, lawyer: true })), []);
|
|
assert.match(validate(withFrame({ lawyer: true })).join('\n'), /lawyer needs secret: true/);
|
|
});
|