import { test } from 'node:test'; import assert from 'node:assert/strict'; import { existsSync, readFileSync } from 'node:fs'; import { validate, emptyDocument, assertValid } from '../src/model.js'; const good = () => ({ schemaVersion: 1, meta: { owner: 'A', title: 'T', lastUpdated: '2022-04-14' }, sections: [ { id: 's-text', title: 'Intro', kind: 'text', body: 'hi' }, { id: 's-rec', title: 'Bank', kind: 'records', records: [ { id: 'r1', title: 'Acct', status: 'ok', updated: '2022-04-14', fields: [{ label: 'PIN', value: '', secret: true }], notes: 'n' } ] }, { id: 's-steps', title: 'Steps', kind: 'steps', items: [ { id: 'i1', text: 'Do it', status: 'open', detail: 'how' } ] }, { id: 's-list', title: 'Add', kind: 'checklist', items: [ { id: 'i2', text: 'More', status: 'tbd' } ] }, ], }); test('valid document passes; emptyDocument is valid', () => { assert.deepEqual(validate(good()), []); assert.deepEqual(validate(emptyDocument('Me')), []); assert.equal(assertValid(good()).meta.owner, 'A'); }); test('rejects structural problems', () => { const cases = [ [(d) => { d.schemaVersion = 2; }, /schemaVersion/], [(d) => { d.meta.lastUpdated = 'yesterday'; }, /lastUpdated/], [(d) => { d.sections[0].kind = 'weird'; }, /unknown kind/], [(d) => { d.sections[1].records[0].status = 'maybe'; }, /bad status/], [(d) => { d.sections[1].records[0].fields[0].secret = 'yes'; }, /secret must be a boolean/], [(d) => { d.sections[1].records[0].fields[0].value = 5; }, /value must be a string/], [(d) => { d.sections[2].items[0].status = 'x'; }, /bad status/], [(d) => { d.sections[1].records[0].id = 's-text'; }, /duplicate id/], [(d) => { delete d.sections[3].items; }, /items must be an array/], [(d) => { delete d.sections[0].body; }, /body/], ]; for (const [mutate, re] of cases) { const d = good(); mutate(d); assert.match(validate(d).join('\n'), re); } assert.throws(() => assertValid({}), /Invalid document/); 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')); assert.deepEqual(validate(seed), []); });