diff --git a/test/unit/security-master-security-type.test.ts b/test/unit/security-master-security-type.test.ts index 1891ad8b..710531f9 100644 --- a/test/unit/security-master-security-type.test.ts +++ b/test/unit/security-master-security-type.test.ts @@ -420,3 +420,88 @@ describe('security-master security-type classifier', () => { expect(recordsPerSec).toBeGreaterThanOrEqual(100_000); }); }); + +describe('security-master security-type classifier QA edges', () => { + test('a conflict that later returns to the first type stays conflicting (A, B, A) in every order', () => { + const records: SecurityTypeInputRecord[] = [ + { isin: 'US0378331005', rawType: 'Common Stock', sourceId: 'A1' }, + { isin: 'US0378331005', rawType: 'ETF', sourceId: 'A2' }, + { isin: 'US0378331005', rawType: 'Common Stock', sourceId: 'A3' }, + ]; + for (const seed of [1, 2, 3, 4, 5, 6]) { + const result = buildSecurityTypeByIsin(shuffleWithSeed(records, seed)); + expect(result.securityTypeByIsin.size).toBe(0); + expect(result.conflicts).toEqual([ + { isin: 'US0378331005', types: ['common', 'etf'], sourceIds: ['A1', 'A2', 'A3'], recordCount: 3 }, + ]); + } + }); + + test('ISIN keys are trimmed so padded and unpadded ISINs collapse into one entry', () => { + const result = buildSecurityTypeByIsin([ + { isin: ' US0378331005 ', rawType: 'Common Stock', sourceId: 'A1' }, + { isin: 'US0378331005', rawType: 'ETF', sourceId: 'A2' }, + { isin: 'CA82509L1076\t', rawType: 'Unit', sourceId: 'B1' }, + ]); + expect([...result.securityTypeByIsin.entries()]).toEqual([['CA82509L1076', 'unit']]); + expect(result.conflicts.map(c => c.isin)).toEqual(['US0378331005']); + expect(result.conflicts[0]?.recordCount).toBe(2); + }); + + test('conflict detail counts only classified records (unknown/missing types on the ISIN are excluded)', () => { + const result = buildSecurityTypeByIsin([ + { isin: 'US0378331005', rawType: 'Common Stock', sourceId: 'A1' }, + { isin: 'US0378331005', rawType: 'Warrant', sourceId: 'A2' }, + { isin: 'US0378331005', rawType: 'FUND', sourceId: 'A3' }, + { isin: 'US0378331005', rawType: null, sourceId: 'A4' }, + ]); + expect(result.conflicts).toEqual([ + { isin: 'US0378331005', types: ['common', 'warrant'], sourceIds: ['A1', 'A2'], recordCount: 2 }, + ]); + expect(result.stats.classified).toBe(2); + }); + + test('empty input yields empty outputs with zeroed stats; Infinity maxConflicts rejects', () => { + const result = buildSecurityTypeByIsin([]); + expect(result.securityTypeByIsin.size).toBe(0); + expect(result.unknownRawTypes).toEqual([]); + expect(result.conflicts).toEqual([]); + expect(Object.values(result.stats).every(v => v === 0)).toBe(true); + expect(() => buildSecurityTypeByIsin([], { maxConflicts: Number.POSITIVE_INFINITY })).toThrow( + RangeError + ); + }); + + test('unknown raw types group by whitespace-collapsed exact case (case variants are reported separately)', () => { + const result = buildSecurityTypeByIsin([ + { isin: 'US0000000001', rawType: 'Mutual Fund', sourceId: 'A' }, + { isin: 'US0000000002', rawType: ' Mutual Fund', sourceId: 'B' }, + { isin: 'US0000000003', rawType: 'mutual fund', sourceId: 'C' }, + ]); + expect(result.unknownRawTypes).toEqual([ + { rawType: 'Mutual Fund', count: 2 }, + { rawType: 'mutual fund', count: 1 }, + ]); + }); + + test('ForStageA: EOD members of an instrument with a different raw ISIN are keyed by primaryIsin, and IB members never supply a type', () => { + const instruments: StageAInstrumentCandidate[] = [ + { + primaryIsin: 'US0378331005', + primaryCusip: '037833100', + issuerCountry: 'US', + canonicalName: 'Apple Inc.', + members: [ + { source: 'eod', sourceId: 'AAPL.US', currency: 'USD', method: 'tier2_cusip' }, + { source: 'ib', sourceId: 'AAPL.US', currency: 'USD', method: 'tier1_isin' }, + ], + }, + ]; + const result = buildSecurityTypeByIsinForStageA( + [{ isin: 'XX-raw-ignored', rawType: 'Preferred Stock', sourceId: 'AAPL.US' }], + instruments + ); + expect([...result.securityTypeByIsin.entries()]).toEqual([['US0378331005', 'preferred']]); + expect(buildSecurityTypeByIsinForStageA([], instruments).securityTypeByIsin.size).toBe(0); + }); +}); diff --git a/test/unit/security-master-source-symbol-readers.test.ts b/test/unit/security-master-source-symbol-readers.test.ts index cf329fef..a578e728 100644 --- a/test/unit/security-master-source-symbol-readers.test.ts +++ b/test/unit/security-master-source-symbol-readers.test.ts @@ -1893,3 +1893,114 @@ describe('security-master source-symbol-readers', () => { expect(docsPerSec).toBeGreaterThanOrEqual(25_000); }); }); + +describe('security-master source-symbol-readers security-type QA edges', () => { + test('Type/type precedence: blank or non-string `Type` falls back to string `type`; string `Type` wins over `type`', async () => { + const exchangeLookup = createPass1ExchangeLookup(); + const base = { Exchange: 'NASDAQ', Country: 'USA', Currency: 'USD' }; + const reader = new FakeSourceSymbolReader( + { + eodSymbols: [ + { ...base, eodSearchCode: 'AAPL.US', Code: 'AAPL', Isin: 'US0378331005', Name: 'Apple', Type: ' ', type: 'Common Stock' }, + { ...base, eodSearchCode: 'SPY.US', Code: 'SPY', Isin: 'US78462F1030', Name: 'SPY', Type: 7, type: 'ETF' }, + { ...base, eodSearchCode: 'Z.US', Code: 'Z', Isin: 'US98954M1018', Name: 'Zillow', Type: 'Common Stock', type: 'ETF' }, + ], + ibSymbols: [], + qmSymbols: [], + ceoSymbols: [], + eodSymbolChange: [], + }, + { readOnly: true, applyFilterAndProjection: true } + ); + + const sources = await readSecurityMasterSources(reader, { exchangeLookup }); + + assertStatsConservation(sources.stats.eodSymbols); + expect(sources.stats.eodSymbols.emitted).toBe(3); + expect([...sources.securityTypeByIsin.entries()]).toEqual([ + ['US0378331005', 'common'], + ['US78462F1030', 'etf'], + ['US98954M1018', 'common'], + ]); + expect(sources.securityTypeStats.missingType).toBe(0); + expect(sources.securityTypeConflicts).toEqual([]); + }); + + test('mixed EOD + IB sources: full-source Stage A EOD members all have a loader type, IB-only instruments are not typed', async () => { + const exchangeLookup = createPass1ExchangeLookup(); + const reader = new FakeSourceSymbolReader( + { + eodSymbols: [ + { eodSearchCode: 'AAPL.US', Code: 'AAPL', Exchange: 'NASDAQ', Country: 'USA', Currency: 'USD', Isin: 'US0378331005', Name: 'Apple', Type: 'Common Stock' }, + { eodSearchCode: 'SHOP.TO', Code: 'SHOP', Exchange: 'TO', Country: 'Canada', Currency: 'CAD', Isin: 'CA82509L1076', Name: 'Shopify', Type: 'Common Stock' }, + { eodSearchCode: 'SHOP.US', Code: 'SHOP', Exchange: 'NYSE', Country: 'USA', Currency: 'USD', Isin: 'CA82509L1076', Name: 'Shopify', Type: 'common stock' }, + ], + ibSymbols: [ + { conid: 265598, symbol: 'AAPL', exchangeId: 'NASDAQ', country: 'US', currency: 'USD', isin: 'US0378331005' }, + { conid: 900, symbol: 'Z', exchangeId: 'NASDAQ', country: 'US', currency: 'USD', isin: 'US98954M1018' }, + ], + qmSymbols: [], + ceoSymbols: [], + eodSymbolChange: [], + }, + { readOnly: true, applyFilterAndProjection: true } + ); + + const sources = await readSecurityMasterSources(reader, { exchangeLookup }); + assertStatsConservation(sources.stats.eodSymbols); + assertStatsConservation(sources.stats.ibSymbols); + + // Same Stage A that the orchestrator will run over every source. + const stageA = matchInstrumentsStageA(sources.stageARecords); + const withEodMember = stageA.instruments.filter(inst => inst.members.some(m => m.source === 'eod')); + expect(withEodMember.map(i => i.primaryIsin)).toEqual(['CA82509L1076', 'US0378331005']); + for (const inst of withEodMember) { + expect(sources.securityTypeByIsin.get(inst.primaryIsin)).toBe('common'); + } + // Zillow is IB-only: IB type inference is out of scope, so it stays untyped (no guessing). + expect(stageA.instruments.some(i => i.primaryIsin === 'US98954M1018')).toBe(true); + expect(sources.securityTypeByIsin.has('US98954M1018')).toBe(false); + expect(sources.securityTypeLoaderStats.isinsMapped).toBe(2); + expect(sources.securityTypeLoaderStats.recordsIn).toBe(3); + }); + + test('maxSecurityTypeConflicts option caps loader conflicts and invalid values reject', async () => { + const exchangeLookup = createPass1ExchangeLookup(); + const docs = [ + { eodSearchCode: 'SHOP.TO', Code: 'SHOP', Exchange: 'TO', Country: 'Canada', Currency: 'CAD', Isin: 'CA82509L1076', Name: 'Shopify', Type: 'Common Stock' }, + { eodSearchCode: 'SHOP.US', Code: 'SHOP', Exchange: 'NYSE', Country: 'USA', Currency: 'USD', Isin: 'CA82509L1076', Name: 'Shopify', Type: 'ETF' }, + { eodSearchCode: 'AAPL.US', Code: 'AAPL', Exchange: 'NASDAQ', Country: 'USA', Currency: 'USD', Isin: 'US0378331005', Name: 'Apple', Type: 'Common Stock' }, + { eodSearchCode: 'AAPL.NEO', Code: 'AAPL', Exchange: 'NYSE', Country: 'USA', Currency: 'USD', Isin: 'US0378331005', Name: 'Apple', Type: 'CDR' }, + ]; + const make = () => + new FakeSourceSymbolReader( + { eodSymbols: docs, ibSymbols: [], qmSymbols: [], ceoSymbols: [], eodSymbolChange: [] }, + { readOnly: true, applyFilterAndProjection: true } + ); + + const capped = await readSecurityMasterSources(make(), { exchangeLookup, maxSecurityTypeConflicts: 1 }); + expect(capped.securityTypeByIsin.size).toBe(0); + expect(capped.securityTypeConflicts.map(c => c.isin)).toEqual(['CA82509L1076']); + expect(capped.securityTypeLoaderStats.isinsConflicting).toBe(2); + expect(capped.securityTypeLoaderStats.conflictsTruncated).toBe(1); + + await expect( + readSecurityMasterSources(make(), { exchangeLookup, maxSecurityTypeConflicts: -1 }) + ).rejects.toThrow(RangeError); + }); + + test('readEodSymbols on an empty collection returns an empty map and zeroed stats', async () => { + const reader = new FakeSourceSymbolReader( + { eodSymbols: [] }, + { readOnly: true, applyFilterAndProjection: true } + ); + const result = await readEodSymbols(reader, { exchangeLookup: createPass1ExchangeLookup() }); + expect(result.securityTypeByIsin.size).toBe(0); + expect(result.securityTypeRecords).toEqual([]); + expect(result.unknownSecurityTypes).toEqual([]); + expect(result.securityTypeConflicts).toEqual([]); + expect(result.securityTypeStats.recordsIn).toBe(0); + expect(result.securityTypeLoaderStats.recordsIn).toBe(0); + assertStatsConservation(result.stats); + }); +});