The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { |
| 2 | EnvironmentId, |
| 3 | PreviewCloseInput, |
| 4 | PreviewSessionSnapshot, |
| 5 | ScopedThreadRef, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { Atom, AsyncResult } from "effect/reactivity"; |
| 8 | import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 9 | |
| 10 | import { |
| 11 | applyPreviewServerSnapshot, |
| 12 | readThreadPreviewState, |
| 13 | resetPreviewStateForTests, |
| 14 | } from "~/previewStateStore"; |
| 15 | |
| 16 | import { useClosedViewStore } from "~/closedViewStore"; |
| 17 | import { appAtomRegistry } from "~/rpc/atomRegistry"; |
| 18 | import { primaryEnvironmentIdAtom } from "~/state/primaryEnvironment"; |
| 19 | |
| 20 | import { closePreviewSession } from "./closePreviewSession"; |
| 21 | |
| 22 | vi.mock("~/env", async (importOriginal) => ({ |
| 23 | ...(await importOriginal<typeof import("~/env")>()), |
| 24 | isElectron: true, |
| 25 | })); |
| 26 | vi.mock("~/state/primaryEnvironment", async (importOriginal) => ({ |
| 27 | ...(await importOriginal<typeof import("~/state/primaryEnvironment")>()), |
| 28 | primaryEnvironmentIdAtom: Atom.make<EnvironmentId | null>(null), |
| 29 | })); |
| 30 | |
| 31 | const threadRef = { |
| 32 | environmentId: "local" as ScopedThreadRef["environmentId"], |
| 33 | threadId: "thread-1" as ScopedThreadRef["threadId"], |
| 34 | }; |
| 35 | |
| 36 | const snapshot: PreviewSessionSnapshot = { |
| 37 | threadId: threadRef.threadId, |
| 38 | tabId: "tab-1", |
| 39 | navStatus: { |
| 40 | _tag: "Success", |
| 41 | url: "http://localhost:3000/", |
| 42 | title: "Local app", |
| 43 | }, |
| 44 | canGoBack: false, |
| 45 | canGoForward: false, |
| 46 | updatedAt: "2026-06-18T19:00:00.000Z", |
| 47 | }; |
| 48 | |
| 49 | const setPrimaryEnvironment = (environmentId: EnvironmentId | null) => { |
| 50 | if (!Atom.isWritable<EnvironmentId | null, EnvironmentId | null>(primaryEnvironmentIdAtom)) { |
| 51 | throw new Error("primaryEnvironmentIdAtom is not writable"); |
| 52 | } |
| 53 | appAtomRegistry.set(primaryEnvironmentIdAtom, environmentId); |
| 54 | }; |
| 55 | |
| 56 | beforeEach(() => { |
| 57 | resetPreviewStateForTests(); |
| 58 | setPrimaryEnvironment(null); |
| 59 | useClosedViewStore.setState({ entries: [] }); |
| 60 | }); |
| 61 | |
| 62 | describe("closePreviewSession", () => { |
| 63 | it("suppresses stale server snapshots while the close is in flight", async () => { |
| 64 | applyPreviewServerSnapshot(threadRef, snapshot); |
| 65 | let finishClose: (() => void) | undefined; |
| 66 | const closePreview = vi.fn( |
| 67 | (_input: PreviewCloseInput) => |
| 68 | new Promise<ReturnType<typeof AsyncResult.success<void>>>((resolve) => { |
| 69 | finishClose = () => resolve(AsyncResult.success(undefined)); |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | const closing = closePreviewSession({ |
| 74 | closePreview: ({ input }) => closePreview(input), |
| 75 | snapshot, |
| 76 | tabId: snapshot.tabId, |
| 77 | threadRef, |
| 78 | }); |
| 79 | |
| 80 | expect(useClosedViewStore.getState().entries).toEqual([]); |
| 81 | expect(readThreadPreviewState(threadRef).sessions).toEqual({}); |
| 82 | applyPreviewServerSnapshot(threadRef, snapshot); |
| 83 | expect(readThreadPreviewState(threadRef).sessions).toEqual({}); |
| 84 | |
| 85 | finishClose?.(); |
| 86 | await closing; |
| 87 | expect(useClosedViewStore.getState().entries).toMatchObject([ |
| 88 | { kind: "browser", threadRef, snapshot }, |
| 89 | ]); |
| 90 | expect(closePreview).toHaveBeenCalledWith({ threadId: "thread-1", tabId: "tab-1" }); |
| 91 | }); |
| 92 | |
| 93 | it("keeps a tab this desktop renders mounted until the server has closed it", async () => { |
| 94 | setPrimaryEnvironment(threadRef.environmentId); |
| 95 | const desktopSnapshot = { ...snapshot, runtime: "server" as const }; |
| 96 | applyPreviewServerSnapshot(threadRef, desktopSnapshot); |
| 97 | let finishClose: (() => void) | undefined; |
| 98 | const closing = closePreviewSession({ |
| 99 | closePreview: () => |
| 100 | new Promise<ReturnType<typeof AsyncResult.success<void>>>((resolve) => { |
| 101 | finishClose = () => resolve(AsyncResult.success(undefined)); |
| 102 | }), |
| 103 | snapshot: desktopSnapshot, |
| 104 | tabId: desktopSnapshot.tabId, |
| 105 | threadRef, |
| 106 | }); |
| 107 | |
| 108 | expect(readThreadPreviewState(threadRef).sessions).toEqual({ "tab-1": desktopSnapshot }); |
| 109 | finishClose?.(); |
| 110 | await closing; |
| 111 | expect(readThreadPreviewState(threadRef).sessions).toEqual({}); |
| 112 | applyPreviewServerSnapshot(threadRef, desktopSnapshot); |
| 113 | expect(readThreadPreviewState(threadRef).sessions).toEqual({}); |
| 114 | }); |
| 115 | }); |