import { describe, expect, it } from 'vitest' import { CodedEntityFlagSchema, codedLabelOf, isCodedEntity, isSupplied, withSupplied, } from '../page-spec-coded-entity.js' // lib/page-spec-coded-entity — the pagespec `codedEntity` flag SSOT: legacy // boolean OR enriched object, every reader goes through isCodedEntity() // instead of the historical `=== true`. describe('CodedEntityFlagSchema — boolean | enriched object union', () => { it('accepts the legacy boolean and the enriched object', () => { expect(CodedEntityFlagSchema.parse(true)).toBe(true) expect(CodedEntityFlagSchema.parse(false)).toBe(false) const parsed = CodedEntityFlagSchema.parse({ label: { fr: 'Référence' }, supplied: true, codeKey: 'crm.budget', codeInputFields: ['Label'], }) expect(parsed).toMatchObject({ supplied: true, codeKey: 'crm.budget' }) }) it('passthrough keeps unknown slots alive across a parse (multi-chantier contract)', () => { const parsed = CodedEntityFlagSchema.parse({ supplied: true, futureSlot: 'kept' }) expect((parsed as Record)['futureSlot']).toBe('kept') }) }) describe('isCodedEntity — THE reader replacing `=== true`', () => { it('true and any object form are coded; false/undefined/null/arrays are not', () => { expect(isCodedEntity(true)).toBe(true) expect(isCodedEntity({})).toBe(true) expect(isCodedEntity({ label: { fr: 'Référence' } })).toBe(true) expect(isCodedEntity(false)).toBe(false) expect(isCodedEntity(undefined)).toBe(false) expect(isCodedEntity(null)).toBe(false) expect(isCodedEntity([])).toBe(false) }) }) describe('isSupplied — create-surface sanction', () => { it('only an object with supplied === true qualifies', () => { expect(isSupplied({ supplied: true })).toBe(true) expect(isSupplied({ supplied: false })).toBe(false) expect(isSupplied({})).toBe(false) expect(isSupplied(true)).toBe(false) expect(isSupplied(undefined)).toBe(false) }) }) describe('codedLabelOf — locale resolution fr→fr??en, others→en??fr', () => { it('resolves per locale with cross-language fallback', () => { const flag = { label: { fr: 'Référence', en: 'Reference' } } expect(codedLabelOf(flag, 'fr')).toBe('Référence') expect(codedLabelOf(flag, 'en')).toBe('Reference') expect(codedLabelOf({ label: { fr: 'Référence' } }, 'de')).toBe('Référence') expect(codedLabelOf({ label: { en: 'Reference' } }, 'fr')).toBe('Reference') }) it('null when the flag carries no label (boolean, empty, blank)', () => { expect(codedLabelOf(true, 'fr')).toBeNull() expect(codedLabelOf({}, 'fr')).toBeNull() expect(codedLabelOf({ label: { fr: ' ' } }, 'fr')).toBeNull() expect(codedLabelOf(undefined, 'en')).toBeNull() }) }) describe('withSupplied — merge-preserving writes, canonical collapse', () => { it('preserves every other slot when setting or clearing supplied', () => { expect(withSupplied({ label: { fr: 'Référence' } }, true)) .toEqual({ label: { fr: 'Référence' }, supplied: true }) expect(withSupplied({ label: { fr: 'Référence' }, supplied: true }, false)) .toEqual({ label: { fr: 'Référence' } }) }) it('collapses an otherwise-empty object back to the canonical boolean true', () => { expect(withSupplied(true, false)).toBe(true) expect(withSupplied(undefined, false)).toBe(true) expect(withSupplied({ supplied: true }, false)).toBe(true) expect(withSupplied(true, true)).toEqual({ supplied: true }) }) })