Skip to content

Commit 78d0668

Browse files
only21milcodexmaria-rcks
authored
fix(mobile): mark the thread worktree's branch as current (pingdotgg#23436)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 2ce3185 commit 78d0668

3 files changed

Lines changed: 109 additions & 2 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
});

‎apps/mobile/src/state/use-selected-thread-git-state.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { useThreadSelection } from "./use-thread-selection";
1010
import { useSelectedThreadWorktree } from "./use-selected-thread-worktree";
1111

1212
export function useSelectedThreadGitState() {
13-
const { selectedThread, selectedThreadProject } = useThreadSelection();
13+
const { selectedThread } = useThreadSelection();
1414
const { selectedThreadCwd } = useSelectedThreadWorktree();
1515

1616
const selectedThreadGitTarget = {
@@ -29,7 +29,7 @@ export function useSelectedThreadGitState() {
2929

3030
const selectedThreadBranchTarget = {
3131
environmentId: selectedThread?.environmentId ?? null,
32-
cwd: selectedThreadProject?.workspaceRoot ?? null,
32+
cwd: selectedThreadCwd,
3333
query: null,
3434
};
3535
const selectedThreadBranchState = useBranches(selectedThreadBranchTarget);

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const reactTests = [
2828
"src/native/useRetryingImageSource.test.tsx",
2929
"src/state/queries.debounce.test.tsx",
3030
"src/state/use-selected-thread-git-actions.test.tsx",
31+
"src/state/use-selected-thread-git-state.test.tsx",
3132
"src/features/terminal/useTerminalExitNavigation.test.tsx",
3233
"src/features/threads/use-thread-list-now.test.tsx",
3334
"src/features/home/home-list-options.test.tsx",

0 commit comments

Comments
 (0)