fix(backtest): preserve creation work during strategy inspection (RP14)
This commit is contained in:
parent
cf04fb36d0
commit
0d022ecc2e
3 changed files with 309 additions and 6 deletions
|
|
@ -938,7 +938,7 @@ describe('actual detail page result states', () => {
|
|||
});
|
||||
});
|
||||
|
||||
async function nameBacktest() {
|
||||
async function selectBacktestStrategy(preselected = false) {
|
||||
const family = { id: 'family', name: 'Test strategy', archived: false };
|
||||
const version = {
|
||||
id: 'version-one',
|
||||
|
|
@ -963,13 +963,22 @@ async function nameBacktest() {
|
|||
);
|
||||
select.dispatchEvent(new browser.Event('change', { bubbles: true }) as unknown as Event);
|
||||
};
|
||||
await flush(() => choose('Strategy family', 'family'));
|
||||
await flush(() => reply(request('/api/v2/strategies/family/versions'), { versions: [version] }));
|
||||
await flush(() => choose('Immutable strategy version', 'version-one'));
|
||||
if (!preselected) {
|
||||
await flush(() => choose('Strategy family', 'family'));
|
||||
await flush(() =>
|
||||
reply(request('/api/v2/strategies/family/versions'), { versions: [version] })
|
||||
);
|
||||
await flush(() => choose('Immutable strategy version', 'version-one'));
|
||||
}
|
||||
await flush(() =>
|
||||
reply(request('/api/v2/strategy-versions/version-one'), { version, reviews: [] })
|
||||
);
|
||||
await flush(() => reply(request('/api/v2/strategies/family'), { strategy: family }));
|
||||
if (preselected) {
|
||||
await flush(() =>
|
||||
reply(request('/api/v2/strategies/family/versions'), { versions: [version] })
|
||||
);
|
||||
}
|
||||
await flush(() => click('Validate parameters'));
|
||||
await flush(() =>
|
||||
reply(request('/api/v2/strategy-versions/version-one/validate', 'POST'), {
|
||||
|
|
@ -978,6 +987,10 @@ async function nameBacktest() {
|
|||
reason: 'Configure canonical settings',
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
async function nameBacktest() {
|
||||
await selectBacktestStrategy();
|
||||
await flush(() => {
|
||||
const input = host.querySelector('input[placeholder="My Strategy Backtest"]')!;
|
||||
Object.getOwnPropertyDescriptor(browser.HTMLInputElement.prototype, 'value')!.set!.call(
|
||||
|
|
@ -998,6 +1011,230 @@ async function loadDefinition(id = 'definition') {
|
|||
});
|
||||
}
|
||||
|
||||
describe('backtest creation owned-link custody', () => {
|
||||
let prompts: string[];
|
||||
let allowDiscard: boolean;
|
||||
let confirmDescriptor: PropertyDescriptor | undefined;
|
||||
beforeEach(() => {
|
||||
prompts = [];
|
||||
allowDiscard = false;
|
||||
confirmDescriptor = Object.getOwnPropertyDescriptor(browser, 'confirm');
|
||||
Object.defineProperty(browser, 'confirm', {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: (message: string) => {
|
||||
prompts.push(message);
|
||||
return allowDiscard;
|
||||
},
|
||||
});
|
||||
});
|
||||
afterEach(() => {
|
||||
if (confirmDescriptor) {
|
||||
Object.defineProperty(browser, 'confirm', confirmDescriptor);
|
||||
} else {
|
||||
Reflect.deleteProperty(browser, 'confirm');
|
||||
}
|
||||
});
|
||||
async function openCreation(preselected = false) {
|
||||
await mount(
|
||||
createElement(
|
||||
router.MemoryRouter,
|
||||
{
|
||||
initialEntries: [preselected ? '/backtests?strategyVersionId=version-one' : '/backtests'],
|
||||
},
|
||||
createElement(
|
||||
router.Routes,
|
||||
null,
|
||||
createElement(router.Route, {
|
||||
path: '/backtests',
|
||||
element: createElement(BacktestListPageV2),
|
||||
}),
|
||||
createElement(router.Route, {
|
||||
path: '/backtests/:id',
|
||||
element: createElement('p', null, 'Saved definition'),
|
||||
}),
|
||||
createElement(router.Route, {
|
||||
path: '/strategies/:id/versions/:versionId',
|
||||
element: createElement('p', null, 'Immutable strategy version'),
|
||||
})
|
||||
),
|
||||
createElement(LocationProbe)
|
||||
)
|
||||
);
|
||||
await flush(() => reply(request('/api/v2/backtests'), []));
|
||||
if (!preselected) {
|
||||
await flush(() => click('New Backtest'));
|
||||
}
|
||||
}
|
||||
const versionLink = () =>
|
||||
host.querySelector('a[href="/strategies/family/versions/version-one"]') as HTMLAnchorElement;
|
||||
const values = () =>
|
||||
[
|
||||
...host.querySelectorAll<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>(
|
||||
'input,select,textarea'
|
||||
),
|
||||
].map(input => input.value);
|
||||
const writes = () => requests.filter(item => item.method !== 'GET');
|
||||
const saves = () =>
|
||||
requests.filter(item => item.path === '/api/v2/backtests' && item.method === 'POST');
|
||||
|
||||
test('declined navigation keeps exact draft values; accepted discard follows the link without Close', async () => {
|
||||
await openCreation();
|
||||
await nameBacktest();
|
||||
const numbers = [...host.querySelectorAll<HTMLInputElement>('input[type="number"]')];
|
||||
for (const [index, value] of ['25000', '1.25', '0.05'].entries()) {
|
||||
await flush(() => {
|
||||
const input = numbers[index]!;
|
||||
Object.getOwnPropertyDescriptor(browser.HTMLInputElement.prototype, 'value')!.set!.call(
|
||||
input,
|
||||
value
|
||||
);
|
||||
input.dispatchEvent(new browser.Event('input', { bubbles: true }) as unknown as Event);
|
||||
});
|
||||
}
|
||||
expect(numbers.map(input => input.value)).toEqual(['25000', '1.25', '0.05']);
|
||||
const before = values();
|
||||
const requestCount = requests.length;
|
||||
const writeCount = writes().length;
|
||||
await flush(() => versionLink().click());
|
||||
expect(pageLocation).toBe('/backtests');
|
||||
expect(values()).toEqual(before);
|
||||
expect(prompts).toEqual(['Discard unsaved changes?']);
|
||||
expect(writes()).toHaveLength(writeCount);
|
||||
allowDiscard = true;
|
||||
await flush(() => versionLink().click());
|
||||
expect(pageLocation).toBe('/strategies/family/versions/version-one');
|
||||
expect(prompts).toEqual(['Discard unsaved changes?', 'Discard unsaved changes?']);
|
||||
expect(requests).toHaveLength(requestCount);
|
||||
expect(saves()).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('clean preselected navigation does not ask to discard', async () => {
|
||||
await openCreation(true);
|
||||
await selectBacktestStrategy(true);
|
||||
await flush(() => versionLink().click());
|
||||
expect(pageLocation).toBe('/strategies/family/versions/version-one');
|
||||
expect(prompts).toHaveLength(0);
|
||||
expect(saves()).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('Save then link before rerender preserves one request and accepted saved-definition navigation', async () => {
|
||||
await openCreation();
|
||||
await nameBacktest();
|
||||
const before = values();
|
||||
await flush(() => {
|
||||
click('Save');
|
||||
versionLink().click();
|
||||
click('Save');
|
||||
});
|
||||
expect(pageLocation).toBe('/backtests');
|
||||
expect(values()).toEqual(before);
|
||||
expect(prompts).toHaveLength(0);
|
||||
expect(saves()).toHaveLength(1);
|
||||
const saving = request('/api/v2/backtests', 'POST');
|
||||
const captured = submittedBody(saving);
|
||||
expect(JSON.parse(captured)).toMatchObject({
|
||||
name: 'Fixture',
|
||||
strategyVersionId: 'version-one',
|
||||
strategyParameters: {},
|
||||
autoStart: false,
|
||||
});
|
||||
await flush(() => versionLink().click());
|
||||
expect(pageLocation).toBe('/backtests');
|
||||
expect(prompts).toHaveLength(0);
|
||||
expect(saves()).toHaveLength(1);
|
||||
expect(submittedBody(saving)).toBe(captured);
|
||||
await flush(() => reply(saving, definition));
|
||||
expect(pageLocation).toBe('/backtests/definition');
|
||||
expect(host.querySelector('[aria-label="Close backtest dialog"]')).toBeNull();
|
||||
expect(requests.some(item => item.path.endsWith('/runs') && item.method === 'POST')).toBe(
|
||||
false
|
||||
);
|
||||
});
|
||||
|
||||
test('a failed Save returns to dirty confirmation and retains its visible error and inputs', async () => {
|
||||
await openCreation();
|
||||
await nameBacktest();
|
||||
const before = values();
|
||||
await flush(() => click('Save'));
|
||||
await flush(() => fail(request('/api/v2/backtests', 'POST')));
|
||||
expect(host.textContent).toContain('Failed to save backtest');
|
||||
await flush(() => versionLink().click());
|
||||
expect(pageLocation).toBe('/backtests');
|
||||
expect(prompts).toEqual(['Discard unsaved changes?']);
|
||||
expect(values()).toEqual(before);
|
||||
expect(host.textContent).toContain('Failed to save backtest');
|
||||
expect(saves()).toHaveLength(1);
|
||||
allowDiscard = true;
|
||||
await flush(() => versionLink().click());
|
||||
expect(pageLocation).toBe('/strategies/family/versions/version-one');
|
||||
expect(saves()).toHaveLength(1);
|
||||
});
|
||||
|
||||
for (const pending of [false, true]) {
|
||||
test(`modified, middle, new-context and prevented clicks preserve ${pending ? 'pending Save' : 'dirty draft'}`, async () => {
|
||||
await openCreation();
|
||||
await nameBacktest();
|
||||
if (pending) {
|
||||
await flush(() => click('Save'));
|
||||
}
|
||||
const before = values();
|
||||
const link = versionLink();
|
||||
let reachedLink = 0;
|
||||
const keepBrowserLocal = (event: Event) => {
|
||||
reachedLink++;
|
||||
event.preventDefault();
|
||||
};
|
||||
// The target listener stops browser defaults after the dialog's capture phase.
|
||||
link.addEventListener('click', keepBrowserLocal);
|
||||
for (const modifiers of [
|
||||
{ ctrlKey: true },
|
||||
{ metaKey: true },
|
||||
{ shiftKey: true },
|
||||
{ altKey: true },
|
||||
{ button: 1 },
|
||||
]) {
|
||||
const beforeClick = reachedLink;
|
||||
await flush(() =>
|
||||
link.dispatchEvent(
|
||||
new browser.MouseEvent('click', {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
...modifiers,
|
||||
}) as unknown as Event
|
||||
)
|
||||
);
|
||||
expect(reachedLink).toBe(beforeClick + 1);
|
||||
}
|
||||
for (const target of ['_blank', 'strategy-inspection']) {
|
||||
// Native anchors exercise explicit targets without changing the shared selector.
|
||||
const external = link.cloneNode(true) as HTMLAnchorElement;
|
||||
external.target = target;
|
||||
external.addEventListener('click', keepBrowserLocal);
|
||||
link.parentElement!.append(external);
|
||||
const beforeClick = reachedLink;
|
||||
await flush(() => external.click());
|
||||
expect(reachedLink).toBe(beforeClick + 1);
|
||||
external.remove();
|
||||
}
|
||||
const prevented = new browser.MouseEvent('click', { bubbles: true, cancelable: true });
|
||||
prevented.preventDefault();
|
||||
const beforePrevented = reachedLink;
|
||||
await flush(() => link.dispatchEvent(prevented as unknown as Event));
|
||||
expect(reachedLink).toBe(beforePrevented + 1);
|
||||
expect(prevented.defaultPrevented).toBe(true);
|
||||
expect(pageLocation).toBe('/backtests');
|
||||
expect(prompts).toHaveLength(0);
|
||||
expect(values()).toEqual(before);
|
||||
expect(saves()).toHaveLength(pending ? 1 : 0);
|
||||
if (pending) {
|
||||
await flush(() => reply(request('/api/v2/backtests', 'POST'), definition));
|
||||
expect(pageLocation).toBe('/backtests/definition');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('explicit save and repeat-safe run submission', () => {
|
||||
test('save_only: definition save explicitly disables auto-start and makes no run request', async () => {
|
||||
const created: Backtest[] = [];
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue