import { describe, expect, it } from 'vitest' import { fact, type Fact } from './detail' /** * The one rule this file has that a review cannot see. * * A fact renders whatever it is handed, so a fact handed nothing renders a term * with a blank beside it — and a blank means "there is none" to whoever wrote * the call and "we could not ask" to whoever reads the screen. The union makes * that shape unspellable; this is the function that decides which side of it a * read landed on, and every one of the cases below is a value that has been * passed to it in earnest. */ describe('a fact either has a value or says why it has none', () => { it('keeps a value, and carries no reason beside it', () => { expect(fact('Holding', '1,000', 'not read')).toStrictEqual({ term: 'Holding', value: '1,000', }) }) it.each([ ['nothing was returned', undefined], ['the field was absent', null], ['the string was empty', ''], ['a conditional child collapsed', false], ])('states the reason when %s', (_why, value) => { expect(fact('Rate', value, 'no venue quotes this')).toStrictEqual({ term: 'Rate', unread: 'no venue quotes this', }) }) it('keeps a zero, which is a reading and not an absence', () => { expect(fact('Supply', 0, 'not read')).toStrictEqual({ term: 'Supply', value: 0 }) }) }) /** * The same rule, one level up. * * `fact` decides which side of the union a read landed on, and a call site that * builds the object itself skips it. So the type has to refuse the same shapes, * and these are compile-time assertions: each is an error tsc must raise, and * the suite fails to build the day one of them stops being one. */ describe('the union refuses the shapes fact() catches', () => { it('cannot spell a fact whose value is a collapsed conditional', () => { const known = false as boolean // @ts-expect-error a value that renders as nothing is not a value const collapsed: Fact = { term: 'Supply', value: known && '1,000' } // @ts-expect-error same shape, written out const no: Fact = { term: 'Supply', value: false } // @ts-expect-error and the other half of the boolean, which draws nothing either const yes: Fact = { term: 'Supply', value: true } // @ts-expect-error a term on its own says neither what was read nor why it was not const bare: Fact = { term: 'Supply' } expect([collapsed, no, yes, bare]).toHaveLength(4) }) it('keeps every shape that draws', () => { const drawn: readonly Fact[] = [ { term: 'Supply', value: 0 }, { term: 'Supply', value: '1,000' }, { term: 'Rate', unread: 'no venue quotes this' }, ] expect(drawn).toHaveLength(3) }) })