Files
TradarrV2/frontend/tests/watchlist-index-search-ticket4.test.tsx
Blomios f7b057efa3 feat(search): add exact company aliases
Embed a versioned local alias overlay and rank exact aliases before legal-name matches and word prefixes. Normalize Unicode consistently, deduplicate by company identity before limiting, and update bilingual search guidance and tests.

Refs #24

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-22 18:08:35 +01:00

159 lines
7.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 = { company_id: 'company-a', name: 'Apple Inc.', country: 'United States', exchange: 'Nasdaq', mic: 'XNAS', symbol: 'SSAAPL INDEX' }
const indexTwo = { company_id: 'company-b', name: 'Apple Holdings', country: 'United Kingdom', exchange: 'London Stock Exchange', mic: 'XLON', symbol: 'AAPL/B' }
function apiError(status: number, message: string) {
return Object.assign(new Error(message), { status })
}
function resultRegion() {
return screen.getByRole('region', { name: /résultats des entreprises/i })
}
describe('Régressions Watchlist — recherche explicite et suivis d’entreprises', () => {
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 nom courant de l’entreprise' })
expect(query).toHaveAttribute('maxLength', '100')
await user.type(query, 'Apple Inc.')
expect(searchIndices).not.toHaveBeenCalled()
await user.click(screen.getByRole('button', { name: 'Rechercher' }))
await waitFor(() => expect(searchIndices).toHaveBeenCalledWith('Apple Inc.', 20))
expect(await within(resultRegion()).findByText('Apple Inc.')).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 company_id et garde les suivis existants', 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).queryByRole('button')).not.toBeInTheDocument()
expect(within(history).getByText('Old historic asset')).toBeInTheDocument()
expect(screen.getByLabelText('Nom ou nom courant de l’entreprise')).toBeInTheDocument()
const query = screen.getByRole('textbox', { name: 'Nom ou nom courant de l’entreprise' })
await user.type(query, 'Apple Inc.{Enter}')
const results = await screen.findByRole('region', { name: /résultats des entreprises/i })
expect(within(results).getByText('Nasdaq')).toBeInTheDocument()
expect(within(results).getByText('London Stock Exchange')).toBeInTheDocument()
expect(within(results).getByRole('button', { name: /déjà suivie/i })).toBeDisabled()
const follow = within(results).getAllByRole('button', { name: /suivre cette entreprise/i })[0]
await user.click(follow)
await waitFor(() => expect(addAsset).toHaveBeenCalledWith({
company_id: 'company-b',
}))
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 nom courant de l’entreprise' })
searchIndices.mockResolvedValueOnce([])
await user.type(query, 'No match{Enter}')
expect(await screen.findByText(/aucune entreprise trouvée/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'), /service.*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 nom courant de l’entreprise' })
await user.type(query, 'Old query{Enter}')
await user.clear(query)
await user.type(query, 'Current query{Enter}')
resolveCurrent([{ company_id: 'current-company', name: 'Current company', exchange: '', country: '', mic: '' }])
expect(await screen.findByText('Current company')).toBeInTheDocument()
resolveOld([{ company_id: 'stale-company', name: 'Stale company', exchange: '', country: '', mic: '' }])
await waitFor(() => expect(screen.queryByText('Stale company')).not.toBeInTheDocument())
expect(screen.getByText('Current company')).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 nom courant de l’entreprise' })
await user.type(query, 'Apple Inc.{Enter}')
const results = await screen.findByRole('region', { name: /résultats des entreprises/i })
await user.click(within(results).getByRole('button', { name: /suivre cette entreprise/i }))
expect(await screen.findByRole('alert')).toBeInTheDocument()
expect(query).toHaveValue('Apple Inc.')
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('Apple Inc.')).toBeInTheDocument()
})
})