Records sections can be split into user-named sub-tabs with a General catch-all. Records and visual sequences move between tabs via a Sub-tab drop-down; up/down, collapse all and print work per tab. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
323 lines
13 KiB
JavaScript
323 lines
13 KiB
JavaScript
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);
|
|
});
|