T-87: M1.1l — Security-type classifier: eodSymbols.Type → securityTypeByIsin for the security-master loader (no new dependencies)

This commit is contained in:
Studio 2026-11-28 04:55:34 -05:00
parent c9a4adabad
commit 2f5ac2e7e7
6 changed files with 920 additions and 7 deletions

View file

@ -3,6 +3,7 @@ import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import type { MongoDBClient } from '@stock-bot/mongodb';
import { BarSpanIndex } from '../../apps/stock/data-pipeline/src/normalization/bar-spans';
import { cusipToIsin } from '../../apps/stock/data-pipeline/src/normalization/identifiers';
import { parseTickerDetailed } from '../../apps/stock/data-pipeline/src/normalization/ticker';
import {
CEO_SYMBOLS_PROJECTION,
@ -339,6 +340,8 @@ describe('security-master source-symbol-readers', () => {
expect('profile' in QM_SYMBOLS_PROJECTION).toBe(false);
expect('symbolInfo' in QM_SYMBOLS_PROJECTION).toBe(false);
expect(EOD_SYMBOLS_PROJECTION._id).toBe(0);
expect(EOD_SYMBOLS_PROJECTION.Type).toBe(1);
expect(EOD_SYMBOLS_PROJECTION.type).toBe(1);
expect(IB_SYMBOLS_PROJECTION._id).toBe(0);
expect(CEO_SYMBOLS_PROJECTION._id).toBe(0);
expect(EOD_SYMBOL_CHANGE_PROJECTION._id).toBe(0);
@ -1366,12 +1369,12 @@ describe('security-master source-symbol-readers', () => {
const baseCollections = {
eodSymbols: [
{ eodSearchCode: 'Z.US', Code: 'Z', Exchange: 'NASDAQ', Country: 'USA', Currency: 'USD', Isin: 'US98954M1018', Name: 'Zillow' },
{ eodSearchCode: 'AAPL.US', Code: 'AAPL', Exchange: 'NASDAQ', Country: 'USA', Currency: 'USD', Isin: 'US0378331005', Name: 'Apple' },
{ eodSearchCode: 'Z.US', Code: 'Z', Exchange: 'NASDAQ', Country: 'USA', Currency: 'USD', Isin: 'US98954M1018', Name: 'Zillow', Type: 'Common Stock' },
{ eodSearchCode: 'AAPL.US', Code: 'AAPL', Exchange: 'NASDAQ', Country: 'USA', Currency: 'USD', Isin: 'US0378331005', Name: 'Apple', Type: 'Common Stock' },
// Duplicate sourceId with different names to test full tie-breaker determinism
{ eodSearchCode: 'DUP.US', Code: 'DUP', Exchange: 'NYSE', Country: 'USA', Currency: 'USD', Name: 'Dup B' },
{ eodSearchCode: 'DUP.US', Code: 'DUP', Exchange: 'NYSE', Country: 'USA', Currency: 'USD', Name: 'Dup A' },
{ eodSearchCode: 'RY.TO', Code: 'RY', Exchange: 'TO', Country: 'Canada', Currency: 'CAD', Isin: 'CA7800871021', Name: 'RBC' },
{ eodSearchCode: 'DUP.US', Code: 'DUP', Exchange: 'NYSE', Country: 'USA', Currency: 'USD', Name: 'Dup B', Type: 'ETF' },
{ eodSearchCode: 'DUP.US', Code: 'DUP', Exchange: 'NYSE', Country: 'USA', Currency: 'USD', Name: 'Dup A', Type: 'ETF' },
{ eodSearchCode: 'RY.TO', Code: 'RY', Exchange: 'TO', Country: 'Canada', Currency: 'CAD', Isin: 'CA7800871021', Name: 'RBC', type: 'Common Stock' },
],
ibSymbols: [
{ conid: 900, symbol: 'Z', exchangeId: 'NASDAQ', country: 'US', currency: 'USD', isin: 'US98954M1018' },
@ -1399,6 +1402,12 @@ describe('security-master source-symbol-readers', () => {
exchangeLookup,
});
const baselineJson = JSON.stringify(baseline);
const baselineTypeEntries = [...baseline.securityTypeByIsin.entries()];
expect(baselineTypeEntries).toEqual([
['CA7800871021', 'common'],
['US0378331005', 'common'],
['US98954M1018', 'common'],
]);
// Verify (source, sourceId) compareAscii sort order.
for (let i = 1; i < baseline.stageARecords.length; i++) {
@ -1426,9 +1435,171 @@ describe('security-master source-symbol-readers', () => {
exchangeLookup,
});
expect(JSON.stringify(candidate)).toBe(baselineJson);
expect([...candidate.securityTypeByIsin.entries()]).toEqual(baselineTypeEntries);
}
});
test('security-type wiring & Stage A integration: maps typed EOD docs (including lowercase type and CUSIP-derived ISIN), keeps non-string Type docs, and reports conflicts/unknowns', async () => {
const exchangeLookup = createPass1ExchangeLookup();
const expectedDerivedCaIsin = cusipToIsin('813137106', 'CA');
expect(expectedDerivedCaIsin).not.toBeNull();
const reader = new FakeSourceSymbolReader(
{
eodSymbols: [
// Standard Type: 'Common Stock'
{
eodSearchCode: 'AAPL.US',
Code: 'AAPL',
Exchange: 'NASDAQ',
Country: 'USA',
Currency: 'USD',
Isin: 'US0378331005',
Name: 'Apple Inc.',
Type: 'Common Stock',
},
// Lowercase `type` fallback
{
eodSearchCode: 'SPY.US',
Code: 'SPY',
Exchange: 'NYSE',
Country: 'USA',
Currency: 'USD',
Isin: 'US78462F1030',
Name: 'SPDR S&P 500 ETF',
type: 'etf',
},
// CUSIP-derived ISIN: bad ISIN check digit with CA prefix + valid CUSIP 813137106
{
eodSearchCode: 'SCU-UN.TO',
Code: 'SCU-UN',
Exchange: 'TO',
Country: 'Canada',
Currency: 'CAD',
Isin: 'CA8131391062',
Cusip: '813137106',
Name: 'Second Cup Income Fund',
Type: 'Unit',
},
// Non-string Type must NOT skip the doc (emitted to Stage A/B, counted in missingType)
{
eodSearchCode: 'RY.TO',
Code: 'RY',
Exchange: 'TO',
Country: 'Canada',
Currency: 'CAD',
Isin: 'CA7800871021',
Name: 'Royal Bank of Canada',
Type: 42 as unknown as string,
},
// Unknown Type ('Mutual Fund') emitted to Stage A/B, omitted from securityTypeByIsin
{
eodSearchCode: 'META.US',
Code: 'META',
Exchange: 'NASDAQ',
Country: 'USA',
Currency: 'USD',
Isin: 'US30303M1027',
Name: 'Meta Platforms Inc.',
Type: 'Mutual Fund',
},
// Conflicting types across two EOD docs sharing the same ISIN (SHOP)
{
eodSearchCode: 'SHOP.TO',
Code: 'SHOP',
Exchange: 'TO',
Country: 'Canada',
Currency: 'CAD',
Isin: 'CA82509L1076',
Name: 'Shopify Inc.',
Type: 'Common Stock',
},
{
eodSearchCode: 'SHOP.US',
Code: 'SHOP',
Exchange: 'NYSE',
Country: 'USA',
Currency: 'USD',
Isin: 'CA82509L1076',
Name: 'Shopify Inc.',
Type: 'ETF',
},
],
ibSymbols: [],
qmSymbols: [],
ceoSymbols: [],
eodSymbolChange: [],
},
{ readOnly: true, applyFilterAndProjection: true }
);
const sources = await readSecurityMasterSources(reader, { exchangeLookup });
// All 7 EOD docs emitted (non-string Type did NOT trigger invalidFieldType skip).
expect(sources.stats.eodSymbols.docsRead).toBe(7);
expect(sources.stats.eodSymbols.emitted).toBe(7);
expect(sources.stats.eodSymbols.skips.invalidFieldType).toBe(0);
assertStatsConservation(sources.stats.eodSymbols);
// Check securityTypeByIsin contents and stats.
expect([...sources.securityTypeByIsin.entries()]).toEqual([
[expectedDerivedCaIsin!, 'unit'],
['US0378331005', 'common'],
['US78462F1030', 'etf'],
]);
expect(sources.unknownSecurityTypes).toEqual([{ rawType: 'Mutual Fund', count: 1 }]);
expect(sources.securityTypeConflicts).toEqual([
{
isin: 'CA82509L1076',
types: ['common', 'etf'],
sourceIds: ['SHOP.TO', 'SHOP.US'],
recordCount: 2,
},
]);
expect(sources.securityTypeStats).toEqual({
recordsIn: 7,
classified: 5,
missingIsin: 0,
missingType: 1,
unknownType: 1,
distinctClassifiedIsins: 4,
isinsMapped: 3,
isinsConflicting: 1,
conflictsTruncated: 0,
});
// Feed reader output directly into Stage A and verify every typed non-conflicting EOD instrument
// resolves to an ISIN present in securityTypeByIsin (zero missing_security_type skips in persistSecurityMaster).
const stageA = matchInstrumentsStageA(sources.stageARecords);
expect(stageA.conflicts).toEqual([]);
expect(stageA.unresolved).toEqual([]);
const stageAIsins = new Set(stageA.instruments.map(i => i.primaryIsin));
expect(stageAIsins).toEqual(
new Set([
'US0378331005',
'US78462F1030',
expectedDerivedCaIsin!,
'CA7800871021',
'US30303M1027',
'CA82509L1076',
])
);
const typedNonConflictingIsins = ['US0378331005', 'US78462F1030', expectedDerivedCaIsin!];
const missingForTyped = stageA.instruments.filter(
inst =>
typedNonConflictingIsins.includes(inst.primaryIsin) &&
!sources.securityTypeByIsin.has(inst.primaryIsin)
);
expect(missingForTyped).toEqual([]);
// Untyped, unknown-typed, and conflicting instruments are omitted from securityTypeByIsin.
expect(sources.securityTypeByIsin.has('CA7800871021')).toBe(false);
expect(sources.securityTypeByIsin.has('US30303M1027')).toBe(false);
expect(sources.securityTypeByIsin.has('CA82509L1076')).toBe(false);
});
test('local benchmark: >= 368k synthetic docs (300k eod + 50k qm + 18k ib) sustain >= 25,000 docs/s', async () => {
const EOD_COUNT = 300_000;
const QM_COUNT = 50_000;