feat(assets): track selected indices by FIGI
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>
This commit is contained in:
32
frontend/tests/assets-api.test.ts
Normal file
32
frontend/tests/assets-api.test.ts
Normal file
@ -0,0 +1,32 @@
|
||||
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')
|
||||
})
|
||||
158
frontend/tests/watchlist-index-search-ticket4.test.tsx
Normal file
158
frontend/tests/watchlist-index-search-ticket4.test.tsx
Normal file
@ -0,0 +1,158 @@
|
||||
import { cleanup, render, screen, waitFor, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { Watchlist } from '../src/pages/Watchlist'
|
||||
|
||||
const { listAssets, searchIndices, addAsset, removeAsset } = vi.hoisted(() => ({
|
||||
listAssets: vi.fn(),
|
||||
searchIndices: vi.fn(),
|
||||
addAsset: vi.fn(),
|
||||
removeAsset: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/api/assets', () => ({
|
||||
assetsApi: {
|
||||
list: listAssets,
|
||||
search: searchIndices,
|
||||
add: addAsset,
|
||||
remove: removeAsset,
|
||||
},
|
||||
}))
|
||||
|
||||
const legacyAsset = {
|
||||
id: 'legacy-asset-1', user_id: 'reader-1', symbol: 'OLDX', name: 'Old historic asset',
|
||||
figi: null, created_at: '2025-01-01T00:00:00.000Z',
|
||||
}
|
||||
const indexOne = { figi: 'BBG000SPX001', name: 'S&P 500', symbol: 'SPX', market: 'US' }
|
||||
const indexTwo = { figi: 'BBG000SPX002', name: 'S&P 500 alternate listing', symbol: 'SPX', market: 'US' }
|
||||
|
||||
function apiError(status: number, message: string) {
|
||||
return Object.assign(new Error(message), { status })
|
||||
}
|
||||
|
||||
function resultRegion() {
|
||||
return screen.getByRole('region', { name: /résultats de recherche/i })
|
||||
}
|
||||
|
||||
describe('Ticket #4 — rechercher et suivre des indices', () => {
|
||||
afterEach(cleanup)
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
listAssets.mockResolvedValue([legacyAsset])
|
||||
searchIndices.mockResolvedValue([])
|
||||
addAsset.mockResolvedValue({ id: 'new-index-1', ...indexOne, user_id: 'reader-1' })
|
||||
removeAsset.mockResolvedValue(undefined)
|
||||
})
|
||||
|
||||
it('déclenche la recherche explicitement au clic ou à Entrée et limite la demande à 20 résultats', async () => {
|
||||
const user = userEvent.setup()
|
||||
searchIndices.mockResolvedValueOnce([indexOne])
|
||||
render(<Watchlist />)
|
||||
|
||||
const query = await screen.findByRole('textbox', { name: 'Nom ou symbole' })
|
||||
expect(query).toHaveAttribute('maxLength', '100')
|
||||
await user.type(query, 'S&P 500')
|
||||
expect(searchIndices).not.toHaveBeenCalled()
|
||||
await user.click(screen.getByRole('button', { name: 'Rechercher' }))
|
||||
await waitFor(() => expect(searchIndices).toHaveBeenCalledWith('S&P 500', 20))
|
||||
expect(await within(resultRegion()).findByText('S&P 500')).toBeInTheDocument()
|
||||
expect(within(resultRegion()).queryByText('BBG000SPX001')).not.toBeInTheDocument()
|
||||
|
||||
await user.clear(query)
|
||||
await user.type(query, 'NASDAQ{Enter}')
|
||||
await waitFor(() => expect(searchIndices).toHaveBeenLastCalledWith('NASDAQ', 20))
|
||||
expect(searchIndices).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('exige le choix explicite, transmet le FIGI, affiche les doublons ambigus et garde les actifs legacy', async () => {
|
||||
const user = userEvent.setup()
|
||||
listAssets.mockResolvedValueOnce([legacyAsset, { id: 'followed-1', ...indexOne, user_id: 'reader-1' }])
|
||||
searchIndices.mockResolvedValueOnce([indexOne, indexTwo])
|
||||
render(<Watchlist />)
|
||||
|
||||
const history = await screen.findByRole('region', { name: /actifs historiques/i })
|
||||
expect(within(history).getByText('Type non vérifié')).toBeInTheDocument()
|
||||
expect(within(history).getByText('Old historic asset')).toBeInTheDocument()
|
||||
expect(screen.getByLabelText('Nom ou symbole')).toBeInTheDocument()
|
||||
|
||||
const query = screen.getByRole('textbox', { name: 'Nom ou symbole' })
|
||||
await user.type(query, 'S&P 500{Enter}')
|
||||
const results = await screen.findByRole('region', { name: /résultats de recherche/i })
|
||||
expect(within(results).getByText('BBG000SPX001')).toBeInTheDocument()
|
||||
expect(within(results).getByText('BBG000SPX002')).toBeInTheDocument()
|
||||
expect(within(results).getByRole('button', { name: /déjà suivi/i })).toBeDisabled()
|
||||
const follow = within(results).getAllByRole('button', { name: /suivre cet indice/i })[0]
|
||||
await user.click(follow)
|
||||
await waitFor(() => expect(addAsset).toHaveBeenCalledWith({
|
||||
symbol: 'SPX', name: 'S&P 500 alternate listing', figi: 'BBG000SPX002',
|
||||
}))
|
||||
expect(addAsset).toHaveBeenCalledTimes(1)
|
||||
expect(within(history).getByText('Old historic asset')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('annonce aucun résultat et les erreurs quota, timeout et indisponibilité sans effacer la recherche', async () => {
|
||||
const user = userEvent.setup()
|
||||
render(<Watchlist />)
|
||||
const query = await screen.findByRole('textbox', { name: 'Nom ou symbole' })
|
||||
|
||||
searchIndices.mockResolvedValueOnce([])
|
||||
await user.type(query, 'No match{Enter}')
|
||||
expect(await screen.findByText(/aucun indice trouvé/i)).toBeInTheDocument()
|
||||
|
||||
for (const [error, message] of [
|
||||
[apiError(429, 'rate limited'), /quota de recherche/i],
|
||||
[apiError(504, 'timeout'), /délai.*dépassé|timeout/i],
|
||||
[apiError(502, 'provider unavailable'), /fournisseur.*indisponible/i],
|
||||
] as const) {
|
||||
searchIndices.mockRejectedValueOnce(error)
|
||||
await user.clear(query)
|
||||
await user.type(query, 'NASDAQ{Enter}')
|
||||
expect(await screen.findByRole('alert')).toHaveTextContent(message)
|
||||
expect(query).toHaveValue('NASDAQ')
|
||||
}
|
||||
expect(searchIndices).toHaveBeenCalledTimes(4)
|
||||
})
|
||||
|
||||
it('ignore les réponses de recherches obsolètes sans remplacer les résultats courants', async () => {
|
||||
let resolveOld!: (value: unknown) => void
|
||||
let resolveCurrent!: (value: unknown) => void
|
||||
searchIndices
|
||||
.mockReturnValueOnce(new Promise(resolve => { resolveOld = resolve }))
|
||||
.mockReturnValueOnce(new Promise(resolve => { resolveCurrent = resolve }))
|
||||
const user = userEvent.setup()
|
||||
render(<Watchlist />)
|
||||
const query = await screen.findByRole('textbox', { name: 'Nom ou symbole' })
|
||||
|
||||
await user.type(query, 'Old query{Enter}')
|
||||
await user.clear(query)
|
||||
await user.type(query, 'Current query{Enter}')
|
||||
resolveCurrent([{ figi: 'BBG-CURRENT', name: 'Current index', symbol: 'CUR' }])
|
||||
expect(await screen.findByText('Current index')).toBeInTheDocument()
|
||||
resolveOld([{ figi: 'BBG-STALE', name: 'Stale index', symbol: 'OLD' }])
|
||||
await waitFor(() => expect(screen.queryByText('Stale index')).not.toBeInTheDocument())
|
||||
expect(screen.getByText('Current index')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('conserve les actifs après erreur de suivi ou retrait et permet le retrait par identifiant', async () => {
|
||||
const user = userEvent.setup()
|
||||
listAssets.mockResolvedValueOnce([{ id: 'tracked-1', ...indexOne, user_id: 'reader-1' }])
|
||||
addAsset.mockRejectedValueOnce(apiError(409, 'ambiguous legacy asset'))
|
||||
removeAsset.mockRejectedValueOnce(new Error('network unavailable'))
|
||||
searchIndices.mockResolvedValueOnce([indexTwo])
|
||||
render(<Watchlist />)
|
||||
|
||||
const query = await screen.findByRole('textbox', { name: 'Nom ou symbole' })
|
||||
await user.type(query, 'S&P 500{Enter}')
|
||||
const results = await screen.findByRole('region', { name: /résultats de recherche/i })
|
||||
await user.click(within(results).getByRole('button', { name: /suivre cet indice/i }))
|
||||
expect(await screen.findByRole('alert')).toBeInTheDocument()
|
||||
expect(query).toHaveValue('S&P 500')
|
||||
|
||||
await user.click(screen.getByRole('button', { name: /ne plus suivre/i }))
|
||||
expect(await screen.findByRole('alert')).toBeInTheDocument()
|
||||
expect(removeAsset).toHaveBeenCalledWith('tracked-1')
|
||||
expect(screen.getByText('S&P 500')).toBeInTheDocument()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user