feat(research): prepare stored data by declared sessions (RP03)
This commit is contained in:
parent
302ba60e53
commit
d2b78eacdf
10 changed files with 805 additions and 72 deletions
|
|
@ -60,7 +60,7 @@ function source() {
|
|||
}));
|
||||
return { metadata, rows };
|
||||
}
|
||||
async function fixture() {
|
||||
async function fixture(input = source(), scopedReads = false) {
|
||||
if (!url || !new URL(url).pathname.startsWith('/stock_bot_safety')) {
|
||||
throw new Error('Dedicated stock_bot_safety database required');
|
||||
}
|
||||
|
|
@ -116,12 +116,17 @@ async function fixture() {
|
|||
return pool.query(sql, values);
|
||||
},
|
||||
});
|
||||
const input = source();
|
||||
let reads = 0;
|
||||
const mongodb = {
|
||||
async find() {
|
||||
async find(_collection: string, filter: { timestamp: { $gte: number; $lt: number } }) {
|
||||
reads++;
|
||||
return structuredClone(input.rows);
|
||||
return structuredClone(
|
||||
scopedReads
|
||||
? input.rows.filter(
|
||||
row => row.timestamp >= filter.timestamp.$gte && row.timestamp < filter.timestamp.$lt
|
||||
)
|
||||
: input.rows
|
||||
);
|
||||
},
|
||||
} as unknown as MongoDBClient;
|
||||
const routes = new Hono().route('/', createDatasetRoutes({ postgres, mongodb, logger }));
|
||||
|
|
@ -175,6 +180,61 @@ async function fixture() {
|
|||
}
|
||||
|
||||
describe.skipIf(!url)('stored source materialization and immutable retention on PostgreSQL', () => {
|
||||
test('declared sessions retain captured rows across an invalid intervening close marker without further source reads', async () => {
|
||||
const input = source();
|
||||
const first = input.metadata.bounds.warmupStart;
|
||||
const day = 86_400_000;
|
||||
const close = first + 180_000;
|
||||
const originals = structuredClone(input.rows);
|
||||
input.metadata = {
|
||||
...input.metadata,
|
||||
calendars: input.metadata.calendars.map(calendar => ({
|
||||
...calendar,
|
||||
endDate: '2024-01-03',
|
||||
sessions: [
|
||||
{ date: '2024-01-01', open: first, close },
|
||||
{ date: '2024-01-02', open: first + day, close: close + day },
|
||||
],
|
||||
})),
|
||||
bounds: { ...input.metadata.bounds, tradeEnd: close + day },
|
||||
corporateActions: input.metadata.corporateActions.map(coverage => ({
|
||||
...coverage,
|
||||
end: close + day,
|
||||
})),
|
||||
};
|
||||
input.rows = [
|
||||
...originals,
|
||||
{ ...at(originals), timestamp: close / 1000, volume: -1 },
|
||||
...originals.map(row => ({ ...row, timestamp: row.timestamp + day / 1000 })),
|
||||
];
|
||||
const f = await fixture(input, true);
|
||||
const before = await f.snapshot();
|
||||
expect((await f.post('stored-source', { metadata: input.metadata })).status).toBe(500);
|
||||
const response = await f.post('stored-source', {
|
||||
metadata: input.metadata,
|
||||
selection: 'declared-sessions',
|
||||
});
|
||||
expect(response.status).toBe(200);
|
||||
const captured = (await response.json()) as CanonicalStoredSourcePreparation;
|
||||
expect(captured.selection).toBe('declared-sessions');
|
||||
expect(captured.input.bars).toHaveLength(6);
|
||||
expect(captured.input.bars.map(bar => bar.sourceSequence)).toEqual([0, 1, 2, 3, 4, 5]);
|
||||
expect(f.reads).toBe(3);
|
||||
expect(await f.snapshot()).toEqual(before);
|
||||
const expected = createDatasetV2(captured.input);
|
||||
const retainBody = { input: captured.input, expectedHash: expected.manifest.hash };
|
||||
input.rows.splice(0);
|
||||
for (let attempt = 0; attempt < 2; attempt++) {
|
||||
expect((await f.post('retain', retainBody)).status).toBe(200);
|
||||
}
|
||||
expect(f.reads).toBe(3);
|
||||
const rows = (await f.pool.query('SELECT payload FROM backtest_datasets')).rows;
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(validateDatasetV2(at(rows).payload)).toEqual(expected);
|
||||
const { backtest_datasets: _datasets, ...unchangedBefore } = before;
|
||||
expect(await f.snapshot(false)).toEqual(unchangedBefore);
|
||||
});
|
||||
|
||||
test('source read writes nothing; edits after capture cannot change exact retention/retry, and a fresh read has a different identity', async () => {
|
||||
const f = await fixture();
|
||||
const before = await f.snapshot(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue