Add the nullable FIGI migration, owner-scoped asset identity operations, and the explicit index search and selection flow. Refs #4 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
33 lines
1.9 KiB
TypeScript
33 lines
1.9 KiB
TypeScript
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 le FIGI et retire par ID', async () => {
|
|
localStorage.setItem('token', 'reader-token')
|
|
const index = { symbol: 'SPX', name: 'S&P 500', figi: 'BBG-INDEX' }
|
|
const legacy = { id: 'legacy-id', symbol: 'OLD', name: 'Historic', figi: null }
|
|
const fetchMock = vi.fn()
|
|
.mockResolvedValueOnce(new Response(JSON.stringify({ data: [index] })))
|
|
.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('S&P 500', 20)).toEqual([index])
|
|
await assetsApi.add(index)
|
|
await assetsApi.remove('asset/id')
|
|
expect(await assetsApi.list()).toEqual([legacy])
|
|
expect(fetchMock).toHaveBeenNthCalledWith(1, '/api/indices/search?q=S%26P+500&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')
|
|
})
|