import { describe, expect, it } from 'vitest' import { computed, ref } from 'vue' import { candidateMatchesSearch, normalizeSearchQuery, useTextSearch, } from '../../composables/useTextSearch' describe('useTextSearch', () => { const items = [ { name: 'Glucose', catalogNumber: 'G-7528', supplier: 'Sigma' }, { name: 'Lactate', catalogNumber: 'L-1250', supplier: 'Cayman' }, { name: 'Pyruvate', catalogNumber: 'P-2256', supplier: 'Thermo' }, ] it('normalizes whitespace and case', () => { expect(normalizeSearchQuery(' GLU ')).toBe('glu') expect(normalizeSearchQuery(undefined)).toBe('') }) it('filters by multiple candidate values', () => { const query = ref('cay') const search = useTextSearch({ items, query, getText: item => [item.name, item.catalogNumber, item.supplier], }) expect(search.filteredItems.value.map(item => item.name)).toEqual(['Lactate']) query.value = ' p-2256 ' expect(search.filteredItems.value.map(item => item.name)).toEqual(['Pyruvate']) }) it('returns all items when disabled', () => { const enabled = ref(false) const search = useTextSearch({ items: computed(() => items), query: 'sigma', enabled, getText: item => item.supplier, }) expect(search.filteredItems.value).toHaveLength(3) enabled.value = true expect(search.filteredItems.value.map(item => item.name)).toEqual(['Glucose']) }) it('matches empty queries and ignores nullish candidates', () => { expect(candidateMatchesSearch([null, undefined, 'Alpha'], '')).toBe(true) expect(candidateMatchesSearch([null, undefined, 'Alpha'], 'alp')).toBe(true) expect(candidateMatchesSearch([null, undefined], 'alp')).toBe(false) }) })