git clone https://garage44.eu/jeroen/midas.git.. /web/src/lib/focus.test.ts
import {expect, test} from 'bun:test'
import {answerWords, capShare, parseBlocks, ruleFromQuestion} from '@/lib/focus'
import type {Focus} from '@/lib/focus'
test('blocks are the times typed, the rest dropped', () => {
expect(parseBlocks('10:00, 15:00')).toEqual(['10:00', '15:00'])
expect(parseBlocks(' 9:30 noon 15:00,')).toEqual(['9:30', '15:00'])
expect(parseBlocks('')).toEqual([])
})
test('an answer reads as words, whatever its shape', () => {
expect(answerWords('allow')).toBe('allow')
expect(answerWords({question_0: 'Land it', question_0_custom: ''})).toBe('Land it')
expect(answerWords({question_0: ['A', 'B'], question_1: 'C'})).toBe('A, B; C')
expect(answerWords(null)).toBe('')
})
test('a rule from a question quotes it, shortened', () => {
expect(ruleFromQuestion('Which tile\ncache size?')).toBe('When a session asks “Which tile cache size?”, take the recommended option if it fits what I asked.')
expect(ruleFromQuestion('x'.repeat(300)).length).toBeLessThan(260)
})
test('the share of the cap is none without a cap', () => {
const f = {settings: {cap_points: null}, today: {points: 12}} as unknown as Focus
expect(capShare(f)).toBeNull()
expect(capShare({...f, settings: {...f.settings, cap_points: 40}})).toBe(0.3)
})