| 1 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 3 | |
| 4 | const state = vi.hoisted(() => ({ |
| 5 | desktop: false, |
| 6 | primary: null as string | null, |
| 7 | serverBrowser: new Set<string>(), |
| 8 | })); |
| 9 | |
| 10 | vi.mock("~/env", () => ({ isElectron: false })); |
| 11 | vi.mock("~/previewStateStore", () => ({ isPreviewSupportedInRuntime: () => state.desktop })); |
| 12 | vi.mock("~/rpc/atomRegistry", () => ({ appAtomRegistry: { get: () => state.primary } })); |
| 13 | vi.mock("~/state/primaryEnvironment", () => ({ primaryEnvironmentIdAtom: {} })); |
| 14 | vi.mock("~/state/entities", () => ({ |
| 15 | readEnvironmentSupportsServerBrowser: (id: string) => state.serverBrowser.has(id), |
| 16 | useEnvironmentSupportsServerBrowser: () => false, |
| 17 | })); |
| 18 | |
| 19 | import { alternatePreviewRuntime, previewRuntimeFor } from "./previewRuntime"; |
| 20 | |
| 21 | const local = "local" as EnvironmentId; |
| 22 | const remote = "remote" as EnvironmentId; |
| 23 | |
| 24 | afterEach(() => { |
| 25 | state.desktop = false; |
| 26 | state.primary = null; |
| 27 | state.serverBrowser = new Set(); |
| 28 | }); |
| 29 | |
| 30 | describe("previewRuntimeFor", () => { |
| 31 | it("opens a remote environment's tabs on this computer in the desktop app", () => { |
| 32 | state.desktop = true; |
| 33 | state.primary = local; |
| 34 | state.serverBrowser = new Set([local, remote]); |
| 35 | |
| 36 | expect(previewRuntimeFor(remote)).toBeUndefined(); |
| 37 | expect(previewRuntimeFor(local)).toBe("server"); |
| 38 | }); |
| 39 | |
| 40 | it("uses the environment's browser where the client has none of its own", () => { |
| 41 | state.serverBrowser = new Set([remote]); |
| 42 | |
| 43 | expect(previewRuntimeFor(remote)).toBe("server"); |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | describe("alternatePreviewRuntime", () => { |
| 48 | it("moves a remote environment's tab between this computer and the environment", () => { |
| 49 | state.desktop = true; |
| 50 | |
| 51 | expect(alternatePreviewRuntime(remote, local, true, {})).toBe("server"); |
| 52 | expect(alternatePreviewRuntime(remote, local, true, { runtime: "server" })).toBe("desktop"); |
| 53 | expect(alternatePreviewRuntime(local, local, true, {})).toBeNull(); |
| 54 | expect(alternatePreviewRuntime(remote, local, false, {})).toBeNull(); |
| 55 | }); |
| 56 | |
| 57 | it("offers no move outside the desktop app", () => { |
| 58 | expect(alternatePreviewRuntime(remote, null, true, { runtime: "server" })).toBeNull(); |
| 59 | }); |
| 60 | }); |