1import type { EnvironmentId } from "@t3tools/contracts";
2import { afterEach, describe, expect, it, vi } from "vite-plus/test";
3
4const state = vi.hoisted(() => ({
5 desktop: false,
6 primary: null as string | null,
7 serverBrowser: new Set<string>(),
8}));
9
10vi.mock("~/env", () => ({ isElectron: false }));
11vi.mock("~/previewStateStore", () => ({ isPreviewSupportedInRuntime: () => state.desktop }));
12vi.mock("~/rpc/atomRegistry", () => ({ appAtomRegistry: { get: () => state.primary } }));
13vi.mock("~/state/primaryEnvironment", () => ({ primaryEnvironmentIdAtom: {} }));
14vi.mock("~/state/entities", () => ({
15 readEnvironmentSupportsServerBrowser: (id: string) => state.serverBrowser.has(id),
16 useEnvironmentSupportsServerBrowser: () => false,
17}));
18
19import { alternatePreviewRuntime, previewRuntimeFor } from "./previewRuntime";
20
21const local = "local" as EnvironmentId;
22const remote = "remote" as EnvironmentId;
23
24afterEach(() => {
25 state.desktop = false;
26 state.primary = null;
27 state.serverBrowser = new Set();
28});
29
30describe("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
47describe("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});