import { describe, expect, test, vi, beforeEach } from 'vitest'; vi.mock('./client', () => ({ api: { get: vi.fn(), post: vi.fn(), put: vi.fn(), del: vi.fn() } })); import { searchCatalogue } from './catalogue'; import { api } from './client'; import { qk } from './queries'; import type { CatalogueSearchResult } from './types'; beforeEach(() => { vi.clearAllMocks(); }); describe('searchCatalogue', () => { test('builds URL with q and kind query params', async () => { (api.get as ReturnType).mockResolvedValueOnce([]); await searchCatalogue('aphex twin', 'artist'); expect(api.get).toHaveBeenCalledWith('/api/catalogue/search?q=aphex+twin&kind=artist'); }); test('encodes special characters in q (quotes, ampersands, plus)', async () => { (api.get as ReturnType).mockResolvedValueOnce([]); await searchCatalogue('boards of canada & friends', 'album'); const calledWith = (api.get as ReturnType).mock.calls[0][0] as string; // URLSearchParams encodes space as '+' and '&' as '%26'. expect(calledWith).toBe( '/api/catalogue/search?q=boards+of+canada+%26+friends&kind=album' ); }); test('passes through results unchanged', async () => { const fixture: CatalogueSearchResult[] = [ { mbid: 'mbid-1', name: 'Aphex Twin', secondary_text: 'Electronic', image_url: '/api/catalogue/art/artist/mbid-1?name=Aphex+Twin', artist_mbid: '', album_mbid: '', in_library: false, requested: false } ]; (api.get as ReturnType).mockResolvedValueOnce(fixture); const out = await searchCatalogue('aphex', 'artist'); expect(out).toBe(fixture); }); test('propagates errors from api.get', async () => { const err = { code: 'musicbrainz_unreachable', message: 'down', status: 503 }; (api.get as ReturnType).mockRejectedValueOnce(err); await expect(searchCatalogue('x', 'track')).rejects.toMatchObject({ code: 'musicbrainz_unreachable', status: 503 }); }); }); describe('qk.catalogueSearch', () => { test('keys include q + kind', () => { expect(qk.catalogueSearch('foo', 'artist')).toEqual([ 'catalogueSearch', { q: 'foo', kind: 'artist' } ]); }); });