import { describe, expect, it } from 'vitest'; import { searchRank } from '../../src/ui/search'; describe('searchRank', () => { const fields = ['Set variable', 'setvariable', 'Writes a value into a variable']; it('ranks everything alike when nothing was typed', () => { expect(searchRank('', fields)).toBe(0); expect(searchRank('', ['anything at all'])).toBe(0); }); it('answers -1 when no field contains the query', () => { expect(searchRank('collider', fields)).toBe(-1); }); it('puts an exact hit first, whichever field it came from', () => { expect(searchRank('set variable', fields)).toBe(0); expect(searchRank('setvariable', fields)).toBe(0); }); it('prefers a prefix over a substring, and the label over the type', () => { const prefix = searchRank('set', fields); const substring = searchRank('variable', fields); expect(prefix).toBeGreaterThan(0); expect(prefix).toBeLessThan(substring); }); it('ranks a description-only hit behind every hit on the label', () => { const description = searchRank('writes', fields); expect(description).toBeGreaterThan(searchRank('variable', fields)); }); it('takes the best field, not the first that matches', () => { // "value" sits in the description only, "Set" in the label -- a query in both takes the label. expect(searchRank('set', ['Set value', 'setvalue', 'Set a value'])).toBe(1); }); });