T-105: tests
This commit is contained in:
parent
2c9d26b1ca
commit
c9fd52cbbc
1 changed files with 310 additions and 0 deletions
|
|
@ -2327,3 +2327,313 @@ describe('runSecurityMaster Pass 4 wiring', () => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('runSecurityMaster Pass 4 wiring (QA edge cases)', () => {
|
||||||
|
test('read runs but returns no closes: pass4Wired=true, composed.pass4 null, artifacts still written', async () => {
|
||||||
|
const base = buildUsCaFixture();
|
||||||
|
const fixture: FakeMongoFixture = {
|
||||||
|
...base,
|
||||||
|
collections: { ...base.collections, eodPrices: [], qmPrices: [] },
|
||||||
|
};
|
||||||
|
const fakeMongo = createStrictReadOnlyFakeMongo(fixture);
|
||||||
|
const outDir = createTempDir('sm-pass4-empty-');
|
||||||
|
const stdoutChunks: string[] = [];
|
||||||
|
|
||||||
|
const result = await runSecurityMaster(
|
||||||
|
{
|
||||||
|
mongoCollectionClient: fakeMongo.mongoCollectionClient,
|
||||||
|
barSpanReader: fakeMongo.barSpanReader,
|
||||||
|
writeStdout: text => stdoutChunks.push(text),
|
||||||
|
},
|
||||||
|
{ asOfDate: '2026-01-15', outDir }
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(fakeMongo.findCalls.filter(c => c === 'eodPrices' || c === 'qmPrices')).toHaveLength(3);
|
||||||
|
expect(result.sampleCloses).not.toBeNull();
|
||||||
|
expect(result.sampleCloses?.closes).toHaveLength(0);
|
||||||
|
expect(result.composed.pass4).toBeNull();
|
||||||
|
expect(result.pass4.skippedReason).toBeNull();
|
||||||
|
expect(result.pass4.byVerdict).toBeNull();
|
||||||
|
expect(result.pass4.sampleDates).toHaveLength(DEFAULT_SAMPLE_DATE_COUNT);
|
||||||
|
|
||||||
|
const stats = JSON.parse(
|
||||||
|
readFileSync(join(outDir, SECURITY_MASTER_OUTPUT_FILES.runStatsJson), 'utf8')
|
||||||
|
) as {
|
||||||
|
pass4Wired: boolean;
|
||||||
|
pass4: {
|
||||||
|
byVerdict: unknown;
|
||||||
|
skippedReason: unknown;
|
||||||
|
stats: { eod: { emitted: number }; qm: { emitted: number }; fx: { emitted: number } };
|
||||||
|
};
|
||||||
|
};
|
||||||
|
expect(stats.pass4Wired).toBe(true);
|
||||||
|
expect(stats.pass4.byVerdict).toBeNull();
|
||||||
|
expect(stats.pass4.skippedReason).toBeNull();
|
||||||
|
expect(stats.pass4.stats.eod.emitted).toBe(0);
|
||||||
|
expect(stats.pass4.stats.qm.emitted).toBe(0);
|
||||||
|
expect(stats.pass4.stats.fx.emitted).toBe(0);
|
||||||
|
|
||||||
|
const conflictsCsv = readFileSync(join(outDir, SECURITY_MASTER_OUTPUT_FILES.conflictsCsv), 'utf8');
|
||||||
|
expect(conflictsCsv).not.toContain('price_ratio_mismatch');
|
||||||
|
const summary = stdoutChunks.join('');
|
||||||
|
expect(summary).toContain('Pass 4 price cross-check: skipped');
|
||||||
|
expect(summary).toContain(`dates=${DEFAULT_SAMPLE_DATE_COUNT}`);
|
||||||
|
expect(summary).toContain('confirmed=0');
|
||||||
|
expect(summary).not.toContain('not yet wired');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('--persist with Pass 4 active persists the second (price-checked) compose result', async () => {
|
||||||
|
const fakeMongo = createStrictReadOnlyFakeMongo(buildUsCaFixture({ qmPriceMultiplier: 2 }));
|
||||||
|
const composeResults: ReturnType<typeof composeSecurityMasterRun>[] = [];
|
||||||
|
const persistInputs: PersistSecurityMasterInput[] = [];
|
||||||
|
|
||||||
|
const result = await runSecurityMaster(
|
||||||
|
{
|
||||||
|
mongoCollectionClient: fakeMongo.mongoCollectionClient,
|
||||||
|
barSpanReader: fakeMongo.barSpanReader,
|
||||||
|
assertSafety: () => {},
|
||||||
|
createPostgresClient: (): SecurityMasterPostgresHandle => ({
|
||||||
|
queryable: {
|
||||||
|
async query() {
|
||||||
|
return { rows: [] };
|
||||||
|
},
|
||||||
|
},
|
||||||
|
close: () => {},
|
||||||
|
}),
|
||||||
|
persistFn: async (_queryable, input) => {
|
||||||
|
persistInputs.push(input);
|
||||||
|
return makeFakePersistResult();
|
||||||
|
},
|
||||||
|
composeFn: (input, options) => {
|
||||||
|
const composedResult = composeSecurityMasterRun(input, options);
|
||||||
|
composeResults.push(composedResult);
|
||||||
|
return composedResult;
|
||||||
|
},
|
||||||
|
writeStdout: () => {},
|
||||||
|
},
|
||||||
|
{ asOfDate: '2026-01-15', outDir: createTempDir('sm-pass4-persist-'), persist: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(composeResults).toHaveLength(2);
|
||||||
|
expect(composeResults[0]?.pass4).toBeNull();
|
||||||
|
expect(composeResults[1]?.pass4?.stats.byVerdict.ratio_mismatch).toBe(1);
|
||||||
|
expect(persistInputs).toHaveLength(1);
|
||||||
|
expect(persistInputs[0] === composeResults[1]?.persistInput).toBe(true);
|
||||||
|
expect(result.composed === composeResults[1]).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('second compose failure rejects at compose after the read, writes nothing, closes Mongo', async () => {
|
||||||
|
const fakeMongo = createStrictReadOnlyFakeMongo(buildUsCaFixture());
|
||||||
|
const writes: string[] = [];
|
||||||
|
const stdout: string[] = [];
|
||||||
|
let composeCalls = 0;
|
||||||
|
let readCalls = 0;
|
||||||
|
let mongoClosed = 0;
|
||||||
|
let caught: unknown = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await runSecurityMaster(
|
||||||
|
{
|
||||||
|
mongoCollectionClient: fakeMongo.mongoCollectionClient,
|
||||||
|
barSpanReader: fakeMongo.barSpanReader,
|
||||||
|
closeMongo: () => {
|
||||||
|
mongoClosed += 1;
|
||||||
|
},
|
||||||
|
readSampleClosesFn: async (reader, options) => {
|
||||||
|
readCalls += 1;
|
||||||
|
return readSampleCloses(reader, options);
|
||||||
|
},
|
||||||
|
composeFn: (input, options) => {
|
||||||
|
composeCalls += 1;
|
||||||
|
if (composeCalls === 2) {
|
||||||
|
throw new Error('simulated second compose failure');
|
||||||
|
}
|
||||||
|
return composeSecurityMasterRun(input, options);
|
||||||
|
},
|
||||||
|
writeFile: filePath => {
|
||||||
|
writes.push(filePath);
|
||||||
|
},
|
||||||
|
writeStdout: text => stdout.push(text),
|
||||||
|
},
|
||||||
|
{ asOfDate: '2026-01-15', outDir: '/virtual/out' }
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
caught = error;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(caught).toBeInstanceOf(Error);
|
||||||
|
expect((caught as Error).message).toContain('compose');
|
||||||
|
expect((caught as Error).message).not.toContain('read-sample-closes');
|
||||||
|
expect(readCalls).toBe(1);
|
||||||
|
expect(composeCalls).toBe(2);
|
||||||
|
expect(writes).toHaveLength(0);
|
||||||
|
expect(stdout).toHaveLength(0);
|
||||||
|
expect(mongoClosed).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('batchSize is forwarded to sample-close finds only when provided', async () => {
|
||||||
|
const withBatch = createStrictReadOnlyFakeMongo(buildUsCaFixture());
|
||||||
|
const capturedWith: ReadSampleClosesOptions[] = [];
|
||||||
|
await runSecurityMaster(
|
||||||
|
{
|
||||||
|
mongoCollectionClient: withBatch.mongoCollectionClient,
|
||||||
|
barSpanReader: withBatch.barSpanReader,
|
||||||
|
readSampleClosesFn: async (reader, options) => {
|
||||||
|
capturedWith.push(options);
|
||||||
|
return readSampleCloses(reader, options);
|
||||||
|
},
|
||||||
|
writeStdout: () => {},
|
||||||
|
},
|
||||||
|
{ asOfDate: '2026-01-15', outDir: createTempDir(), batchSize: 7 }
|
||||||
|
);
|
||||||
|
expect(capturedWith[0]?.batchSize).toBe(7);
|
||||||
|
const priceFindsWith = withBatch.findCallDetails.filter(
|
||||||
|
c => c.collectionName === 'eodPrices' || c.collectionName === 'qmPrices'
|
||||||
|
);
|
||||||
|
expect(priceFindsWith).toHaveLength(3);
|
||||||
|
for (const call of priceFindsWith) {
|
||||||
|
expect(call.options?.batchSize).toBe(7);
|
||||||
|
}
|
||||||
|
|
||||||
|
const withoutBatch = createStrictReadOnlyFakeMongo(buildUsCaFixture());
|
||||||
|
const capturedWithout: ReadSampleClosesOptions[] = [];
|
||||||
|
await runSecurityMaster(
|
||||||
|
{
|
||||||
|
mongoCollectionClient: withoutBatch.mongoCollectionClient,
|
||||||
|
barSpanReader: withoutBatch.barSpanReader,
|
||||||
|
readSampleClosesFn: async (reader, options) => {
|
||||||
|
capturedWithout.push(options);
|
||||||
|
return readSampleCloses(reader, options);
|
||||||
|
},
|
||||||
|
writeStdout: () => {},
|
||||||
|
},
|
||||||
|
{ asOfDate: '2026-01-15', outDir: createTempDir() }
|
||||||
|
);
|
||||||
|
expect(capturedWithout).toHaveLength(1);
|
||||||
|
expect('batchSize' in (capturedWithout[0] ?? {})).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('programmatic pass4 rejects null and non-boolean enabled at validate-options before any Mongo read', async () => {
|
||||||
|
const badPass4Values: unknown[] = [null, 'on', { enabled: 'yes' }, { enabled: 1 }, { sampleDateCount: '10' }];
|
||||||
|
for (const badPass4 of badPass4Values) {
|
||||||
|
const fakeMongo = createStrictReadOnlyFakeMongo(buildUsCaFixture());
|
||||||
|
let mongoClosed = 0;
|
||||||
|
let caught: unknown = null;
|
||||||
|
try {
|
||||||
|
await runSecurityMaster(
|
||||||
|
{
|
||||||
|
mongoCollectionClient: fakeMongo.mongoCollectionClient,
|
||||||
|
barSpanReader: fakeMongo.barSpanReader,
|
||||||
|
closeMongo: () => {
|
||||||
|
mongoClosed += 1;
|
||||||
|
},
|
||||||
|
writeStdout: () => {},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
asOfDate: '2026-01-15',
|
||||||
|
outDir: createTempDir(),
|
||||||
|
pass4: badPass4 as { enabled?: boolean },
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
caught = error;
|
||||||
|
}
|
||||||
|
expect(caught).toBeInstanceOf(Error);
|
||||||
|
expect((caught as Error).message).toContain('validate-options');
|
||||||
|
expect(fakeMongo.findCalls).toHaveLength(0);
|
||||||
|
expect(fakeMongo.aggregateCalls).toHaveLength(0);
|
||||||
|
expect(mongoClosed).toBe(1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('selected sample dates are ascending, unique, Mon-Fri, inside the overlap window, and mirrored in run-stats.json', async () => {
|
||||||
|
const fakeMongo = createStrictReadOnlyFakeMongo(buildUsCaFixture());
|
||||||
|
const outDir = createTempDir('sm-pass4-dates-');
|
||||||
|
const result = await runSecurityMaster(
|
||||||
|
{
|
||||||
|
mongoCollectionClient: fakeMongo.mongoCollectionClient,
|
||||||
|
barSpanReader: fakeMongo.barSpanReader,
|
||||||
|
writeStdout: () => {},
|
||||||
|
},
|
||||||
|
{ asOfDate: '2026-01-15', outDir }
|
||||||
|
);
|
||||||
|
const dates = result.pass4.sampleDates;
|
||||||
|
expect(dates).toHaveLength(DEFAULT_SAMPLE_DATE_COUNT);
|
||||||
|
expect([...dates].sort(compareAscii)).toEqual([...dates]);
|
||||||
|
expect(new Set(dates).size).toBe(dates.length);
|
||||||
|
for (const date of dates) {
|
||||||
|
const dow = new Date(`${date}T00:00:00Z`).getUTCDay();
|
||||||
|
expect(dow).toBeGreaterThanOrEqual(1);
|
||||||
|
expect(dow).toBeLessThanOrEqual(5);
|
||||||
|
// Overlap of fixture spans: QM starts 2018-01-02, both end 2026-01-14.
|
||||||
|
expect(date >= '2018-01-02').toBe(true);
|
||||||
|
expect(date <= '2026-01-14').toBe(true);
|
||||||
|
}
|
||||||
|
const stats = JSON.parse(
|
||||||
|
readFileSync(join(outDir, SECURITY_MASTER_OUTPUT_FILES.runStatsJson), 'utf8')
|
||||||
|
) as { pass4: { sampleDates: string[] } };
|
||||||
|
expect(stats.pass4.sampleDates).toEqual([...dates]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('buildSampleDateCalendar (QA edge cases)', () => {
|
||||||
|
function twoSourceIndex(first: string, last: string): BarSpanIndex {
|
||||||
|
const index = new BarSpanIndex();
|
||||||
|
index.merge({ source: 'eod', sourceId: 'X.US', firstBarDate: first, lastBarDate: last, barCount: 5 });
|
||||||
|
index.merge({ source: 'qm', sourceId: 'X:US', firstBarDate: first, lastBarDate: last, barCount: 5 });
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
test('handles year boundaries and leap days (2000 and 2024)', () => {
|
||||||
|
expect(buildSampleDateCalendar({ index: twoSourceIndex('2023-12-28', '2024-01-02') }, '2024-01-02')).toEqual([
|
||||||
|
'2023-12-28',
|
||||||
|
'2023-12-29',
|
||||||
|
'2024-01-01',
|
||||||
|
'2024-01-02',
|
||||||
|
]);
|
||||||
|
expect(buildSampleDateCalendar({ index: twoSourceIndex('2024-02-28', '2024-03-01') }, '2024-03-01')).toEqual([
|
||||||
|
'2024-02-28',
|
||||||
|
'2024-02-29',
|
||||||
|
'2024-03-01',
|
||||||
|
]);
|
||||||
|
expect(buildSampleDateCalendar({ index: twoSourceIndex('2000-02-28', '2000-03-01') }, '2000-03-01')).toEqual([
|
||||||
|
'2000-02-28',
|
||||||
|
'2000-02-29',
|
||||||
|
'2000-03-01',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('matches a Date.getUTCDay weekday oracle over 2019-2026', () => {
|
||||||
|
const calendar = buildSampleDateCalendar({ index: twoSourceIndex('2019-01-01', '2026-12-31') }, '2026-12-31');
|
||||||
|
const expected: string[] = [];
|
||||||
|
for (
|
||||||
|
let ms = Date.UTC(2019, 0, 1);
|
||||||
|
ms <= Date.UTC(2026, 11, 31);
|
||||||
|
ms += 86_400_000
|
||||||
|
) {
|
||||||
|
const d = new Date(ms);
|
||||||
|
const dow = d.getUTCDay();
|
||||||
|
if (dow >= 1 && dow <= 5) {
|
||||||
|
expected.push(d.toISOString().slice(0, 10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect(calendar).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ignores zero-bar, inverted, and invalid-date spans when computing the window', () => {
|
||||||
|
const index = twoSourceIndex('2024-01-08', '2024-01-12');
|
||||||
|
index.merge({ source: 'eod', sourceId: 'Z.US', firstBarDate: '2020-01-01', lastBarDate: '2030-01-01', barCount: 0 });
|
||||||
|
index.merge({ source: 'qm', sourceId: 'Y:US', firstBarDate: '2024-02-30', lastBarDate: '2024-03-05', barCount: 3 });
|
||||||
|
expect(buildSampleDateCalendar({ index }, '2030-01-01')).toEqual([
|
||||||
|
'2024-01-08',
|
||||||
|
'2024-01-09',
|
||||||
|
'2024-01-10',
|
||||||
|
'2024-01-11',
|
||||||
|
'2024-01-12',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('asOfDate before the overlap start yields an empty calendar', () => {
|
||||||
|
expect(buildSampleDateCalendar({ index: twoSourceIndex('2024-01-08', '2024-01-12') }, '2024-01-05')).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue