TypeScript
fix(web): keep known preview tabs while the session list refreshes (#14955)
Alex Southwell26c241144a9e
diff --git a/apps/web/src/components/preview/previewSessionSync.test.ts b/apps/web/src/components/preview/previewSessionSync.test.tsnew file mode 100644index 00000000000..d0b1a1955e2--- /dev/null+++ b/apps/web/src/components/preview/previewSessionSync.test.ts@@ -0,0 +1,219 @@+import {+ EnvironmentId,+ ThreadId,+ type PreviewEvent,+ type PreviewListResult,+ type ScopedThreadRef,+} from "@t3tools/contracts";+import * as Cause from "effect/Cause";+import { AsyncResult, Atom } from "effect/reactivity";+import { act, createElement } from "react";+import { create, type ReactTestRenderer } from "react-test-renderer";+import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";++// Only the transport is substituted. The hook, atom registry and preview+// store run unchanged, including lazy evaluation and refresh notifications.+type ListResult = AsyncResult.AsyncResult<PreviewListResult, never>;+type EventResult = AsyncResult.AsyncResult<PreviewEvent, never>;+const harness = vi.hoisted(() => ({+ lists: new Map<+ string,+ {+ box: { current: ListResult; requests: number };+ atom: Atom.Writable<ListResult>;+ }+ >(),+ events: new Map<string, Atom.Writable<EventResult>>(),+}));++vi.mock("~/state/preview", async () => {+ const { AsyncResult, Atom } = await import("effect/reactivity");+ return {+ previewEnvironment: {+ list: (target: { input: { threadId: string } }) => {+ const key = target.input.threadId;+ let entry = harness.lists.get(key);+ if (!entry) {+ const box: { current: ListResult; requests: number } = {+ current: AsyncResult.initial(),+ requests: 0,+ };+ const atom = Atom.writable(+ () => {+ box.requests += 1;+ return AsyncResult.waiting(box.current);+ },+ (ctx, value: ListResult) => {+ box.current = value;+ ctx.setSelf(value);+ },+ );+ entry = { box, atom };+ harness.lists.set(key, entry);+ }+ return entry.atom;+ },+ events: (target: { environmentId: string }) => {+ let atom = harness.events.get(target.environmentId);+ if (!atom) {+ atom = Atom.make<EventResult>(AsyncResult.initial());+ harness.events.set(target.environmentId, atom);+ }+ return atom;+ },+ },+ };+});++import {+ applyPreviewServerSnapshot,+ applyPreviewDesktopState,+ readThreadPreviewState,+ reconcilePreviewServerSessions,+ resetPreviewStateForTests,+} from "~/previewStateStore";+import { AppAtomRegistryProvider, appAtomRegistry } from "~/rpc/atomRegistry";+import { previewEnvironment } from "~/state/preview";+import { usePreviewSession } from "./usePreviewSession";++let renderer: ReactTestRenderer | null = null;+let sequence = 0;+let ref: ScopedThreadRef;++function SessionSync() {+ usePreviewSession(ref);+ return null;+}++const listAtom = () => {+ previewEnvironment.list({ environmentId: ref.environmentId, input: { threadId: ref.threadId } });+ return harness.lists.get(ref.threadId)!.atom;+};+const result = (+ revision: number,+ tabIds: string[],+ serverEpoch = "epoch-1",+): PreviewListResult => ({+ serverEpoch,+ revision,+ sessions: tabIds.map((tabId) => ({+ threadId: ref.threadId,+ tabId,+ navStatus: { _tag: "Idle" },+ canGoBack: false,+ canGoForward: false,+ updatedAt: "2026-10-02T00:00:00.000Z",+ })),+});++async function mount() {+ await act(async () => {+ renderer = create(createElement(AppAtomRegistryProvider, null, createElement(SessionSync)));+ });+}++beforeEach(() => {+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);+ sequence += 1;+ ref = {+ environmentId: EnvironmentId.make(`preview-sync-${sequence}`),+ threadId: ThreadId.make(`thread-sync-${sequence}`),+ };+});++afterEach(async () => {+ await act(async () => renderer?.unmount());+ renderer = null;+ resetPreviewStateForTests();+ vi.unstubAllGlobals();+});++describe("native preview session synchronization", () => {+ it("evaluates the lazy list once and restores sessions that predate mounting", async () => {+ const atom = listAtom();+ await mount();+ expect(harness.lists.get(ref.threadId)?.box.requests).toBe(1);+ appAtomRegistry.set(atom, AsyncResult.success(result(1, ["restored"])));+ expect(readThreadPreviewState(ref).activeTabId).toBe("restored");+ });++ it("preserves a local tab while cached absence is waiting for a fresh response", async () => {+ const atom = listAtom();+ appAtomRegistry.set(atom, AsyncResult.waiting(AsyncResult.success(result(1, []))));+ applyPreviewServerSnapshot(ref, result(1, ["local"]).sessions[0]!);+ await mount();+ expect(readThreadPreviewState(ref).activeTabId).toBe("local");+ expect(harness.lists.get(ref.threadId)?.box.requests).toBe(0);+ appAtomRegistry.set(atom, AsyncResult.success(result(2, ["local"])));+ expect(readThreadPreviewState(ref).activeTabId).toBe("local");+ appAtomRegistry.set(atom, AsyncResult.success(result(3, [])));+ expect(readThreadPreviewState(ref).activeTabId).toBeNull();+ });++ it("refreshes a completed cache without treating its old absence as authoritative", async () => {+ const atom = listAtom();+ appAtomRegistry.set(atom, AsyncResult.success(result(1, [])));+ reconcilePreviewServerSessions(ref, result(1, []));+ applyPreviewServerSnapshot(ref, result(1, ["local"]).sessions[0]!);+ applyPreviewDesktopState(ref, "local", {+ hasWebContents: true,+ canGoBack: true,+ canGoForward: false,+ loading: false,+ zoomFactor: 1.5,+ pictureInPicture: false,+ colorScheme: "system",+ audioMuted: false,+ audible: false,+ controller: "human",+ favicon: null,+ });+ await mount();+ expect(harness.lists.get(ref.threadId)?.box.requests).toBe(1);+ expect(readThreadPreviewState(ref).activeTabId).toBe("local");+ expect(readThreadPreviewState(ref).desktopOverlay?.zoomFactor).toBe(1.5);+ appAtomRegistry.set(atom, AsyncResult.success(result(2, ["local"])));+ expect(readThreadPreviewState(ref).activeTabId).toBe("local");+ expect(readThreadPreviewState(ref).desktopOverlay?.canGoBack).toBe(true);+ });++ it("retains newer event state when an older list response completes, then retries the list", async () => {+ const atom = listAtom();+ await mount();+ const snapshot = result(2, ["opened"]).sessions[0]!;+ previewEnvironment.events({ environmentId: ref.environmentId, input: {} });+ appAtomRegistry.set(+ harness.events.get(ref.environmentId)!,+ AsyncResult.success({+ type: "opened",+ threadId: ref.threadId,+ tabId: snapshot.tabId,+ snapshot,+ serverEpoch: "epoch-1",+ revision: 2,+ createdAt: snapshot.updatedAt,+ }),+ );+ await act(async () => appAtomRegistry.set(atom, AsyncResult.success(result(1, []))));+ expect(readThreadPreviewState(ref).activeTabId).toBe("opened");+ expect(readThreadPreviewState(ref).serverRevision).toBe(2);+ expect(readThreadPreviewState(ref).listLoaded).toBe(false);+ // The overtaken list is not a baseline, so the hook asks again; its+ // waiting state must not reconcile, and the fresh answer completes it.+ expect(harness.lists.get(ref.threadId)?.box.requests).toBe(2);+ expect(readThreadPreviewState(ref).activeTabId).toBe("opened");+ appAtomRegistry.set(atom, AsyncResult.success(result(2, ["opened"])));+ expect(readThreadPreviewState(ref).listLoaded).toBe(true);+ expect(readThreadPreviewState(ref).activeTabId).toBe("opened");+ });++ it("keeps the local snapshot on a completed failure and reconciles a later success", async () => {+ const atom = listAtom();+ applyPreviewServerSnapshot(ref, result(1, ["local"]).sessions[0]!);+ await mount();+ appAtomRegistry.set(atom, AsyncResult.failure(Cause.die("offline")));+ expect(readThreadPreviewState(ref).activeTabId).toBe("local");+ appAtomRegistry.set(atom, AsyncResult.success(result(2, [])));+ expect(readThreadPreviewState(ref).activeTabId).toBeNull();+ });+});diff --git a/apps/web/src/components/preview/usePreviewSession.ts b/apps/web/src/components/preview/usePreviewSession.tsindex b529d044f39..6166da8e428 100644--- a/apps/web/src/components/preview/usePreviewSession.ts+++ b/apps/web/src/components/preview/usePreviewSession.ts@@ -42,9 +42,9 @@ const previewSessionSyncAtom = Atom.family((threadKey: string) => {let eventsVersion = 0;const reconcileSessions = (result: Atom.Type<typeof sessionsAtom>) => {- if (!AsyncResult.isSuccess(result)) return;+ if (result.waiting || !AsyncResult.isSuccess(result)) return;reconcilePreviewServerSessions(threadRef, result.value);- if (!result.waiting && !readThreadPreviewState(threadRef).listLoaded) {+ if (!readThreadPreviewState(threadRef).listLoaded) {// An event overtook the first list. Retry the authoritative baseline;// do not declare a partial event-only index ready for surface cleanup.queueMicrotask(() => {@@ -67,6 +67,9 @@ const previewSessionSyncAtom = Atom.family((threadKey: string) => {disposed = true;});const initialEvent = get.once(eventsAtom);+ // Subscribing alone does not evaluate a lazy list. Start it now, but+ // reconcile only subsequent responses: a completed cache may be stale.+ get.once(sessionsAtom);get.subscribe(sessionsAtom, (result) => {reconcileSessions(result);});@@ -81,7 +84,8 @@ const previewSessionSyncAtom = Atom.family((threadKey: string) => {// The cached list can predate an automation-created tab. Keep the local// snapshot visible until an authoritative refresh arrives instead of// reconciling against a stale empty result when the panel first mounts.- get.refresh(sessionsAtom);+ const current = get.once(sessionsAtom);+ if (!AsyncResult.isInitial(current) && !current.waiting) get.refresh(sessionsAtom);if (eventsVersion === 0) applyLatestEvent(initialEvent);});}).pipe(Atom.setIdleTTL(1_000), Atom.withLabel(`preview:session-sync:${threadKey}`));