feat(research): prepare stored data by declared sessions (RP03)

This commit is contained in:
Boki 2026-10-27 01:07:45 -04:00
parent 302ba60e53
commit d2b78eacdf
10 changed files with 805 additions and 72 deletions

View file

@ -119,7 +119,7 @@ function upload(value: unknown = datasetInput(), name = 'source.json', read?: Pr
Object.defineProperty(file, 'text', { value: () => read });
}
const label =
field('Dataset input').value === 'stored-eod' ? 'Source metadata JSON' : 'Source dataset JSON';
field('Dataset input').value !== 'export' ? 'Source metadata JSON' : 'Source dataset JSON';
Object.defineProperty(field(label), 'files', {
configurable: true,
value: [file],
@ -1326,3 +1326,168 @@ describe('stored source panel', () => {
expect(host.querySelector('[aria-label="Prepared dataset metadata"]')).toBeNull();
});
});
function declaredSessionFixture(): CanonicalStoredSourcePreparation {
const offset = 86400000;
const metadata: HistoricalDatasetMetadataV2 = {
...storedMetadata,
bounds: { ...storedMetadata.bounds, tradeEnd: storedMetadata.bounds.tradeEnd + offset },
calendars: storedMetadata.calendars.map(calendar => ({
...calendar,
endDate: '2024-03-13',
sessions: [
...calendar.sessions,
...calendar.sessions.map(session => ({
date: '2024-03-12',
open: session.open + offset,
close: session.close + offset,
})),
],
})),
corporateActions: storedMetadata.corporateActions.map(coverage => ({
...coverage,
end: coverage.end + offset,
})),
};
const input = {
metadata,
bars: [
...stored.input.bars,
...stored.input.bars.map(bar => ({ ...bar, timestamp: Number(bar.timestamp) + offset })),
].map((bar, sourceSequence) => ({ ...bar, sourceSequence })),
};
const normalized = createDatasetV2(input);
return {
input,
selection: 'declared-sessions',
preparation: {
...stored.preparation,
hash: normalized.manifest.hash,
manifest: normalized.manifest,
},
};
}
describe('declared-session stored preparation contract and panel', () => {
test('requires the exact requested selection echo while keeping the default response unchanged', () => {
const sessions = declaredSessionFixture();
expect(
parseStoredDatasetPreparation(sessions, sessions.input.metadata, 'declared-sessions')
).toEqual(sessions);
expect(parseStoredDatasetPreparation(stored, storedMetadata)).toEqual(stored);
for (const selection of [undefined, null, 'continuous', false]) {
expect(() =>
parseStoredDatasetPreparation(
{ ...sessions, selection },
sessions.input.metadata,
'declared-sessions'
)
).toThrow();
}
expect(() => parseStoredDatasetPreparation(sessions, sessions.input.metadata)).toThrow();
});
test('shows declared UTC windows, captures explicit session mode and retries retention without rereading', async () => {
const sessions = declaredSessionFixture();
await mountPanel();
await flush(() => edit('Dataset input', 'stored-eod-sessions'));
expect(requests).toHaveLength(0);
expect(host.textContent).toContain('does not verify the calendar');
await flush(() => upload(sessions.input.metadata, 'sessions.json'));
const windows = host.querySelector('[aria-label="Declared UTC query windows"]');
expect(windows?.textContent).toContain('2024-03-11T13:30:00.000Z');
expect(windows?.textContent).toContain('2024-03-11T20:00:00.000Z');
expect(windows?.textContent).toContain('2024-03-12T13:30:00.000Z');
expect(windows?.textContent).toContain('2024-03-12T20:00:00.000Z');
expect(requests).toHaveLength(0);
await flush(() => click('Preview dataset'));
expect(request(storedPath).body).toEqual({
metadata: sessions.input.metadata,
selection: 'declared-sessions',
});
await flush(() => reply(request(storedPath), sessions));
expect(host.textContent).toContain('Declared sessions within bounds');
await flush(() => click('Retain dataset'));
const first = request(retainPath);
expect(first.body).toEqual({ input: sessions.input, expectedHash: sessions.preparation.hash });
await flush(() => fail(first));
expect(field('Dataset input').disabled).toBe(true);
expect(field('Source metadata JSON').disabled).toBe(true);
await flush(() => edit('Dataset input', 'stored-eod'));
expect(field('Dataset input').value).toBe('stored-eod-sessions');
await flush(() => click('Retry retaining same dataset'));
expect(request(retainPath).body).toEqual(first.body);
expect(requests.filter(value => value.path === storedPath)).toHaveLength(1);
await flush(() => reply(request(retainPath), { preparation: sessions.preparation }));
expect(retained).toEqual([{ hash: sessions.preparation.hash, available: true }]);
});
test('missing or wrong echoed mode fails closed and preserves metadata for retry', async () => {
const sessions = declaredSessionFixture();
await mountPanel();
await flush(() => edit('Dataset input', 'stored-eod-sessions'));
await flush(() => upload(sessions.input.metadata));
for (const selection of [undefined, 'continuous']) {
await flush(() => click('Preview dataset'));
await flush(() => reply(request(storedPath), { ...sessions, selection }));
expect(button('Retain dataset').disabled).toBe(true);
expect(host.querySelector('[role="alert"]')?.textContent).toContain('could not be verified');
expect(host.textContent).toContain('Selected metadata: source.json');
expect(button('Preview dataset').disabled).toBe(false);
}
});
test('switching between stored modes cancels the old preview and late replies cannot restore it', async () => {
const sessions = declaredSessionFixture();
await mountPanel();
await flush(() => edit('Dataset input', 'stored-eod-sessions'));
await flush(() => upload(sessions.input.metadata));
await flush(() => click('Preview dataset'));
const old = request(storedPath);
await flush(() => edit('Dataset input', 'stored-eod'));
expect(old.signal?.aborted).toBe(true);
await flush(() => reply(old, sessions));
expect(host.querySelector('[aria-label="Prepared dataset metadata"]')).toBeNull();
expect(host.querySelector('[aria-label="Declared session selection"]')).toBeNull();
expect(button('Retain dataset').disabled).toBe(true);
await flush(() => upload(storedMetadata));
await flush(() => click('Preview dataset'));
expect(request(storedPath).body).toEqual({ metadata: storedMetadata });
await flush(() => reply(request(storedPath), stored));
expect(button('Retain dataset').disabled).toBe(false);
});
test('clipped windows and an empty intersection are displayed without starting a source read', async () => {
const sessions = declaredSessionFixture();
const metadata = sessions.input.metadata;
await mountPanel();
await flush(() => edit('Dataset input', 'stored-eod-sessions'));
await flush(() =>
upload({
...metadata,
bounds: {
...metadata.bounds,
warmupStart: metadata.bounds.warmupStart + 300000,
tradeStart: metadata.bounds.warmupStart + 300000,
tradeEnd: metadata.bounds.tradeEnd - 300000,
},
})
);
const windows = host.querySelector('[aria-label="Declared UTC query windows"]');
expect(windows?.textContent).toContain('2024-03-11T13:35:00.000Z');
expect(windows?.textContent).toContain('2024-03-12T19:55:00.000Z');
await flush(() =>
upload({
...metadata,
bounds: {
warmupStart: Date.parse('2024-03-11T20:00:00Z'),
tradeStart: Date.parse('2024-03-11T21:00:00Z'),
tradeEnd: Date.parse('2024-03-12T13:30:00Z'),
},
})
);
expect(host.textContent).toContain('No declared sessions intersect the requested bounds');
expect(requests).toHaveLength(0);
expect(button('Retain dataset').disabled).toBe(true);
});
});