Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions frontend/src/components/BenchPractice.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,69 @@ describe("bench practice", () => {
);
expect(screen.queryByRole("heading", { name: "Debrief" })).toBeNull();
});

it("keeps the active run and draft when a restart fails", async () => {
const user = userEvent.setup();
const random = vi.spyOn(Math, "random").mockReturnValue(0.25);
vi.spyOn(api, "benchProtocols").mockResolvedValue([protocol]);
vi.spyOn(api, "benchProtocol").mockResolvedValue(protocol);
const replay = vi
.spyOn(api, "benchReplay")
.mockResolvedValueOnce(run({ seed: 500_000_000 }))
.mockResolvedValueOnce(run({ seed: 500_000_000, clock_minutes: 1 }))
.mockRejectedValueOnce(new ApiError("Bench unavailable", 503))
.mockResolvedValueOnce(run({ seed: 500_000_000, clock_minutes: 2 }))
.mockResolvedValueOnce(run({ seed: 500_000_000 }));
render(<BenchPractice />);

await user.click(await screen.findByRole("button", { name: "Start run" }));
await user.click(await screen.findByRole("button", { name: "Do it" }));
await screen.findByText("(clock 1.0 min)");
await user.selectOptions(
screen.getByRole("combobox", { name: "Action" }),
"set_volume",
);
await user.type(
screen.getByRole("spinbutton", { name: "Volume (µL)" }),
"450",
);

random.mockReturnValue(0.75);
await user.click(screen.getByRole("button", { name: "Start a new run" }));
expect(await screen.findByRole("alert")).toHaveTextContent(
"Bench unavailable",
);
expect(screen.getByText("(clock 1.0 min)")).toBeInTheDocument();
expect(screen.getByRole("spinbutton", { name: "Volume (µL)" })).toHaveValue(
450,
);
expect(replay).toHaveBeenNthCalledWith(
3,
protocol.id,
1_500_000_000,
"guided",
[],
);

await user.click(screen.getByRole("button", { name: "Do it" }));
await screen.findByText("(clock 2.0 min)");
expect(replay).toHaveBeenNthCalledWith(
4,
protocol.id,
500_000_000,
"guided",
[
{ name: "fit_tip", params: {} },
{ name: "set_volume", params: { volume_ul: 450 } },
],
);

random.mockReturnValue(0.25);
await user.click(screen.getByRole("button", { name: "Start a new run" }));
await screen.findByText("(clock 0.0 min)");
expect(screen.getByRole("combobox", { name: "Action" })).toHaveValue(
"fit_tip",
);
expect(screen.queryByRole("spinbutton")).toBeNull();
});
});
41 changes: 28 additions & 13 deletions frontend/src/components/BenchPractice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ export function BenchPractice({ protocolId }: { protocolId?: string }) {
function ProtocolRunner({ id }: { id: string }) {
const [protocol, setProtocol] = useState<BenchProtocol | null>(null);
const [mode, setMode] = useState<Mode>("guided");
const [seed, setSeed] = useState<number | null>(null);
const [actions, setActions] = useState<BenchAction[]>([]);
const [run, setRun] = useState<BenchRun | null>(null);
const [session, setSession] = useState<{
actions: BenchAction[];
run: BenchRun;
generation: number;
} | null>(null);
const [error, setError] = useState("");
const [busy, setBusy] = useState(false);
const run = session?.run ?? null;
const actions = session?.actions ?? [];

useEffect(() => {
api
Expand All @@ -86,14 +90,26 @@ function ProtocolRunner({ id }: { id: string }) {
.catch((reason) => setError(errorMessage(reason)));
}, [id]);

async function submit(next: BenchAction[], runSeed = seed, runMode = mode) {
if (runSeed === null) return;
async function submit(
next: BenchAction[],
restart?: { seed: number; mode: Mode },
) {
const request = restart ?? run;
if (!request) return;
setBusy(true);
setError("");
try {
const result = await api.benchReplay(id, runSeed, runMode, next);
setActions(next);
setRun(result);
const result = await api.benchReplay(
id,
request.seed,
request.mode,
next,
);
setSession((previous) => ({
run: result,
actions: next,
generation: (previous?.generation ?? 0) + (restart ? 1 : 0),
}));
} catch (reason) {
setError(errorMessage(reason));
} finally {
Expand All @@ -102,9 +118,7 @@ function ProtocolRunner({ id }: { id: string }) {
}

function start() {
const fresh = newSeed();
setSeed(fresh);
void submit([], fresh, mode);
void submit([], { seed: newSeed(), mode });
}

if (!protocol) {
Expand All @@ -130,7 +144,7 @@ function ProtocolRunner({ id }: { id: string }) {
Mode{" "}
<select
value={mode}
disabled={run !== null && !run.finished}
disabled={busy || (run !== null && !run.finished)}
onChange={(event) => setMode(event.target.value as Mode)}
>
<option value="guided">
Expand Down Expand Up @@ -191,14 +205,15 @@ function ProtocolRunner({ id }: { id: string }) {
<div>
{!run.finished ? (
<ActionForm
key={session?.generation}
specs={protocol.actions}
busy={busy}
onAct={(action) => void submit([...actions, action])}
/>
) : (
<Debrief run={run} />
)}
{mode === "guided" && actions.length > 0 && !run.finished && (
{run.mode === "guided" && actions.length > 0 && !run.finished && (
<button
onClick={() => void submit(actions.slice(0, -1))}
disabled={busy}
Expand Down
Loading