Add configurable running print header
The line printed on every page is now a header stored in the document as header.fields: any number of Text / Last updated fields, each optionally aligned left or right (at most one of each). Printed order is left, unaligned, right. Edited from a Print header panel with live preview; documents without a header use the previous title/last-update default. The header is a repeating table header so it sits in flow and cannot overlap body text, replacing the fixed-position footer. Also add "Open your existing Final Wishes file" to the start screen so data in an older encrypted file can be carried into a newer build. Closes #9 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -46,6 +46,23 @@ test('rejects structural problems', () => {
|
||||
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'));
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
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';
|
||||
import { move, removeAt, newId, addSection, addRecord, addField, addItem, touch, ensureHeader, addHeaderField, setAlign, takenAligns } from '../src/ops.js';
|
||||
|
||||
test('move swaps neighbors and refuses out of range', () => {
|
||||
const a = [1, 2, 3];
|
||||
@@ -56,3 +56,28 @@ test('touch stamps record and document dates', () => {
|
||||
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
|
||||
});
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { displayValue, footerText, countSecrets } from '../src/print.js';
|
||||
import { displayValue, headerLayout, countSecrets } from '../src/print.js';
|
||||
|
||||
test('displayValue shows or withholds secrets, never confuses empty with hidden', () => {
|
||||
assert.equal(displayValue({ label: 'a', value: 'x' }, false), 'x');
|
||||
@@ -16,10 +16,31 @@ test('masked output never contains the secret text', () => {
|
||||
assert.ok(!out.includes('hunter2'));
|
||||
});
|
||||
|
||||
test('footerText and countSecrets', () => {
|
||||
test('countSecrets counts only filled secret fields', () => {
|
||||
const doc = { meta: { title: 'T', lastUpdated: '2022-04-14' }, sections: [
|
||||
{ records: [{ fields: [{ label: 'a', value: 'x', secret: true }, { label: 'b', value: '', secret: true }, { label: 'c', value: 'y' }] }] },
|
||||
{ items: [] } ] };
|
||||
assert.equal(footerText(doc), 'T — Last update: 2022-04-14');
|
||||
assert.equal(countSecrets(doc), 1);
|
||||
});
|
||||
|
||||
const D = (header) => ({ meta: { title: 'My Title', lastUpdated: '2026-11-09' }, header, sections: [] });
|
||||
|
||||
test('header defaults: title left, last update right', () => {
|
||||
assert.deepEqual(headerLayout(D(undefined)), { left: 'My Title', middle: [], right: 'Last update: 2026-11-09' });
|
||||
});
|
||||
|
||||
test('header layout order is left, unaligned in list order, right, regardless of field order', () => {
|
||||
const doc = D({ fields: [
|
||||
{ kind: 'lastUpdated', text: 'Updated ', align: 'right' },
|
||||
{ kind: 'text', text: 'Middle A' },
|
||||
{ kind: 'text', text: 'Left one', align: 'left' },
|
||||
{ kind: 'text', text: 'Middle B' },
|
||||
] });
|
||||
assert.deepEqual(headerLayout(doc), { left: 'Left one', middle: ['Middle A', 'Middle B'], right: 'Updated 2026-11-09' });
|
||||
});
|
||||
|
||||
test('header with only unaligned fields, and empty text dropped', () => {
|
||||
const doc = D({ fields: [{ kind: 'text', text: 'A' }, { kind: 'text', text: '' }, { kind: 'lastUpdated', text: '' }] });
|
||||
assert.deepEqual(headerLayout(doc), { left: '', middle: ['A', '2026-11-09'], right: '' });
|
||||
assert.deepEqual(headerLayout(D({ fields: [] })), { left: '', middle: [], right: '' });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user