import { describe, expect, it } from 'vitest' import { CATALOGUE_COMMAND_ITEM, createCommandPaletteItems, createMachineCommandItem, filterCommandPaletteItems, normaliseForCommandSearch, } from '../src/utils/commandPalette' describe('command palette search', () => { it('normalises punctuation, accents, ampersands, and casing', () => { expect(normaliseForCommandSearch('Crème-Brûlée & Pump’s')).toBe( 'creme brulee and pumps', ) }) it('creates machine destinations from registry-like records', () => { const item = createMachineCommandItem({ id: 'external-gear-pump', slug: 'pumps/external gear pump', name: 'External Gear Pump', category: 'Pumps', difficulty: 'Intermediate', summary: 'Positive displacement pump with meshing spur gears.', tags: ['hydraulic', 'positive displacement'], }) expect(item).toMatchObject({ id: 'machine:pumps/external gear pump', kind: 'machine', label: 'External Gear Pump', href: '/machines/pumps%2Fexternal%20gear%20pump', category: 'Pumps', difficulty: 'Intermediate', }) expect(item?.keywords).toEqual( expect.arrayContaining([ 'External Gear Pump', 'pumps/external gear pump', 'hydraulic', 'positive displacement', ]), ) }) it('places the catalogue route first for an empty query and then sorts machines by label', () => { const items = createCommandPaletteItems([ { id: 'z-machine', name: 'Z Machine', }, { id: 'a-machine', name: 'A Machine', }, ]) const results = filterCommandPaletteItems(items, '', { maxResults: 3 }) expect(results.map((result) => result.id)).toEqual([ CATALOGUE_COMMAND_ITEM.id, 'machine:a-machine', 'machine:z-machine', ]) }) it('matches multi-token queries across labels, aliases, and tags', () => { const items = createCommandPaletteItems( [ { id: 'planetary-gearbox', name: 'Planetary Gearbox', category: 'Gearboxes', difficulty: 'Advanced', summary: 'Compact epicyclic gear train with a sun gear and carrier.', tags: ['epicyclic', 'carrier'], }, { id: 'wankel-rotary-engine', name: 'Wankel Rotary Engine', category: 'Engines', difficulty: 'Advanced', aliases: ['rotor engine'], tags: ['epitrochoid rotor'], }, ], { includeCatalogue: false }, ) const results = filterCommandPaletteItems(items, 'rotor engine') expect(results[0]).toMatchObject({ id: 'machine:wankel-rotary-engine', label: 'Wankel Rotary Engine', href: '/machines/wankel-rotary-engine', }) expect(results[0]?.matches).toEqual( expect.arrayContaining(['label', 'keywords']), ) }) it('supports multi-field searches such as difficulty plus machine family', () => { const items = createCommandPaletteItems( [ { id: 'external-gear-pump', name: 'External Gear Pump', category: 'Pumps', difficulty: 'Advanced', }, { id: 'planetary-gearbox', name: 'Planetary Gearbox', category: 'Gearboxes', difficulty: 'Advanced', }, ], { includeCatalogue: false }, ) const results = filterCommandPaletteItems(items, 'advanced pump') expect(results).toHaveLength(1) expect(results[0]).toMatchObject({ id: 'machine:external-gear-pump', label: 'External Gear Pump', }) expect(results[0]?.matches).toEqual( expect.arrayContaining(['label', 'difficulty']), ) }) it('ranks direct label matches above keyword-only matches', () => { const items = createCommandPaletteItems( [ { id: 'centrifugal-pump', name: 'Centrifugal Pump', category: 'Pumps', }, { id: 'hydraulic-test-rig', name: 'Hydraulic Test Rig', category: 'Test Rigs', tags: ['centrifugal pump'], }, ], { includeCatalogue: false }, ) const results = filterCommandPaletteItems(items, 'centrifugal') expect(results.map((result) => result.label)).toEqual([ 'Centrifugal Pump', 'Hydraulic Test Rig', ]) }) })