fix(backtest): keep run history available while metrics load (RP12)
This commit is contained in:
parent
32f40ee753
commit
a050c71012
4 changed files with 312 additions and 53 deletions
|
|
@ -648,6 +648,164 @@ describe('canonical research workbench', () => {
|
|||
expect(host.textContent).not.toContain('Canonical');
|
||||
});
|
||||
|
||||
describe('nonblocking run history metrics', () => {
|
||||
const rows = () => [...host.querySelectorAll('tbody tr[data-index]')];
|
||||
const view = (runs: Run[], onSelectRun = (_id: string) => {}) =>
|
||||
createElement(
|
||||
VirtuosoMockContext.Provider,
|
||||
{ value: { viewportHeight: 1000, itemHeight: 40 } },
|
||||
createElement(RunsListWithMetrics, { runs, onSelectRun })
|
||||
);
|
||||
const update = (runs: Run[]) =>
|
||||
flush(() =>
|
||||
root!.render(
|
||||
createElement(
|
||||
router.MemoryRouter,
|
||||
{ initialEntries: ['/'] },
|
||||
view(runs),
|
||||
createElement(Probe)
|
||||
)
|
||||
)
|
||||
);
|
||||
const resultFor = (run: Run, totalReturn: number) => {
|
||||
const value = report();
|
||||
value.runId = run.id;
|
||||
value.backtestId = run.backtestId;
|
||||
value.analytics.metrics.totalReturn = totalReturn;
|
||||
return value;
|
||||
};
|
||||
|
||||
test('pending metrics leave every current row visible and selectable without submitting', async () => {
|
||||
const selected: string[] = [];
|
||||
const runs = [
|
||||
completedRun(),
|
||||
completedRun({ id: 'failed', runNumber: 2, status: 'failed' }),
|
||||
completedRun({ id: 'active', runNumber: 3, status: 'pending', progress: 0 }),
|
||||
];
|
||||
await mount(view(runs, id => selected.push(id)));
|
||||
expect(request('/api/v2/runs/run/results')).toBeTruthy();
|
||||
expect(rows()).toHaveLength(3);
|
||||
expect(rows()[0]!.textContent).toContain('Loading metrics...');
|
||||
expect(rows()[0]!.querySelectorAll('td')[3]!.textContent).toBe('-');
|
||||
expect(rows()[1]!.textContent).toContain('Failed');
|
||||
expect(rows()[2]!.textContent).toContain('Pending');
|
||||
await flush(() => (rows()[1] as HTMLElement).click());
|
||||
expect(selected).toEqual(['failed']);
|
||||
expect(requests).toHaveLength(1);
|
||||
expect(requests.every(item => item.method === 'GET')).toBe(true);
|
||||
});
|
||||
|
||||
test('status polls and another completion preserve pending reads and publish healthy metrics independently', async () => {
|
||||
const first = completedRun();
|
||||
const slow = completedRun({ id: 'slow', runNumber: 2 });
|
||||
const active = completedRun({ id: 'active', runNumber: 3, status: 'running', progress: 20 });
|
||||
await mount(view([first, slow, active]));
|
||||
const firstRead = request('/api/v2/runs/run/results');
|
||||
const slowRead = request('/api/v2/runs/slow/results');
|
||||
await update([{ ...first }, { ...slow }, { ...active, status: 'paused', progress: 40 }]);
|
||||
expect(requests).toHaveLength(2);
|
||||
expect(firstRead.signal?.aborted).toBe(false);
|
||||
expect(slowRead.signal?.aborted).toBe(false);
|
||||
expect(rows()[2]!.textContent).toContain('Paused');
|
||||
expect(rows()[2]!.textContent).toContain('40%');
|
||||
await flush(() => reply(firstRead, resultFor(first, 0.125)));
|
||||
expect(rows()[0]!.textContent).toContain('12.50%');
|
||||
expect(rows()[1]!.textContent).toContain('Loading metrics...');
|
||||
await update([first, slow, { ...active, status: 'completed', progress: 100 }]);
|
||||
expect(requests).toHaveLength(3);
|
||||
expect(request('/api/v2/runs/active/results')).toBeTruthy();
|
||||
expect(slowRead.signal?.aborted).toBe(false);
|
||||
expect(rows()[0]!.textContent).toContain('12.50%');
|
||||
await flush(() => reply(slowRead, resultFor(slow, 0.25)));
|
||||
expect(rows()[1]!.textContent).toContain('25.00%');
|
||||
expect(rows()[2]!.textContent).toContain('Loading metrics...');
|
||||
});
|
||||
|
||||
test('failed metrics stay unavailable until explicit row-local retry without navigation or duplicate reads', async () => {
|
||||
const selected: string[] = [];
|
||||
const first = completedRun();
|
||||
const healthy = completedRun({ id: 'healthy', runNumber: 2 });
|
||||
await mount(view([first, healthy], id => selected.push(id)));
|
||||
await flush(() => {
|
||||
reply(request('/api/v2/runs/run/results'), { error: 'Result store offline' }, 503);
|
||||
reply(request('/api/v2/runs/healthy/results'), resultFor(healthy, 0.25));
|
||||
});
|
||||
expect(rows()).toHaveLength(2);
|
||||
expect(rows()[0]!.textContent).toContain('Metrics unavailable');
|
||||
expect(rows()[0]!.querySelector('[title="Result store offline"]')).not.toBeNull();
|
||||
expect(rows()[0]!.querySelectorAll('td')[3]!.textContent).toBe('-');
|
||||
expect(rows()[1]!.textContent).toContain('25.00%');
|
||||
const retry = button('Retry metrics for run 1');
|
||||
await flush(() => {
|
||||
retry.click();
|
||||
retry.click();
|
||||
});
|
||||
expect(selected).toEqual([]);
|
||||
expect(requests).toHaveLength(3);
|
||||
expect(requests.filter(item => item.path === '/api/v2/runs/healthy/results')).toHaveLength(1);
|
||||
expect(rows()[0]!.textContent).toContain('Loading metrics...');
|
||||
await flush(() => reply(request('/api/v2/runs/run/results'), resultFor(first, 0.125)));
|
||||
expect(rows()[0]!.textContent).toContain('12.50%');
|
||||
expect(host.textContent).not.toContain('Metrics unavailable');
|
||||
expect(requests.every(item => item.method === 'GET')).toBe(true);
|
||||
});
|
||||
|
||||
test.each(['removed', 'parent', 'status'] as const)(
|
||||
'%s collection change fences late metrics and keeps current records authoritative',
|
||||
async change => {
|
||||
const original = completedRun();
|
||||
await mount(view([original]));
|
||||
const stale = request('/api/v2/runs/run/results');
|
||||
const current =
|
||||
change === 'removed'
|
||||
? []
|
||||
: [
|
||||
change === 'parent'
|
||||
? { ...original, backtestId: 'other-definition' }
|
||||
: { ...original, status: 'failed' as const },
|
||||
];
|
||||
await update(current);
|
||||
expect(stale.signal?.aborted).toBe(true);
|
||||
await flush(() => reply(stale, resultFor(original, 1.23)));
|
||||
expect(host.textContent).not.toContain('123.00%');
|
||||
expect(rows()).toHaveLength(current.length);
|
||||
if (change === 'removed') {
|
||||
expect(host.textContent).toContain('No runs yet');
|
||||
await update([original]);
|
||||
expect(rows()[0]!.textContent).toContain('Loading metrics...');
|
||||
await flush(() => reply(request('/api/v2/runs/run/results'), resultFor(original, 0.25)));
|
||||
expect(rows()[0]!.textContent).toContain('25.00%');
|
||||
} else if (change === 'parent') {
|
||||
expect(rows()[0]!.textContent).toContain('Loading metrics...');
|
||||
await flush(() =>
|
||||
reply(request('/api/v2/runs/run/results'), resultFor(current[0]!, 0.25))
|
||||
);
|
||||
expect(rows()[0]!.textContent).toContain('25.00%');
|
||||
} else {
|
||||
expect(rows()[0]!.textContent).toContain('Failed');
|
||||
expect(rows()[0]!.textContent).toContain('Not completed');
|
||||
expect(requests).toHaveLength(1);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
test('unmount aborts ownership and a late result cannot affect a remounted history', async () => {
|
||||
const original = completedRun();
|
||||
await mount(view([original]));
|
||||
const stale = request('/api/v2/runs/run/results');
|
||||
await flush(() => root!.unmount());
|
||||
root = undefined;
|
||||
expect(stale.signal?.aborted).toBe(true);
|
||||
await mount(view([original]));
|
||||
await flush(() => reply(stale, resultFor(original, 1.23)));
|
||||
expect(rows()).toHaveLength(1);
|
||||
expect(rows()[0]!.textContent).toContain('Loading metrics...');
|
||||
expect(host.textContent).not.toContain('123.00%');
|
||||
await flush(() => reply(request('/api/v2/runs/run/results'), resultFor(original, 0.25)));
|
||||
expect(rows()[0]!.textContent).toContain('25.00%');
|
||||
});
|
||||
});
|
||||
|
||||
test('history uses positive canonical drawdown severity and preserves signed legacy thresholds', async () => {
|
||||
const cases = [
|
||||
{ kind: 'canonical', value: 0, color: 'text-success' },
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue