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>
147 lines
5.5 KiB
Go
147 lines
5.5 KiB
Go
package companycatalog
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestTicket24RealSnapshotFindsIBMAndIntelFirst(t *testing.T) {
|
|
catalog, err := Default()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct{ query, id, name, symbol string }{
|
|
{"IBM", "isin:US4592001014", "International Business Machines Corporation", "IBM"},
|
|
{"Intel", "isin:US4581401001", "Intel Corporation", "INTC"},
|
|
{" ÍNTÈL. ", "isin:US4581401001", "Intel Corporation", "INTC"},
|
|
{"I.B.M.", "isin:US4592001014", "International Business Machines Corporation", "IBM"},
|
|
} {
|
|
t.Run(tc.query, func(t *testing.T) {
|
|
canonical, err := catalog.ResolveCompany(tc.id)
|
|
if err != nil || canonical.Name != tc.name || canonical.YahooSymbol != tc.symbol {
|
|
t.Fatalf("local identity mismatch %+v %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 TestTicket24LegalWordPrefixKeepsUsefulSearchWithoutInteriorSubstring(t *testing.T) {
|
|
c := &Catalog{companies: []Company{{CompanyID: "isin:1", Name: "International Business Machines Corporation"}, {CompanyID: "isin:2", Name: "Intel Corporation"}, {CompanyID: "isin:3", Name: "Société Générale"}}}
|
|
for _, tc := range []struct{ q, id string }{
|
|
{"Business Mach", "isin:1"}, {"international-business machines", "isin:1"},
|
|
{"INTEL CORPORATION.", "isin:2"}, {"societe ge\u0301ne\u0301rale", "isin:3"},
|
|
} {
|
|
got, err := c.SearchCompanies(tc.q, 20)
|
|
if err != nil || len(got) != 1 || got[0].CompanyID != tc.id {
|
|
t.Fatalf("query=%q got=%+v err=%v", tc.q, got, err)
|
|
}
|
|
}
|
|
got, err := c.SearchCompanies("ternational", 20)
|
|
if err != nil || len(got) != 0 {
|
|
t.Fatalf("interior substring returned %+v %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestTicket24AliasesAreExactOnly(t *testing.T) {
|
|
catalog, err := Default()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, query := range []string{"IB", "BM", "some IBM company"} {
|
|
got, err := catalog.SearchCompanies(query, 20)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, company := range got {
|
|
if company.CompanyID == "isin:US4592001014" {
|
|
t.Fatalf("non-exact alias %q matched IBM", query)
|
|
}
|
|
}
|
|
}
|
|
// A legal prefix still works independently of aliases.
|
|
isolated := &Catalog{companies: []Company{{CompanyID: "isin:ibm", Name: "International Business Machines Corporation"}}}
|
|
got, err := isolated.SearchCompanies("Internation", 20)
|
|
if err != nil || len(got) != 1 {
|
|
t.Fatalf("legal prefix lost %+v %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestTicket24SameTierOrderingUsesNormalizedLegalNameThenIdentity(t *testing.T) {
|
|
c := &Catalog{companies: []Company{{CompanyID: "isin:z", Name: "Élite Corporation"}, {CompanyID: "isin:b", Name: "Elite Corporation"}, {CompanyID: "isin:a", Name: "Élite Corporation"}, {CompanyID: "isin:x", Name: "Alpha Elite Services"}}}
|
|
got, err := c.SearchCompanies("Elite", 20)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ids := []string{}
|
|
for _, company := range got {
|
|
ids = append(ids, company.CompanyID)
|
|
}
|
|
if !reflect.DeepEqual(ids, []string{"isin:x", "isin:a", "isin:b", "isin:z"}) {
|
|
t.Fatalf("unstable normalized order: %v", ids)
|
|
}
|
|
if strings.Contains(normalizeName("Société"), "é") {
|
|
t.Fatal("accents not normalized")
|
|
}
|
|
}
|
|
|
|
func TestTicket24AliasCollisionPriorityAndDeduplicationBeforeLimit(t *testing.T) {
|
|
companies := []Company{{CompanyID: "isin:z", Name: "Zeta Holdings"}, {CompanyID: "isin:a", Name: "Alias First Holdings"}, {CompanyID: "isin:legal", Name: "Alpha"}, {CompanyID: "isin:prefix", Name: "Alpha Ventures"}}
|
|
c := &Catalog{byID: map[string]Company{}}
|
|
for _, company := range companies {
|
|
c.byID[company.CompanyID] = company
|
|
c.companies = append(c.companies, company, company)
|
|
}
|
|
data := []byte(`{"version":1,"aliases":[{"alias":"Alpha","company_ids":["isin:z","isin:a","isin:a"]},{"alias":" ÁLPHA! ","company_ids":["isin:z"]}]}`)
|
|
if err := c.loadAliases(data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct {
|
|
limit int
|
|
want []string
|
|
}{{1, []string{"isin:a"}}, {2, []string{"isin:a", "isin:z"}}, {3, []string{"isin:a", "isin:z", "isin:legal"}}, {20, []string{"isin:a", "isin:z", "isin:legal", "isin:prefix"}}} {
|
|
for repeat := 0; repeat < 3; repeat++ {
|
|
got, err := c.SearchCompanies("alpha", 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)
|
|
}
|
|
}
|
|
}
|
|
// Prefix matching applies to legal names, never to the alias dictionary.
|
|
got, err := c.SearchCompanies("Alph", 20)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 2 || got[0].CompanyID != "isin:legal" || got[1].CompanyID != "isin:prefix" {
|
|
t.Fatalf("alias prefix leaked: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestTicket24RejectsInvalidAliasFilesWithoutPartialApplication(t *testing.T) {
|
|
c := &Catalog{byID: map[string]Company{"isin:1": {CompanyID: "isin:1", Name: "Legal Name"}}}
|
|
for _, data := range []string{
|
|
`{"version":2,"aliases":[]}`,
|
|
`{"version":1,"aliases":[{"alias":"Known","company_ids":["isin:1"]},{"alias":"Unknown","company_ids":["isin:missing"]}]}`,
|
|
`{"version":1,"aliases":[{"alias":"...","company_ids":["isin:1"]}]}`,
|
|
`{"version":1,"aliases":[{"alias":"Empty","company_ids":[]}]}`,
|
|
} {
|
|
if err := c.loadAliases([]byte(data)); err == nil {
|
|
t.Fatalf("accepted invalid alias file")
|
|
}
|
|
}
|
|
got, err := c.SearchCompanies("Known", 20)
|
|
if err != nil || len(got) != 0 {
|
|
t.Fatal("partially applied invalid alias file")
|
|
}
|
|
}
|