From f518c8638016dd895205b9fa9a0979cd13b3bd76 Mon Sep 17 00:00:00 2001 From: Studio Date: Fri, 27 Nov 2026 20:34:03 -0500 Subject: [PATCH] T-68: tests --- ...isting-stage-b-symbol-changes-edge.test.ts | 485 ++++++++++++++++++ 1 file changed, 485 insertions(+) create mode 100644 test/unit/security-master-listing-stage-b-symbol-changes-edge.test.ts diff --git a/test/unit/security-master-listing-stage-b-symbol-changes-edge.test.ts b/test/unit/security-master-listing-stage-b-symbol-changes-edge.test.ts new file mode 100644 index 00000000..b2d4d01b --- /dev/null +++ b/test/unit/security-master-listing-stage-b-symbol-changes-edge.test.ts @@ -0,0 +1,485 @@ +import { describe, expect, test } from 'bun:test'; +import { + matchListingsStageB, + STAGE_B_SOURCES, + type StageAInstrumentCandidate, + type StageBEodListingInput, + type StageBInput, + type StageBMatchResult, + type StageBSymbolChangeInput, + type StageBTickerSourceInput, +} from '../../apps/stock/data-pipeline/src/security-master'; + +// QA edge cases for T-68 (T-58 frozen listing keys, T-59 symbolChanges aliases) not covered by +// security-master-listing-stage-b.test.ts. + +type Member = StageAInstrumentCandidate['members'][number]; + +function instrument(primaryIsin: string, members: Member[]): StageAInstrumentCandidate { + return { primaryIsin, primaryCusip: null, issuerCountry: 'US', canonicalName: null, members }; +} + +function eodMember(sourceId: string, currency = 'USD'): Member { + return { source: 'eod', sourceId, currency, method: 'tier1_isin' }; +} + +function eod( + sourceId: string, + ticker: string, + mic: string, + span: [string, string] | null, + currency = 'USD' +): StageBEodListingInput { + return { + sourceId, + ticker, + mic, + role: 'primary_venue', + currency, + span: span === null ? null : { firstBarDate: span[0], lastBarDate: span[1] }, + }; +} + +function qm( + sourceId: string, + ticker: string, + mic: string, + span: [string, string], + currency = 'USD', + source: 'qm' | 'ceo' = 'qm' +): StageBTickerSourceInput { + return { + source, + sourceId, + ticker, + mic, + role: 'primary_venue', + currency, + span: { firstBarDate: span[0], lastBarDate: span[1] }, + }; +} + +function assertReconciles(result: StageBMatchResult): void { + expect(result.stats.totalListings).toBe(result.listings.length); + expect(result.stats.totalProviderMappings).toBe(result.providerMappings.length); + for (const source of STAGE_B_SOURCES) { + const s = result.stats.bySource[source]; + expect(s.matched).toBe(result.providerMappings.filter(m => m.source === source).length); + expect(s.unresolved).toBe(result.unresolved.filter(u => u.source === source).length); + expect(s.conflict).toBe(result.conflicts.filter(c => c.source === source).length); + expect(s.matched + s.unresolved + s.conflict).toBe(s.total); + } +} + +type Outcome = string; + +/** Summarises each ticker-source record as `isin:validFrom..validTo`, `unresolved:` or `conflict::`. */ +function outcomes(result: StageBMatchResult): Record { + const out: Record = {}; + for (const m of result.providerMappings) { + if (m.source === 'qm' || m.source === 'ceo') { + out[m.sourceId] = `${m.listingKey.primaryIsin}:${m.validFrom}..${m.validTo}`; + } + } + for (const u of result.unresolved) { + if (u.source === 'qm' || u.source === 'ceo') { + out[u.sourceId] = `unresolved:${u.reason}`; + } + } + for (const c of result.conflicts) { + if (c.source === 'qm' || c.source === 'ceo') { + out[c.sourceId] = + `conflict:${c.conflictKind}:` + + c.detail.candidateListingKeys.map(k => k.primaryIsin).join(','); + } + } + return out; +} + +function deepFreeze(value: T): T { + if (value !== null && typeof value === 'object' && !Object.isFrozen(value)) { + Object.freeze(value); + for (const key of Object.keys(value)) { + deepFreeze((value as Record)[key]); + } + } + return value; +} + +const ISIN_A = 'US1111111111'; +const ISIN_B = 'US2222222222'; +const ISIN_C = 'US3333333333'; + +describe('Pass 3 Stage B symbolChanges — QA edge cases (T-68)', () => { + test('alias cut-off is the exact calendar day before effectiveDate across leap days, month and year ends', () => { + // Arrange: one long-lived listing per rename so each boundary is probed independently. + const cases: { isin: string; oldTicker: string; effectiveDate: string; lastOld: string }[] = [ + { + isin: 'US1000000001', + oldTicker: 'LEAPA', + effectiveDate: '2000-03-01', + lastOld: '2000-02-29', + }, + { + isin: 'US1000000002', + oldTicker: 'NOLEAP', + effectiveDate: '1900-03-01', + lastOld: '1900-02-28', + }, + { + isin: 'US1000000003', + oldTicker: 'YEAREND', + effectiveDate: '2023-01-01', + lastOld: '2022-12-31', + }, + { + isin: 'US1000000004', + oldTicker: 'APREND', + effectiveDate: '2021-05-01', + lastOld: '2021-04-30', + }, + { + isin: 'US1000000005', + oldTicker: 'LEAPB', + effectiveDate: '2024-03-01', + lastOld: '2024-02-29', + }, + ]; + const input: StageBInput = { + instruments: cases.map((c, i) => instrument(c.isin, [eodMember(`NEW${i}.US`)])), + eodListings: cases.map((_, i) => + eod(`NEW${i}.US`, `NEW${i}`, 'XNAS', ['1899-01-03', '2025-11-26']) + ), + ibContracts: [], + tickerSources: cases.flatMap((c, i) => [ + qm(`LAST_${i}`, c.oldTicker, 'XNAS', [c.lastOld, c.lastOld]), + qm(`EFF_${i}`, c.oldTicker, 'XNAS', [c.effectiveDate, c.effectiveDate]), + ]), + symbolChanges: cases.map((c, i) => ({ + eodSourceId: `NEW${i}.US`, + oldTicker: c.oldTicker, + effectiveDate: c.effectiveDate, + })), + }; + + // Act + const result = matchListingsStageB(input); + + // Assert + const got = outcomes(result); + cases.forEach((c, i) => { + expect(got[`LAST_${i}`]).toBe(`${c.isin}:${c.lastOld}..${c.lastOld}`); + expect(got[`EFF_${i}`]).toBe('unresolved:no_span_overlap'); + }); + assertReconciles(result); + }); + + test('accepts the earliest representable effectiveDate and rejects impossible calendar dates', () => { + const base = { instruments: [], eodListings: [], ibContracts: [], tickerSources: [] }; + + expect(() => + matchListingsStageB({ + ...base, + symbolChanges: [{ eodSourceId: 'X.US', oldTicker: 'X', effectiveDate: '0001-01-02' }], + }) + ).not.toThrow(); + + for (const effectiveDate of [ + '2022-02-30', + '2022-13-01', + '2022-00-10', + '2022-6-9', + '1900-02-29', + '', + ]) { + expect(() => + matchListingsStageB({ + ...base, + symbolChanges: [{ eodSourceId: 'X.US', oldTicker: 'X', effectiveDate }], + }) + ).toThrow(RangeError); + } + }); + + test('validates every symbolChanges entry even when its eodSourceId matches no listing', () => { + const input: StageBInput = { + instruments: [instrument(ISIN_A, [eodMember('A.US')])], + eodListings: [eod('A.US', 'A', 'XNAS', ['2010-01-04', '2025-11-26'])], + ibContracts: [], + tickerSources: [], + symbolChanges: [ + { eodSourceId: 'A.US', oldTicker: 'AOLD', effectiveDate: '2015-01-01' }, + { eodSourceId: 'GHOST.US', oldTicker: 'GH', effectiveDate: '2015-02-30' }, + ], + }; + + expect(() => matchListingsStageB(input)).toThrow(/symbolChanges\[1\]/); + }); + + test('rejects non-string field types with TypeError', () => { + const base = { instruments: [], eodListings: [], ibContracts: [], tickerSources: [] }; + const bad = [ + { eodSourceId: 'X.US', oldTicker: 7, effectiveDate: '2022-06-09' }, + { eodSourceId: 'X.US', oldTicker: 'X', effectiveDate: 20220609 }, + { eodSourceId: 'X.US', oldTicker: 'X', effectiveDate: '2022-06-09', validFrom: 20200101 }, + ]; + for (const entry of bad) { + expect(() => + matchListingsStageB({ + ...base, + symbolChanges: [entry as unknown as StageBSymbolChangeInput], + }) + ).toThrow(TypeError); + } + }); + + test('explicit validFrom: null behaves exactly like an omitted validFrom', () => { + const make = (change: StageBSymbolChangeInput): StageBMatchResult => + matchListingsStageB({ + instruments: [instrument(ISIN_A, [eodMember('A.US')])], + eodListings: [eod('A.US', 'ANEW', 'XNAS', ['2010-01-04', '2025-11-26'])], + ibContracts: [], + tickerSources: [ + qm('Q_EARLY', 'AOLD', 'XNAS', ['2010-01-04', '2010-12-31']), + qm('Q_LATE', 'AOLD', 'XNAS', ['2016-01-04', '2016-12-30']), + ], + symbolChanges: [change], + }); + + const omitted = make({ eodSourceId: 'A.US', oldTicker: 'AOLD', effectiveDate: '2015-01-01' }); + const explicitNull = make({ + eodSourceId: 'A.US', + oldTicker: 'AOLD', + effectiveDate: '2015-01-01', + validFrom: null, + }); + + expect(JSON.stringify(explicitNull)).toBe(JSON.stringify(omitted)); + expect(outcomes(omitted)).toEqual({ + Q_EARLY: `${ISIN_A}:2010-01-04..2010-12-31`, + Q_LATE: 'unresolved:no_span_overlap', + }); + }); + + test('aliases are scoped to the renamed listing MIC and currency', () => { + const input: StageBInput = { + instruments: [instrument(ISIN_A, [eodMember('META.US')])], + eodListings: [eod('META.US', 'META', 'XNAS', ['2012-05-18', '2025-11-26'])], + ibContracts: [], + tickerSources: [ + qm('Q_XNAS_USD', 'FB', 'XNAS', ['2015-01-02', '2015-12-31']), + qm('Q_XNYS_USD', 'FB', 'XNYS', ['2015-01-02', '2015-12-31']), + qm('Q_XNAS_CAD', 'FB', 'XNAS', ['2015-01-02', '2015-12-31'], 'CAD'), + ], + symbolChanges: [{ eodSourceId: 'META.US', oldTicker: 'FB', effectiveDate: '2022-06-09' }], + }; + + const got = outcomes(matchListingsStageB(input)); + + expect(got).toEqual({ + Q_XNAS_USD: `${ISIN_A}:2015-01-02..2015-12-31`, + Q_XNYS_USD: 'unresolved:no_matching_listing', + Q_XNAS_CAD: 'unresolved:no_matching_listing', + }); + }); + + test('class-suffixed old tickers resolve across EOD and QM punctuation, and ceo sources use aliases too', () => { + const input: StageBInput = { + instruments: [instrument(ISIN_A, [eodMember('NEWB.US')])], + eodListings: [eod('NEWB.US', 'NEW-B', 'XNYS', ['2005-01-03', '2025-11-26'])], + ibContracts: [], + tickerSources: [ + qm('Q_OLD_DOT_B', 'OLD.B', 'XNYS', ['2008-01-02', '2008-12-31']), + qm('C_OLD_SPACE_B', 'OLD B', 'XNYS', ['2009-01-02', '2009-12-31'], 'USD', 'ceo'), + qm('Q_OLD_CLASS_A', 'OLD.A', 'XNYS', ['2008-01-02', '2008-12-31']), + ], + symbolChanges: [{ eodSourceId: 'NEWB.US', oldTicker: 'OLD-B', effectiveDate: '2010-01-01' }], + }; + + const result = matchListingsStageB(input); + + expect(outcomes(result)).toEqual({ + Q_OLD_DOT_B: `${ISIN_A}:2008-01-02..2008-12-31`, + C_OLD_SPACE_B: `${ISIN_A}:2009-01-02..2009-12-31`, + Q_OLD_CLASS_A: 'unresolved:no_matching_listing', + }); + expect(result.providerMappings.find(m => m.sourceId === 'Q_OLD_DOT_B')?.listingKey).toEqual({ + primaryIsin: ISIN_A, + mic: 'XNYS', + tickerRoot: 'NEW', + classSuffix: 'B', + currency: 'USD', + }); + assertReconciles(result); + }); + + test('alias windows that fall entirely outside the listing bar span never match', () => { + const input: StageBInput = { + instruments: [ + instrument(ISIN_A, [eodMember('A.US')]), + instrument(ISIN_B, [eodMember('B.US')]), + ], + eodListings: [ + eod('A.US', 'ANEW', 'XNAS', ['2015-01-02', '2025-11-26']), + eod('B.US', 'BNEW', 'XNAS', ['2010-01-04', '2014-12-31']), + ], + ibContracts: [], + tickerSources: [ + // A's alias ends 2012-12-31, before A has any bars. + qm('Q_A_BEFORE_BARS', 'AOLD', 'XNAS', ['2011-01-03', '2012-12-31']), + // B's alias starts 2016-01-01, after B's last bar. + qm('Q_B_AFTER_BARS', 'BOLD', 'XNAS', ['2016-01-04', '2016-12-30']), + ], + symbolChanges: [ + { eodSourceId: 'A.US', oldTicker: 'AOLD', effectiveDate: '2013-01-01' }, + { + eodSourceId: 'B.US', + oldTicker: 'BOLD', + validFrom: '2016-01-01', + effectiveDate: '2018-01-01', + }, + ], + }; + + expect(outcomes(matchListingsStageB(input))).toEqual({ + Q_A_BEFORE_BARS: 'unresolved:no_span_overlap', + Q_B_AFTER_BARS: 'unresolved:no_span_overlap', + }); + }); + + test('two listings that historically used the same old ticker resolve by date, and overlapping claims quarantine', () => { + // XYZ was used by A until 2010-01-01, then by B from 2012-01-01 until 2018-01-01. + // C (bad data) also claims XYZ during 2016–2017 to provoke an ambiguous overlap. + const input: StageBInput = { + instruments: [ + instrument(ISIN_A, [eodMember('A.US')]), + instrument(ISIN_B, [eodMember('B.US')]), + instrument(ISIN_C, [eodMember('C.US')]), + ], + eodListings: [ + eod('A.US', 'ANEW', 'XNAS', ['2000-01-03', '2025-11-26']), + eod('B.US', 'BNEW', 'XNAS', ['2012-01-03', '2025-11-26']), + eod('C.US', 'CNEW', 'XNAS', ['2016-01-04', '2025-11-26']), + ], + ibContracts: [], + tickerSources: [ + qm('Q_2005', 'XYZ', 'XNAS', ['2005-01-03', '2005-12-30']), + qm('Q_2011_GAP', 'XYZ', 'XNAS', ['2011-01-03', '2011-12-30']), + qm('Q_2013', 'XYZ', 'XNAS', ['2013-01-02', '2013-12-31']), + qm('Q_2016', 'XYZ', 'XNAS', ['2016-06-01', '2016-06-30']), + qm('Q_2009_2013', 'XYZ', 'XNAS', ['2009-01-02', '2013-12-31']), + ], + symbolChanges: [ + { eodSourceId: 'A.US', oldTicker: 'XYZ', effectiveDate: '2010-01-01' }, + { + eodSourceId: 'B.US', + oldTicker: 'XYZ', + validFrom: '2012-01-01', + effectiveDate: '2018-01-01', + }, + { + eodSourceId: 'C.US', + oldTicker: 'XYZ', + validFrom: '2016-01-01', + effectiveDate: '2018-01-01', + }, + ], + }; + + const result = matchListingsStageB(input); + + expect(outcomes(result)).toEqual({ + Q_2005: `${ISIN_A}:2005-01-03..2005-12-30`, + Q_2011_GAP: 'unresolved:no_span_overlap', + Q_2013: `${ISIN_B}:2013-01-02..2013-12-31`, + Q_2016: `conflict:ambiguous_ticker_span:${ISIN_B},${ISIN_C}`, + Q_2009_2013: `conflict:ambiguous_ticker_span:${ISIN_A},${ISIN_B}`, + }); + assertReconciles(result); + }); + + test('a dated alias match is still blocked by an undated listing that holds the same ticker today', () => { + const input: StageBInput = { + instruments: [ + instrument(ISIN_A, [eodMember('META.US')]), + instrument(ISIN_B, [eodMember('FB.US')]), + ], + eodListings: [ + eod('META.US', 'META', 'XNAS', ['2012-05-18', '2025-11-26']), + eod('FB.US', 'FB', 'XNAS', null), + ], + ibContracts: [], + tickerSources: [qm('Q_FB_2015', 'FB', 'XNAS', ['2015-01-02', '2015-12-31'])], + symbolChanges: [{ eodSourceId: 'META.US', oldTicker: 'FB', effectiveDate: '2022-06-09' }], + }; + + expect(outcomes(matchListingsStageB(input))).toEqual({ + Q_FB_2015: 'unresolved:missing_span', + }); + }); + + test('alias resolution works through any EOD sourceId attached to the same listing', () => { + // Two EOD source rows collapse to one listing natural key; the rename references the second. + const input: StageBInput = { + instruments: [instrument(ISIN_A, [eodMember('META.US'), eodMember('META_ALT.US')])], + eodListings: [ + eod('META.US', 'META', 'XNAS', ['2012-05-18', '2025-11-26']), + eod('META_ALT.US', 'META', 'XNAS', ['2012-05-18', '2025-11-26']), + ], + ibContracts: [], + tickerSources: [qm('Q_FB_2015', 'FB', 'XNAS', ['2015-01-02', '2015-12-31'])], + symbolChanges: [{ eodSourceId: 'META_ALT.US', oldTicker: 'FB', effectiveDate: '2022-06-09' }], + }; + + const result = matchListingsStageB(input); + + expect(result.listings).toHaveLength(1); + expect(outcomes(result)).toEqual({ Q_FB_2015: `${ISIN_A}:2015-01-02..2015-12-31` }); + assertReconciles(result); + }); + + test('deep-frozen input including symbolChanges is accepted and left untouched', () => { + const input: StageBInput = { + instruments: [instrument(ISIN_A, [eodMember('META.US')])], + eodListings: [eod('META.US', 'META', 'XNAS', ['2012-05-18', '2025-11-26'])], + ibContracts: [], + tickerSources: [qm('Q_FB_2015', 'FB', 'XNAS', ['2015-01-02', '2015-12-31'])], + symbolChanges: [ + { eodSourceId: 'META.US', oldTicker: 'FB', effectiveDate: '2022-06-09', validFrom: null }, + ], + }; + const before = JSON.stringify(input); + deepFreeze(input); + + const result = matchListingsStageB(input); + + expect(JSON.stringify(input)).toBe(before); + expect(outcomes(result)).toEqual({ Q_FB_2015: `${ISIN_A}:2015-01-02..2015-12-31` }); + }); + + test('frozen listing keys (T-58) are separate from mutable listing rows and survive repeat runs', () => { + const input: StageBInput = { + instruments: [instrument(ISIN_A, [eodMember('META.US')])], + eodListings: [eod('META.US', 'META', 'XNAS', ['2012-05-18', '2025-11-26'])], + ibContracts: [], + tickerSources: [qm('Q_META', 'META', 'XNAS', ['2024-01-02', '2024-12-31'])], + }; + + const first = matchListingsStageB(input); + const second = matchListingsStageB(input); + + const eodKey = first.providerMappings.find(m => m.source === 'eod')?.listingKey; + const qmKey = first.providerMappings.find(m => m.source === 'qm')?.listingKey; + expect(eodKey).toBeDefined(); + expect(Object.isFrozen(eodKey)).toBe(true); + expect(Object.isFrozen(qmKey)).toBe(true); + // Writers may still annotate listing rows (e.g. DB ids) without touching keys. + const listing = first.listings[0] as unknown as Record; + expect(Object.isFrozen(listing)).toBe(false); + listing.dbId = 1; + expect((eodKey as unknown as Record).dbId).toBeUndefined(); + // No key object leaks between independent runs. + expect(second.providerMappings[0]?.listingKey).not.toBe(first.providerMappings[0]?.listingKey); + expect(second.listings[0]).not.toHaveProperty('dbId'); + }); +});