Skip to content

Commit f214a3e

Browse files
fix(studio): play a scenario one column per produced slot
fix(studio): play a scenario one column per produced slot
1 parent 237d1e6 commit f214a3e

2 files changed

Lines changed: 350 additions & 102 deletions

File tree

‎apps/studio/src/components/svm/scenario-editor.test.tsx‎

Lines changed: 199 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { fetchDynamicRefOptions } from '@/lib/scenarios-api';
22
import { comboboxResults } from '@/test-utils';
3-
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
3+
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
44
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
55
import ScenarioEditor from './scenario-editor';
66

@@ -377,3 +377,201 @@ it('steps forward to the slot after the processed one, which surfnet reads ahead
377377
expect(travel?.params).toEqual([{ absoluteSlot: 664 }]);
378378
});
379379
});
380+
381+
const rpcCalls = () =>
382+
fetchMock.mock.calls.filter(([url]) => url === 'http://rpc').map(([, init]) => JSON.parse(init.body));
383+
384+
it('plays one column per produced slot: the first at Play, the next on each step', async () => {
385+
const column = (id: string) => ({
386+
id,
387+
name: 'Slot',
388+
type: 'slot' as const,
389+
actions: [
390+
{
391+
protocolId: 'phoenix-eternal',
392+
actionId: collateralTemplate.id,
393+
protocol: 'Phoenix Eternal',
394+
action: collateralTemplate.name,
395+
account: collateralTemplate.address,
396+
overrides: { 'traderState.quoteLotCollateral': id },
397+
},
398+
],
399+
});
400+
fetchMock.mockImplementation(async (url: string) => ({
401+
ok: true,
402+
json: async () => (url.endsWith('/templates') ? [collateralTemplate] : { result: { absoluteSlot: 500 } }),
403+
}));
404+
render(<ScenarioEditor scenarioId="columns" initialSteps={[column('1'), column('2')]} />);
405+
406+
fireEvent.click(await screen.findByTitle('Play scenario'));
407+
await waitFor(() =>
408+
expect(rpcCalls().map((call) => call.method)).toEqual([
409+
'surfnet_registerScenario',
410+
'surfnet_pauseClock',
411+
'getEpochInfo',
412+
'surfnet_timeTravel',
413+
])
414+
);
415+
const [first] = rpcCalls();
416+
expect(first.params[0].name).toBe('Scenario · column 1/2');
417+
expect(first.params[0].overrides).toHaveLength(1);
418+
expect(first.params[0].overrides[0]).toMatchObject({
419+
scenarioRelativeSlot: 0,
420+
values: { 'traderState.quoteLotCollateral': '1' },
421+
});
422+
423+
fireEvent.click(await screen.findByTitle('Step forward'));
424+
await waitFor(() =>
425+
expect(
426+
rpcCalls()
427+
.map((call) => call.method)
428+
.slice(4)
429+
).toEqual(['surfnet_registerScenario', 'getEpochInfo', 'surfnet_timeTravel'])
430+
);
431+
const [, , , , second, , timeTravel] = rpcCalls();
432+
expect(second.params[0].name).toBe('Scenario · column 2/2');
433+
expect(second.params[0].overrides).toHaveLength(1);
434+
expect(second.params[0].overrides[0]).toMatchObject({
435+
scenarioRelativeSlot: 0,
436+
values: { 'traderState.quoteLotCollateral': '2' },
437+
});
438+
expect(timeTravel.params).toEqual([{ absoluteSlot: 501 }]);
439+
});
440+
441+
it('stays in read mode and never pauses the clock when the first column is rejected', async () => {
442+
fetchMock.mockImplementation(async (url: string) => ({
443+
ok: true,
444+
json: async () =>
445+
url.endsWith('/templates')
446+
? [collateralTemplate]
447+
: { error: { code: -32602, message: 'base slot 500 is already produced; the next slot is 501' } },
448+
}));
449+
renderEditor(stepAction(collateralTemplate));
450+
await waitFor(() => expect(fetchMock).toHaveBeenCalled());
451+
fireEvent.click(await screen.findByTitle('Play scenario'));
452+
await waitFor(() => expect(rpcCalls().map((call) => call.method)).toEqual(['surfnet_registerScenario']));
453+
expect(screen.getByTitle('Play scenario')).toBeInTheDocument();
454+
});
455+
456+
it('retries a step whose time travel failed without registering the column again', async () => {
457+
const column = (id: string) => ({
458+
id,
459+
name: 'Slot',
460+
type: 'slot' as const,
461+
actions: [
462+
{
463+
protocolId: 'phoenix-eternal',
464+
actionId: collateralTemplate.id,
465+
protocol: 'Phoenix Eternal',
466+
action: collateralTemplate.name,
467+
account: collateralTemplate.address,
468+
overrides: { 'traderState.quoteLotCollateral': id },
469+
},
470+
],
471+
});
472+
let failNextTimeTravel = false;
473+
fetchMock.mockImplementation(async (url: string, init?: RequestInit) => {
474+
const method = url === 'http://rpc' ? JSON.parse(`${init?.body}`).method : undefined;
475+
if (method === 'surfnet_timeTravel' && failNextTimeTravel) {
476+
failNextTimeTravel = false;
477+
return { ok: true, json: async () => ({ error: { code: -32603, message: 'Failed to confirm clock update' } }) };
478+
}
479+
return {
480+
ok: true,
481+
json: async () => (url.endsWith('/templates') ? [collateralTemplate] : { result: { absoluteSlot: 500 } }),
482+
};
483+
});
484+
render(<ScenarioEditor scenarioId="retry" initialSteps={[column('1'), column('2')]} />);
485+
fireEvent.click(await screen.findByTitle('Play scenario'));
486+
await waitFor(() => expect(rpcCalls()).toHaveLength(4));
487+
488+
failNextTimeTravel = true;
489+
fireEvent.click(await screen.findByTitle('Step forward'));
490+
await waitFor(() => expect(rpcCalls()).toHaveLength(7));
491+
expect(await screen.findByTitle('Step forward')).toBeInTheDocument();
492+
493+
fireEvent.click(screen.getByTitle('Step forward'));
494+
await waitFor(() => expect(rpcCalls()).toHaveLength(9));
495+
expect(
496+
rpcCalls()
497+
.map((call) => call.method)
498+
.slice(4)
499+
).toEqual(['surfnet_registerScenario', 'getEpochInfo', 'surfnet_timeTravel', 'getEpochInfo', 'surfnet_timeTravel']);
500+
});
501+
502+
it('registers the first column again on the next Play after Play cannot advance', async () => {
503+
let failTimeTravel = true;
504+
fetchMock.mockImplementation(async (url: string, init?: RequestInit) => {
505+
const method = url === 'http://rpc' ? JSON.parse(`${init?.body}`).method : undefined;
506+
if (method === 'surfnet_timeTravel' && failTimeTravel) {
507+
return { ok: true, json: async () => ({ error: { code: -32603, message: 'Failed to confirm clock update' } }) };
508+
}
509+
return {
510+
ok: true,
511+
json: async () => (url.endsWith('/templates') ? [collateralTemplate] : { result: { absoluteSlot: 500 } }),
512+
};
513+
});
514+
renderEditor(stepAction(collateralTemplate));
515+
516+
fireEvent.click(await screen.findByTitle('Play scenario'));
517+
await waitFor(() => expect(rpcCalls()).toHaveLength(4));
518+
failTimeTravel = false;
519+
fireEvent.click(await screen.findByTitle('Play scenario'));
520+
await waitFor(() =>
521+
expect(
522+
rpcCalls()
523+
.map((call) => call.method)
524+
.slice(4)
525+
).toEqual(['surfnet_registerScenario', 'surfnet_pauseClock', 'getEpochInfo', 'surfnet_timeTravel'])
526+
);
527+
});
528+
529+
it('ignores a second Step and keeps Stop disabled while the first one is still registering its column', async () => {
530+
let finishRegistering!: () => void;
531+
let holdRegistration = false;
532+
fetchMock.mockImplementation(async (url: string, init?: RequestInit) => {
533+
const method = url === 'http://rpc' ? JSON.parse(`${init?.body}`).method : undefined;
534+
if (method === 'surfnet_registerScenario' && holdRegistration) {
535+
await new Promise<void>((resolve) => {
536+
finishRegistering = resolve;
537+
});
538+
}
539+
return {
540+
ok: true,
541+
json: async () => (url.endsWith('/templates') ? [collateralTemplate] : { result: { absoluteSlot: 500 } }),
542+
};
543+
});
544+
render(
545+
<ScenarioEditor
546+
scenarioId="double-step"
547+
initialSteps={[1, 2, 3].map((height) => ({
548+
id: `${height}`,
549+
name: 'Slot',
550+
type: 'slot',
551+
actions: [stepAction(collateralTemplate)],
552+
}))}
553+
/>
554+
);
555+
fireEvent.click(await screen.findByTitle('Play scenario'));
556+
await waitFor(() => expect(rpcCalls()).toHaveLength(4));
557+
558+
const step = await screen.findByTitle('Step forward');
559+
holdRegistration = true;
560+
// Both clicks land before React renders again, so only a guard outside state can stop the second
561+
act(() => {
562+
step.click();
563+
holdRegistration = false;
564+
step.click();
565+
});
566+
expect(screen.getByTitle('Stop scenario')).toBeDisabled();
567+
finishRegistering();
568+
await waitFor(() => expect(rpcCalls()).toHaveLength(7));
569+
570+
fireEvent.click(await screen.findByTitle('Step forward'));
571+
await waitFor(() => expect(rpcCalls()).toHaveLength(10));
572+
expect(
573+
rpcCalls()
574+
.filter((call) => call.method === 'surfnet_registerScenario')
575+
.map((call) => call.params[0].name)
576+
).toEqual(['Scenario · column 1/3', 'Scenario · column 2/3', 'Scenario · column 3/3']);
577+
});

0 commit comments

Comments
 (0)