import { describe, expect, it } from 'vitest'; import { diffOf } from './audit'; describe('diffOf', () => { it('reports nothing when nothing moved', () => { // A log full of empty changes is a log nobody reads. expect(diffOf({ a: 1 }, { a: 1 })).toBeNull(); expect(diffOf(null, null)).toBeNull(); expect(diffOf({}, {})).toBeNull(); }); it('reports a changed field', () => { expect(diffOf({ isClosed: false }, { isClosed: true })).toEqual({ isClosed: { before: false, after: true }, }); }); it('reports only the fields that moved', () => { expect(diffOf({ a: 1, b: 2 }, { a: 1, b: 3 })).toEqual({ b: { before: 2, after: 3 } }); }); it('reports an added and a removed field', () => { expect(diffOf({ a: 1 }, { a: 1, b: 2 })).toEqual({ b: { before: null, after: 2 } }); expect(diffOf({ a: 1, b: 2 }, { a: 1 })).toEqual({ b: { before: 2, after: null } }); }); it('treats a missing field and an explicit null as the same absence', () => { expect(diffOf({ note: null }, {})).toBeNull(); expect(diffOf({ note: undefined }, { note: null })).toBeNull(); }); it('compares slot arrays by value, not by identity', () => { const before = { slots: [{ open: '10:00', close: '18:30' }] }; const after = { slots: [{ open: '10:00', close: '18:30' }] }; expect(diffOf(before, after)).toBeNull(); expect(diffOf(before, { slots: [{ open: '14:00', close: '18:30' }] })).toEqual({ slots: { before: [{ open: '10:00', close: '18:30' }], after: [{ open: '14:00', close: '18:30' }], }, }); }); it('compares dates by their instant, not their object', () => { const before = { startsAt: new Date('2026-09-20T00:00:00Z') }; expect(diffOf(before, { startsAt: new Date('2026-09-20T00:00:00Z') })).toBeNull(); const changed = diffOf(before, { startsAt: new Date('2026-09-21T00:00:00Z') }); expect(changed?.startsAt).toEqual({ before: '2026-09-20T00:00:00.000Z', after: '2026-09-21T00:00:00.000Z', }); }); it('records a creation and a deletion', () => { expect(diffOf(null, { textFr: 'Bonjour' })).toEqual({ textFr: { before: null, after: 'Bonjour' }, }); expect(diffOf({ textFr: 'Bonjour' }, null)).toEqual({ textFr: { before: 'Bonjour', after: null }, }); }); it('distinguishes values that only look alike', () => { expect(diffOf({ priority: 1 }, { priority: '1' })).not.toBeNull(); expect(diffOf({ isActive: false }, { isActive: null })).not.toBeNull(); }); });