|
8 | 8 | type PreviewOpenInput, |
9 | 9 | type PreviewSessionSnapshot, |
10 | 10 | } from "@t3tools/contracts"; |
| 11 | +import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime"; |
| 12 | +import * as Cause from "effect/Cause"; |
11 | 13 | import { AsyncResult, Atom } from "effect/unstable/reactivity"; |
12 | 14 | import { act } from "react"; |
13 | 15 | import { create, type ReactTestRenderer } from "react-test-renderer"; |
@@ -38,7 +40,7 @@ const mocks = vi.hoisted(() => ({ |
38 | 40 | vi.fn< |
39 | 41 | (target: { environmentId: EnvironmentId; input: PreviewAutomationResponse }) => Promise<void> |
40 | 42 | >(), |
41 | | - focus: vi.fn(async () => undefined), |
| 43 | + focus: vi.fn<() => Promise<AtomCommandResult<void, Error>>>(), |
42 | 44 | })); |
43 | 45 |
|
44 | 46 | vi.mock("~/localApi", () => ({ |
@@ -117,6 +119,7 @@ beforeEach(async () => { |
117 | 119 | vi.clearAllMocks(); |
118 | 120 | mocks.getClientSettings.mockReset().mockResolvedValue(savedSettings); |
119 | 121 | mocks.respond.mockReset(); |
| 122 | + mocks.focus.mockReset().mockResolvedValue(AsyncResult.success(undefined)); |
120 | 123 | __resetClientSettingsPersistenceForTests(); |
121 | 124 | resetPreviewStateForTests(); |
122 | 125 | useBrowserSurfaceStore.setState({ byTabId: {} }); |
@@ -297,6 +300,42 @@ describe("PreviewAutomationHosts ownership", () => { |
297 | 300 | ); |
298 | 301 | }); |
299 | 302 |
|
| 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 | + |
300 | 339 | it("does not claim an available runtime from a server snapshot alone", async () => { |
301 | 340 | const response = deferred<PreviewAutomationResponse>(); |
302 | 341 | mocks.respond.mockImplementationOnce(async ({ input }) => response.resolve(input)); |
|
0 commit comments