Skip to content

Commit 3688254

Browse files
committed
fix(web): show Preparing machine when Auto returns to an earlier machine
1 parent a01ded5 commit 3688254

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

‎apps/web/src/components/chat/useDraftEnvironmentRouting.browser.test.tsx‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const scratchProject = (environmentId: EnvironmentId) => ({
108108
});
109109

110110
describe("useDraftEnvironmentRouting", () => {
111-
it("ignores a late scratch open for an old Auto pick and retries when Auto is re-armed", async () => {
111+
it("shows Preparing machine only while the current Auto scratch open runs", async () => {
112112
balancer.setup(machineA);
113113
scratchOpens.setup();
114114
useComposerDraftStore
@@ -120,27 +120,35 @@ describe("useDraftEnvironmentRouting", () => {
120120
{ environmentSelection: "auto" },
121121
);
122122
await using screen = await render(<Harness />);
123+
const opened = () => scratchOpens.opens.map((open) => open.environmentId);
123124
await expect.element(screen.getByText("Preparing machine")).toBeInTheDocument();
124125

126+
// A late open for an old pick neither moves the draft nor leaves it preparing.
125127
await act(() => balancer.set(machineB));
126-
expect(scratchOpens.opens.map((open) => open.environmentId)).toEqual([machineA, machineB]);
127128
await act(() => scratchOpens.opens[1]!.resolve(null));
128129
await act(() => scratchOpens.opens[0]!.resolve(scratchProject(machineA)));
129130
await expect.element(screen.getByText("Ready")).toBeInTheDocument();
130131
expect(useComposerDraftStore.getState().getDraftSession(draftId)?.environmentId).toBe(local);
131-
expect(scratchOpens.opens).toHaveLength(2);
132+
expect(opened()).toEqual([machineA, machineB]);
132133

134+
// Re-arming Auto retries the failed open.
133135
await userEvent.click(screen.getByRole("button"));
134-
expect(scratchOpens.opens.map((open) => open.environmentId)).toEqual([
135-
machineA,
136-
machineB,
137-
machineB,
138-
]);
139-
await act(() => scratchOpens.opens[2]!.resolve(scratchProject(machineB)));
136+
expect(opened()).toEqual([machineA, machineB, machineB]);
137+
await act(() => scratchOpens.opens[2]!.resolve(null));
138+
139+
// Returning to a target whose open failed opens it again, and shows it.
140+
await act(() => balancer.set(machineA));
141+
await act(() => scratchOpens.opens[3]!.resolve(null));
142+
await expect.element(screen.getByText("Ready")).toBeInTheDocument();
143+
await act(() => balancer.set(machineB));
144+
await act(() => balancer.set(machineA));
145+
expect(opened()).toEqual([machineA, machineB, machineB, machineA, machineB, machineA]);
146+
await expect.element(screen.getByText("Preparing machine")).toBeInTheDocument();
147+
await act(() => scratchOpens.opens[5]!.resolve(scratchProject(machineA)));
140148
await expect.element(screen.getByText("Ready")).toBeInTheDocument();
141149
expect(useComposerDraftStore.getState().getDraftSession(draftId)).toMatchObject({
142-
environmentId: machineB,
143-
loadBalancedEnvironmentId: machineB,
150+
environmentId: machineA,
151+
loadBalancedEnvironmentId: machineA,
144152
});
145153
});
146154
});

‎apps/web/src/components/chat/useDraftEnvironmentRouting.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ export function useDraftEnvironmentRouting(input: DraftEnvironmentRoutingInput)
146146
? `${draftId}:${autoScratchPlan.environmentId}`
147147
: null;
148148
const [settledAutoScratchKey, setSettledAutoScratchKey] = useState<string | null>(null);
149+
// A new target cancels the old open, so a return to an earlier target opens it again.
150+
const [seenAutoScratchKey, setSeenAutoScratchKey] = useState(autoScratchRequestKey);
151+
if (seenAutoScratchKey !== autoScratchRequestKey) {
152+
setSeenAutoScratchKey(autoScratchRequestKey);
153+
setSettledAutoScratchKey(null);
154+
}
149155
const isEnvironmentChanging =
150156
isManualEnvironmentChanging ||
151157
(autoScratchRequestKey !== null && settledAutoScratchKey !== autoScratchRequestKey);

0 commit comments

Comments
 (0)