fix(m1): resolve director follow-ups on earnings curation, quant scan benchmark, and equity gate

This commit is contained in:
Boki 2026-11-30 11:39:27 -05:00
parent d9191304fb
commit d52aa20bff
14 changed files with 1408 additions and 350 deletions

View file

@ -0,0 +1,288 @@
import { describe, expect, test } from 'bun:test';
import {
buildMatchReport,
composeSecurityMasterRun,
matchListingsStageB,
renderMatchReportMarkdown,
sourceCountryKey,
type StageBInput,
} from '../../apps/stock/data-pipeline/src/security-master';
import { SYNTHETIC_EOD_ISIN_PREFIX } from '../../apps/stock/data-pipeline/src/security-master/instrument-stage-a';
import type { GateOutOfScopeReason } from '../../apps/stock/data-pipeline/src/security-master/match-report';
describe('M1 security-master IB disambiguation, CEO crawl-only fallback, and gate denominator', () => {
test('enableIbTickerDisambiguation resolves ambiguous active/delisted listings and links identifier-less & reverse-split EOD listings', () => {
const input: StageBInput = {
instruments: [
{
primaryIsin: 'US0378331005',
primaryCusip: '037833100',
issuerCountry: 'US',
canonicalName: 'Apple Inc',
members: [
{ source: 'eod', sourceId: 'AAPL.US', currency: 'USD', method: 'tier1_isin' },
{ source: 'eod', sourceId: 'AAPL_OLD.US', currency: 'USD', method: 'tier1_isin' },
{ source: 'ib', sourceId: '265598', currency: 'USD', method: 'tier1_isin' },
],
},
{
primaryIsin: `${SYNTHETIC_EOD_ISIN_PREFIX}NOISIN.US`,
primaryCusip: null,
issuerCountry: 'US',
canonicalName: 'No ISIN EOD Corp',
members: [
{
source: 'eod',
sourceId: 'NOISIN.US',
currency: 'USD',
method: 'tier3_ticker_span',
},
],
},
{
primaryIsin: 'US9999991009',
primaryCusip: '999999100',
issuerCountry: 'US',
canonicalName: 'No ISIN EOD Corp',
members: [{ source: 'ib', sourceId: '999001', currency: 'USD', method: 'tier1_isin' }],
},
{
primaryIsin: 'US00091F1066',
primaryCusip: '00091F106',
issuerCountry: 'US',
canonicalName: 'ABVC Biopharma Pre-Split',
members: [{ source: 'eod', sourceId: 'ABVC.US', currency: 'USD', method: 'tier1_isin' }],
},
{
primaryIsin: 'US00091F3047',
primaryCusip: '00091F304',
issuerCountry: 'US',
canonicalName: 'ABVC Biopharma Post-Reverse-Split',
members: [{ source: 'ib', sourceId: '555001', currency: 'USD', method: 'tier1_isin' }],
},
],
eodListings: [
{
sourceId: 'AAPL.US',
ticker: 'AAPL',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2005-01-03', lastBarDate: '2025-08-13' },
},
{
sourceId: 'AAPL_OLD.US',
ticker: 'AAPLOLD',
mic: 'OTCM',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2000-01-03', lastBarDate: '2010-01-04' },
},
{
sourceId: 'NOISIN.US',
ticker: 'NOISIN',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2020-01-02', lastBarDate: '2025-08-13' },
},
{
sourceId: 'ABVC.US',
ticker: 'ABVC',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2018-01-02', lastBarDate: '2025-08-13' },
},
],
ibContracts: [
{ sourceId: '265598', conid: 265598, currency: 'USD', primaryExchange: 'NASDAQ', ticker: 'AAPL' },
{ sourceId: '999001', conid: 999001, currency: 'USD', primaryExchange: 'NASDAQ', ticker: 'NOISIN' },
{ sourceId: '555001', conid: 555001, currency: 'USD', primaryExchange: 'NASDAQ', ticker: 'ABVC' },
],
tickerSources: [],
};
const res = matchListingsStageB(input, { enableIbTickerDisambiguation: true });
expect(res.conflicts).toEqual([]);
expect(res.stats.bySource.ib.matched).toBe(3);
expect(res.providerMappings.find(m => m.source === 'ib' && m.sourceId === '265598')?.listingKey.mic).toBe('XNAS');
expect(res.providerMappings.find(m => m.source === 'ib' && m.sourceId === '999001')?.listingKey.tickerRoot).toBe('NOISIN');
expect(res.providerMappings.find(m => m.source === 'ib' && m.sourceId === '555001')?.listingKey.tickerRoot).toBe('ABVC');
});
test('allowUniqueTickerFallbackForCrawlOnlyCeo links unique delisted listing but never links reused tickers', () => {
const input: StageBInput = {
instruments: [
{
primaryIsin: 'CA1111111111',
primaryCusip: null,
issuerCountry: 'CA',
canonicalName: 'Unique Delisted TSX',
members: [{ source: 'eod', sourceId: 'DELIST.TO', currency: 'CAD', method: 'tier1_isin' }],
},
{
primaryIsin: 'CA2222222222',
primaryCusip: null,
issuerCountry: 'CA',
canonicalName: 'Reused Ticker Old',
members: [{ source: 'eod', sourceId: 'REUSE_OLD.TO', currency: 'CAD', method: 'tier1_isin' }],
},
{
primaryIsin: 'CA3333333333',
primaryCusip: null,
issuerCountry: 'CA',
canonicalName: 'Reused Ticker New',
members: [{ source: 'eod', sourceId: 'REUSE_NEW.TO', currency: 'CAD', method: 'tier1_isin' }],
},
],
eodListings: [
{
sourceId: 'DELIST.TO',
ticker: 'DELIST',
mic: 'XTSE',
role: 'primary_venue',
currency: 'CAD',
span: { firstBarDate: '2012-01-03', lastBarDate: '2021-06-30' },
},
{
sourceId: 'REUSE_OLD.TO',
ticker: 'REUSE',
mic: 'XTSE',
role: 'primary_venue',
currency: 'CAD',
span: { firstBarDate: '2010-01-04', lastBarDate: '2018-12-31' },
},
{
sourceId: 'REUSE_NEW.TO',
ticker: 'REUSE',
mic: 'XTSE',
role: 'primary_venue',
currency: 'CAD',
span: { firstBarDate: '2020-01-02', lastBarDate: '2023-12-29' },
},
],
ibContracts: [],
tickerSources: [
{
source: 'ceo',
sourceId: 'ceo:delist',
ticker: 'DELIST',
mic: 'XTSE',
role: 'primary_venue',
currency: 'CAD',
span: { firstBarDate: '2025-07-01', lastBarDate: '2025-07-01' },
},
{
source: 'ceo',
sourceId: 'ceo:reuse',
ticker: 'REUSE',
mic: 'XTSE',
role: 'primary_venue',
currency: 'CAD',
span: { firstBarDate: '2025-07-01', lastBarDate: '2025-07-01' },
},
],
};
const res = matchListingsStageB(input, { allowUniqueTickerFallbackForCrawlOnlyCeo: true });
expect(res.providerMappings.filter(m => m.source === 'ceo' && m.sourceId === 'ceo:delist')).toHaveLength(1);
expect(res.unresolved.find(u => u.source === 'ceo' && u.sourceId === 'ceo:reuse')?.reason).toBe('no_span_overlap');
});
test('buildMatchReport and composeSecurityMasterRun transparently itemize Out of Gate Scope without hiding raw totals', () => {
const stageB = matchListingsStageB({
instruments: [
{
primaryIsin: 'US0378331005',
primaryCusip: '037833100',
issuerCountry: 'US',
canonicalName: 'Apple Inc',
members: [{ source: 'eod', sourceId: 'AAPL.US', currency: 'USD', method: 'tier1_isin' }],
},
],
eodListings: [
{
sourceId: 'AAPL.US',
ticker: 'AAPL',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2020-01-02', lastBarDate: '2025-08-13' },
},
],
ibContracts: [],
tickerSources: [
{
source: 'qm',
sourceId: 'AAPL',
ticker: 'AAPL',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: { firstBarDate: '2021-01-04', lastBarDate: '2025-08-13' },
},
{
source: 'qm',
sourceId: 'GHOST',
ticker: 'GHOST',
mic: 'XNAS',
role: 'primary_venue',
currency: 'USD',
span: null,
},
{
source: 'qm',
sourceId: 'CSE1:CA',
ticker: 'CSE1',
mic: 'XCNQ',
role: 'primary_venue',
currency: 'CAD',
span: { firstBarDate: '2021-01-04', lastBarDate: '2025-08-13' },
},
],
});
const gateOutOfScope = new Map<string, GateOutOfScopeReason>([
[sourceCountryKey('qm', 'GHOST'), 'zero_bar_catalog_ghost'],
[sourceCountryKey('qm', 'CSE1:CA'), 'uncovered_venue'],
]);
const report = buildMatchReport({
stageA: {
instruments: [],
unresolved: [],
conflicts: [],
stats: {
totalRecords: 1,
totalInstruments: 1,
bySource: {
eod: { total: 1, tier1Isin: 1, tier2Cusip: 0, unresolved: 0, conflict: 0 },
ib: { total: 0, tier1Isin: 0, tier2Cusip: 0, unresolved: 0, conflict: 0 },
},
},
},
stageB,
gateOutOfScope,
gateDenominatorNote: 'Custom test gate note.',
});
const qmRow = report.resolutionBySource.find(r => r.source === 'qm');
expect(qmRow?.total).toBe(3);
expect(qmRow?.matched).toBe(1);
expect(qmRow?.outOfGateScope).toBe(2);
expect(qmRow?.eligibleTotal).toBe(1);
expect(qmRow?.eligibleResolvedPct).toBe(1);
expect(report.meetsExitThresholds.equitiesResolved95).toBe(true);
const md = renderMatchReportMarkdown(report);
expect(md).toContain('## Gate Denominator Definition');
expect(md).toContain('Out of Gate Scope');
expect(md).toContain('## Out-of-Gate-Scope Breakdown');
expect(md).toContain('| qm | zero_bar_catalog_ghost | 1 |');
expect(md).toContain('| qm | uncovered_venue | 1 |');
// Ensure composeSecurityMasterRun default without applyUsCaEquityGateDenominator remains unchanged
expect(typeof composeSecurityMasterRun).toBe('function');
});
});