The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | EnvironmentId, |
| 5 | type PreviewViewportSetting, |
| 6 | ThreadId, |
| 7 | type PreviewSessionSnapshot, |
| 8 | } from "@t3tools/contracts"; |
| 9 | |
| 10 | const mocks = vi.hoisted(() => ({ |
| 11 | resize: Symbol("preview-resize"), |
| 12 | runAtomCommand: vi.fn(), |
| 13 | updatePreviewServerSnapshot: vi.fn(), |
| 14 | })); |
| 15 | |
| 16 | vi.mock("@t3tools/client-runtime/state/runtime", () => ({ |
| 17 | runAtomCommand: mocks.runAtomCommand, |
| 18 | squashAtomCommandFailure: (result: { readonly error: unknown }) => result.error, |
| 19 | })); |
| 20 | vi.mock("~/rpc/atomRegistry", () => ({ appAtomRegistry: {} })); |
| 21 | vi.mock("~/state/preview", () => ({ previewEnvironment: { resize: mocks.resize } })); |
| 22 | vi.mock("~/previewStateStore", () => ({ |
| 23 | updatePreviewServerSnapshot: mocks.updatePreviewServerSnapshot, |
| 24 | })); |
| 25 | vi.mock("~/components/ui/toast", () => ({ toastManager: { add: vi.fn() } })); |
| 26 | |
| 27 | import { commitBrowserViewportChange, queueBrowserViewportCommit } from "./browserViewportActions"; |
| 28 | |
| 29 | describe("browserViewportActions", () => { |
| 30 | it("commits viewport changes in order for each tab", async () => { |
| 31 | let releaseFirst: (() => void) | undefined; |
| 32 | let markFirstStarted: (() => void) | undefined; |
| 33 | const firstPending = new Promise<void>((resolve) => { |
| 34 | releaseFirst = resolve; |
| 35 | }); |
| 36 | const firstStarted = new Promise<void>((resolve) => { |
| 37 | markFirstStarted = resolve; |
| 38 | }); |
| 39 | const calls: Array<number> = []; |
| 40 | const resize = async (setting: PreviewViewportSetting) => { |
| 41 | if (setting._tag === "fill") return; |
| 42 | calls.push(setting.width); |
| 43 | if (setting.width === 800) { |
| 44 | markFirstStarted?.(); |
| 45 | await firstPending; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | const first = queueBrowserViewportCommit("tab-serial", () => |
| 50 | resize({ _tag: "freeform", width: 800, height: 600 }), |
| 51 | ); |
| 52 | const second = queueBrowserViewportCommit("tab-serial", () => |
| 53 | resize({ _tag: "freeform", width: 900, height: 700 }), |
| 54 | ); |
| 55 | await firstStarted; |
| 56 | expect(calls).toEqual([800]); |
| 57 | |
| 58 | releaseFirst?.(); |
| 59 | await Promise.all([first, second]); |
| 60 | expect(calls).toEqual([800, 900]); |
| 61 | }); |
| 62 | |
| 63 | it("resizes a tab that no preview panel is showing", async () => { |
| 64 | const threadRef = { |
| 65 | environmentId: EnvironmentId.make("env-1"), |
| 66 | threadId: ThreadId.make("thread-1"), |
| 67 | }; |
| 68 | const snapshot = { tabId: "tab-1" } as PreviewSessionSnapshot; |
| 69 | mocks.runAtomCommand.mockResolvedValueOnce({ _tag: "Success", value: snapshot }); |
| 70 | const viewport: PreviewViewportSetting = { _tag: "freeform", width: 390, height: 844 }; |
| 71 | |
| 72 | await commitBrowserViewportChange( |
| 73 | { threadRef, tabId: "tab-1", runtimeTabId: "runtime-tab-1" }, |
| 74 | viewport, |
| 75 | ); |
| 76 | |
| 77 | expect(mocks.runAtomCommand).toHaveBeenCalledWith( |
| 78 | expect.anything(), |
| 79 | mocks.resize, |
| 80 | { |
| 81 | environmentId: threadRef.environmentId, |
| 82 | input: { threadId: threadRef.threadId, tabId: "tab-1", viewport }, |
| 83 | }, |
| 84 | expect.anything(), |
| 85 | ); |
| 86 | expect(mocks.updatePreviewServerSnapshot).toHaveBeenCalledWith(threadRef, snapshot); |
| 87 | }); |
| 88 | }); |