refactor(api): validate shared product response contracts (CL25)

This commit is contained in:
Boki 2026-10-28 13:57:37 -04:00
parent e2292e4883
commit 366a6054bf
22 changed files with 989 additions and 451 deletions

View file

@ -83,7 +83,11 @@ function request(path: string, method = 'GET') {
}
return found;
}
function reply(pending: Request, body: unknown, status = 200) {
function reply(
pending: Request,
body: unknown,
status = pending.method === 'POST' && pending.path === '/api/v2/backtests' ? 201 : 200
) {
pending.settled = true;
pending.resolve(Response.json(body, { status }));
}
@ -729,7 +733,7 @@ describe('run selection and persisted status authority', () => {
await flush(() => reply(first, run('A', 'running', { progress: 20 })));
expect(requests.filter(item => item.path === '/api/v2/runs/A')).toHaveLength(3);
await flush(() => fail(request('/api/v2/runs/A')));
expect(latest.error).toContain('Network unavailable');
expect(latest.error).toContain('The backtest response was lost');
expect(latest.currentRun?.progress).toBe(20);
expect(polls()).toBe(1);
await advance(1000);
@ -793,7 +797,9 @@ describe('run selection and persisted status authority', () => {
await selectRunning('A', initial);
await flush(() => click('Cancel run'));
expect(host.textContent).toContain('Cancellation requested');
await flush(() => reply(request('/api/v2/runs/A/cancel', 'POST'), { accepted: true }, 202));
await flush(() =>
reply(request('/api/v2/runs/A/cancel', 'POST'), { message: 'Cancellation requested' }, 202)
);
expect(latest.currentRun?.status).toBe(initial);
const acknowledged = initial === 'pending' ? 'cancelled' : 'running';
await flush(() =>
@ -831,9 +837,11 @@ describe('run selection and persisted status authority', () => {
await flush(() => click('Cancel run'));
await flush(() => fail(request('/api/v2/runs/A/cancel', 'POST')));
expect(latest.cancelRequested).toBe(false);
expect(latest.error).toContain('Network unavailable');
expect(latest.error).toContain('The backtest response was lost');
await flush(() => click('Cancel run'));
await flush(() => reply(request('/api/v2/runs/A/cancel', 'POST'), { accepted: true }, 202));
await flush(() =>
reply(request('/api/v2/runs/A/cancel', 'POST'), { message: 'Cancellation requested' }, 202)
);
await flush(() => reply(request('/api/v2/runs/A'), run('A', 'completed')));
await flush(() => reply(request('/api/v2/runs/A/results'), results('A')));
expect(latest.currentRun?.status).toBe('completed');
@ -889,7 +897,9 @@ describe('actual detail page result states', () => {
await mountPage('running');
await flush(() => click('Cancel run'));
expect(host.textContent).toContain('Cancellation requested');
await flush(() => reply(request('/api/v2/runs/A/cancel', 'POST'), {}, 202));
await flush(() =>
reply(request('/api/v2/runs/A/cancel', 'POST'), { message: 'Cancellation requested' }, 202)
);
expect(host.textContent).not.toContain('Run cancelled');
await flush(() => reply(request('/api/v2/runs/A'), run('A', 'cancelled')));
expect(host.textContent).toContain('Run cancelled');
@ -939,14 +949,41 @@ describe('actual detail page result states', () => {
});
async function selectBacktestStrategy(preselected = false) {
const family = { id: 'family', name: 'Test strategy', archived: false };
const family = {
id: 'family',
name: 'Test strategy',
description: '',
archived: false,
ownerPrincipal: 'operator',
updatedBy: 'operator',
createdAt: '2026-01-01',
updatedAt: '2026-01-01',
};
const version = {
id: 'version-one',
strategyId: 'family',
ordinal: 1,
origin: 'builtin',
parentVersionId: null,
createdBy: 'operator',
createdAt: '2026-01-01',
reviewStatus: 'approved',
runnable: false,
reason: 'Configure canonical settings',
package: {
version: 1,
program: {
strategyId: 'fixture',
strategyVersion: 'v1',
apiVersion: 'canonical-ts/v1',
checkpointVersion: 'fixture/v1',
},
artifact: { format: 'esm-bundle/v1', entryExport: 'createFixture', sha256: 'a'.repeat(64) },
dependencyLock: { format: 'bun-lock/v1', sha256: 'b'.repeat(64) },
source: { revision: 'c'.repeat(40), dirty: false },
build: { tool: 'bun', version: '1.2.17', target: 'browser' },
parameterSchema: {
version: 1,
type: 'object',
properties: {},
required: [],
@ -1390,7 +1427,7 @@ describe('explicit save and repeat-safe run submission', () => {
await flush(() => reply(request('/api/v2/runs/B'), run('B', 'failed', { error: 'B failure' })));
await flush(() => fail(submitting));
expect(pageLocation).toBe('/backtests/definition/run/B');
expect(host.textContent).toContain('Run submission failed: Network unavailable');
expect(host.textContent).toContain('Run submission failed: The backtest response was lost');
expect(host.textContent).toContain('Run failed: B failure');
await flush(() => click('Retry submission'));
const retry = request('/api/v2/backtests/definition/runs', 'POST');
@ -1409,7 +1446,7 @@ describe('explicit save and repeat-safe run submission', () => {
expect(requests.filter(item => item.method === 'POST')).toHaveLength(1);
expect(latest.isSubmitting).toBe(true);
await flush(() => fail(first));
expect(latest.submissionError).toContain('Network unavailable');
expect(latest.submissionError).toContain('The backtest response was lost');
await flush(() => {
void latest.updateBacktest('definition', { initialCapital: 2000 });
});
@ -1450,7 +1487,7 @@ describe('explicit save and repeat-safe run submission', () => {
expect(requests.filter(item => item.method === 'POST')).toHaveLength(1);
await flush(() => reply(first, { error: 'Submission unavailable' }, 503));
expect(host.textContent).toContain('Run submission failed');
expect(host.textContent).toContain('Network unavailable');
expect(host.textContent).toContain('The backtest response was lost');
expect(host.textContent).toContain('Submit the current configuration');
await flush(() => click('Retry submission'));
const retry = request('/api/v2/backtests/definition/runs', 'POST');
@ -1698,7 +1735,7 @@ describe('template archive and restore', () => {
expect(original.signal?.aborted).toBe(true);
await flush(() => (lateFailure ? fail(original) : reply(original, [definition])));
expect(host.textContent).toContain('Loading backtests...');
expect(host.textContent).not.toContain('Network unavailable');
expect(host.textContent).not.toContain('The backtest response was lost');
await flush(() => reply(current, []));
expect(host.textContent).toContain('No backtests yet.');
expect(host.textContent).not.toContain('Loading backtests...');