import { afterEach, expect, it, vi } from 'vitest' import { assetsApi } from '../src/api/assets' import { ApiError } from '../src/api/client' afterEach(() => { vi.unstubAllGlobals(); localStorage.clear() }) it('recherche explicitement avec q/limit, suit une entreprise et retire par ID', async () => { localStorage.setItem('token', 'reader-token') const index = { company_id: 'isin:US0378331005' } const company = { ...index, name: 'Apple Inc.', exchange: 'Nasdaq', country: 'United States', mic: 'XNAS' } const legacy = { id: 'legacy-id', symbol: 'OLD', name: 'Historic', figi: null } const fetchMock = vi.fn() .mockResolvedValueOnce(new Response(JSON.stringify({ data: [company] }))) .mockResolvedValueOnce(new Response(JSON.stringify({ data: { id: 'asset-id', ...index } }), { status: 201 })) .mockResolvedValueOnce(new Response(null, { status: 204 })) .mockResolvedValueOnce(new Response(JSON.stringify({ data: [legacy] }))) vi.stubGlobal('fetch', fetchMock) expect(await assetsApi.search('Apple Inc.', 20)).toEqual([company]) await assetsApi.add(index) await assetsApi.remove('asset/id') expect(await assetsApi.list()).toEqual([legacy]) expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/companies/search?q=Apple+Inc.&limit=20', expect.objectContaining({ headers: expect.objectContaining({ Authorization: 'Bearer reader-token' }) })) expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/me/assets', expect.objectContaining({ method: 'POST', body: JSON.stringify(index) })) expect(fetchMock).toHaveBeenNthCalledWith(3, '/api/me/assets/asset%2Fid', expect.objectContaining({ method: 'DELETE' })) }) it('conserve le statut HTTP pour les messages quota et indisponibilité', async () => { vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify({ error: 'rate limited' }), { status: 429 }))) const error = await assetsApi.search('NASDAQ', 20).catch(error => error) expect(error).toBeInstanceOf(ApiError) expect(error.status).toBe(429) expect(error.message).toBe('rate limited') })