|
| 1 | +import * as NodeChildProcess from "node:child_process"; |
| 2 | +import * as NodeFS from "node:fs"; |
| 3 | +import * as NodeOS from "node:os"; |
| 4 | +import * as NodePath from "node:path"; |
| 5 | +import { act, useLayoutEffect } from "react"; |
| 6 | +import { createRoot } from "react-dom/client"; |
| 7 | +import { expect, it, vi } from "vite-plus/test"; |
| 8 | + |
| 9 | +const harness = vi.hoisted(() => ({ |
| 10 | + root: "", |
| 11 | + cwd: "", |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("./use-thread-selection", () => ({ |
| 15 | + useThreadSelection: () => ({ |
| 16 | + selectedThread: { environmentId: "environment-1" }, |
| 17 | + selectedThreadProject: { workspaceRoot: harness.root }, |
| 18 | + }), |
| 19 | +})); |
| 20 | +vi.mock("./use-selected-thread-worktree", () => ({ |
| 21 | + useSelectedThreadWorktree: () => ({ selectedThreadCwd: harness.cwd }), |
| 22 | +})); |
| 23 | +vi.mock("./use-vcs-action-state", () => ({ |
| 24 | + useVcsActionState: () => ({ currentLabel: null }), |
| 25 | +})); |
| 26 | +vi.mock("./sourceControl", () => ({ |
| 27 | + sourceControlEnvironment: { discovery: () => null }, |
| 28 | +})); |
| 29 | +vi.mock("./query", () => ({ useEnvironmentQuery: () => ({ data: null }) })); |
| 30 | +vi.mock("./queries", () => ({ |
| 31 | + useBranches: ({ cwd }: { cwd: string }) => ({ |
| 32 | + data: { |
| 33 | + refs: NodeChildProcess.execFileSync( |
| 34 | + "git", |
| 35 | + ["for-each-ref", "--format=%(refname:short) %(HEAD)", "refs/heads"], |
| 36 | + { |
| 37 | + cwd, |
| 38 | + encoding: "utf8", |
| 39 | + }, |
| 40 | + ) |
| 41 | + .trim() |
| 42 | + .split("\n") |
| 43 | + .map((line) => ({ |
| 44 | + name: line.split(" ")[0], |
| 45 | + current: line.endsWith("*"), |
| 46 | + isRemote: false, |
| 47 | + })), |
| 48 | + }, |
| 49 | + isPending: false, |
| 50 | + }), |
| 51 | +})); |
| 52 | + |
| 53 | +import { useSelectedThreadGitState } from "./use-selected-thread-git-state"; |
| 54 | + |
| 55 | +it("marks the selected checkout's branch current when switching threads in one project", () => { |
| 56 | + const directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "mobile-branches-")); |
| 57 | + const git = (...args: string[]) => |
| 58 | + NodeChildProcess.execFileSync("git", ["-c", "commit.gpgsign=false", ...args], { |
| 59 | + cwd: directory, |
| 60 | + encoding: "utf8", |
| 61 | + stdio: "pipe", |
| 62 | + }); |
| 63 | + const root = createRoot(document.createElement("div")); |
| 64 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 65 | + let currentBranches: string[] = []; |
| 66 | + function Consumer() { |
| 67 | + const { selectedThreadBranches } = useSelectedThreadGitState(); |
| 68 | + useLayoutEffect(() => { |
| 69 | + currentBranches = selectedThreadBranches |
| 70 | + .filter((branch) => branch.current) |
| 71 | + .map((branch) => branch.name); |
| 72 | + }, [selectedThreadBranches]); |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + git("init", "--initial-branch=main"); |
| 78 | + git( |
| 79 | + "-c", |
| 80 | + "user.name=Test", |
| 81 | + "-c", |
| 82 | + |
| 83 | + "commit", |
| 84 | + "--allow-empty", |
| 85 | + "-m", |
| 86 | + "initial", |
| 87 | + ); |
| 88 | + const worktree = NodePath.join(directory, "linked"); |
| 89 | + git("worktree", "add", "-b", "feature/mobile", worktree); |
| 90 | + harness.root = directory; |
| 91 | + harness.cwd = directory; |
| 92 | + act(() => root.render(<Consumer />)); |
| 93 | + expect(currentBranches).toEqual(["main"]); |
| 94 | + |
| 95 | + harness.cwd = worktree; |
| 96 | + act(() => root.render(<Consumer />)); |
| 97 | + expect(currentBranches).toEqual(["feature/mobile"]); |
| 98 | + |
| 99 | + harness.cwd = directory; |
| 100 | + act(() => root.render(<Consumer />)); |
| 101 | + expect(currentBranches).toEqual(["main"]); |
| 102 | + } finally { |
| 103 | + act(() => root.unmount()); |
| 104 | + NodeFS.rmSync(directory, { recursive: true, force: true }); |
| 105 | + } |
| 106 | +}); |
0 commit comments