341 lines
12 KiB
TypeScript
341 lines
12 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
import {
|
|
DEFAULT_OTC_MICS,
|
|
linkDelistingChains,
|
|
SHUMWAY_PRIMARY_TERMINAL_RETURN,
|
|
type DelistingChainResult,
|
|
type ListingNaturalKey,
|
|
type StageBListingCandidate,
|
|
} from '../../apps/stock/data-pipeline/src/security-master';
|
|
|
|
// QA regression coverage for T-69 (T-67 malformed slots, T-71 dropped-OTC suppression).
|
|
// Complements security-master-delisting-chains.test.ts with normalization, group isolation,
|
|
// date-parse and ordering edge cases the implementation tests do not exercise.
|
|
|
|
const AS_OF = '2025-11-27';
|
|
const ISIN = 'US7169000001';
|
|
|
|
function listing(
|
|
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 primaryAndOtc(isin: string, root: string, currency = 'USD'): StageBListingCandidate[] {
|
|
return [
|
|
listing({
|
|
primaryIsin: isin,
|
|
mic: 'XNAS',
|
|
tickerRoot: root,
|
|
currency,
|
|
listedFrom: '2018-01-02',
|
|
listedTo: '2023-03-10',
|
|
}),
|
|
listing({
|
|
primaryIsin: isin,
|
|
mic: 'OTCM',
|
|
tickerRoot: `${root}Q`,
|
|
currency,
|
|
listedFrom: '2023-03-15',
|
|
listedTo: '2024-01-01',
|
|
}),
|
|
];
|
|
}
|
|
|
|
function run(listings: StageBListingCandidate[], asOfDate = AS_OF): DelistingChainResult {
|
|
return linkDelistingChains({ listings, otcMics: DEFAULT_OTC_MICS }, { asOfDate });
|
|
}
|
|
|
|
function kinds(result: DelistingChainResult): string[] {
|
|
return result.conflicts.map(c => c.conflictKind);
|
|
}
|
|
|
|
function outcomeFor(result: DelistingChainResult, root: string) {
|
|
return result.listingOutcomes.find(o => o.listingKey.tickerRoot === root);
|
|
}
|
|
|
|
function expectReconciles(result: DelistingChainResult, inputCount: number): void {
|
|
expect(result.stats.totalInputListings).toBe(inputCount);
|
|
expect(
|
|
result.stats.processedListings +
|
|
result.stats.skippedMalformedEntries +
|
|
result.stats.skippedInvalidDates +
|
|
result.stats.skippedDuplicateKeys
|
|
).toBe(inputCount);
|
|
expect(result.stats.processedListings).toBe(result.listingOutcomes.length);
|
|
}
|
|
|
|
describe('T-71 QA: dropped OTC rows block only their own normalized (primaryIsin, currency) group', () => {
|
|
test('a dropped OTC row whose ISIN/MIC/currency differ only by case and whitespace still blocks the group', () => {
|
|
const listings = [
|
|
...primaryAndOtc(ISIN, 'NRM'),
|
|
listing({
|
|
primaryIsin: ` ${ISIN.toLowerCase()} `,
|
|
mic: ' otcm',
|
|
tickerRoot: 'NRMU',
|
|
currency: 'usd ',
|
|
listedFrom: null,
|
|
listedTo: '2024-13-45',
|
|
}),
|
|
];
|
|
|
|
const result = run(listings);
|
|
|
|
expect(result.continuities).toEqual([]);
|
|
expect(kinds(result)).toEqual(['invalid_listing_dates', 'missing_otc_listed_from']);
|
|
const blocking = result.conflicts[1];
|
|
expect(blocking?.detail.primaryIsin).toBe(ISIN);
|
|
expect(blocking?.detail.currency).toBe('USD');
|
|
expect(blocking?.detail.toListingKeys).toEqual([
|
|
{ primaryIsin: ISIN, mic: 'OTCM', tickerRoot: 'NRMU', classSuffix: '', currency: 'USD' },
|
|
]);
|
|
expect(outcomeFor(result, 'NRM')).toMatchObject({
|
|
delistReason: 'unknown_involuntary',
|
|
terminalReturn: SHUMWAY_PRIMARY_TERMINAL_RETURN,
|
|
});
|
|
expectReconciles(result, listings.length);
|
|
});
|
|
|
|
test('a dropped OTC row in another currency or another ISIN does not block the USD group, while its own group in the same batch is blocked', () => {
|
|
const blockedIsin = 'US7169000002';
|
|
const listings = [
|
|
...primaryAndOtc(ISIN, 'KEEP'),
|
|
// Same ISIN, CAD currency: separate group, must not block USD linking.
|
|
listing({
|
|
primaryIsin: ISIN,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'KEEPC',
|
|
currency: 'CAD',
|
|
listedFrom: null,
|
|
listedTo: '2024-13-45',
|
|
}),
|
|
// Different ISIN, same currency: separate group.
|
|
listing({
|
|
primaryIsin: 'US7169000099',
|
|
mic: 'OTCM',
|
|
tickerRoot: 'OTHU',
|
|
currency: 'USD',
|
|
listedFrom: null,
|
|
listedTo: '2024-13-45',
|
|
}),
|
|
...primaryAndOtc(blockedIsin, 'BLK'),
|
|
listing({
|
|
primaryIsin: blockedIsin,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'BLKU',
|
|
listedFrom: null,
|
|
listedTo: '2024-13-45',
|
|
}),
|
|
];
|
|
|
|
const result = run(listings);
|
|
|
|
expect(result.continuities).toHaveLength(1);
|
|
expect(result.continuities[0]?.fromListingKey.tickerRoot).toBe('KEEP');
|
|
expect(result.continuities[0]?.toListingKey.tickerRoot).toBe('KEEPQ');
|
|
const blocking = result.conflicts.filter(c => c.conflictKind === 'missing_otc_listed_from');
|
|
expect(blocking).toHaveLength(1);
|
|
expect(blocking[0]?.detail.primaryIsin).toBe(blockedIsin);
|
|
expect(blocking[0]?.detail.toListingKeys.map(k => k.tickerRoot)).toEqual(['BLKU']);
|
|
expect(outcomeFor(result, 'KEEP')?.delistReason).toBe('moved_to_otc');
|
|
expect(outcomeFor(result, 'BLK')?.delistReason).toBe('unknown_involuntary');
|
|
expectReconciles(result, listings.length);
|
|
});
|
|
|
|
test('a dropped OTC row with an unparseable listedFrom is treated as undated and blocks (never guess)', () => {
|
|
const listings = [
|
|
...primaryAndOtc(ISIN, 'UNP'),
|
|
listing({
|
|
primaryIsin: ISIN,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'UNPU',
|
|
listedFrom: '2023/03/20',
|
|
listedTo: '2024-01-01',
|
|
}),
|
|
];
|
|
|
|
const result = run(listings);
|
|
|
|
expect(result.continuities).toEqual([]);
|
|
expect(kinds(result)).toEqual(['invalid_listing_dates', 'missing_otc_listed_from']);
|
|
expectReconciles(result, listings.length);
|
|
});
|
|
|
|
test('an OTC row dropped for an inverted range with a past listedFrom blocks the group', () => {
|
|
const listings = [
|
|
...primaryAndOtc(ISIN, 'INV'),
|
|
listing({
|
|
primaryIsin: ISIN,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'INVU',
|
|
listedFrom: '2023-05-01',
|
|
listedTo: '2023-04-01',
|
|
}),
|
|
];
|
|
|
|
const result = run(listings);
|
|
|
|
expect(result.continuities).toEqual([]);
|
|
expect(kinds(result)).toEqual(['invalid_listing_dates', 'missing_otc_listed_from']);
|
|
expect(result.conflicts[1]?.detail.reason).toContain('dropped');
|
|
expectReconciles(result, listings.length);
|
|
});
|
|
|
|
test('a duplicate OTC key where one copy started before asOfDate blocks even if the other copy is future-dated', () => {
|
|
const listings = [
|
|
...primaryAndOtc(ISIN, 'MIX'),
|
|
listing({
|
|
primaryIsin: ISIN,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'MIXU',
|
|
listedFrom: '2023-03-20',
|
|
listedTo: '2024-01-01',
|
|
}),
|
|
listing({
|
|
primaryIsin: ISIN,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'MIXU',
|
|
listedFrom: '2023-09-01',
|
|
listedTo: '2024-06-01',
|
|
}),
|
|
];
|
|
|
|
const replay = run(listings, '2023-04-03');
|
|
|
|
expect(replay.continuities).toEqual([]);
|
|
expect(kinds(replay)).toEqual(['duplicate_listing_key', 'missing_otc_listed_from']);
|
|
expect(replay.conflicts[1]?.detail.toListingKeys.map(k => k.tickerRoot)).toEqual(['MIXU']);
|
|
expectReconciles(replay, listings.length);
|
|
});
|
|
|
|
test('a dropped OTC row in a group with no ended primary emits no blocking conflict and changes no outcome', () => {
|
|
const active = listing({ primaryIsin: ISIN, mic: 'XNAS', tickerRoot: 'ACTV', listedTo: null });
|
|
const dropped = listing({
|
|
primaryIsin: ISIN,
|
|
mic: 'OTCM',
|
|
tickerRoot: 'ACTVU',
|
|
listedFrom: null,
|
|
listedTo: '2024-13-45',
|
|
});
|
|
|
|
const withDropped = run([active, dropped]);
|
|
const without = run([active]);
|
|
|
|
expect(kinds(withDropped)).toEqual(['invalid_listing_dates']);
|
|
expect(withDropped.listingOutcomes).toEqual(without.listingOutcomes);
|
|
expectReconciles(withDropped, 2);
|
|
});
|
|
|
|
test('blocking conflict keys are fresh clones, not shared with the duplicate_listing_key candidate or each other', () => {
|
|
const listings = [
|
|
...primaryAndOtc(ISIN, 'CLN'),
|
|
listing({ primaryIsin: ISIN, mic: 'OTCM', tickerRoot: 'CLNU', listedFrom: null }),
|
|
listing({ primaryIsin: ISIN, mic: 'OTCM', tickerRoot: 'CLNU', listedFrom: null }),
|
|
];
|
|
|
|
const result = run(listings);
|
|
|
|
expect(kinds(result)).toEqual(['duplicate_listing_key', 'missing_otc_listed_from']);
|
|
const dupKey = result.conflicts[0]?.detail.candidateListingKeys[0] as ListingNaturalKey;
|
|
const blocking = result.conflicts[1]?.detail;
|
|
const toKey = blocking?.toListingKeys[0] as ListingNaturalKey;
|
|
const candidateOtcKey = blocking?.candidateListingKeys.find(
|
|
k => k.tickerRoot === 'CLNU'
|
|
) as ListingNaturalKey;
|
|
expect(toKey).toEqual(dupKey);
|
|
expect(toKey).not.toBe(dupKey);
|
|
expect(candidateOtcKey).toEqual(toKey);
|
|
expect(candidateOtcKey).not.toBe(toKey);
|
|
|
|
// Mutating an emitted key must not leak into a later run's output.
|
|
toKey.tickerRoot = 'MUTATED';
|
|
const rerun = run(listings);
|
|
expect(rerun.conflicts[1]?.detail.toListingKeys[0]?.tickerRoot).toBe('CLNU');
|
|
});
|
|
});
|
|
|
|
describe('T-67 QA: malformed listing slots are quarantined without disturbing other passes', () => {
|
|
test('a malformed slot between two copies of a duplicate key does not change duplicate detection', () => {
|
|
const dupA = listing({ primaryIsin: ISIN, mic: 'XNAS', tickerRoot: 'DUP', listedTo: null });
|
|
const dupB = listing({ primaryIsin: ISIN, mic: 'XNAS', tickerRoot: 'DUP', listedTo: null });
|
|
const listings = [dupA, null, dupB] as unknown as StageBListingCandidate[];
|
|
|
|
const result = run(listings);
|
|
|
|
// Conflicts are emitted sorted by conflictKind, not input order.
|
|
expect(kinds(result)).toEqual(['duplicate_listing_key', 'malformed_listing_entry']);
|
|
expect(result.stats.skippedMalformedEntries).toBe(1);
|
|
expect(result.stats.skippedDuplicateKeys).toBe(2);
|
|
expect(result.stats.processedListings).toBe(0);
|
|
expectReconciles(result, 3);
|
|
});
|
|
|
|
test('an all-malformed batch returns one conflict per slot in index order and zero outcomes', () => {
|
|
const listings = new Array<StageBListingCandidate>(2);
|
|
listings.push(
|
|
null as unknown as StageBListingCandidate,
|
|
true as unknown as StageBListingCandidate,
|
|
Symbol('x') as unknown as StageBListingCandidate,
|
|
(() => undefined) as unknown as StageBListingCandidate
|
|
);
|
|
|
|
const result = run(listings);
|
|
|
|
expect(result.continuities).toEqual([]);
|
|
expect(result.listingOutcomes).toEqual([]);
|
|
expect(result.stats.skippedMalformedEntries).toBe(6);
|
|
expect(result.conflicts.map(c => c.detail.reason)).toEqual([
|
|
expect.stringMatching(/input\.listings\[0\].*sparse slot/),
|
|
expect.stringMatching(/input\.listings\[1\].*sparse slot/),
|
|
expect.stringMatching(/input\.listings\[2\].*null/),
|
|
expect.stringMatching(/input\.listings\[3\].*boolean/),
|
|
expect.stringMatching(/input\.listings\[4\].*symbol/),
|
|
expect.stringMatching(/input\.listings\[5\].*function/),
|
|
]);
|
|
expectReconciles(result, 6);
|
|
});
|
|
|
|
test('malformed slots do not alter T-71 blocking for valid rows in the same batch', () => {
|
|
const valid = [
|
|
...primaryAndOtc(ISIN, 'MAL'),
|
|
listing({ primaryIsin: ISIN, mic: 'OTCM', tickerRoot: 'MALU', listedFrom: null }),
|
|
listing({ primaryIsin: ISIN, mic: 'OTCM', tickerRoot: 'MALU', listedFrom: null }),
|
|
];
|
|
const withMalformed = [undefined, ...valid, 7] as unknown as StageBListingCandidate[];
|
|
|
|
const clean = run(valid);
|
|
const dirty = run(withMalformed);
|
|
|
|
expect(dirty.continuities).toEqual(clean.continuities);
|
|
expect(dirty.listingOutcomes).toEqual(clean.listingOutcomes);
|
|
expect(dirty.conflicts.filter(c => c.conflictKind !== 'malformed_listing_entry')).toEqual(
|
|
clean.conflicts
|
|
);
|
|
expectReconciles(dirty, withMalformed.length);
|
|
});
|
|
|
|
test('top-level container and options validation still throws', () => {
|
|
const opts = { asOfDate: AS_OF };
|
|
expect(() => linkDelistingChains(null as never, opts)).toThrow(TypeError);
|
|
expect(() =>
|
|
linkDelistingChains({ listings: null as never, otcMics: DEFAULT_OTC_MICS }, opts)
|
|
).toThrow(TypeError);
|
|
expect(() => linkDelistingChains({ listings: [], otcMics: ['OTCM'] as never }, opts)).toThrow(
|
|
TypeError
|
|
);
|
|
expect(() =>
|
|
linkDelistingChains({ listings: [], otcMics: DEFAULT_OTC_MICS }, { asOfDate: '2025-02-30' })
|
|
).toThrow(RangeError);
|
|
});
|
|
});
|