feat(search): find companies by stock symbol

Index exact symbols from selected primary listings, preserve canonical company identities, and add BABA as an explicit alias for Alibaba's primary identity. Update bilingual help and cover symbol collisions, aliases, and DTO stability.

Refs #25

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 18:31:46 +01:00
parent f7b057efa3
commit f97ae18d6c
10 changed files with 354 additions and 19 deletions

View File

@ -0,0 +1,118 @@
package companycatalog
import (
"reflect"
"strings"
"testing"
)
func TestTicket25RealCatalogExactSymbols(t *testing.T) {
catalog, err := Default()
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct{ query, id, name string }{
{" amzn ", "isin:US0231351067", "Amazon.com, Inc."},
{"INTC", "isin:US4581401001", "Intel Corporation"},
{"9888.hk", "isin:KYG070341048", "Baidu, Inc."},
{"BRK-B", "isin:US0846707026", "Berkshire Hathaway Inc."},
{"9988.HK", "isin:KYG017191142", "Alibaba Group Holding Limited"},
} {
t.Run(tc.query, func(t *testing.T) {
canonical, err := catalog.ResolveCompany(tc.id)
if err != nil || canonical.Name != tc.name {
t.Fatalf("real snapshot identity=%+v err=%v", canonical, err)
}
got, err := catalog.SearchCompanies(tc.query, 1)
if err != nil || len(got) != 1 || got[0].CompanyID != tc.id || got[0].Name != tc.name {
t.Fatalf("query=%q got=%+v err=%v", tc.query, got, err)
}
})
}
}
func TestTicket25SymbolExactnessPreservesPunctuationSuffixAndInnerSpaces(t *testing.T) {
c := &Catalog{companies: []Company{{CompanyID: "isin:brk", Name: "Berkshire Hathaway", YahooSymbol: "BRK-B"}, {CompanyID: "isin:baidu", Name: "Baidu", YahooSymbol: "9888.HK"}, {CompanyID: "isin:accent", Name: "Other Company", YahooSymbol: "ECO"}}}
for _, query := range []string{"brkb", "BRK B", "BRK.B", "BRK- B", "BRK", "9888", "9888-HK", "9888 HK", "9888.HK.EXTRA", "ÉCO", "E.C.O."} {
got, err := c.SearchCompanies(query, 20)
if err != nil || len(got) != 0 {
t.Fatalf("non-exact symbol %q matched %+v err=%v", query, got, err)
}
}
for _, query := range []string{"BRK-B", " \tbrk-b\n"} {
got, err := c.SearchCompanies(query, 20)
if err != nil || len(got) != 1 || got[0].CompanyID != "isin:brk" {
t.Fatalf("trim/case exact %q got=%+v err=%v", query, got, err)
}
}
}
func TestTicket25SymbolCollisionsPrecedeAliasesAndNamesAndDeduplicateBeforeLimit(t *testing.T) {
companies := []Company{
{CompanyID: "isin:s-z", Name: "Zed Company", YahooSymbol: "DUP.X"},
{CompanyID: "isin:s-a", Name: "Alpha Company", YahooSymbol: "dup.x"},
{CompanyID: "isin:alias", Name: "Alias Company", YahooSymbol: "OTHER"},
{CompanyID: "isin:legal", Name: "DUP.X", YahooSymbol: "LEGAL"},
{CompanyID: "isin:prefix", Name: "DUP.X Services", YahooSymbol: "PREFIX"},
}
c := &Catalog{byID: map[string]Company{}}
for _, company := range companies {
c.companies = append(c.companies, company, company)
c.byID[company.CompanyID] = company
}
if err := c.loadAliases([]byte(`{"version":1,"aliases":[{"alias":"DUP.X","company_ids":["isin:alias","isin:s-a","isin:s-a"]}]}`)); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
limit int
want []string
}{
{1, []string{"isin:s-a"}}, {2, []string{"isin:s-a", "isin:s-z"}}, {3, []string{"isin:s-a", "isin:s-z", "isin:alias"}},
{20, []string{"isin:s-a", "isin:s-z", "isin:alias", "isin:legal", "isin:prefix"}},
} {
for run := 0; run < 3; run++ {
got, err := c.SearchCompanies(" DuP.X ", tc.limit)
if err != nil {
t.Fatal(err)
}
ids := []string{}
for _, company := range got {
ids = append(ids, company.CompanyID)
}
if !reflect.DeepEqual(ids, tc.want) {
t.Fatalf("limit=%d ids=%v want=%v", tc.limit, ids, tc.want)
}
}
}
}
func TestTicket25AllPrimarySymbolsRemainSearchableWithoutChangingCanonicalListing(t *testing.T) {
data := "symbol,name,exchange,mic,country,isin,figi,delisted,primary_listing,listing_score\n" +
"ONE.A,Legal Company,Venue A,XAAA,Country,US1234567890,BBG000AAAAAA,false,true,4\n" +
"TWO.B,Legal Company,Venue B,XBBB,Country,US1234567890,BBG000BBBBBB,false,true,3\n"
catalog, err := readCatalog(strings.NewReader(data))
if err != nil {
t.Fatal(err)
}
got, err := catalog.SearchCompanies("TWO.B", 20)
if err != nil || len(got) != 1 || got[0].CompanyID != "isin:US1234567890" || got[0].YahooSymbol != "ONE.A" {
t.Fatalf("symbol lookup changed canonical company: %+v %v", got, err)
}
}
func TestTicket25DoesNotInventMissingSymbolOrBroadenItToPrefix(t *testing.T) {
catalog, err := Default()
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct{ query, excluded string }{{"BABAF", "isin:KYG017191142"}, {"AMZ", "isin:US0231351067"}, {"9888", "isin:KYG070341048"}} {
got, err := catalog.SearchCompanies(tc.query, 20)
if err != nil {
t.Fatal(err)
}
for _, company := range got {
if company.CompanyID == tc.excluded {
t.Fatalf("invented symbol/prefix %q for %s", tc.query, tc.excluded)
}
}
}
}