import { describe, it, expect } from 'vitest'; import { parseCancerList, parseExplorerParams, serializeExplorerParams, apiQueryFor, yearRangeLabel, sexLabel, ageLabel, MAX_CANCERS } from '@/lib/explorer-params'; const D = { metric: 'mortality_count', geography: 'united-states', cancers: ['a', 'b', 'c', 'd', 'e'], from: 1999, to: 2024 }; describe('parseCancerList', () => { it('accepts comma-separated, repeated and mixed forms', () => { expect(parseCancerList('lung,breast')).toEqual(['lung', 'breast']); expect(parseCancerList(['lung', 'breast'])).toEqual(['lung', 'breast']); expect(parseCancerList(['lung,breast', 'colon'])).toEqual(['lung', 'breast', 'colon']); }); it('normalizes case, drops duplicates, invalid tokens and blanks', () => { expect(parseCancerList(' Lung , lung, ,CI-CAN-00000042, ../etc, ok_slug ')).toEqual(['lung', 'CI-CAN-00000042']); }); it('caps at MAX_CANCERS keeping order', () => { const many = Array.from({ length: 10 }, (_, i) => `c${i}`).join(','); expect(parseCancerList(many)).toEqual(['c0', 'c1', 'c2', 'c3', 'c4', 'c5']); expect(parseCancerList(many).length).toBe(MAX_CANCERS); }); it('returns [] for undefined', () => { expect(parseCancerList(undefined)).toEqual([]); }); }); describe('parseExplorerParams', () => { it('fills every gap from the computed defaults', () => { const s = parseExplorerParams({}, D); expect(s).toEqual({ metric: 'mortality_count', cancers: ['a', 'b', 'c', 'd', 'e'], geography: 'united-states', sex: 'all', age: 'all', from: 1999, to: 2024, view: 'lines', normalize: 'none', page: 1 }); }); it('reads valid values and rejects malformed ones', () => { const s = parseExplorerParams({ metric: 'AS_Mortality_Rate', cancers: 'lung', geography: 'usa', sex: 'Female', age: '65+', from: '2010', to: '2005', view: 'multiples', page: '3' }, D); expect(s.metric).toBe('as_mortality_rate'); expect(s.cancers).toEqual(['lung']); expect(s.geography).toBe('USA'); // 3-letter → ISO3 upper-case expect(s.sex).toBe('female'); expect(s.age).toBe('65+'); expect([s.from, s.to]).toEqual([2005, 2010]); // swapped when inverted expect(s.view).toBe('multiples'); expect(s.page).toBe(3); const bad = parseExplorerParams({ metric: 'DROP TABLE', geography: 'a b', sex: '1;2', age: '