Skip to content

Commit fa2a2dc

Browse files
only21milcodexmaria-rcks
authored
fix(mobile): recognize this thread's main checkout (#23764)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent de386ad commit fa2a2dc

3 files changed

Lines changed: 103 additions & 2 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { act, type ReactNode } from "react";
2+
import { createRoot } from "react-dom/client";
3+
import { expect, it, vi } from "vite-plus/test";
4+
5+
const checkout = vi.hoisted(() => ({
6+
projectRoot: "",
7+
worktreePath: null as string | null,
8+
branches: [] as Array<{ name: string; worktreePath: string | null }>,
9+
}));
10+
11+
vi.mock("react-native", () => ({
12+
Platform: { OS: "android" },
13+
View: ({ children }: { children: ReactNode }) => <div>{children}</div>,
14+
ScrollView: ({ children }: { children: ReactNode }) => <div>{children}</div>,
15+
Pressable: ({ children, disabled }: { children: ReactNode; disabled: boolean }) => (
16+
<button disabled={disabled}>{children}</button>
17+
),
18+
useWindowDimensions: () => ({ height: 800 }),
19+
}));
20+
vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({}) }));
21+
vi.mock("react-native-safe-area-context", () => ({ useSafeAreaInsets: () => ({ bottom: 0 }) }));
22+
vi.mock("../../../components/ui/AndroidScreenHeader", () => ({ AndroidSheetHeader: () => null }));
23+
vi.mock("../../../components/ui/SheetHeader", () => ({ SheetHeader: () => null }));
24+
vi.mock("../../../components/ui/ScreenContent", () => ({
25+
ScreenContent: ({ children }: { children: ReactNode }) => <div>{children}</div>,
26+
}));
27+
vi.mock("../../../native/StackHeader", () => ({ NativeStackScreenOptions: () => null }));
28+
vi.mock("../../../components/ui/AppText", () => ({
29+
AppText: ({ children }: { children: ReactNode }) => <span>{children}</span>,
30+
AppTextInput: () => null,
31+
}));
32+
vi.mock("./gitSheetComponents", () => ({ SheetActionButton: () => null }));
33+
vi.mock("../../../state/use-thread-selection", () => ({
34+
useThreadSelection: () => ({
35+
selectedThread: { environmentId: "environment-1", worktreePath: checkout.worktreePath },
36+
selectedThreadProject: { workspaceRoot: checkout.projectRoot },
37+
}),
38+
}));
39+
vi.mock("../../../state/use-thread-detail", () => ({
40+
useSelectedThreadWorktreePath: () => checkout.worktreePath,
41+
}));
42+
vi.mock("../../../state/use-selected-thread-git-state", () => ({
43+
useSelectedThreadGitState: () => ({
44+
selectedThreadBranches: checkout.branches,
45+
selectedThreadBranchesLoading: false,
46+
gitOperationLabel: null,
47+
}),
48+
}));
49+
vi.mock("../../../state/use-selected-thread-git-actions", () => ({
50+
useSelectedThreadGitActions: () => ({ canChangeThreadBranch: true }),
51+
}));
52+
vi.mock("../../../state/query", () => ({ useEnvironmentQuery: () => ({ data: null }) }));
53+
vi.mock("../../../state/vcs", () => ({ vcsEnvironment: { status: () => null } }));
54+
55+
import { GitBranchesSheet } from "./GitBranchesSheet";
56+
57+
it("recognizes both the main checkout and a linked worktree as this thread's checkout", () => {
58+
const directory = "/repo";
59+
const linked = "/repo-linked";
60+
checkout.projectRoot = directory;
61+
checkout.branches = [
62+
{ name: "feature/available", worktreePath: null },
63+
{ name: "feature/linked", worktreePath: linked },
64+
{ name: "main", worktreePath: directory },
65+
];
66+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
67+
const container = document.createElement("div");
68+
const root = createRoot(container);
69+
const rows = () => {
70+
act(() => {
71+
root.render(
72+
<GitBranchesSheet
73+
route={{ params: { environmentId: "environment-1", threadId: "thread-1" } }}
74+
/>,
75+
);
76+
});
77+
return [...container.querySelectorAll("button")].map((button) => ({
78+
name: button.querySelectorAll("span")[0]?.textContent,
79+
subtitle: button.querySelectorAll("span")[1]?.textContent,
80+
disabled: button.disabled,
81+
}));
82+
};
83+
try {
84+
checkout.worktreePath = null;
85+
expect(rows()).toEqual([
86+
{ name: "feature/available", subtitle: "Local branch", disabled: false },
87+
{ name: "feature/linked", subtitle: "Checked out in another worktree", disabled: true },
88+
{ name: "main", subtitle: "Checked out in this thread", disabled: false },
89+
]);
90+
91+
checkout.worktreePath = linked;
92+
expect(rows()).toEqual([
93+
{ name: "feature/available", subtitle: "Local branch", disabled: false },
94+
{ name: "feature/linked", subtitle: "Checked out in this thread", disabled: false },
95+
{ name: "main", subtitle: "Checked out in another worktree", disabled: true },
96+
]);
97+
} finally {
98+
act(() => root.unmount());
99+
}
100+
});

‎apps/mobile/src/features/threads/git/GitBranchesSheet.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function GitBranchesSheet(_props: GitBranchesSheetProps) {
2828
const insets = useSafeAreaInsets();
2929
const { height: windowHeight } = useWindowDimensions();
3030
const { selectedThread } = useThreadSelection();
31-
const { selectedThreadCwd, selectedThreadWorktreePath } = useSelectedThreadWorktree();
31+
const { selectedThreadCwd } = useSelectedThreadWorktree();
3232
const gitState = useSelectedThreadGitState();
3333
const gitActions = useSelectedThreadGitActions();
3434
const { canChangeThreadBranch } = gitActions;
@@ -43,7 +43,7 @@ export function GitBranchesSheet(_props: GitBranchesSheetProps) {
4343
);
4444

4545
const currentBranchLabel = gitStatus.data?.refName ?? selectedThread?.branch ?? "Detached HEAD";
46-
const currentWorktreePath = selectedThreadWorktreePath;
46+
const currentWorktreePath = selectedThreadCwd;
4747
const availableBranches = gitState.selectedThreadBranches;
4848
const branchesLoading = gitState.selectedThreadBranchesLoading;
4949
const busy = gitState.gitOperationLabel !== null;

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { defineConfig, normalizePath } from "vite-plus";
44
import "vite-plus/test/config";
55

66
const reactTests = [
7+
"src/features/threads/git/GitBranchesSheet.occupancy.test.tsx",
78
"src/features/files/useFileTreeEntries.test.tsx",
89
"src/features/projects/AddProjectScreen.test.ts",
910
"src/lib/useMediaVideoPlayback.test.tsx",

0 commit comments

Comments
 (0)