Add visual sequences: numbered image + text steps in any section

Closes #12

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 18:35:30 -05:00
parent d588e30b9b
commit e9ac581e38
8 changed files with 315 additions and 35 deletions

View File

@@ -68,3 +68,62 @@ test('data/seed.json, when present, is valid', { skip: !existsSync('data/seed.js
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);
});