T-88: M1.1m — Security-master run composer: readers output + bar spans → Stage A → Stage B → Pass 6 → Pass 4 → match report + persist input (pure, in-memory, no new dependencies)
This commit is contained in:
parent
33b1d23f84
commit
3bdf7a8da6
9 changed files with 2722 additions and 6 deletions
|
|
@ -10,6 +10,8 @@ import {
|
|||
stableJsonStringify,
|
||||
type DelistingChainResult,
|
||||
type PersistSecurityMasterInput,
|
||||
type PriceCrossCheckConflictRecord,
|
||||
type PriceCrossCheckResult,
|
||||
type SecurityMasterQueryable,
|
||||
type SecurityMasterQueryResult,
|
||||
type SecurityType,
|
||||
|
|
@ -801,4 +803,151 @@ describe('persistSecurityMaster unit tests', () => {
|
|||
expect(result.providerMappingsUpdated).toBe(1); // eod (existing+overridden)
|
||||
expect(result.providerMappingsUnchanged).toBe(2); // qm AAPL:US + OVR_SAME
|
||||
});
|
||||
|
||||
test('pass4 omitted leaves conflict SQL and counts identical to non-Pass-4 behavior', async () => {
|
||||
const stageA = emptyStageA();
|
||||
stageA.conflicts.push({
|
||||
source: 'eod',
|
||||
sourceId: 'BAD.US',
|
||||
conflictKind: 'isin_cusip_mismatch',
|
||||
detail: {
|
||||
isin: 'US0378331005',
|
||||
cusip: '594918104',
|
||||
country: 'US',
|
||||
derivedIsin: 'US5949181045',
|
||||
},
|
||||
});
|
||||
const stageB = emptyStageB();
|
||||
|
||||
const { client, queries } = createMockClient();
|
||||
const result = await persistSecurityMaster(client, {
|
||||
stageA,
|
||||
stageB,
|
||||
securityTypeByIsin: new Map<string, SecurityType>(),
|
||||
});
|
||||
|
||||
expect(result.conflictsInserted).toBe(1);
|
||||
expect(result.conflictsUnchanged).toBe(0);
|
||||
expect(result.conflictsDeduplicated).toBe(0);
|
||||
|
||||
// Must run ONLY the 3-column non-Pass-4 existence query, never the Pass 4 JSONB detail query.
|
||||
const conflictSelects = queries.filter(q => q.text.includes('FROM resolution_conflict rc'));
|
||||
expect(conflictSelects.length).toBe(1);
|
||||
expect(conflictSelects[0]?.text).not.toContain('rc.detail = u.detail');
|
||||
});
|
||||
|
||||
test('pass4 supplied preserves distinct pairs sharing a left endpoint, deduplicates exact duplicates, and checks rc.detail = u.detail', async () => {
|
||||
const stageA = emptyStageA();
|
||||
const stageB = emptyStageB();
|
||||
|
||||
const conflictPair1: PriceCrossCheckConflictRecord = {
|
||||
source: 'eod',
|
||||
sourceId: 'AAPL.US',
|
||||
conflictKind: 'price_ratio_mismatch',
|
||||
detail: {
|
||||
left: { source: 'eod', sourceId: 'AAPL.US', currency: 'USD' },
|
||||
right: { source: 'qm', sourceId: 'AAPL:US', currency: 'USD' },
|
||||
verdict: 'ratio_mismatch',
|
||||
usableDates: 5,
|
||||
medianRatio: 1.5,
|
||||
priceCorr: 0.99,
|
||||
ratioTolerance: 0.02,
|
||||
minCorrelation: 0.9,
|
||||
},
|
||||
};
|
||||
const conflictPair2SameLeft: PriceCrossCheckConflictRecord = {
|
||||
source: 'eod',
|
||||
sourceId: 'AAPL.US',
|
||||
conflictKind: 'price_ratio_mismatch',
|
||||
detail: {
|
||||
left: { source: 'eod', sourceId: 'AAPL.US', currency: 'USD' },
|
||||
right: { source: 'ceo', sourceId: 'AAPL', currency: 'USD' },
|
||||
verdict: 'ratio_mismatch',
|
||||
usableDates: 5,
|
||||
medianRatio: 2.0,
|
||||
priceCorr: 0.98,
|
||||
ratioTolerance: 0.02,
|
||||
minCorrelation: 0.9,
|
||||
},
|
||||
};
|
||||
|
||||
const pass4: PriceCrossCheckResult = {
|
||||
results: [],
|
||||
conflicts: [conflictPair1, conflictPair2SameLeft, conflictPair1], // 1 exact duplicate
|
||||
stats: {
|
||||
pairsIn: 2,
|
||||
invalidPairs: 0,
|
||||
byVerdict: {
|
||||
unsupported_currency_pair: 0,
|
||||
insufficient_data: 0,
|
||||
ratio_mismatch: 2,
|
||||
low_correlation: 0,
|
||||
confirmed: 0,
|
||||
},
|
||||
conflictsEmitted: 3,
|
||||
droppedInvalidCloses: 0,
|
||||
droppedConflictingCloses: 0,
|
||||
droppedInvalidFxRates: 0,
|
||||
droppedConflictingFxRates: 0,
|
||||
skippedMissingFxDates: 0,
|
||||
},
|
||||
};
|
||||
|
||||
// First run: neither pair exists in DB -> both inserted, 1 exact duplicate deduplicated.
|
||||
const first = createMockClient();
|
||||
const firstResult = await persistSecurityMaster(first.client, {
|
||||
stageA,
|
||||
stageB,
|
||||
pass4,
|
||||
securityTypeByIsin: new Map<string, SecurityType>(),
|
||||
});
|
||||
|
||||
expect(firstResult.conflictsInserted).toBe(2);
|
||||
expect(firstResult.conflictsUnchanged).toBe(0);
|
||||
expect(firstResult.conflictsDeduplicated).toBe(1);
|
||||
|
||||
const pass4Selects = first.queries.filter(
|
||||
q => q.text.includes('FROM resolution_conflict rc') && q.text.includes('rc.detail = u.detail')
|
||||
);
|
||||
expect(pass4Selects.length).toBe(1);
|
||||
const conflictInserts = first.queries.filter(q =>
|
||||
q.text.startsWith('INSERT INTO resolution_conflict')
|
||||
);
|
||||
expect(conflictInserts.length).toBe(1);
|
||||
expect((conflictInserts[0]?.values?.[0] as string[]).length).toBe(2);
|
||||
|
||||
// Second run: simulate pair 1 already existing in DB via its conflict_key -> only pair 2 inserted.
|
||||
const existingKey =
|
||||
'eod\u0000AAPL.US\u0000price_ratio_mismatch\u0000qm\u0000AAPL:US';
|
||||
const second = createMockClient(text => {
|
||||
if (text.includes('FROM resolution_conflict rc') && text.includes('rc.detail = u.detail')) {
|
||||
return { rows: [{ conflict_key: existingKey }] };
|
||||
}
|
||||
return { rows: [] };
|
||||
});
|
||||
|
||||
const secondResult = await persistSecurityMaster(second.client, {
|
||||
stageA,
|
||||
stageB,
|
||||
pass4,
|
||||
securityTypeByIsin: new Map<string, SecurityType>(),
|
||||
});
|
||||
|
||||
expect(secondResult.conflictsUnchanged).toBe(1);
|
||||
expect(secondResult.conflictsInserted).toBe(1);
|
||||
expect(secondResult.conflictsDeduplicated).toBe(1);
|
||||
});
|
||||
|
||||
test('rejects invalid pass4.conflicts shape before BEGIN', async () => {
|
||||
const { client, queries } = createMockClient();
|
||||
await expect(
|
||||
persistSecurityMaster(client, {
|
||||
stageA: emptyStageA(),
|
||||
stageB: emptyStageB(),
|
||||
pass4: { conflicts: 'bad' } as unknown as PriceCrossCheckResult,
|
||||
securityTypeByIsin: new Map<string, SecurityType>(),
|
||||
})
|
||||
).rejects.toThrow(TypeError);
|
||||
expect(queries.length).toBe(0);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue