import { describe, expect, it } from 'vitest' import type { OfficialSheetAdminSummary } from '@faunosteo/supabase' import { NO_FILTER, activeFilterCount } from '@/admin/filters' import { searchSheets, sortSheets } from '@/admin/sheetList' const sheet = (id: string, fr: string, en: string, at: string) => ({ sheet_id: id, name: { fr, en }, last_modified_at: at }) as OfficialSheetAdminSummary const A = sheet('a', 'Âne, squelette', 'Donkey, skeleton', '2026-11-03T10:02:00+00:00') const C = sheet('c', 'Cheval (tête)', 'Horse (head)', '2026-11-09T11:06:00.123456+00:00') const E = sheet('e', 'Émeu', 'Emu', '2026-11-05T15:20:00+00:00') const Z = sheet('z', 'Zèbre', 'Zebra', '2026-11-05T15:20:00+00:00') const ids = (list: OfficialSheetAdminSummary[]) => list.map((s) => s.sheet_id).join('') describe('searchSheets', () => { it('matches the French or the English name, accents and case ignored', () => { expect(ids(searchSheets([A, C, E, Z], 'ane'))).toBe('a') expect(ids(searchSheets([A, C, E, Z], 'HORSE'))).toBe('c') expect(ids(searchSheets([A, C, E, Z], 'émeu'))).toBe('e') }) it('ignores surrounding spaces; an empty search keeps every sheet; brackets are plain text', () => { expect(ids(searchSheets([A, C], ' cheval '))).toBe('c') expect(ids(searchSheets([A, C], ' '))).toBe('ac') expect(ids(searchSheets([A, C], '(tête)'))).toBe('c') }) }) describe('sortSheets', () => { it('modified: newest first, equal dates by name', () => { expect(ids(sortSheets([A, Z, E, C], 'modified', 'fr'))).toBe('ceza') }) it('name: the collation of the interface language, not code points', () => { expect(ids(sortSheets([Z, E, C, A], 'name', 'fr'))).toBe('acez') expect(ids(sortSheets([Z, E, C, A], 'name', 'en'))).toBe('aecz') }) it('equal names fall back to the id, whatever the input order', () => { const twin = { ...A, sheet_id: 'b' } expect(ids(sortSheets([twin, A], 'name', 'fr'))).toBe('ab') expect(ids(sortSheets([A, twin], 'modified', 'fr'))).toBe('ab') }) it('returns a new array and leaves its input alone', () => { const input = [Z, A] expect(sortSheets(input, 'name', 'fr')).not.toBe(input) expect(ids(input)).toBe('za') }) }) describe('activeFilterCount', () => { it('counts the filters that are not empty', () => { expect(activeFilterCount(NO_FILTER)).toBe(0) const two = { ...NO_FILTER, species: 'equine', roundStatus: 'open' } expect(activeFilterCount(two)).toBe(2) }) })