feat(watchlist): add company identity catalog

Embed a pinned FinanceDatabase equity snapshot, add authenticated company search and identity-based follows, preserve legacy assets, and map selected Yahoo symbols for collection.

Refs #19

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 15:08:22 +01:00
parent 1123360914
commit d65222169a
50 changed files with 49897 additions and 193 deletions

View File

@ -0,0 +1,191 @@
package companycatalog
import (
"crypto/sha256"
"encoding/csv"
"encoding/hex"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
const testdataDir = "testdata"
type financeDatabaseManifest struct {
UpstreamRepository string `json:"upstream_repository"`
UpstreamCommit string `json:"upstream_commit"`
CommitDate string `json:"upstream_commit_date_utc"`
PackageVersion string `json:"package_version"`
License string `json:"license"`
UpstreamDataFile string `json:"upstream_equities_data_file"`
UpstreamDataSHA256 string `json:"upstream_equities_data_sha256"`
SnapshotDate string `json:"snapshot_date"`
Selection string `json:"selection"`
Evidence string `json:"primary_listing_evidence"`
Fixtures map[string]struct {
SHA256 string `json:"sha256"`
Records int `json:"records"`
} `json:"fixtures"`
}
type financeDatabaseRow struct {
Symbol string
Name string
Exchange string
MIC string
Country string
ISIN string
FIGI string
CompositeFIGI string
Delisted string
ListingScore string
PrimaryListing string
MainVenue string
Listings string
UsualCurrency string
HomeKnown string
}
func readFinanceDatabaseRows(t *testing.T, filename string) []financeDatabaseRow {
t.Helper()
file, err := os.Open(filepath.Join(testdataDir, filename))
if err != nil {
t.Fatal(err)
}
defer file.Close()
reader := csv.NewReader(file)
reader.FieldsPerRecord = -1
rows, err := reader.ReadAll()
if err != nil {
t.Fatal(err)
}
if len(rows) < 2 {
t.Fatalf("%s has only %d records", filename, len(rows)-1)
}
columns := make(map[string]int, len(rows[0]))
for i, name := range rows[0] {
columns[name] = i
}
for _, column := range []string{"symbol", "name", "exchange", "mic", "country", "isin", "figi", "composite_figi", "delisted", "listing_score", "primary_listing", "main_venue", "listings", "usual_currency", "home_known"} {
if _, ok := columns[column]; !ok {
t.Fatalf("%s lacks %q column", filename, column)
}
}
out := make([]financeDatabaseRow, 0, len(rows)-1)
for _, values := range rows[1:] {
if len(values) != len(rows[0]) {
t.Fatalf("malformed row in %s: %v", filename, values)
}
value := func(column string) string { return values[columns[column]] }
out = append(out, financeDatabaseRow{
Symbol: value("symbol"), Name: value("name"), Exchange: value("exchange"), MIC: value("mic"),
Country: value("country"), ISIN: value("isin"), FIGI: value("figi"), CompositeFIGI: value("composite_figi"),
Delisted: value("delisted"), ListingScore: value("listing_score"), PrimaryListing: value("primary_listing"),
MainVenue: value("main_venue"), Listings: value("listings"), UsualCurrency: value("usual_currency"), HomeKnown: value("home_known"),
})
}
return out
}
func TestFinanceDatabaseSnapshotHasPinnedRuntimeProvenanceAndChecksums(t *testing.T) {
manifestBytes, err := os.ReadFile(filepath.Join(testdataDir, "finance_database_manifest_2026-11-22.json"))
if err != nil {
t.Fatal(err)
}
var manifest financeDatabaseManifest
if err := json.Unmarshal(manifestBytes, &manifest); err != nil {
t.Fatal(err)
}
if manifest.UpstreamRepository != "https://github.com/JerBouma/FinanceDatabase" || manifest.UpstreamCommit != "93b872f0e4b4e05e8d35b7f0a39efe9245bcc2c1" || manifest.CommitDate != "2026-11-17T13:27:37Z" {
t.Fatalf("unexpected upstream provenance: %+v", manifest)
}
if manifest.PackageVersion != "2.5.2" || manifest.License != "MIT" || manifest.SnapshotDate != "2026-11-22" {
t.Fatalf("unexpected version/license/date: %+v", manifest)
}
if manifest.UpstreamDataFile != "compression/equities.parquet" || len(manifest.UpstreamDataSHA256) != 64 {
t.Fatalf("manifest lacks the pinned upstream dataset digest: %+v", manifest)
}
if !strings.Contains(manifest.Selection, "Equities(use_local_location=True).select(only_primary_listing=True, exclude_delisted=True)") || manifest.Evidence == "" {
t.Fatal("manifest must state the actual primary-listing selection and its evidence")
}
for name, want := range manifest.Fixtures {
content, err := os.ReadFile(filepath.Join(testdataDir, name))
if err != nil {
t.Fatal(err)
}
sum := sha256.Sum256(content)
if got := hex.EncodeToString(sum[:]); got != want.SHA256 {
t.Errorf("%s sha256=%s, manifest says %s", name, got, want.SHA256)
}
rows := readFinanceDatabaseRows(t, name)
if len(rows) != want.Records {
t.Errorf("%s records=%d, manifest says %d", name, len(rows), want.Records)
}
}
}
func TestFinanceDatabaseRawMultiListingsAndPrimaryRuntimeSnapshot(t *testing.T) {
raw := readFinanceDatabaseRows(t, "finance_database_raw_multilistings_2026-11-22.csv")
primary := readFinanceDatabaseRows(t, "finance_database_equities_primary_2026-11-22.csv")
rawBySymbol := make(map[string]financeDatabaseRow, len(raw))
for _, row := range raw {
if _, exists := rawBySymbol[row.Symbol]; exists {
t.Fatalf("raw fixture repeats symbol %s", row.Symbol)
}
rawBySymbol[row.Symbol] = row
}
// Keep both the requested Baidu ADR (BIDU) and the two Hong Kong listings in
// the raw data. The upstream selector chooses 9888.HK as primary for Baidu.
for _, symbol := range []string{"NVDA", "AAPL", "BIDU", "9888.HK", "89888.HK", "1211.HK", "002594.SZ", "0285.HK", "BYDIF", "BYDIY"} {
if _, ok := rawBySymbol[symbol]; !ok {
t.Errorf("raw multi-listing fixture lacks %s", symbol)
}
}
if !strings.EqualFold(rawBySymbol["BIDU"].PrimaryListing, "false") || !strings.EqualFold(rawBySymbol["9888.HK"].PrimaryListing, "true") {
t.Errorf("FinanceDatabase primary-listing result for Baidu: BIDU=%s 9888.HK=%s", rawBySymbol["BIDU"].PrimaryListing, rawBySymbol["9888.HK"].PrimaryListing)
}
if !strings.EqualFold(rawBySymbol["1211.HK"].PrimaryListing, "false") || !strings.EqualFold(rawBySymbol["002594.SZ"].PrimaryListing, "true") {
t.Errorf("FinanceDatabase primary-listing result for BYD Co: 1211.HK=%s 002594.SZ=%s", rawBySymbol["1211.HK"].PrimaryListing, rawBySymbol["002594.SZ"].PrimaryListing)
}
if !strings.EqualFold(rawBySymbol["0285.HK"].PrimaryListing, "true") || !strings.EqualFold(rawBySymbol["BYDIF"].PrimaryListing, "false") {
t.Errorf("FinanceDatabase primary-listing result for BYD Electronic: 0285.HK=%s BYDIF=%s", rawBySymbol["0285.HK"].PrimaryListing, rawBySymbol["BYDIF"].PrimaryListing)
}
want := map[string]string{
"NVDA": "NVIDIA Corporation",
"AAPL": "Apple Inc.",
"9888.HK": "Baidu, Inc.",
"002594.SZ": "BYD Company Limited",
"0285.HK": "BYD Electronic (International) Company Limited",
}
got := make(map[string]financeDatabaseRow, len(primary))
nameCounts := make(map[string]int)
for _, row := range primary {
if !strings.EqualFold(row.Delisted, "false") || !strings.EqualFold(row.PrimaryListing, "true") {
t.Errorf("runtime snapshot contains non-primary or delisted row: %+v", row)
}
got[row.Symbol] = row
nameCounts[row.Name]++
}
if len(got) != len(want) {
t.Fatalf("runtime selector snapshot has %d target rows; want %d: %+v", len(got), len(want), got)
}
for symbol, name := range want {
row, ok := got[symbol]
if !ok || row.Name != name {
t.Errorf("runtime primary row %s=%+v; want company %q", symbol, row, name)
}
if nameCounts[name] != 1 {
t.Errorf("runtime snapshot has %d rows for %q; want one", nameCounts[name], name)
}
}
if got["002594.SZ"].Name == got["0285.HK"].Name || got["002594.SZ"].ISIN == got["0285.HK"].ISIN {
t.Fatal("FinanceDatabase runtime snapshot collapsed BYD Co and BYD Electronic")
}
if got["002594.SZ"].ListingScore == "" || got["0285.HK"].ListingScore == "" {
t.Fatal("runtime snapshot lacks selector ranking evidence")
}
}