fix(research): preserve strategy work before backtest handoff (RP09)
This commit is contained in:
parent
c24410e5dd
commit
49da701aa7
5 changed files with 258 additions and 7 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import { createHash } from 'node:crypto';
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
|
||||
import { GlobalWindow } from 'happy-dom';
|
||||
import { act, createElement, useState, type ReactElement } from 'react';
|
||||
|
|
@ -29,6 +30,7 @@ let root: Root | undefined;
|
|||
let host: HTMLDivElement;
|
||||
let navigate: ReturnType<typeof router.useNavigate>;
|
||||
let location = '';
|
||||
let locationState: unknown;
|
||||
let latest: ReturnType<typeof useBacktestV2>;
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void;
|
||||
|
|
@ -112,6 +114,7 @@ function edit(label: string, value: string) {
|
|||
}
|
||||
function Probe() {
|
||||
location = router.useLocation().pathname + router.useLocation().search;
|
||||
locationState = router.useLocation().state;
|
||||
navigate = router.useNavigate();
|
||||
return null;
|
||||
}
|
||||
|
|
@ -202,7 +205,11 @@ const definition = {
|
|||
createdAt: '2026-01-01',
|
||||
updatedAt: '2026-01-01',
|
||||
};
|
||||
async function detail(path = '/strategies/family/versions/version-one', versions = [version()]) {
|
||||
async function detail(
|
||||
path = '/strategies/family/versions/version-one',
|
||||
versions = [version()],
|
||||
strategy = family()
|
||||
) {
|
||||
await mount(
|
||||
createElement(
|
||||
router.Routes,
|
||||
|
|
@ -215,9 +222,12 @@ async function detail(path = '/strategies/family/versions/version-one', versions
|
|||
path
|
||||
);
|
||||
await flush(() => {
|
||||
reply(request('/api/v2/strategies/family'), { strategy: family() });
|
||||
reply(request('/api/v2/strategies/family/versions'), { versions });
|
||||
reply(request('/api/v2/strategy-versions/version-one'), { version: versions[0], reviews: [] });
|
||||
reply(request(`/api/v2/strategies/${strategy.id}`), { strategy });
|
||||
reply(request(`/api/v2/strategies/${strategy.id}/versions`), { versions });
|
||||
reply(request(`/api/v2/strategy-versions/${path.split('/').at(-1)}`), {
|
||||
version: versions[0],
|
||||
reviews: [],
|
||||
});
|
||||
});
|
||||
}
|
||||
async function selected(archived = false) {
|
||||
|
|
@ -267,6 +277,7 @@ beforeAll(async () => {
|
|||
beforeEach(() => {
|
||||
requests.length = 0;
|
||||
location = '';
|
||||
locationState = undefined;
|
||||
host = browser.document.createElement('div') as unknown as HTMLDivElement;
|
||||
browser.document.body.append(host as never);
|
||||
Object.defineProperty(browser, 'confirm', {
|
||||
|
|
@ -693,6 +704,165 @@ describe('strategy workbench', () => {
|
|||
expect(requests.some(item => item.path.endsWith('/runs'))).toBe(false);
|
||||
});
|
||||
|
||||
describe('strategy work custody before backtest handoff', () => {
|
||||
const parameters = { fastPeriod: 3, slowPeriod: 8 };
|
||||
const validate = async (id = 'version-one') => {
|
||||
await flush(() => click('Validate parameters'));
|
||||
await flush(() =>
|
||||
reply(request(`/api/v2/strategy-versions/${id}/validate`, 'POST'), {
|
||||
parameters,
|
||||
runnable: false,
|
||||
reason: 'Fixture execution unavailable',
|
||||
})
|
||||
);
|
||||
};
|
||||
const confirm = (answer: boolean) => {
|
||||
const messages: string[] = [];
|
||||
Object.defineProperty(browser, 'confirm', {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: (message: string) => {
|
||||
messages.push(message);
|
||||
return answer;
|
||||
},
|
||||
});
|
||||
return messages;
|
||||
};
|
||||
|
||||
test('pending and uncertain clones keep their original receipt until explicit discard', async () => {
|
||||
await detail();
|
||||
await validate();
|
||||
await flush(() => click('Clone version'));
|
||||
expect(button('Use in backtest').disabled).toBe(true);
|
||||
expect((field('Candidate package (JSON)') as HTMLInputElement).disabled).toBe(true);
|
||||
expect(host.textContent).toContain('Finish or retry the publication or clone');
|
||||
await flush(() => click('Create clone'));
|
||||
const first = request('/api/v2/strategy-versions/version-one/clone', 'POST');
|
||||
const count = requests.length;
|
||||
await flush(() => click('Use in backtest'));
|
||||
expect(location).toBe('/strategies/family/versions/version-one');
|
||||
expect(requests).toHaveLength(count);
|
||||
expect(button('Close clone').disabled).toBe(true);
|
||||
await flush(() => fail(first));
|
||||
expect(button('Use in backtest').disabled).toBe(true);
|
||||
await flush(() => click('Use in backtest'));
|
||||
expect(requests).toHaveLength(count);
|
||||
await flush(() => click('Retry clone'));
|
||||
const retry = request('/api/v2/strategy-versions/version-one/clone', 'POST');
|
||||
expect(retry.body).toEqual(first.body);
|
||||
expect(retry.headers.get('Idempotency-Key')).toBe(first.headers.get('Idempotency-Key'));
|
||||
await flush(() => fail(retry));
|
||||
const denied = confirm(false);
|
||||
await flush(() => click('Close clone'));
|
||||
expect(denied[0]).toContain('Abandon this clone receipt?');
|
||||
expect(button('Use in backtest').disabled).toBe(true);
|
||||
const accepted = confirm(true);
|
||||
await flush(() => click('Close clone'));
|
||||
expect(accepted[0]).toContain('Abandon this clone receipt?');
|
||||
expect(button('Use in backtest').disabled).toBe(false);
|
||||
expect((field('Candidate package (JSON)') as HTMLInputElement).disabled).toBe(false);
|
||||
const handoffPrompts = confirm(false);
|
||||
await flush(() => click('Use in backtest'));
|
||||
expect(location).toBe('/backtests?strategyVersionId=version-one');
|
||||
expect(locationState).toEqual({ strategyParameters: parameters });
|
||||
expect(handoffPrompts).toEqual([]);
|
||||
expect(requests).toHaveLength(count + 1);
|
||||
});
|
||||
|
||||
test('package drafts and uncertain publication block handoff and competing clone admission', async () => {
|
||||
const familyId = '11111111-1111-4111-8111-111111111111';
|
||||
const versionId = '22222222-2222-4222-8222-222222222222';
|
||||
const path = `/strategies/${familyId}/versions/${versionId}`;
|
||||
await detail(path, [version(versionId, { strategyId: familyId })], family(familyId));
|
||||
await validate(versionId);
|
||||
const candidate = pkg('export function createFixture() {}');
|
||||
candidate.execution = { kind: 'isolated-candidate/v1', warmupBars: 0 };
|
||||
candidate.artifact.sha256 = createHash('sha256')
|
||||
.update(candidate.artifact.content)
|
||||
.digest('hex');
|
||||
candidate.dependencyLock.sha256 = createHash('sha256')
|
||||
.update(candidate.dependencyLock.content)
|
||||
.digest('hex');
|
||||
const upload = async () => {
|
||||
await flush(() => {
|
||||
const input = field('Candidate package (JSON)');
|
||||
Object.defineProperty(input, 'files', {
|
||||
configurable: true,
|
||||
value: [{ name: 'candidate.json', text: async () => JSON.stringify(candidate) }],
|
||||
});
|
||||
input.dispatchEvent(new browser.Event('change', { bubbles: true }) as unknown as Event);
|
||||
});
|
||||
};
|
||||
const blocked = async () => {
|
||||
const count = requests.length;
|
||||
expect(button('Use in backtest').disabled).toBe(true);
|
||||
expect(button('Clone version').disabled).toBe(true);
|
||||
await flush(() => {
|
||||
click('Use in backtest');
|
||||
click('Clone version');
|
||||
});
|
||||
expect(location).toBe(path);
|
||||
expect(requests).toHaveLength(count);
|
||||
expect(host.textContent).not.toContain('Create clone');
|
||||
};
|
||||
await upload();
|
||||
await blocked();
|
||||
await flush(() => click('Clear package'));
|
||||
expect(button('Use in backtest').disabled).toBe(false);
|
||||
expect(button('Clone version').disabled).toBe(false);
|
||||
await upload();
|
||||
await flush(() => click('Publish immutable candidate'));
|
||||
const first = request(`/api/v2/strategies/${familyId}/versions`, 'POST');
|
||||
await blocked();
|
||||
await flush(() => fail(first));
|
||||
await blocked();
|
||||
expect(button('Clear package').disabled).toBe(true);
|
||||
await flush(() => click('Retry same publication'));
|
||||
const retry = request(`/api/v2/strategies/${familyId}/versions`, 'POST');
|
||||
expect(retry.body).toEqual(first.body);
|
||||
expect(retry.headers.get('Idempotency-Key')).toBe(first.headers.get('Idempotency-Key'));
|
||||
const publishedId = '33333333-3333-4333-8333-333333333333';
|
||||
await flush(() =>
|
||||
reply(
|
||||
retry,
|
||||
{
|
||||
version: {
|
||||
id: publishedId,
|
||||
strategyId: familyId,
|
||||
ordinal: 2,
|
||||
origin: 'candidate',
|
||||
parentVersionId: versionId,
|
||||
package: candidate,
|
||||
},
|
||||
},
|
||||
201
|
||||
)
|
||||
);
|
||||
expect(location).toBe(`/strategies/${familyId}/versions/${publishedId}`);
|
||||
expect(
|
||||
requests.some(item => item.path.endsWith('/clone') || item.path.endsWith('/runs'))
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
test('ordinary handoff preserves parameters and only confirms actual metadata loss', async () => {
|
||||
await detail();
|
||||
await validate();
|
||||
await flush(() => edit('Strategy name', 'Unsaved metadata'));
|
||||
const denied = confirm(false);
|
||||
await flush(() => click('Use in backtest'));
|
||||
expect(location).toBe('/strategies/family/versions/version-one');
|
||||
expect(denied).toEqual(['Discard unsaved changes?']);
|
||||
const accepted = confirm(true);
|
||||
await flush(() => click('Use in backtest'));
|
||||
expect(accepted).toEqual(['Discard unsaved changes?']);
|
||||
expect(location).toBe('/backtests?strategyVersionId=version-one');
|
||||
expect(locationState).toEqual({ strategyParameters: parameters });
|
||||
expect(requests.filter(item => item.method !== 'GET').map(item => item.path)).toEqual([
|
||||
'/api/v2/strategy-versions/version-one/validate',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
test('reopened catalog settings save exact version parameters without legacy pacing and keep notices outside heading', async () => {
|
||||
await mount(
|
||||
createElement(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue