T-68: Bug sweep (5 bugs: T-58, T-59, T-61, T-62, T-63)

This commit is contained in:
Studio 2026-11-27 20:14:25 -05:00
parent e440d8d5a0
commit 1692aba2fc
5 changed files with 800 additions and 30 deletions

View file

@ -11,6 +11,7 @@ import {
type StageBIbContractInput,
type StageBInput,
type StageBMatchResult,
type StageBSymbolChangeInput,
type StageBTickerSourceInput,
} from '../../apps/stock/data-pipeline/src/security-master';
import { MAX_CLASS_SUFFIX_LENGTH } from '../../apps/stock/data-pipeline/src/normalization/ticker';
@ -1359,6 +1360,580 @@ describe('security master Pass 3 Stage B listing matcher', () => {
}
});
test('freezes listingKey objects across providerMappings and conflicts so strict-mode mutation throws without side effects (T-58)', () => {
const result = matchListingsStageB({
instruments: [
{
primaryIsin: 'US30303M1027',
primaryCusip: '30303M102',
issuerCountry: 'US',
canonicalName: 'Meta Platforms Inc.',
members: [
{ source: 'eod', sourceId: 'META.US', currency: 'USD', method: 'tier1_isin' },
{ source: 'ib', sourceId: 'ib-meta-1', currency: 'USD', method: 'tier1_isin' },
{ source: 'ib', sourceId: 'ib-meta-2', currency: 'USD', method: 'tier1_isin' },
],
},
{
primaryIsin: 'US0000000026',
primaryCusip: null,
issuerCountry: 'US',
canonicalName: 'FB Reused Listing',
members: [{ source: 'eod', sourceId: 'FB_NEW.US', currency: 'USD', method: 'tier1_isin' }],
},
],
eodListings: [
{
sourceId: 'META.US',
ticker: 'META',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2012-05-18', lastBarDate: '2025-11-26' },
},
{
sourceId: 'FB_NEW.US',
ticker: 'FB',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2025-01-02', lastBarDate: '2025-11-26' },
},
],
ibContracts: [
{ sourceId: 'ib-meta-1', conid: 107113386, currency: 'USD', primaryExchange: 'NASDAQ' },
{ sourceId: 'ib-meta-2', conid: 999113386, currency: 'USD', primaryExchange: 'NASDAQ' },
],
tickerSources: [
{
source: 'qm',
sourceId: 'QM_META_2024',
ticker: 'META',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2024-01-02', lastBarDate: '2024-12-31' },
},
{
source: 'qm',
sourceId: 'QM_FB_BOTH',
ticker: 'FB',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2020-01-02', lastBarDate: '2025-06-01' },
},
],
symbolChanges: [{ eodSourceId: 'META.US', oldTicker: 'FB', effectiveDate: '2022-06-09' }],
});
expect(result.providerMappings.length).toBeGreaterThan(0);
expect(result.conflicts.length).toBeGreaterThan(0);
for (const mapping of result.providerMappings) {
expect(Object.isFrozen(mapping.listingKey)).toBe(true);
}
for (const conflict of result.conflicts) {
for (const candidateKey of conflict.detail.candidateListingKeys) {
expect(Object.isFrozen(candidateKey)).toBe(true);
}
}
const snapshotBefore = JSON.stringify(result);
const firstMappingKey = result.providerMappings[0]?.listingKey as unknown as Record<
string,
unknown
>;
expect(() => {
firstMappingKey.tickerRoot = 'MUTATED';
}).toThrow(TypeError);
expect(() => {
firstMappingKey.dbId = 42;
}).toThrow(TypeError);
const firstConflictKey = result.conflicts[0]?.detail.candidateListingKeys[0] as unknown as
| Record<string, unknown>
| undefined;
expect(firstConflictKey).toBeDefined();
expect(() => {
if (firstConflictKey !== undefined) {
firstConflictKey.mic = 'XXXX';
}
}).toThrow(TypeError);
expect(JSON.stringify(result)).toBe(snapshotBefore);
assertReconciliationAndContracts(result);
});
test('resolves historical QM FB rows to META.US via dated EOD symbolChanges without mis-linking 2025 FB.US (T-59)', () => {
const instruments: StageAInstrumentCandidate[] = [
{
primaryIsin: 'US30303M1027',
primaryCusip: '30303M102',
issuerCountry: 'US',
canonicalName: 'Meta Platforms Inc.',
members: [{ source: 'eod', sourceId: 'META.US', currency: 'USD', method: 'tier1_isin' }],
},
{
primaryIsin: 'US0000000026',
primaryCusip: null,
issuerCountry: 'US',
canonicalName: 'FB Reused Ticker 2025',
members: [{ source: 'eod', sourceId: 'FB.US', currency: 'USD', method: 'tier1_isin' }],
},
];
const eodListings: StageBEodListingInput[] = [
{
sourceId: 'META.US',
ticker: 'META',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2012-05-18', lastBarDate: '2025-11-26' },
},
{
sourceId: 'FB.US',
ticker: 'FB',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2025-01-02', lastBarDate: '2025-11-26' },
},
];
const tickerSources: StageBTickerSourceInput[] = [
{
source: 'qm',
sourceId: 'QM_FB_2015',
ticker: 'FB',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2015-01-02', lastBarDate: '2015-12-31' },
},
{
source: 'qm',
sourceId: 'QM_FB_2022_06_08',
ticker: 'FB',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2022-06-08', lastBarDate: '2022-06-08' },
},
{
source: 'qm',
sourceId: 'QM_FB_2022_06_09',
ticker: 'FB',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2022-06-09', lastBarDate: '2022-06-09' },
},
{
source: 'qm',
sourceId: 'QM_FB_2023',
ticker: 'FB',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2023-01-03', lastBarDate: '2023-12-29' },
},
{
source: 'qm',
sourceId: 'QM_FB_2025',
ticker: 'FB',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2025-02-03', lastBarDate: '2025-10-31' },
},
{
source: 'qm',
sourceId: 'QM_META_2024',
ticker: 'META',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2024-01-02', lastBarDate: '2024-12-31' },
},
{
source: 'qm',
sourceId: 'QM_FB_CROSS_SPAN',
ticker: 'FB',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2020-01-02', lastBarDate: '2025-06-01' },
},
];
const symbolChanges: StageBSymbolChangeInput[] = [
{ eodSourceId: 'META.US', oldTicker: 'FB', effectiveDate: '2022-06-09' },
];
const withHistory = matchListingsStageB({
instruments,
eodListings,
ibContracts: [],
tickerSources,
symbolChanges,
});
const qmMappings = withHistory.providerMappings.filter(m => m.source === 'qm');
expect(qmMappings).toEqual([
{
source: 'qm',
sourceId: 'QM_FB_2015',
listingKey: {
primaryIsin: 'US30303M1027',
mic: 'XNAS',
tickerRoot: 'META',
classSuffix: '',
currency: 'USD',
},
method: 'tier3_ticker_span',
validFrom: '2015-01-02',
validTo: '2015-12-31',
},
{
source: 'qm',
sourceId: 'QM_FB_2022_06_08',
listingKey: {
primaryIsin: 'US30303M1027',
mic: 'XNAS',
tickerRoot: 'META',
classSuffix: '',
currency: 'USD',
},
method: 'tier3_ticker_span',
validFrom: '2022-06-08',
validTo: '2022-06-08',
},
{
source: 'qm',
sourceId: 'QM_FB_2025',
listingKey: {
primaryIsin: 'US0000000026',
mic: 'XNAS',
tickerRoot: 'FB',
classSuffix: '',
currency: 'USD',
},
method: 'tier3_ticker_span',
validFrom: '2025-02-03',
validTo: '2025-10-31',
},
{
source: 'qm',
sourceId: 'QM_META_2024',
listingKey: {
primaryIsin: 'US30303M1027',
mic: 'XNAS',
tickerRoot: 'META',
classSuffix: '',
currency: 'USD',
},
method: 'tier3_ticker_span',
validFrom: '2024-01-02',
validTo: '2024-12-31',
},
]);
expect(withHistory.unresolved).toEqual([
{ source: 'qm', sourceId: 'QM_FB_2022_06_09', reason: 'no_span_overlap', primaryIsin: null },
{ source: 'qm', sourceId: 'QM_FB_2023', reason: 'no_span_overlap', primaryIsin: null },
]);
expect(withHistory.conflicts).toEqual([
{
source: 'qm',
sourceId: 'QM_FB_CROSS_SPAN',
conflictKind: 'ambiguous_ticker_span',
detail: {
primaryIsin: null,
currency: 'USD',
ibConid: null,
competingConids: [],
candidateListingKeys: [
{
primaryIsin: 'US0000000026',
mic: 'XNAS',
tickerRoot: 'FB',
classSuffix: '',
currency: 'USD',
},
{
primaryIsin: 'US30303M1027',
mic: 'XNAS',
tickerRoot: 'META',
classSuffix: '',
currency: 'USD',
},
],
},
},
]);
assertReconciliationAndContracts(withHistory);
// Omitted or empty symbolChanges leaves historical QM FB rows unresolved as no_span_overlap.
for (const noHistoryInput of [
{ instruments, eodListings, ibContracts: [], tickerSources },
{ instruments, eodListings, ibContracts: [], tickerSources, symbolChanges: [] },
]) {
const noHistory = matchListingsStageB(noHistoryInput);
expect(noHistory.conflicts).toEqual([]);
expect(
noHistory.unresolved.find(u => u.sourceId === 'QM_FB_2015')?.reason
).toBe('no_span_overlap');
expect(
noHistory.unresolved.find(u => u.sourceId === 'QM_FB_2022_06_08')?.reason
).toBe('no_span_overlap');
expect(
noHistory.providerMappings.find(m => m.sourceId === 'QM_FB_2025')?.listingKey.primaryIsin
).toBe('US0000000026');
expect(
noHistory.providerMappings.find(m => m.sourceId === 'QM_META_2024')?.listingKey.primaryIsin
).toBe('US30303M1027');
assertReconciliationAndContracts(noHistory);
}
});
test('handles symbolChanges edge cases: chained renames, duplicate/self aliases, unknown eodSourceId, undated listing, shuffle invariance, and malformed inputs (T-59)', () => {
const instruments: StageAInstrumentCandidate[] = [
{
primaryIsin: 'US5555555555',
primaryCusip: null,
issuerCountry: 'US',
canonicalName: 'Chained Rename Corp',
members: [{ source: 'eod', sourceId: 'CUR.US', currency: 'USD', method: 'tier1_isin' }],
},
{
primaryIsin: 'US6666666666',
primaryCusip: null,
issuerCountry: 'US',
canonicalName: 'Undated Aliased Corp',
members: [{ source: 'eod', sourceId: 'UNDATED.US', currency: 'USD', method: 'tier1_isin' }],
},
];
const eodListings: StageBEodListingInput[] = [
{
sourceId: 'CUR.US',
ticker: 'CUR',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2010-01-04', lastBarDate: '2025-11-26' },
},
{
sourceId: 'UNDATED.US',
ticker: 'UND',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: null,
},
];
const symbolChanges: StageBSymbolChangeInput[] = [
// Chained renames: OLD1 (< 2015-01-01) -> OLD2 (2015-01-01..2020-02-29, leap day) -> CUR
{ eodSourceId: 'CUR.US', oldTicker: 'OLD1', effectiveDate: '2015-01-01' },
{ eodSourceId: 'CUR.US', oldTicker: 'OLD2', validFrom: '2015-01-01', effectiveDate: '2020-03-01' },
// Duplicate alias entry and self-referential alias must not cause ambiguous_ticker_span.
{ eodSourceId: 'CUR.US', oldTicker: 'OLD2', validFrom: '2015-01-01', effectiveDate: '2020-03-01' },
{ eodSourceId: 'CUR.US', oldTicker: 'CUR', effectiveDate: '2022-01-01' },
// Unknown eodSourceId is a no-op.
{ eodSourceId: 'UNKNOWN.US', oldTicker: 'GHOST', effectiveDate: '2021-05-01' },
// Alias on an undated listing must preserve the missing_span safety guard.
{ eodSourceId: 'UNDATED.US', oldTicker: 'OLDUND', effectiveDate: '2021-05-01' },
];
const tickerSources: StageBTickerSourceInput[] = [
{
source: 'qm',
sourceId: 'QM_OLD1_2014',
ticker: 'OLD1',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2014-01-02', lastBarDate: '2014-12-31' },
},
{
source: 'qm',
sourceId: 'QM_OLD2_TOO_EARLY',
ticker: 'OLD2',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2014-06-01', lastBarDate: '2014-12-31' },
},
{
source: 'qm',
sourceId: 'QM_OLD2_LEAP_DAY',
ticker: 'OLD2',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2020-02-29', lastBarDate: '2020-02-29' },
},
{
source: 'qm',
sourceId: 'QM_OLD2_AFTER',
ticker: 'OLD2',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2020-03-01', lastBarDate: '2020-03-01' },
},
{
source: 'qm',
sourceId: 'QM_CUR_SELF_ALIAS',
ticker: 'CUR',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2019-01-02', lastBarDate: '2021-12-31' },
},
{
source: 'qm',
sourceId: 'QM_OLDUND',
ticker: 'OLDUND',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2019-01-02', lastBarDate: '2020-12-31' },
},
];
const baseline = matchListingsStageB({
instruments,
eodListings,
ibContracts: [],
tickerSources,
symbolChanges,
});
expect(baseline.conflicts).toEqual([]);
expect(baseline.providerMappings.filter(m => m.source === 'qm').map(m => m.sourceId)).toEqual([
'QM_CUR_SELF_ALIAS',
'QM_OLD1_2014',
'QM_OLD2_LEAP_DAY',
]);
for (const m of baseline.providerMappings.filter(x => x.source === 'qm')) {
expect(m.listingKey.primaryIsin).toBe('US5555555555');
expect(m.listingKey.tickerRoot).toBe('CUR');
}
expect(baseline.unresolved).toEqual([
{ source: 'qm', sourceId: 'QM_OLD2_AFTER', reason: 'no_span_overlap', primaryIsin: null },
{ source: 'qm', sourceId: 'QM_OLD2_TOO_EARLY', reason: 'no_span_overlap', primaryIsin: null },
{ source: 'qm', sourceId: 'QM_OLDUND', reason: 'missing_span', primaryIsin: null },
]);
assertReconciliationAndContracts(baseline);
// Shuffle invariance including symbolChanges.
const baselineJson = JSON.stringify(baseline);
for (const seed of [11, 99, 2026]) {
const shuffled = matchListingsStageB({
instruments: shuffleWithSeed(instruments, seed),
eodListings: shuffleWithSeed(eodListings, seed + 1),
ibContracts: [],
tickerSources: shuffleWithSeed(tickerSources, seed + 2),
symbolChanges: shuffleWithSeed(symbolChanges, seed + 3),
});
expect(JSON.stringify(shuffled)).toBe(baselineJson);
}
// Malformed symbolChanges inputs throw explicit validation errors.
const baseEmpty = { instruments: [], eodListings: [], ibContracts: [], tickerSources: [] };
expect(() =>
matchListingsStageB({
...baseEmpty,
symbolChanges: {} as unknown as StageBSymbolChangeInput[],
})
).toThrow(TypeError);
const sparseChanges = new Array(2) as unknown as StageBSymbolChangeInput[];
sparseChanges[1] = { eodSourceId: 'META.US', oldTicker: 'FB', effectiveDate: '2022-06-09' };
expect(() => matchListingsStageB({ ...baseEmpty, symbolChanges: sparseChanges })).toThrow(
/symbolChanges\[0\]/
);
expect(() =>
matchListingsStageB({
...baseEmpty,
symbolChanges: [null as unknown as StageBSymbolChangeInput],
})
).toThrow(TypeError);
expect(() =>
matchListingsStageB({
...baseEmpty,
symbolChanges: [{ eodSourceId: '', oldTicker: 'FB', effectiveDate: '2022-06-09' }],
})
).toThrow(/symbolChanges\[0\]/);
expect(() =>
matchListingsStageB({
...baseEmpty,
symbolChanges: [{ eodSourceId: 'META.US', oldTicker: 'BRK..B', effectiveDate: '2022-06-09' }],
})
).toThrow(/symbolChanges\[0\]/);
expect(() =>
matchListingsStageB({
...baseEmpty,
symbolChanges: [{ eodSourceId: 'META.US', oldTicker: 'FB', effectiveDate: '2023-02-29' }],
})
).toThrow(RangeError);
expect(() =>
matchListingsStageB({
...baseEmpty,
symbolChanges: [{ eodSourceId: 'META.US', oldTicker: 'FB', effectiveDate: '0001-01-01' }],
})
).toThrow(RangeError);
expect(() =>
matchListingsStageB({
...baseEmpty,
symbolChanges: [
{ eodSourceId: 'META.US', oldTicker: 'FB', effectiveDate: '2022-06-09T00:00:00Z' },
],
})
).toThrow(RangeError);
expect(() =>
matchListingsStageB({
...baseEmpty,
symbolChanges: [
{
eodSourceId: 'META.US',
oldTicker: 'FB',
effectiveDate: '2022-06-09',
validFrom: '2022-06-09',
},
],
})
).toThrow(RangeError);
expect(() =>
matchListingsStageB({
...baseEmpty,
symbolChanges: [
{
eodSourceId: 'META.US',
oldTicker: 'FB',
effectiveDate: '2022-06-09',
validFrom: '2023-02-29',
},
],
})
).toThrow(RangeError);
});
test('local scale benchmark: ~20k instruments, ~60k EOD, ~18k IB, ~40k QM/CEO complete under 5000 ms', () => {
const NUM_INSTRUMENTS = 20_000;
const instruments: StageAInstrumentCandidate[] = new Array<StageAInstrumentCandidate>(