Add viewer/editor UI
Passphrase unlock (or open a plain JSON file while no payload is embedded), sectioned navigation, read-only cards with masked secret fields and a reveal toggle, and an edit mode with add/remove/reorder for sections, records, fields and items. Pure edit operations live in ops.js with tests. DOM is built with textContent only. Closes #4 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
58
test/ops.test.js
Normal file
58
test/ops.test.js
Normal file
@@ -0,0 +1,58 @@
|
||||
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 } 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');
|
||||
});
|
||||
Reference in New Issue
Block a user