|
| 1 | +import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | +import { act } from "react"; |
| 3 | +import { createRoot, type Root } from "react-dom/client"; |
| 4 | +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; |
| 5 | + |
| 6 | +const state = vi.hoisted(() => ({ |
| 7 | + environments: [] as Array<{ |
| 8 | + environmentId: string; |
| 9 | + connection: { phase: string }; |
| 10 | + serverConfig: { scratchWorkspaceRoot: string }; |
| 11 | + }>, |
| 12 | +})); |
| 13 | +vi.mock(import("../state/environments"), async (original) => ({ |
| 14 | + ...(await original()), |
| 15 | + useEnvironments: (() => ({ environments: state.environments })) as never, |
| 16 | +})); |
| 17 | +vi.mock(import("../state/use-atom-command"), async (original) => ({ |
| 18 | + ...(await original()), |
| 19 | + useAtomCommand: (() => vi.fn()) as never, |
| 20 | +})); |
| 21 | +vi.mock(import("./useHandleNewThread"), async (original) => ({ |
| 22 | + ...(await original()), |
| 23 | + useNewThreadHandler: (() => vi.fn()) as never, |
| 24 | +})); |
| 25 | +import { useScratchProject } from "./useScratchProject"; |
| 26 | + |
| 27 | +const LOCAL = EnvironmentId.make("local"); |
| 28 | +const REMOTE = EnvironmentId.make("remote"); |
| 29 | +const OTHER = EnvironmentId.make("other"); |
| 30 | + |
| 31 | +function environment(environmentId: EnvironmentId, phase: string) { |
| 32 | + return { |
| 33 | + environmentId, |
| 34 | + connection: { phase }, |
| 35 | + serverConfig: { scratchWorkspaceRoot: `/scratch/${environmentId}` }, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function Probe({ current }: { current: EnvironmentId }) { |
| 40 | + return <output>{useScratchProject().scratchEnvironmentId(current) ?? "none"}</output>; |
| 41 | +} |
| 42 | + |
| 43 | +let root: Root; |
| 44 | +let container: HTMLDivElement; |
| 45 | + |
| 46 | +beforeEach(() => { |
| 47 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 48 | + container = document.createElement("div"); |
| 49 | + root = createRoot(container); |
| 50 | +}); |
| 51 | +afterEach(async () => { |
| 52 | + await act(async () => root.unmount()); |
| 53 | + vi.unstubAllGlobals(); |
| 54 | +}); |
| 55 | + |
| 56 | +async function scratchTargetFrom(current: EnvironmentId) { |
| 57 | + await act(async () => root.render(<Probe current={current} />)); |
| 58 | + return container.textContent; |
| 59 | +} |
| 60 | + |
| 61 | +it("falls back from an unavailable current machine only to a sole connected alternative", async () => { |
| 62 | + state.environments = [environment(REMOTE, "reconnecting"), environment(LOCAL, "connected")]; |
| 63 | + expect(await scratchTargetFrom(REMOTE)).toBe(LOCAL); |
| 64 | + |
| 65 | + state.environments = [environment(REMOTE, "connected"), environment(LOCAL, "connected")]; |
| 66 | + expect(await scratchTargetFrom(REMOTE)).toBe(REMOTE); |
| 67 | + |
| 68 | + state.environments = [ |
| 69 | + environment(REMOTE, "reconnecting"), |
| 70 | + environment(LOCAL, "connected"), |
| 71 | + environment(OTHER, "connected"), |
| 72 | + ]; |
| 73 | + expect(await scratchTargetFrom(REMOTE)).toBe("none"); |
| 74 | +}); |
0 commit comments