Skip to content

Commit 4a5fd65

Browse files
committed
fix(preview): allow failed host reports to retry
1 parent 9d0d36f commit 4a5fd65

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎apps/web/src/components/preview/PreviewAutomationHosts.test.tsx‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
type PreviewOpenInput,
99
type PreviewSessionSnapshot,
1010
} from "@t3tools/contracts";
11+
import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime";
12+
import * as Cause from "effect/Cause";
1113
import { AsyncResult, Atom } from "effect/unstable/reactivity";
1214
import { act } from "react";
1315
import { create, type ReactTestRenderer } from "react-test-renderer";
@@ -38,7 +40,7 @@ const mocks = vi.hoisted(() => ({
3840
vi.fn<
3941
(target: { environmentId: EnvironmentId; input: PreviewAutomationResponse }) => Promise<void>
4042
>(),
41-
focus: vi.fn(async () => undefined),
43+
focus: vi.fn<() => Promise<AtomCommandResult<void, Error>>>(),
4244
}));
4345

4446
vi.mock("~/localApi", () => ({
@@ -117,6 +119,7 @@ beforeEach(async () => {
117119
vi.clearAllMocks();
118120
mocks.getClientSettings.mockReset().mockResolvedValue(savedSettings);
119121
mocks.respond.mockReset();
122+
mocks.focus.mockReset().mockResolvedValue(AsyncResult.success(undefined));
120123
__resetClientSettingsPersistenceForTests();
121124
resetPreviewStateForTests();
122125
useBrowserSurfaceStore.setState({ byTabId: {} });
@@ -297,6 +300,42 @@ describe("PreviewAutomationHosts ownership", () => {
297300
);
298301
});
299302

303+
it.each([false, true])(
304+
"retries failed reports without clearing newer connection reports (reconnect: %s)",
305+
async (reconnect) => {
306+
const report = deferred<Awaited<ReturnType<typeof mocks.focus>>>();
307+
mocks.focus.mockReturnValueOnce(report.promise);
308+
await act(() => {
309+
appAtomRegistry.set(
310+
requestsAtom,
311+
AsyncResult.success({ type: "connected", connectionId: "first" }),
312+
);
313+
});
314+
if (reconnect) {
315+
await act(() => {
316+
appAtomRegistry.set(
317+
requestsAtom,
318+
AsyncResult.success({ type: "connected", connectionId: "second" }),
319+
);
320+
});
321+
}
322+
await act(async () => {
323+
report.resolve(AsyncResult.failure(Cause.fail(new Error("Focus report failed"))));
324+
await report.promise;
325+
});
326+
expect(mocks.focus).toHaveBeenCalledTimes(reconnect ? 2 : 1);
327+
await act(() => {
328+
document.dispatchEvent(new Event("visibilitychange"));
329+
});
330+
expect(mocks.focus).toHaveBeenCalledTimes(2);
331+
expect(mocks.focus).toHaveBeenLastCalledWith(
332+
expect.objectContaining({
333+
input: expect.objectContaining({ connectionId: reconnect ? "second" : "first" }),
334+
}),
335+
);
336+
},
337+
);
338+
300339
it("does not claim an available runtime from a server snapshot alone", async () => {
301340
const response = deferred<PreviewAutomationResponse>();
302341
mocks.respond.mockImplementationOnce(async ({ input }) => response.resolve(input));

‎apps/web/src/components/preview/PreviewAutomationHosts.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,11 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
844844
const reportKey = JSON.stringify(input);
845845
if (lastFocusReportRef.current === reportKey) return;
846846
lastFocusReportRef.current = reportKey;
847-
void focusAutomationHost({ environmentId, input });
847+
void focusAutomationHost({ environmentId, input }).then((result) => {
848+
if (result._tag === "Failure" && lastFocusReportRef.current === reportKey) {
849+
lastFocusReportRef.current = null;
850+
}
851+
});
848852
};
849853
report();
850854
window.addEventListener("focus", report);

0 commit comments

Comments
 (0)