import { describe, expect, it } from 'vitest' import { reasons, said, type Figure, type Said, type Stat } from './tally' /** * The two decisions a tally makes out of what it was handed. * * Both are the component's whole argument, and neither is visible in a * screenshot. A dash drawn for a read that failed and a dash drawn for an * answer of nothing are the same picture, and four entries repeating one * reason under themselves is a screen that looks like four problems. */ const say: Said = { since: 'today', missing: 'not available', none: 'none', percent: (of) => `${Math.round(of * 100)}%`, } const stat = (name: string, of: Figure): Stat => ({ name, of }) describe('what an entry puts where the figure goes', () => { it('writes the figure it was read as', () => { expect(said({ at: 'read', it: '$1,284,003' }, say)).toBe('$1,284,003') }) it('says nothing in the word the caller chose, not a dash', () => { expect(said({ at: 'read', it: null }, say)).toBe(say.none) }) it('says a failed read differently from an answer of nothing', () => { const failed = said({ at: 'failed', why: 'the indexer did not answer' }, say) expect(failed).toBe(say.missing) // The substitution the component exists to prevent. One mark for both is a // surface reporting an empty venue when its indexer is down. expect(failed).not.toBe(said({ at: 'read', it: null }, say)) }) it('marks a running read rather than naming it', () => { // The ellipsis says the same thing in every language, and a catalog entry // holding one character is one a translator can only get wrong. expect(said({ at: 'reading' }, say)).toBe('…') }) it('never writes the reason into the figure', () => { expect(said({ at: 'failed', why: 'HTTP 502 from the indexer' }, say)).not.toContain('502') }) }) describe('the reasons under the row', () => { it('says one reason once however many entries carry it', () => { const why = 'the indexer did not answer' expect( reasons([ stat('Volume', { at: 'failed', why }), stat('Locked', { at: 'failed', why }), stat('Fees', { at: 'failed', why }), ]), ).toEqual([why]) }) it('states reasons that genuinely differ', () => { expect( reasons([ stat('Volume', { at: 'failed', why: 'the indexer did not answer' }), stat('Pools', { at: 'failed', why: 'this venue has no indexer' }), ]), ).toEqual(['the indexer did not answer', 'this venue has no indexer']) }) it('keeps them in the order the entries stand in', () => { expect( reasons([ stat('a', { at: 'failed', why: 'second' }), stat('b', { at: 'read', it: '1' }), stat('c', { at: 'failed', why: 'first' }), ]), ).toEqual(['second', 'first']) }) it('has nothing to say where every read arrived', () => { expect(reasons([stat('a', { at: 'read', it: '1' }), stat('b', { at: 'reading' })])).toEqual([]) }) })