654 lines
20 KiB
TypeScript
654 lines
20 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
import {
|
|
DEFAULT_OTC_MICS,
|
|
linkDelistingChains,
|
|
type DelistingChainResult,
|
|
type ListingNaturalKey,
|
|
type StageBListingCandidate,
|
|
} from '../../apps/stock/data-pipeline/src/security-master';
|
|
|
|
function makeListing(
|
|
overrides: Partial<StageBListingCandidate> &
|
|
Pick<StageBListingCandidate, 'primaryIsin' | 'mic' | 'tickerRoot'>
|
|
): StageBListingCandidate {
|
|
return {
|
|
primaryIsin: overrides.primaryIsin,
|
|
mic: overrides.mic,
|
|
tickerRoot: overrides.tickerRoot,
|
|
classSuffix: overrides.classSuffix ?? '',
|
|
currency: overrides.currency ?? 'USD',
|
|
ibConid: overrides.ibConid ?? null,
|
|
ibPrimaryExchange: overrides.ibPrimaryExchange ?? null,
|
|
listedFrom: overrides.listedFrom === undefined ? '2015-01-02' : overrides.listedFrom,
|
|
listedTo: overrides.listedTo === undefined ? null : overrides.listedTo,
|
|
};
|
|
}
|
|
|
|
function outcomeByTicker(result: DelistingChainResult, ticker: string) {
|
|
return result.listingOutcomes.find(o => o.listingKey.tickerRoot === ticker);
|
|
}
|
|
|
|
function links(result: DelistingChainResult): string[] {
|
|
return result.continuities.map(
|
|
c => `${c.fromListingKey.tickerRoot}->${c.toListingKey.tickerRoot}@${c.effectiveDate}`
|
|
);
|
|
}
|
|
|
|
function makeRng(seed: number): () => number {
|
|
let state = seed >>> 0;
|
|
return () => {
|
|
state = (state * 1664525 + 1013904223) >>> 0;
|
|
return state / 4294967296;
|
|
};
|
|
}
|
|
|
|
function shuffleWithSeed<T>(items: readonly T[], seed: number): T[] {
|
|
const copy = [...items];
|
|
const rng = makeRng(seed);
|
|
for (let i = copy.length - 1; i > 0; i--) {
|
|
const j = Math.floor(rng() * (i + 1));
|
|
const a = copy[i] as T;
|
|
copy[i] = copy[j] as T;
|
|
copy[j] = a;
|
|
}
|
|
return copy;
|
|
}
|
|
|
|
const AS_OF = '2025-11-27';
|
|
|
|
// Three ended primaries on one ISIN; O1 window covers P1+P2, O2 window covers P2+P3 (default 30d gap / 5d overlap).
|
|
// Only P2 is shared, so the components must be merged via range extension.
|
|
function rangeExtensionFixture(): StageBListingCandidate[] {
|
|
const isin = 'US6000000001';
|
|
return [
|
|
makeListing({ primaryIsin: isin, mic: 'XNAS', tickerRoot: 'RXP1', listedTo: '2023-01-01' }),
|
|
makeListing({ primaryIsin: isin, mic: 'XNYS', tickerRoot: 'RXP2', listedTo: '2023-01-20' }),
|
|
makeListing({ primaryIsin: isin, mic: 'ARCX', tickerRoot: 'RXP3', listedTo: '2023-02-10' }),
|
|
makeListing({
|
|
primaryIsin: isin,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'RXO1',
|
|
listedFrom: '2023-01-25',
|
|
listedTo: '2024-01-01',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: isin,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'RXO2',
|
|
listedFrom: '2023-02-15',
|
|
listedTo: '2024-01-01',
|
|
}),
|
|
];
|
|
}
|
|
|
|
describe('security master Pass 6 delisting chains: edge cases', () => {
|
|
test('merges chained candidate ranges that share only a middle primary into one conflict with all five keys', () => {
|
|
const result = linkDelistingChains(
|
|
{ listings: rangeExtensionFixture(), otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
|
|
expect(result.continuities).toEqual([]);
|
|
expect(result.conflicts).toHaveLength(1);
|
|
const conflict = result.conflicts[0];
|
|
expect(conflict?.conflictKind).toBe('ambiguous_otc_continuation');
|
|
expect(conflict?.detail.fromListingKeys.map(k => k.tickerRoot)).toEqual([
|
|
'RXP3',
|
|
'RXP1',
|
|
'RXP2',
|
|
]);
|
|
expect(conflict?.detail.toListingKeys.map(k => k.tickerRoot)).toEqual(['RXO1', 'RXO2']);
|
|
expect(conflict?.detail.candidateListingKeys).toHaveLength(5);
|
|
for (const ticker of ['RXP1', 'RXP2', 'RXP3']) {
|
|
expect(outcomeByTicker(result, ticker)).toMatchObject({
|
|
delistReason: 'unknown_involuntary',
|
|
terminalReturn: -0.3,
|
|
});
|
|
}
|
|
});
|
|
|
|
test('narrowing the transfer gap splits the chained component into two clean one-to-one links (mutation check)', () => {
|
|
// maxTransferGapDays=10: O1 window [01-15, 01-30] -> RXP2 only; O2 window [02-05, 02-20] -> RXP3 only.
|
|
const result = linkDelistingChains(
|
|
{ listings: rangeExtensionFixture(), otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF, maxTransferGapDays: 10 }
|
|
);
|
|
|
|
expect(result.conflicts).toEqual([]);
|
|
// Sorted by fromListingKey natural key: RXP3 (ARCX) before RXP2 (XNYS).
|
|
expect(links(result)).toEqual(['RXP3->RXO2@2023-02-15', 'RXP2->RXO1@2023-01-25']);
|
|
expect(outcomeByTicker(result, 'RXP1')).toMatchObject({
|
|
delistReason: 'unknown_involuntary',
|
|
terminalReturn: -0.3,
|
|
});
|
|
});
|
|
|
|
test('adjacent but non-overlapping candidate ranges link independently without a false conflict', () => {
|
|
const isin = 'US6000000002';
|
|
const listings = [
|
|
makeListing({ primaryIsin: isin, mic: 'XNAS', tickerRoot: 'ADJ1', listedTo: '2023-01-01' }),
|
|
makeListing({ primaryIsin: isin, mic: 'XNYS', tickerRoot: 'ADJ2', listedTo: '2023-01-10' }),
|
|
makeListing({
|
|
primaryIsin: isin,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'ADJQ1',
|
|
listedFrom: '2023-01-03',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: isin,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'ADJQ2',
|
|
listedFrom: '2023-01-12',
|
|
}),
|
|
];
|
|
// overlap 0 / gap 5: ADJQ1 window [12-29, 01-03] -> ADJ1; ADJQ2 window [01-07, 01-12] -> ADJ2.
|
|
const result = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF, maxTransferGapDays: 5, overlapToleranceDays: 0 }
|
|
);
|
|
|
|
expect(result.conflicts).toEqual([]);
|
|
expect(links(result)).toEqual(['ADJ1->ADJQ1@2023-01-03', 'ADJ2->ADJQ2@2023-01-12']);
|
|
expect(outcomeByTicker(result, 'ADJQ1')).toMatchObject({
|
|
delistReason: 'active',
|
|
terminalReturn: null,
|
|
});
|
|
});
|
|
|
|
test('range-extension fixture output is identical across seeded shuffles', () => {
|
|
const fixture = rangeExtensionFixture();
|
|
const baseline = JSON.stringify(
|
|
linkDelistingChains({ listings: fixture, otcMics: DEFAULT_OTC_MICS }, { asOfDate: AS_OF })
|
|
);
|
|
for (const seed of [7, 2024, 31337]) {
|
|
const shuffled = shuffleWithSeed(fixture, seed);
|
|
expect(
|
|
JSON.stringify(
|
|
linkDelistingChains(
|
|
{ listings: shuffled, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
)
|
|
)
|
|
).toBe(baseline);
|
|
}
|
|
});
|
|
|
|
test('ended primary before its OTC continuation starts is unknown_involuntary, then flips to moved_to_otc on the OTC start date', () => {
|
|
const listings = [
|
|
makeListing({
|
|
primaryIsin: 'US78486Q1013',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'SIVB',
|
|
listedFrom: '1987-10-26',
|
|
listedTo: '2023-03-10',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US78486Q1013',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'SIVBQ',
|
|
listedFrom: '2023-03-28',
|
|
listedTo: '2024-11-07',
|
|
}),
|
|
];
|
|
|
|
const before = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: '2023-03-27' }
|
|
);
|
|
expect(before.continuities).toEqual([]);
|
|
expect(outcomeByTicker(before, 'SIVB')).toMatchObject({
|
|
delistReason: 'unknown_involuntary',
|
|
terminalReturn: -0.3,
|
|
});
|
|
|
|
const onStart = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: '2023-03-28' }
|
|
);
|
|
expect(links(onStart)).toEqual(['SIVB->SIVBQ@2023-03-28']);
|
|
});
|
|
|
|
test('zero-valued options: same-day transfer links, next-day does not; zero grace makes listedTo === asOfDate active', () => {
|
|
const listings = [
|
|
makeListing({
|
|
primaryIsin: 'US6100000001',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'SAME',
|
|
listedTo: '2023-03-10',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6100000001',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'SAMEQ',
|
|
listedFrom: '2023-03-10',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6100000002',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'NEXT',
|
|
listedTo: '2023-03-10',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6100000002',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'NEXTQ',
|
|
listedFrom: '2023-03-11',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6100000003',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'TODAY',
|
|
listedTo: AS_OF,
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6100000004',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'YDAY',
|
|
listedTo: '2025-11-26',
|
|
}),
|
|
];
|
|
const result = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF, activeGraceDays: 0, maxTransferGapDays: 0, overlapToleranceDays: 0 }
|
|
);
|
|
|
|
expect(links(result)).toEqual(['SAME->SAMEQ@2023-03-10']);
|
|
expect(outcomeByTicker(result, 'NEXT')).toMatchObject({ delistReason: 'unknown_involuntary' });
|
|
expect(outcomeByTicker(result, 'TODAY')).toMatchObject({
|
|
delistReason: 'active',
|
|
terminalReturn: null,
|
|
});
|
|
expect(outcomeByTicker(result, 'YDAY')).toMatchObject({
|
|
delistReason: 'unknown_involuntary',
|
|
terminalReturn: -0.3,
|
|
});
|
|
});
|
|
|
|
test('default options apply the 30-day gap across a year boundary and leap days', () => {
|
|
const listings = [
|
|
// 2022-12-15 + 30d = 2023-01-14 -> link; +31d = 2023-01-15 -> no link.
|
|
makeListing({
|
|
primaryIsin: 'US6200000001',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'YR30',
|
|
listedTo: '2022-12-15',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6200000001',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'YR30Q',
|
|
listedFrom: '2023-01-14',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6200000002',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'YR31',
|
|
listedTo: '2022-12-15',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6200000002',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'YR31Q',
|
|
listedFrom: '2023-01-15',
|
|
}),
|
|
// 2024-02-01 + 30d = 2024-03-02 (2024 is a leap year) -> link; 2024-03-03 -> no link.
|
|
makeListing({
|
|
primaryIsin: 'US6200000003',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'LP30',
|
|
listedTo: '2024-02-01',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6200000003',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'LP30Q',
|
|
listedFrom: '2024-03-02',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6200000004',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'LP31',
|
|
listedTo: '2024-02-01',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6200000004',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'LP31Q',
|
|
listedFrom: '2024-03-03',
|
|
}),
|
|
// Valid leap day as listedTo.
|
|
makeListing({
|
|
primaryIsin: 'US6200000005',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'LEAP',
|
|
listedTo: '2024-02-29',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6200000005',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'LEAPQ',
|
|
listedFrom: '2024-03-05',
|
|
}),
|
|
];
|
|
const result = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
|
|
expect(result.conflicts).toEqual([]);
|
|
expect(links(result)).toEqual([
|
|
'YR30->YR30Q@2023-01-14',
|
|
'LP30->LP30Q@2024-03-02',
|
|
'LEAP->LEAPQ@2024-03-05',
|
|
]);
|
|
});
|
|
|
|
test('century leap rules: 1900-02-29 is rejected, 2000-02-29 is accepted', () => {
|
|
const listings = [
|
|
makeListing({
|
|
primaryIsin: 'US6300000001',
|
|
mic: 'XNYS',
|
|
tickerRoot: 'C1900',
|
|
listedFrom: '1900-02-29',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6300000002',
|
|
mic: 'XNYS',
|
|
tickerRoot: 'C2000',
|
|
listedFrom: '2000-02-29',
|
|
}),
|
|
];
|
|
const result = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
|
|
expect(result.stats.skippedInvalidDates).toBe(1);
|
|
expect(
|
|
result.conflicts.map(c => [c.conflictKind, c.detail.candidateListingKeys[0]?.tickerRoot])
|
|
).toEqual([['invalid_listing_dates', 'C1900']]);
|
|
expect(outcomeByTicker(result, 'C2000')).toMatchObject({ delistReason: 'active' });
|
|
});
|
|
|
|
test('missing (undefined) listedFrom is a typed invalid_listing_dates conflict, not a throw', () => {
|
|
const row = {
|
|
primaryIsin: 'US6400000001',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'UNDEF',
|
|
classSuffix: '',
|
|
currency: 'USD',
|
|
ibConid: null,
|
|
ibPrimaryExchange: null,
|
|
listedTo: null,
|
|
} as unknown as StageBListingCandidate;
|
|
const result = linkDelistingChains(
|
|
{ listings: [row], otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
|
|
expect(result.listingOutcomes).toEqual([]);
|
|
expect(result.stats.skippedInvalidDates).toBe(1);
|
|
expect(result.conflicts[0]?.conflictKind).toBe('invalid_listing_dates');
|
|
});
|
|
|
|
test('a still-open OTC continuation (listedTo null) links and stays active', () => {
|
|
const listings = [
|
|
makeListing({
|
|
primaryIsin: 'US6500000001',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'OPNP',
|
|
listedTo: '2024-06-03',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6500000001',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'OPNPF',
|
|
listedFrom: '2024-06-04',
|
|
}),
|
|
];
|
|
const result = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
|
|
expect(links(result)).toEqual(['OPNP->OPNPF@2024-06-04']);
|
|
expect(outcomeByTicker(result, 'OPNP')).toMatchObject({
|
|
delistReason: 'moved_to_otc',
|
|
terminalReturn: null,
|
|
});
|
|
expect(outcomeByTicker(result, 'OPNPF')).toMatchObject({
|
|
delistReason: 'active',
|
|
terminalReturn: null,
|
|
});
|
|
});
|
|
|
|
test('primary still inside the grace window is never linked, even when an OTC listing already started', () => {
|
|
const listings = [
|
|
makeListing({
|
|
primaryIsin: 'US6600000001',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'GRCE',
|
|
listedTo: '2025-11-20',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6600000001',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'GRCEQ',
|
|
listedFrom: '2025-11-21',
|
|
}),
|
|
];
|
|
const result = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
|
|
expect(result.continuities).toEqual([]);
|
|
expect(result.conflicts).toEqual([]);
|
|
expect(result.listingOutcomes.every(o => o.delistReason === 'active')).toBe(true);
|
|
});
|
|
|
|
test('an active sibling primary does not compete with the ended primary for the OTC continuation', () => {
|
|
const isin = 'US6700000001';
|
|
const listings = [
|
|
makeListing({ primaryIsin: isin, mic: 'XNAS', tickerRoot: 'SIBE', listedTo: '2023-03-10' }),
|
|
makeListing({ primaryIsin: isin, mic: 'XNYS', tickerRoot: 'SIBA', listedTo: null }),
|
|
makeListing({
|
|
primaryIsin: isin,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'SIBEQ',
|
|
listedFrom: '2023-03-15',
|
|
listedTo: '2023-12-01',
|
|
}),
|
|
];
|
|
const result = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
|
|
expect(result.conflicts).toEqual([]);
|
|
expect(links(result)).toEqual(['SIBE->SIBEQ@2023-03-15']);
|
|
expect(outcomeByTicker(result, 'SIBA')).toMatchObject({ delistReason: 'active' });
|
|
expect(outcomeByTicker(result, 'SIBEQ')).toMatchObject({
|
|
delistReason: 'bankruptcy_cancelled',
|
|
terminalReturn: -1.0,
|
|
});
|
|
});
|
|
|
|
test('OTC classification is driven only by otcMics: PINX is primary under defaults, OTC when supplied (case-insensitive set)', () => {
|
|
const listings = [
|
|
makeListing({
|
|
primaryIsin: 'US6800000001',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'PNKP',
|
|
listedTo: '2023-03-10',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US6800000001',
|
|
mic: 'PINX',
|
|
tickerRoot: 'PNKPQ',
|
|
listedFrom: '2023-03-20',
|
|
listedTo: '2023-10-01',
|
|
}),
|
|
];
|
|
|
|
const defaults = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
expect(defaults.continuities).toEqual([]);
|
|
expect(outcomeByTicker(defaults, 'PNKPQ')).toMatchObject({
|
|
delistReason: 'unknown_involuntary',
|
|
terminalReturn: -0.3,
|
|
});
|
|
|
|
const withPinx = linkDelistingChains(
|
|
{ listings, otcMics: new Set(['otcm', ' pinx ']) },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
expect(links(withPinx)).toEqual(['PNKP->PNKPQ@2023-03-20']);
|
|
expect(outcomeByTicker(withPinx, 'PNKPQ')).toMatchObject({
|
|
delistReason: 'bankruptcy_cancelled',
|
|
terminalReturn: -1.0,
|
|
});
|
|
});
|
|
|
|
test('natural keys that collide only after trim/case normalization are quarantined as duplicates, unlinking the primary', () => {
|
|
const isin = 'US6900000001';
|
|
const listings = [
|
|
makeListing({ primaryIsin: isin, mic: 'XNAS', tickerRoot: 'NRMP', listedTo: '2023-03-10' }),
|
|
makeListing({
|
|
primaryIsin: isin,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'NRMPQ',
|
|
listedFrom: '2023-03-15',
|
|
listedTo: '2023-12-01',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: ` ${isin.toLowerCase()} `,
|
|
mic: 'otcm',
|
|
tickerRoot: ' NRMPQ ',
|
|
listedFrom: '2023-03-16',
|
|
listedTo: '2023-12-01',
|
|
}),
|
|
];
|
|
const result = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
|
|
expect(result.continuities).toEqual([]);
|
|
expect(result.stats.skippedDuplicateKeys).toBe(2);
|
|
expect(result.conflicts.map(c => c.conflictKind)).toEqual([
|
|
'duplicate_listing_key',
|
|
'missing_otc_listed_from',
|
|
]);
|
|
expect(result.conflicts[0]?.detail.candidateListingKeys[0]).toEqual({
|
|
primaryIsin: isin,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'NRMPQ',
|
|
classSuffix: '',
|
|
currency: 'USD',
|
|
});
|
|
expect(outcomeByTicker(result, 'NRMP')).toMatchObject({
|
|
delistReason: 'unknown_involuntary',
|
|
terminalReturn: -0.3,
|
|
});
|
|
});
|
|
|
|
test('different class suffix on the same ISIN and currency still forms one chain and keeps each key intact', () => {
|
|
const isin = 'US7000000001';
|
|
const listings = [
|
|
makeListing({
|
|
primaryIsin: isin,
|
|
mic: 'XNYS',
|
|
tickerRoot: 'CLS',
|
|
classSuffix: 'A',
|
|
listedTo: '2023-03-10',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: isin,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'CLSAQ',
|
|
classSuffix: '',
|
|
listedFrom: '2023-03-14',
|
|
}),
|
|
];
|
|
const result = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
|
|
expect(result.continuities).toHaveLength(1);
|
|
expect(result.continuities[0]?.fromListingKey.classSuffix).toBe('A');
|
|
expect(result.continuities[0]?.toListingKey.classSuffix).toBe('');
|
|
});
|
|
|
|
test('empty input returns empty, zeroed result', () => {
|
|
const result = linkDelistingChains(
|
|
{ listings: [], otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
expect(result).toEqual({
|
|
continuities: [],
|
|
listingOutcomes: [],
|
|
conflicts: [],
|
|
stats: {
|
|
totalInputListings: 0,
|
|
processedListings: 0,
|
|
skippedMalformedEntries: 0,
|
|
skippedInvalidDates: 0,
|
|
skippedDuplicateKeys: 0,
|
|
continuitiesCreated: 0,
|
|
conflictsDetected: 0,
|
|
byOutcome: { active: 0, moved_to_otc: 0, bankruptcy_cancelled: 0, unknown_involuntary: 0 },
|
|
},
|
|
});
|
|
});
|
|
|
|
test('keys inside multiple conflicts and continuities never share references with each other', () => {
|
|
const listings = [
|
|
...rangeExtensionFixture(),
|
|
makeListing({
|
|
primaryIsin: 'US7100000001',
|
|
mic: 'XNAS',
|
|
tickerRoot: 'REFP',
|
|
listedTo: '2023-03-10',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US7100000001',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'REFPQ',
|
|
listedFrom: '2023-03-14',
|
|
}),
|
|
makeListing({
|
|
primaryIsin: 'US7100000001',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'REFPR',
|
|
listedFrom: null,
|
|
}),
|
|
];
|
|
const result = linkDelistingChains(
|
|
{ listings, otcMics: DEFAULT_OTC_MICS },
|
|
{ asOfDate: AS_OF }
|
|
);
|
|
|
|
const seen = new Set<ListingNaturalKey>();
|
|
const all: ListingNaturalKey[] = [
|
|
...result.continuities.flatMap(c => [c.fromListingKey, c.toListingKey]),
|
|
...result.listingOutcomes.map(o => o.listingKey),
|
|
...result.conflicts.flatMap(c => [
|
|
...c.detail.fromListingKeys,
|
|
...c.detail.toListingKeys,
|
|
...c.detail.candidateListingKeys,
|
|
]),
|
|
];
|
|
for (const key of all) {
|
|
expect(seen.has(key)).toBe(false);
|
|
seen.add(key);
|
|
}
|
|
expect(result.conflicts.map(c => c.conflictKind).sort()).toEqual([
|
|
'ambiguous_otc_continuation',
|
|
'missing_otc_listed_from',
|
|
]);
|
|
});
|
|
});
|