T-69: Bug sweep (3 bugs: T-64, T-67, T-71)

This commit is contained in:
Studio 2026-11-27 19:36:35 -05:00
parent cb59177c3e
commit cd43681351
2 changed files with 178 additions and 9 deletions

View file

@ -447,11 +447,16 @@ export function linkDelistingChains(
const reportedDuplicateKeyIds = new Set<string>();
const droppedOtcKeysByGroup = new Map<string, Map<string, ListingNaturalKey>>();
const recordDroppedOtcIfApplicable = (key: ListingNaturalKey, keyId: string): void => {
const recordDroppedOtcIfApplicable = (
key: ListingNaturalKey,
keyId: string,
listedFromDay: number | null
): void => {
if (
!normalizedOtcMics.has(key.mic) ||
key.primaryIsin.length === 0 ||
key.currency.length === 0
key.currency.length === 0 ||
(listedFromDay !== null && listedFromDay > asOfDay)
) {
return;
}
@ -481,7 +486,7 @@ export function linkDelistingChains(
const occurrences = keyCounts.get(keyId) ?? 0;
if (occurrences > 1) {
skippedDuplicateKeys += 1;
recordDroppedOtcIfApplicable(key, keyId);
recordDroppedOtcIfApplicable(key, keyId, parseExactIsoDateToUtcDay(row.listedFrom));
if (!reportedDuplicateKeyIds.has(keyId)) {
reportedDuplicateKeyIds.add(keyId);
conflicts.push({
@ -533,7 +538,7 @@ export function linkDelistingChains(
if (dateErrorReason !== null) {
skippedInvalidDates += 1;
recordDroppedOtcIfApplicable(key, keyId);
recordDroppedOtcIfApplicable(key, keyId, listedFromDay);
conflicts.push({
conflictKind: 'invalid_listing_dates',
detail: {
@ -624,10 +629,8 @@ export function linkDelistingChains(
const allKeys = [...sortedPrimaryKeys, ...sortedBlockingOtcKeys].sort(
compareListingNaturalKey
);
const singleMissingOnly =
group.missingFromOtcs.length === 1 && droppedOtcCount === 0
? group.missingFromOtcs[0]
: undefined;
const singleMissingOtc =
group.missingFromOtcs.length === 1 ? group.missingFromOtcs[0] : undefined;
let reason: string;
if (droppedOtcCount === 0) {
reason =
@ -651,7 +654,7 @@ export function linkDelistingChains(
toListingKeys: sortedBlockingOtcKeys.map(cloneListingKey),
candidateListingKeys: allKeys.map(cloneListingKey),
listedFrom: null,
listedTo: singleMissingOnly !== undefined ? singleMissingOnly.listedTo : null,
listedTo: singleMissingOtc !== undefined ? singleMissingOtc.listedTo : null,
reason,
},
});

View file

@ -1678,6 +1678,172 @@ describe('security master Pass 6 OTC delisting chain linker', () => {
assertReconciliationAndContracts(customBlocked, customOtcListings.length);
});
test('T-71 point-in-time & mixed-conflict invariants: future dropped OTC rows do not alter earlier replays, and single undated OTC listedTo is preserved alongside dropped OTC rows', () => {
const basePair: StageBListingCandidate[] = [
makeListing({
primaryIsin: 'US7100000020',
mic: 'XNAS',
tickerRoot: 'AAA',
currency: 'USD',
listedFrom: '2018-01-02',
listedTo: '2023-03-10',
}),
makeListing({
primaryIsin: 'US7100000020',
mic: 'OTCM',
tickerRoot: 'AAAQ',
currency: 'USD',
listedFrom: '2023-03-15',
listedTo: '2024-01-01',
}),
];
const baselineReplay = linkDelistingChains(
{ listings: basePair, otcMics: DEFAULT_OTC_MICS },
{ asOfDate: '2023-04-03' }
);
expect(baselineReplay.continuities).toHaveLength(1);
expect(baselineReplay.conflicts).toEqual([]);
expect(
baselineReplay.listingOutcomes.find(o => o.listingKey.tickerRoot === 'AAA')
).toMatchObject({
delistReason: 'moved_to_otc',
terminalReturn: null,
});
assertReconciliationAndContracts(baselineReplay, basePair.length);
// Future-dated OTC row with invalid listedTo added in a later snapshot must still report
// invalid_listing_dates, but must NOT suppress the AAA -> AAAQ link as of 2023-04-03.
const withFutureInvalidOtc: StageBListingCandidate[] = [
...basePair,
makeListing({
primaryIsin: 'US7100000020',
mic: 'OTCM',
tickerRoot: 'AAAU',
currency: 'USD',
listedFrom: '2023-06-01',
listedTo: '2024-13-45',
}),
];
const earlierReplayWithFutureInvalid = linkDelistingChains(
{ listings: withFutureInvalidOtc, otcMics: DEFAULT_OTC_MICS },
{ asOfDate: '2023-04-03' }
);
expect(earlierReplayWithFutureInvalid.continuities).toEqual(baselineReplay.continuities);
expect(earlierReplayWithFutureInvalid.listingOutcomes).toEqual(baselineReplay.listingOutcomes);
expect(earlierReplayWithFutureInvalid.conflicts.map(c => c.conflictKind)).toEqual([
'invalid_listing_dates',
]);
assertReconciliationAndContracts(earlierReplayWithFutureInvalid, withFutureInvalidOtc.length);
// Once asOfDate reaches the future row's listedFrom (2023-06-01), it DOES suppress linking.
const laterReplayWithInvalid = linkDelistingChains(
{ listings: withFutureInvalidOtc, otcMics: DEFAULT_OTC_MICS },
{ asOfDate: '2023-06-01' }
);
expect(laterReplayWithInvalid.continuities).toEqual([]);
expect(laterReplayWithInvalid.conflicts.map(c => c.conflictKind)).toEqual([
'invalid_listing_dates',
'missing_otc_listed_from',
]);
expect(
laterReplayWithInvalid.listingOutcomes.find(o => o.listingKey.tickerRoot === 'AAA')
).toMatchObject({
delistReason: 'unknown_involuntary',
terminalReturn: SHUMWAY_PRIMARY_TERMINAL_RETURN,
});
assertReconciliationAndContracts(laterReplayWithInvalid, withFutureInvalidOtc.length);
// Duplicate OTC rows whose copies all start after asOfDate report duplicate_listing_key
// without suppressing the earlier AAA -> AAAQ replay.
const withFutureDuplicateOtc: StageBListingCandidate[] = [
...basePair,
makeListing({
primaryIsin: 'US7100000020',
mic: 'OTCM',
tickerRoot: 'AAAU',
currency: 'USD',
listedFrom: '2023-06-01',
listedTo: '2024-01-01',
}),
makeListing({
primaryIsin: 'US7100000020',
mic: 'OTCM',
tickerRoot: 'AAAU',
currency: 'USD',
listedFrom: '2023-06-05',
listedTo: '2024-06-01',
}),
];
const earlierReplayWithFutureDup = linkDelistingChains(
{ listings: withFutureDuplicateOtc, otcMics: DEFAULT_OTC_MICS },
{ asOfDate: '2023-04-03' }
);
expect(earlierReplayWithFutureDup.continuities).toEqual(baselineReplay.continuities);
expect(earlierReplayWithFutureDup.listingOutcomes).toEqual(baselineReplay.listingOutcomes);
expect(earlierReplayWithFutureDup.conflicts.map(c => c.conflictKind)).toEqual([
'duplicate_listing_key',
]);
assertReconciliationAndContracts(earlierReplayWithFutureDup, withFutureDuplicateOtc.length);
// Mixed-case blocking conflict: 1 valid undated OTC row (listedTo: '2024-01-01') plus a
// different OTC row dropped for invalid dates lists both OTC keys AND preserves listedTo.
const mixedBlockingListings: StageBListingCandidate[] = [
makeListing({
primaryIsin: 'US7100000021',
mic: 'XNAS',
tickerRoot: 'MIXP',
currency: 'USD',
listedFrom: '2018-01-02',
listedTo: '2023-03-10',
}),
makeListing({
primaryIsin: 'US7100000021',
mic: 'OTCM',
tickerRoot: 'MIXQ',
currency: 'USD',
listedFrom: '2023-03-15',
listedTo: '2024-01-01',
}),
makeListing({
primaryIsin: 'US7100000021',
mic: 'OTCM',
tickerRoot: 'MIXU',
currency: 'USD',
listedFrom: null,
listedTo: '2024-01-01',
}),
makeListing({
primaryIsin: 'US7100000021',
mic: 'OTCM',
tickerRoot: 'MIXB',
currency: 'USD',
listedFrom: '2023-03-20',
listedTo: '2024-13-45',
}),
];
const mixedBlockingResult = linkDelistingChains(
{ listings: mixedBlockingListings, otcMics: DEFAULT_OTC_MICS },
{ asOfDate: '2025-11-27' }
);
expect(mixedBlockingResult.continuities).toEqual([]);
expect(mixedBlockingResult.conflicts.map(c => c.conflictKind)).toEqual([
'invalid_listing_dates',
'missing_otc_listed_from',
]);
const mixedMissingConflict = mixedBlockingResult.conflicts.find(
c => c.conflictKind === 'missing_otc_listed_from'
);
expect(mixedMissingConflict?.detail.fromListingKeys.map(k => k.tickerRoot)).toEqual(['MIXP']);
expect(mixedMissingConflict?.detail.toListingKeys.map(k => k.tickerRoot)).toEqual([
'MIXB',
'MIXU',
]);
expect(mixedMissingConflict?.detail.listedFrom).toBeNull();
expect(mixedMissingConflict?.detail.listedTo).toBe('2024-01-01');
assertReconciliationAndContracts(mixedBlockingResult, mixedBlockingListings.length);
});
test('throws TypeError or RangeError on invalid options or container arguments', () => {
const emptyInput = { listings: [], otcMics: DEFAULT_OTC_MICS };
const validOpts = { asOfDate: '2025-11-27' };