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>
This commit is contained in:
39
frontend/tests/watchlist-alias-copy-ticket24.test.tsx
Normal file
39
frontend/tests/watchlist-alias-copy-ticket24.test.tsx
Normal file
@ -0,0 +1,39 @@
|
||||
import { cleanup, render, screen, waitFor, within } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
|
||||
import { Watchlist } from '../src/pages/Watchlist'
|
||||
|
||||
const { search, list, add } = vi.hoisted(() => ({ search: vi.fn(), list: vi.fn(), add: vi.fn() }))
|
||||
vi.mock('@/api/assets', () => ({ assetsApi: { search, list, add, remove: vi.fn() } }))
|
||||
afterEach(cleanup)
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
list.mockResolvedValue([])
|
||||
search.mockResolvedValue([{ company_id: 'company-ibm', name: 'International Business Machines Corporation', country: 'United States', exchange: 'New York Stock Exchange', mic: 'XNYS', symbol: 'IBM' }])
|
||||
})
|
||||
|
||||
it.each([
|
||||
{ language: 'fr', label: 'Nom ou nom courant de l’entreprise', help: /Intel.*IBM/, results: 'Résultats des entreprises', query: 'Intel', empty: 'Aucune entreprise trouvée. Essayez un autre nom ou nom courant, par exemple Intel ou IBM.' },
|
||||
{ language: 'en', label: 'Company name or commonly used name', help: /Intel.*IBM/, results: 'Company results', query: 'IBM', empty: 'No company found. Try another name or commonly used name, such as Intel or IBM.' },
|
||||
] as const)('affiche les textes accessibles $language et garde la recherche explicite au clavier', async ({ language, label, help, results, query, empty }) => {
|
||||
const user = userEvent.setup()
|
||||
render(<Watchlist />)
|
||||
if (language === 'en') await user.selectOptions(screen.getByLabelText('Langue / Language'), 'en')
|
||||
const input = screen.getByRole('textbox', { name: label })
|
||||
expect(input).toHaveAccessibleDescription(help)
|
||||
expect(input).toHaveAttribute('maxLength', '100')
|
||||
await user.type(input, query)
|
||||
expect(search).not.toHaveBeenCalled()
|
||||
await user.keyboard('{Enter}')
|
||||
await waitFor(() => expect(search).toHaveBeenCalledWith(query, 20))
|
||||
const region = screen.getByRole('region', { name: results })
|
||||
expect(await within(region).findByText('International Business Machines Corporation')).toBeInTheDocument()
|
||||
expect(within(region).getByText('United States')).toBeInTheDocument()
|
||||
expect(within(region).getByText('New York Stock Exchange')).toBeInTheDocument()
|
||||
for (const value of ['IBM', 'XNYS', 'company-ibm']) expect(within(region).queryByText(value, { exact: true })).not.toBeInTheDocument()
|
||||
expect(add).not.toHaveBeenCalled()
|
||||
search.mockResolvedValueOnce([])
|
||||
await user.clear(input)
|
||||
await user.type(input, 'Unknown{Enter}')
|
||||
expect(await screen.findByText(empty)).toHaveAttribute('role', 'status')
|
||||
})
|
||||
@ -49,7 +49,7 @@ describe('Ticket #19 — company-first watchlist search', () => {
|
||||
const history = await screen.findByRole('region', { name: /actifs historiques/i })
|
||||
expect(within(history).getByText('Historic S&P index')).toBeInTheDocument()
|
||||
|
||||
const query = await screen.findByRole('textbox', { name: /nom de l'entreprise/i })
|
||||
const query = await screen.findByRole('textbox', { name: /nom ou nom courant de l’entreprise/i })
|
||||
expect(screen.getAllByRole('textbox')).toHaveLength(1)
|
||||
await user.type(query, 'BYD')
|
||||
expect(searchCompanies).not.toHaveBeenCalled()
|
||||
@ -79,7 +79,7 @@ describe('Ticket #19 — company-first watchlist search', () => {
|
||||
const history = await screen.findByRole('region', { name: /autres suivis existants/i })
|
||||
expect(within(history).getByText('Existing index')).toBeInTheDocument()
|
||||
expect(within(history).queryByRole('button')).not.toBeInTheDocument()
|
||||
await user.type(screen.getByLabelText("Nom de l'entreprise"), 'BYD{Enter}')
|
||||
await user.type(screen.getByLabelText('Nom ou nom courant de l’entreprise'), 'BYD{Enter}')
|
||||
const results = await screen.findByRole('region', { name: /résultats des entreprises/i })
|
||||
expect(await within(results).findByText('Shenzhen Stock Exchange')).toBeInTheDocument()
|
||||
expect(within(results).getByText('China')).toBeInTheDocument()
|
||||
@ -95,7 +95,7 @@ describe('Ticket #19 — company-first watchlist search', () => {
|
||||
render(<Watchlist />)
|
||||
await user.selectOptions(screen.getByLabelText('Langue / Language'), 'en')
|
||||
await screen.findByText('BYD Company Limited')
|
||||
expect(screen.getByLabelText('Company name')).toBeInTheDocument()
|
||||
expect(screen.getByLabelText('Company name or commonly used name')).toBeInTheDocument()
|
||||
expect(screen.queryByText('002594.SZ')).not.toBeInTheDocument()
|
||||
await user.click(screen.getByRole('button', { name: 'Unfollow BYD Company Limited' }))
|
||||
expect(await screen.findByText('Company unfollowed.')).toBeInTheDocument()
|
||||
@ -110,9 +110,9 @@ describe('Ticket #19 — company-first watchlist search', () => {
|
||||
searchCompanies.mockRejectedValueOnce(new Error('offline'))
|
||||
const user = userEvent.setup()
|
||||
render(<Watchlist />)
|
||||
await user.type(screen.getByLabelText("Nom de l'entreprise"), 'BYD{Enter}')
|
||||
await user.type(screen.getByLabelText('Nom ou nom courant de l’entreprise'), 'BYD{Enter}')
|
||||
expect(await screen.findByRole('alert')).toHaveTextContent('Impossible de rechercher des entreprises')
|
||||
expect(screen.getByLabelText("Nom de l'entreprise")).toHaveValue('BYD')
|
||||
expect(screen.getByLabelText('Nom ou nom courant de l’entreprise')).toHaveValue('BYD')
|
||||
expect(searchCompanies).toHaveBeenCalledTimes(1)
|
||||
await user.click(screen.getByRole('button', { name: 'Réessayer' }))
|
||||
expect(await screen.findByText('BYD Company Limited')).toBeInTheDocument()
|
||||
@ -126,7 +126,7 @@ describe('Ticket #19 — company-first watchlist search', () => {
|
||||
] satisfies CompanySearchResult[])
|
||||
const user = userEvent.setup()
|
||||
render(<Watchlist />)
|
||||
await user.type(screen.getByLabelText("Nom de l'entreprise"), 'Example{Enter}')
|
||||
await user.type(screen.getByLabelText('Nom ou nom courant de l’entreprise'), 'Example{Enter}')
|
||||
const results = screen.getByRole('region', { name: /résultats des entreprises/i })
|
||||
const names = await within(results).findAllByText('Example Company')
|
||||
expect(names).toHaveLength(2)
|
||||
@ -145,7 +145,7 @@ describe('Ticket #19 — company-first watchlist search', () => {
|
||||
searchCompanies.mockResolvedValueOnce([{ company_id: 'company-unknown', name: 'Example Company', country: '', exchange: '', mic: 'XNAS' }] satisfies CompanySearchResult[])
|
||||
const user = userEvent.setup()
|
||||
render(<Watchlist />)
|
||||
await user.type(screen.getByLabelText("Nom de l'entreprise"), 'Example{Enter}')
|
||||
await user.type(screen.getByLabelText('Nom ou nom courant de l’entreprise'), 'Example{Enter}')
|
||||
const name = await screen.findByText('Example Company')
|
||||
expect(name).toHaveClass('font-semibold')
|
||||
expect(screen.queryByText('XNAS')).not.toBeInTheDocument()
|
||||
|
||||
@ -50,7 +50,7 @@ describe('Régressions Watchlist — recherche explicite et suivis d’entrepris
|
||||
searchIndices.mockResolvedValueOnce([indexOne])
|
||||
render(<Watchlist />)
|
||||
|
||||
const query = await screen.findByRole('textbox', { name: "Nom de l'entreprise" })
|
||||
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()
|
||||
@ -74,9 +74,9 @@ describe('Régressions Watchlist — recherche explicite et suivis d’entrepris
|
||||
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 de l'entreprise")).toBeInTheDocument()
|
||||
expect(screen.getByLabelText('Nom ou nom courant de l’entreprise')).toBeInTheDocument()
|
||||
|
||||
const query = screen.getByRole('textbox', { name: "Nom de l'entreprise" })
|
||||
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()
|
||||
@ -94,7 +94,7 @@ describe('Régressions Watchlist — recherche explicite et suivis d’entrepris
|
||||
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 de l'entreprise" })
|
||||
const query = await screen.findByRole('textbox', { name: 'Nom ou nom courant de l’entreprise' })
|
||||
|
||||
searchIndices.mockResolvedValueOnce([])
|
||||
await user.type(query, 'No match{Enter}')
|
||||
@ -122,7 +122,7 @@ describe('Régressions Watchlist — recherche explicite et suivis d’entrepris
|
||||
.mockReturnValueOnce(new Promise(resolve => { resolveCurrent = resolve }))
|
||||
const user = userEvent.setup()
|
||||
render(<Watchlist />)
|
||||
const query = await screen.findByRole('textbox', { name: "Nom de l'entreprise" })
|
||||
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)
|
||||
@ -142,7 +142,7 @@ describe('Régressions Watchlist — recherche explicite et suivis d’entrepris
|
||||
searchIndices.mockResolvedValueOnce([indexTwo])
|
||||
render(<Watchlist />)
|
||||
|
||||
const query = await screen.findByRole('textbox', { name: "Nom de l'entreprise" })
|
||||
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 }))
|
||||
|
||||
Reference in New Issue
Block a user