import assert from 'node:assert/strict' import { test } from 'node:test' import * as s from 'remix/data-schema' import { applyConfigPatch, configFieldsShape, defaultConfig, diffConfig, } from '../src/domain/config.ts' import { parseInput, strictObject } from '../src/domain/validation.ts' const schema = strictObject(configFieldsShape) const parse = (raw: unknown) => parseInput(schema, raw, 'update-config') test('accepts a realistic partial update', () => { const patch = parse({ siteName: 'Jane Doe Photography', contactEmail: 'jane@example.com', social: { instagram: 'https://instagram.com/janedoe' }, bookingUrl: 'https://cal.com/janedoe/session', accentColor: '#3A6EA5', theme: 'dark', siteUrl: 'https://janedoe.photo/', }) const config = applyConfigPatch(defaultConfig, patch) assert.equal(config.siteName, 'Jane Doe Photography') assert.equal(config.accentColor, '#3a6ea5') assert.equal(config.siteUrl, 'https://janedoe.photo') assert.deepEqual(config.social, { instagram: 'https://instagram.com/janedoe', }) assert.equal( config.tagline, defaultConfig.tagline, 'untouched fields keep their value', ) }) test('rejects unknown keys, bad emails, colors, URLs, and empty names with one readable error', () => { assert.throws( () => parse({ photographer: 'Jane' }), /Invalid update-config input/, ) assert.throws( () => parse({ contactEmail: 'jane' }), /contactEmail: must be an email address/, ) assert.throws( () => parse({ accentColor: 'blue' }), /accentColor: must look like #b8763a/, ) assert.throws( () => parse({ bookingUrl: 'calendly.com/jane' }), /bookingUrl: must be an http\(s\) URL/, ) assert.throws( () => parse({ siteUrl: 'https://jane.photo/?x=1' }), /siteUrl: must be a plain origin/, ) assert.throws(() => parse({ siteName: ' ' }), /siteName: must not be empty/) assert.throws( () => parse({ social: { myspace: 'https://myspace.com/jane' } }), /Invalid update-config input/, ) assert.throws(() => parse({ theme: 'sepia' }), /theme/) }) test('null clears optional fields and social links', () => { const withExtras = applyConfigPatch(defaultConfig, { phone: '555-0100', bookingUrl: 'https://cal.com/jane', social: { instagram: 'https://instagram.com/jane', website: 'https://jane.example', }, }) const cleared = applyConfigPatch(withExtras, { phone: null, bookingUrl: null, location: null, social: { instagram: null }, }) assert.equal(cleared.phone, undefined) assert.equal(cleared.bookingUrl, undefined) assert.equal(cleared.location, undefined) assert.deepEqual(cleared.social, { website: 'https://jane.example' }) assert.equal(withExtras.phone, '555-0100', 'the input config is not mutated') }) test('diffConfig reports changed fields and summarizes the headshot', () => { const headshot = { id: 'ph_abc', source: 'stored' as const, width: 800, height: 1000, alt: 'Jane', blurhash: 'x', lqip: 'data:', variants: [], addedAt: '2026-01-01T00:00:00.000Z', } const next = applyConfigPatch(defaultConfig, { tagline: 'Light chaser', headshot, }) assert.deepEqual(diffConfig(defaultConfig, next), [ { field: 'tagline', from: defaultConfig.tagline, to: 'Light chaser' }, { field: 'headshot', from: undefined, to: 'photo ph_abc' }, ]) assert.deepEqual(diffConfig(next, next), []) }) test('every documented default validates against the schema', () => { const { headshot: _headshot, ...fields } = defaultConfig assert.doesNotThrow(() => s.parse(schema, fields)) })