import { test } from 'node:test'; import assert from 'node:assert/strict'; import { emptyDocument, validate } from '../src/model.js'; import { move, removeAt, newId, addSection, addRecord, addField, addItem, touch, ensureHeader, addHeaderField, setAlign, takenAligns, snapshotDoc, docDiffers, sectionDiffers, headerDiffers, revertSection, revertHeader } from '../src/ops.js'; test('move swaps neighbors and refuses out of range', () => { const a = [1, 2, 3]; assert.equal(move(a, 0, 1), true); assert.deepEqual(a, [2, 1, 3]); assert.equal(move(a, 0, -1), false); assert.equal(move(a, 2, 1), false); assert.deepEqual(a, [2, 1, 3]); }); test('removeAt removes one element, ignores bad index', () => { const a = ['x', 'y']; assert.equal(removeAt(a, 0), 'x'); assert.deepEqual(a, ['y']); assert.equal(removeAt(a, 5), undefined); }); test('added sections/records/items/fields keep the document valid with unique ids', () => { const doc = emptyDocument('Me'); const rs = addSection(doc, 'records', 'Banks'); const ts = addSection(doc, 'text', 'Intro'); const ss = addSection(doc, 'steps', 'Steps'); const cs = addSection(doc, 'checklist', 'Todo'); const r1 = addRecord(doc, rs); const r2 = addRecord(doc, rs); addField(r1); addItem(doc, ss); addItem(doc, cs); assert.deepEqual(validate(doc), []); assert.notEqual(r1.id, r2.id); assert.equal(ts.body, ''); const ids = [rs, ts, ss, cs, r1, r2].map((x) => x.id); assert.equal(new Set(ids).size, ids.length); }); test('newId never collides after deletions', () => { const doc = emptyDocument('Me'); const s = addSection(doc, 'records', 'B'); const a = addRecord(doc, s); const b = addRecord(doc, s); removeAt(s.records, 0); const c = addRecord(doc, s); assert.notEqual(c.id, b.id); assert.equal(new Set(s.records.map((r) => r.id)).size, 2); }); test('touch stamps record and document dates', () => { const doc = emptyDocument('Me'); doc.meta.lastUpdated = '2000-01-01'; const rec = { updated: '2000-01-01' }; touch(doc, rec); assert.match(doc.meta.lastUpdated, /^\d{4}-\d{2}-\d{2}$/); assert.notEqual(rec.updated, '2000-01-01'); }); test('ensureHeader materializes defaults once; addHeaderField keeps document valid', () => { const doc = emptyDocument('Me', 'My Title'); assert.equal(doc.header, undefined); const h = ensureHeader(doc); assert.equal(ensureHeader(doc), h); assert.equal(h.fields[0].text, 'My Title'); addHeaderField(doc); assert.equal(doc.header.fields.length, 3); assert.deepEqual(validate(doc), []); }); test('setAlign enforces one left and one right', () => { const fields = [{ kind: 'text', text: 'a' }, { kind: 'text', text: 'b' }, { kind: 'text', text: 'c' }]; assert.equal(setAlign(fields, fields[0], 'left'), true); assert.equal(setAlign(fields, fields[1], 'left'), false); assert.equal(fields[1].align, undefined); assert.equal(setAlign(fields, fields[1], 'right'), true); assert.equal(setAlign(fields, fields[2], 'right'), false); assert.deepEqual([...takenAligns(fields, fields[2])].sort(), ['left', 'right']); assert.equal(setAlign(fields, fields[0], 'left'), true); // re-selecting its own value is fine assert.equal(setAlign(fields, fields[0], ''), true); assert.equal('align' in fields[0], false); assert.equal(setAlign(fields, fields[2], 'left'), true); // freed }); // ---- revert (Cancel) ---- function sample() { const d = emptyDocument('Owner'); d.meta.lastUpdated = '2020-01-01'; const a = addSection(d, 'records', 'Banks'); const r = addRecord(d, a); r.title = 'Bank one'; addField(r).value = 'v'; const b = addSection(d, 'records', 'Cards'); d.meta.lastUpdated = '2020-01-01'; return { d, a, b, r }; } test('revertSection restores an edited section and leaves others alone', () => { const { d, a, b } = sample(); const base = snapshotDoc(d); a.records[0].title = 'Changed'; a.records[0].fields.push({ label: 'x', value: 'y' }); b.title = 'Cards edited'; touch(d, a.records[0]); assert.equal(sectionDiffers(d, base, a.id), true); assert.equal(revertSection(d, base, a.id), a.id); assert.deepEqual(d.sections[0], base.sections[0]); assert.equal(d.sections[1].title, 'Cards edited'); assert.equal(docDiffers(d, base), true); // Cards still differs assert.notEqual(d.meta.lastUpdated, '2020-01-01'); // date stays while something differs }); test('last revert clears the difference and restores the date', () => { const { d, a } = sample(); const base = snapshotDoc(d); a.title = 'Edited'; touch(d, a); revertSection(d, base, a.id); assert.equal(docDiffers(d, base), false); assert.equal(d.meta.lastUpdated, '2020-01-01'); }); test('the date stamp alone is not a difference', () => { const { d } = sample(); const base = snapshotDoc(d); d.meta.lastUpdated = '2030-05-05'; assert.equal(docDiffers(d, base), false); }); test('a section added since the baseline is removed, selecting a neighbour', () => { const { d, b } = sample(); const base = snapshotDoc(d); const c = addSection(d, 'text', 'New'); assert.equal(sectionDiffers(d, base, c.id), true); assert.equal(revertSection(d, base, c.id), b.id); assert.equal(d.sections.length, 2); const only = emptyDocument(''); const base2 = snapshotDoc(only); const x = addSection(only, 'text', 'X'); assert.equal(revertSection(only, base2, x.id), undefined); assert.equal(only.sections.length, 0); }); test('an unchanged section does not differ; a moved one still reverts in place', () => { const { d, a, b } = sample(); const base = snapshotDoc(d); assert.equal(sectionDiffers(d, base, a.id), false); move(d.sections, 0, 1); a.title = 'Edited'; revertSection(d, base, a.id); assert.equal(d.sections[1].title, 'Banks'); assert.equal(d.sections[0].id, b.id); }); test('header revert restores the default (absent) and a custom header', () => { const { d } = sample(); let base = snapshotDoc(d); ensureHeader(d); addHeaderField(d); assert.equal(headerDiffers(d, base), true); revertHeader(d, base); assert.equal(d.header, undefined); assert.equal(headerDiffers(d, base), false); ensureHeader(d); base = snapshotDoc(d); d.header.fields[0].text = 'changed'; revertHeader(d, base); assert.notEqual(d.header.fields[0].text, 'changed'); assert.notEqual(d.header, base.header); // a copy, not shared }); // ---- delete-section confirmation ---- import { sectionContents, deleteSectionMessage, addSequence, addFrame } from '../src/ops.js'; const PNGX = 'data:image/png;base64,iVBORw0KGgo='; test('deleteSectionMessage names the section and counts what is in it', () => { const d = emptyDocument('o'); const s = addSection(d, 'records', 'Bank Accounts'); assert.equal(deleteSectionMessage(s), 'Delete the empty section "Bank Accounts"?'); const r1 = addRecord(d, s); addRecord(d, s); r1.fields.push({ kind: 'image', label: 'ID', value: PNGX }, { kind: 'image', label: 'blank', value: '' }, { label: 'a', value: 'b' }); const q = addSequence(d, s); addFrame(q, PNGX); addFrame(q, ''); assert.deepEqual(sectionContents(s), { records: 2, items: 0, sequences: 1, images: 2, text: false }); assert.equal(deleteSectionMessage(s), 'Delete the section "Bank Accounts" and everything in it (2 records, 1 visual sequence, 2 images)? This cannot be undone with Cancel.'); }); test('deleteSectionMessage: items, singular forms, and text sections', () => { const d = emptyDocument('o'); const c = addSection(d, 'checklist', 'To do'); addItem(d, c); const q = addSequence(d, c); addFrame(q, PNGX); addFrame(q, PNGX); assert.match(deleteSectionMessage(c), /\(1 item, 1 visual sequence, 2 images\)/); const t = addSection(d, 'text', 'Start'); assert.match(deleteSectionMessage(t), /^Delete the empty section "Start"\?$/); t.body = 'hello'; assert.match(deleteSectionMessage(t), /\(its text\)/); addSequence(d, t); assert.match(deleteSectionMessage(t), /\(1 visual sequence, its text\)/); }); // ---- sub-tabs ---- import { hasTabs, tabsOf, entryTab, validTab, entriesInTab, siblingIndex, moveWithin, addSubsection, moveSubsection, renameTab, moveToTab, removeSubsection, deleteSubsectionMessage, printGroups } from '../src/ops.js'; function bank() { const doc = emptyDocument('Me'); const s = addSection(doc, 'records', 'Banking'); const a = addRecord(doc, s); a.title = 'A'; const b = addRecord(doc, s); b.title = 'B'; const c = addRecord(doc, s); c.title = 'C'; return { doc, s, a, b, c }; } test('a section has no tabs until one is added; then General comes first', () => { const { doc, s } = bank(); assert.equal(hasTabs(s), false); assert.deepEqual(tabsOf(s), []); const t = addSubsection(doc, s, ' Online '); assert.equal(t.title, 'Online'); assert.deepEqual(tabsOf(s), [{ id: '', title: 'General' }, { id: t.id, title: 'Online' }]); assert.deepEqual(validate(doc), []); }); test('records without a sub-tab, or with a dangling one, are in General', () => { const { doc, s, a, b } = bank(); const t = addSubsection(doc, s, 'Online'); moveToTab(s, b, t.id); assert.equal(entryTab(s, a), ''); assert.equal(entryTab(s, b), t.id); b.subsection = 'gone'; assert.equal(entryTab(s, b), ''); assert.equal(validTab(s, 'gone'), ''); assert.equal(validTab(s, t.id), t.id); }); test('moveToTab changes the tab, refuses an unknown one, and General clears it', () => { const { doc, s, a } = bank(); const t = addSubsection(doc, s, 'Online'); assert.equal(moveToTab(s, a, t.id), true); assert.equal(a.subsection, t.id); assert.equal(moveToTab(s, a, 'nope'), false); assert.equal(a.subsection, t.id); assert.equal(moveToTab(s, a, ''), true); assert.equal('subsection' in a, false); assert.deepEqual(validate(doc), []); }); test('up/down moves within a tab, stepping over records in other tabs', () => { const { doc, s, a, b, c } = bank(); const t = addSubsection(doc, s, 'Online'); moveToTab(s, b, t.id); // list: A(General) B(Online) C(General) const same = (e) => entryTab(s, e) === ''; assert.equal(siblingIndex(s.records, 0, 1, same), 2); assert.equal(moveWithin(s.records, 0, 1, same), true); assert.deepEqual(s.records.map((r) => r.title), ['C', 'B', 'A']); assert.equal(moveWithin(s.records, 2, 1, same), false); assert.deepEqual(entriesInTab(s, '').map((x) => x.e.title), ['C', 'A']); assert.deepEqual(entriesInTab(s, t.id).map((x) => x.i), [1]); }); test('new records and sequences go into the given tab', () => { const { doc, s } = bank(); const t = addSubsection(doc, s, 'Online'); const r = addRecord(doc, s, t.id); const q = addSequence(doc, s, t.id); assert.equal(r.subsection, t.id); assert.equal(q.subsection, t.id); assert.equal('subsection' in addRecord(doc, s), false); assert.deepEqual(validate(doc), []); }); test('renameTab renames General or a tab, and refuses an empty name', () => { const { doc, s } = bank(); const t = addSubsection(doc, s, 'Online'); assert.equal(renameTab(s, '', 'Everyday'), true); assert.equal(renameTab(s, t.id, 'Web'), true); assert.deepEqual(tabsOf(s).map((x) => x.title), ['Everyday', 'Web']); assert.equal(renameTab(s, t.id, ' '), false); assert.equal(renameTab(s, 'nope', 'x'), false); assert.deepEqual(validate(doc), []); }); test('moveSubsection reorders tabs', () => { const { doc, s } = bank(); const x = addSubsection(doc, s, 'X'); const y = addSubsection(doc, s, 'Y'); assert.equal(moveSubsection(s, y.id, -1), true); assert.deepEqual(s.subsections.map((t) => t.title), ['Y', 'X']); assert.equal(moveSubsection(s, y.id, -1), false); assert.equal(moveSubsection(s, 'nope', 1), false); void x; }); test('removing a tab sends its records to General; removing the last tab restores a plain section', () => { const { doc, s, a, b } = bank(); const t = addSubsection(doc, s, 'Online'); renameTab(s, '', 'Everyday'); moveToTab(s, a, t.id); assert.match(deleteSubsectionMessage(s, t.id), /Delete the sub-tab "Online"\? Its 1 entry moves to "Everyday"\./); assert.equal(removeSubsection(s, t.id), true); assert.equal('subsection' in a, false); assert.equal(s.records.length, 3); assert.equal(hasTabs(s), false); assert.equal('subsections' in s, false); assert.equal('generalTitle' in s, false); assert.equal(removeSubsection(s, t.id), false); void b; assert.deepEqual(validate(doc), []); }); test('printGroups: one untitled group without tabs, else one per non-empty tab', () => { const { doc, s, a, b } = bank(); assert.equal(printGroups(s).length, 1); assert.equal(printGroups(s)[0].title, null); const t = addSubsection(doc, s, 'Online'); addSubsection(doc, s, 'Empty'); moveToTab(s, b, t.id); const g = printGroups(s); assert.deepEqual(g.map((x) => x.title), ['General', 'Online']); assert.deepEqual(g[1].entries, [b]); void a; }); test('sub-tab ids are unique across the document', () => { const { doc, s } = bank(); const t = addSubsection(doc, s, 'Online'); const ids = new Set([t.id, ...s.records.map((r) => r.id), s.id]); assert.equal(ids.size, 5); }); test('clearSecrets erases secret fields and frames, keeps the rest, returns the count', async () => { const { clearSecrets, addSequence, addFrame } = await import('../src/ops.js'); const { countSecrets } = await import('../src/print.js'); const doc = emptyDocument('x'); const s = addSection(doc, 'records', 'Accounts'); const r = addRecord(doc, s); addField(r); addField(r); r.fields[0].label = 'User'; r.fields[0].value = 'bob'; r.fields[1].label = 'PIN'; r.fields[1].value = '1234'; r.fields[1].secret = true; const q = addSequence(doc, s); const fr = addFrame(q); fr.text = 'tap here'; fr.secret = true; const before = countSecrets(doc); assert.equal(before, 2); assert.equal(clearSecrets(doc), 2); assert.equal(countSecrets(doc), 0); assert.equal(r.fields[0].value, 'bob'); assert.equal(r.fields[1].value, ''); assert.equal(r.fields[1].secret, true); assert.equal(r.fields[1].label, 'PIN'); assert.equal(fr.text, ''); assert.deepEqual(validate(doc), []); assert.equal(clearSecrets(doc), 0); }); test('addCapture adds a frame and stamps the sequence captured date; clearSecrets clears ifStuck on secret steps', async () => { const { addCapture, clearSecrets, addSequence } = await import('../src/ops.js'); const doc = emptyDocument('x'); const s = addSection(doc, 'records', 'Banking'); const q = addSequence(doc, s); assert.equal(q.captured, undefined); const f = addCapture(q, ''); assert.equal(q.frames.length, 1); assert.equal(f.layout, 'under'); assert.match(q.captured, /^\d{4}-\d{2}-\d{2}$/); f.secret = true; f.ifStuck = 'call 555-0100'; f.stop = true; f.image = 'data:image/png;base64,AAAA'; f.marks = [{ type: 'marker', x: 0.5, y: 0.5 }]; assert.equal(clearSecrets(doc), 1); assert.equal(f.marks, undefined); assert.equal(f.ifStuck, ''); assert.equal(f.stop, true); assert.deepEqual(validate(doc), []); }); test('credential references: field ids are stable, refs resolve, missing ones are detected', async () => { const { addUse, removeUse, findFieldRef, ensureFieldId, addSequence, addFrame } = await import('../src/ops.js'); const { usesView } = await import('../src/print.js'); const doc = emptyDocument('x'); const s = addSection(doc, 'records', 'Banking'); const r = addRecord(doc, s); addField(r); addField(r); r.fields[0].label = 'User'; r.fields[0].value = 'bob'; r.fields[1].label = 'Password'; r.fields[1].value = 'hunter2'; r.fields[1].secret = true; const q = addSequence(doc, s); const fr = addFrame(q); assert.equal(addUse(fr, r, r.fields[1]), true); assert.equal(addUse(fr, r, r.fields[1]), false); // no duplicates addUse(fr, r, r.fields[0]); assert.equal(ensureFieldId(r, r.fields[1]), r.fields[1].id); // stable assert.notEqual(r.fields[0].id, r.fields[1].id); assert.deepEqual(validate(doc), []); const moved = r.fields.reverse(); // reorder: references still resolve assert.equal(findFieldRef(doc, fr.uses[0]).field.label, 'Password'); assert.deepEqual(usesView(doc, fr, false).map((u) => [u.label, u.value]), [['Password', '[withheld]'], ['User', 'bob']]); assert.equal(usesView(doc, fr, true)[0].value, 'hunter2'); r.fields.splice(0, 1); // delete the password field assert.equal(findFieldRef(doc, fr.uses[0]), null); assert.equal(usesView(doc, fr, true)[0].missing, true); assert.deepEqual(validate(doc), []); // a dangling reference is not an error removeUse(fr, 0); removeUse(fr, 0); assert.equal(fr.uses, undefined); }); test('validation: uses and field ids', async () => { const { addSequence, addFrame } = await import('../src/ops.js'); const doc = emptyDocument('x'); const s = addSection(doc, 'records', 'B'); const r = addRecord(doc, s); addField(r); addField(r); const q = addSequence(doc, s); const fr = addFrame(q); const bad = (mut, re) => { const c = structuredClone(doc); mut(c.sections[0]); assert.match(validate(c).join('|'), re); }; bad((x) => { x.records[1].frames[0].uses = 'no'; }, /uses must be an array/); bad((x) => { x.records[1].frames[0].uses = [{ record: 'a' }]; }, /needs record and field ids/); bad((x) => { x.records[0].fields[0].id = 'f1'; x.records[0].fields[1].id = 'f1'; }, /duplicate field id/); bad((x) => { x.records[0].fields[0].id = ''; }, /id must be a non-empty string/); }); test('levelOf and setLevel keep lawyer/secret flags canonical', async () => { const { levelOf, setLevel } = await import('../src/ops.js'); const f = { label: 'a', value: 'x' }; assert.equal(levelOf(f), 'normal'); setLevel(f, 'lawyer'); assert.deepEqual(f, { label: 'a', value: 'x', secret: true, lawyer: true }); assert.equal(levelOf(f), 'lawyer'); setLevel(f, 'secret'); assert.deepEqual(f, { label: 'a', value: 'x', secret: true }); assert.equal(levelOf(f), 'secret'); setLevel(f, 'lawyer'); setLevel(f, 'normal'); assert.deepEqual(f, { label: 'a', value: 'x' }); // no stray keys assert.equal(levelOf({ lawyer: true }), 'normal'); // lawyer alone is not a secret }); test('clearSecrets erases lawyer-level values too', async () => { const { clearSecrets } = await import('../src/ops.js'); const doc = { sections: [{ records: [{ fields: [{ label: 'n', value: '12345', secret: true, lawyer: true }, { label: 'o', value: 'open' }] }] }] }; assert.equal(clearSecrets(doc), 1); assert.equal(doc.sections[0].records[0].fields[0].value, ''); assert.equal(doc.sections[0].records[0].fields[1].value, 'open'); });