|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { act, useEffect, useReducer } from "react"; |
| 3 | +import { createRoot, type Root } from "react-dom/client"; |
| 4 | +import { AsyncResult } from "effect/unstable/reactivity"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 6 | + |
| 7 | +const harness = vi.hoisted(() => ({ |
| 8 | + selection: { |
| 9 | + selectedThread: { environmentId: "environment-1", id: "thread-1" } as unknown, |
| 10 | + selectedThreadProject: { workspaceRoot: "/repo" } as unknown, |
| 11 | + selectedEnvironmentRuntime: { connectionState: "connected" } as unknown, |
| 12 | + }, |
| 13 | + worktree: { selectedThreadCwd: "/repo" as string | null, selectedThreadWorktreePath: null }, |
| 14 | + refreshStatusCalls: [] as Array<unknown>, |
| 15 | + renderCount: 0, |
| 16 | + // Mirrors refreshStatus invalidating refs: settled refreshes rerender consumers. |
| 17 | + onRefreshSettled: () => {}, |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock("./use-atom-command", () => ({ |
| 21 | + useAtomCommand: (command: unknown) => |
| 22 | + command === "refreshStatus" |
| 23 | + ? async (input: unknown) => { |
| 24 | + harness.refreshStatusCalls.push(input); |
| 25 | + harness.onRefreshSettled(); |
| 26 | + return AsyncResult.success({}); |
| 27 | + } |
| 28 | + : async () => AsyncResult.success({}), |
| 29 | +})); |
| 30 | +vi.mock("./vcs", () => ({ |
| 31 | + vcsEnvironment: { refreshStatus: "refreshStatus" }, |
| 32 | + vcsActionManager: { runStackedAction: () => "runStackedAction", track: vi.fn() }, |
| 33 | +})); |
| 34 | +vi.mock("./threads", () => ({ threadEnvironment: { updateMetadata: "updateMetadata" } })); |
| 35 | +vi.mock("./queries", () => ({ useBranches: () => ({ data: null, refresh: () => {} }) })); |
| 36 | +vi.mock("../lib/uuid", () => ({ uuidv4: () => "uuid" })); |
| 37 | +vi.mock("./atom-registry", () => ({ appAtomRegistry: {} })); |
| 38 | +vi.mock("./use-remote-environment-registry", () => ({ setPendingConnectionError: () => {} })); |
| 39 | +vi.mock("./use-vcs-action-state", () => ({ showGitActionResult: () => {} })); |
| 40 | +// Runs on every render: a refresh loop fails fast here instead of exhausting memory. |
| 41 | +vi.mock("./use-thread-selection", () => ({ |
| 42 | + useThreadSelection: () => { |
| 43 | + harness.renderCount += 1; |
| 44 | + if (harness.renderCount > 50) { |
| 45 | + throw new Error("Consumer rerendered more than 50 times"); |
| 46 | + } |
| 47 | + return harness.selection; |
| 48 | + }, |
| 49 | +})); |
| 50 | +vi.mock("./use-selected-thread-worktree", () => ({ |
| 51 | + useSelectedThreadWorktree: () => harness.worktree, |
| 52 | +})); |
| 53 | + |
| 54 | +import { useSelectedThreadGitActions } from "./use-selected-thread-git-actions"; |
| 55 | + |
| 56 | +let root: Root | null = null; |
| 57 | +let rerender: () => void = () => {}; |
| 58 | + |
| 59 | +// Stands in for a Git sheet: every settled refresh causes a rerender, as the |
| 60 | +// refs invalidation does on device. |
| 61 | +function Consumer() { |
| 62 | + const [, bump] = useReducer((count: number) => count + 1, 0); |
| 63 | + useEffect(() => { |
| 64 | + rerender = bump; |
| 65 | + }); |
| 66 | + useSelectedThreadGitActions(); |
| 67 | + return null; |
| 68 | +} |
| 69 | + |
| 70 | +async function flush() { |
| 71 | + for (let index = 0; index < 5; index += 1) { |
| 72 | + await act(async () => { |
| 73 | + await Promise.resolve(); |
| 74 | + }); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +beforeEach(() => { |
| 79 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 80 | + harness.selection = { |
| 81 | + selectedThread: { environmentId: "environment-1", id: "thread-1" }, |
| 82 | + selectedThreadProject: { workspaceRoot: "/repo" }, |
| 83 | + selectedEnvironmentRuntime: { connectionState: "connected" }, |
| 84 | + }; |
| 85 | + harness.worktree = { selectedThreadCwd: "/repo", selectedThreadWorktreePath: null }; |
| 86 | + harness.refreshStatusCalls = []; |
| 87 | + harness.renderCount = 0; |
| 88 | + harness.onRefreshSettled = () => rerender(); |
| 89 | +}); |
| 90 | + |
| 91 | +afterEach(() => { |
| 92 | + act(() => root?.unmount()); |
| 93 | + root = null; |
| 94 | +}); |
| 95 | + |
| 96 | +describe("useSelectedThreadGitActions", () => { |
| 97 | + it("refreshes once on mount and does not loop when refreshes rerender the consumer", async () => { |
| 98 | + root = createRoot(document.createElement("div")); |
| 99 | + act(() => root!.render(<Consumer />)); |
| 100 | + await flush(); |
| 101 | + |
| 102 | + // Shell updates replace the thread object without changing the selection. |
| 103 | + harness.selection = { |
| 104 | + selectedThread: { environmentId: "environment-1", id: "thread-1" }, |
| 105 | + selectedThreadProject: { workspaceRoot: "/repo" }, |
| 106 | + selectedEnvironmentRuntime: { connectionState: "connected" }, |
| 107 | + }; |
| 108 | + act(() => rerender()); |
| 109 | + await flush(); |
| 110 | + |
| 111 | + expect(harness.refreshStatusCalls).toEqual([ |
| 112 | + { environmentId: "environment-1", input: { cwd: "/repo" } }, |
| 113 | + ]); |
| 114 | + }); |
| 115 | + |
| 116 | + it("refreshes again when the selected thread or cwd changes", async () => { |
| 117 | + root = createRoot(document.createElement("div")); |
| 118 | + act(() => root!.render(<Consumer />)); |
| 119 | + await flush(); |
| 120 | + |
| 121 | + harness.selection = { |
| 122 | + selectedThread: { environmentId: "environment-1", id: "thread-2" }, |
| 123 | + selectedThreadProject: { workspaceRoot: "/repo" }, |
| 124 | + selectedEnvironmentRuntime: { connectionState: "connected" }, |
| 125 | + }; |
| 126 | + act(() => rerender()); |
| 127 | + await flush(); |
| 128 | + |
| 129 | + harness.worktree = { selectedThreadCwd: "/repo/worktree", selectedThreadWorktreePath: null }; |
| 130 | + act(() => rerender()); |
| 131 | + await flush(); |
| 132 | + |
| 133 | + expect(harness.refreshStatusCalls).toEqual([ |
| 134 | + { environmentId: "environment-1", input: { cwd: "/repo" } }, |
| 135 | + { environmentId: "environment-1", input: { cwd: "/repo" } }, |
| 136 | + { environmentId: "environment-1", input: { cwd: "/repo/worktree" } }, |
| 137 | + ]); |
| 138 | + }); |
| 139 | + |
| 140 | + it("refreshes once after the environment reconnects and not while it is away", async () => { |
| 141 | + root = createRoot(document.createElement("div")); |
| 142 | + act(() => root!.render(<Consumer />)); |
| 143 | + await flush(); |
| 144 | + |
| 145 | + for (const connectionState of ["reconnecting", "connecting", "offline"]) { |
| 146 | + harness.selection = { |
| 147 | + ...harness.selection, |
| 148 | + selectedEnvironmentRuntime: { connectionState }, |
| 149 | + }; |
| 150 | + act(() => rerender()); |
| 151 | + await flush(); |
| 152 | + } |
| 153 | + expect(harness.refreshStatusCalls).toHaveLength(1); |
| 154 | + |
| 155 | + // The server's cached status can be stale after a long background, so the |
| 156 | + // new session refreshes it once. |
| 157 | + harness.selection = { |
| 158 | + ...harness.selection, |
| 159 | + selectedEnvironmentRuntime: { connectionState: "connected" }, |
| 160 | + }; |
| 161 | + act(() => rerender()); |
| 162 | + await flush(); |
| 163 | + act(() => rerender()); |
| 164 | + await flush(); |
| 165 | + |
| 166 | + expect(harness.refreshStatusCalls).toEqual([ |
| 167 | + { environmentId: "environment-1", input: { cwd: "/repo" } }, |
| 168 | + { environmentId: "environment-1", input: { cwd: "/repo" } }, |
| 169 | + ]); |
| 170 | + }); |
| 171 | + |
| 172 | + it("waits for the environment to connect before the first refresh", async () => { |
| 173 | + harness.selection = { |
| 174 | + ...harness.selection, |
| 175 | + selectedEnvironmentRuntime: { connectionState: "connecting" }, |
| 176 | + }; |
| 177 | + root = createRoot(document.createElement("div")); |
| 178 | + act(() => root!.render(<Consumer />)); |
| 179 | + await flush(); |
| 180 | + expect(harness.refreshStatusCalls).toEqual([]); |
| 181 | + |
| 182 | + harness.selection = { |
| 183 | + ...harness.selection, |
| 184 | + selectedEnvironmentRuntime: { connectionState: "connected" }, |
| 185 | + }; |
| 186 | + act(() => rerender()); |
| 187 | + await flush(); |
| 188 | + |
| 189 | + expect(harness.refreshStatusCalls).toEqual([ |
| 190 | + { environmentId: "environment-1", input: { cwd: "/repo" } }, |
| 191 | + ]); |
| 192 | + }); |
| 193 | +}); |
0 commit comments