From 0485186be0eed4d03fb6e28b1d3e190f313173f7 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Fri, 20 Nov 2026 08:02:33 +0100 Subject: [PATCH 1/4] test(mobile): cover branch actions finishing after sheet dismissal Co-authored-by: GPT 6.2 Sol --- .../threads/git/GitBranchesSheet.test.tsx | 107 +++++++++++++++--- 1 file changed, 91 insertions(+), 16 deletions(-) diff --git a/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx b/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx index fef45b5c88003..bfb46719d040f 100644 --- a/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx +++ b/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx @@ -1,3 +1,4 @@ +import * as NodeModule from "node:module"; import { act, type ReactNode } from "react"; import { createRoot } from "react-dom/client"; import { expect, it, vi } from "vite-plus/test"; @@ -8,15 +9,21 @@ type InputProps = { onChangeText: (value: string) => void; }; type ActionProps = { label: string; disabled: boolean; onPress: () => void }; +type Navigation = Pick< + import("@react-navigation/native").NavigationProp, + "goBack" | "isFocused" +>; const state = vi.hoisted(() => ({ refName: null as string | null, branch: null as string | null, inputs: new Map(), actions: new Map(), worktrees: [] as Array<{ baseBranch: string; newBranch: string }>, + navigation: null as Navigation | null, + gitResult: Promise.resolve(null), })); -vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({ goBack() {} }) })); +vi.mock("@react-navigation/native", () => ({ useNavigation: () => state.navigation })); vi.mock("../../../components/ui/SheetHeader", () => ({ SheetHeader: () => null })); vi.mock("../../../components/ui/FitSheet", () => ({ FitSheet: ({ children }: { children: ReactNode }) => children, @@ -29,8 +36,15 @@ vi.mock("../../../components/ui/FormSection", () => ({ return null; }, })); -vi.mock("../../../components/ui/ListSection", () => ({ ListSection: () => null })); -vi.mock("../../../components/ui/ListChoiceRow", () => ({ ListChoiceRow: () => null })); +vi.mock("../../../components/ui/ListSection", () => ({ + ListSection: ({ children }: { children: ReactNode }) => children, +})); +vi.mock("../../../components/ui/ListChoiceRow", () => ({ + ListChoiceRow: (props: ActionProps) => { + state.actions.set(props.label, props); + return null; + }, +})); vi.mock("../../../state/query", () => ({ useEnvironmentQuery: () => ({ data: state.refName === null ? undefined : { refName: state.refName }, @@ -49,7 +63,7 @@ vi.mock("../../../state/use-selected-thread-worktree", () => ({ })); vi.mock("../../../state/use-selected-thread-git-state", () => ({ useSelectedThreadGitState: () => ({ - selectedThreadBranches: [], + selectedThreadBranches: [{ name: "release/stable", worktreePath: null }], selectedThreadBranchesLoading: false, gitOperationLabel: null, }), @@ -57,9 +71,11 @@ vi.mock("../../../state/use-selected-thread-git-state", () => ({ vi.mock("../../../state/use-selected-thread-git-actions", () => ({ useSelectedThreadGitActions: () => ({ canChangeThreadBranch: true, - onCreateSelectedThreadWorktree: async (input: { baseBranch: string; newBranch: string }) => { + onCreateSelectedThreadBranch: () => state.gitResult, + onCheckoutSelectedThreadBranch: () => state.gitResult, + onCreateSelectedThreadWorktree: (input: { baseBranch: string; newBranch: string }) => { state.worktrees.push(input); - return null; + return state.gitResult; }, }), })); @@ -73,6 +89,16 @@ vi.mock("./gitSheetComponents", () => ({ import { GitBranchesSheet } from "./GitBranchesSheet"; +const requireNavigation = NodeModule.createRequire( + NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"), +); +const { CommonActions, StackActions, StackRouter } = requireNavigation( + "@react-navigation/routers", +) as Pick< + typeof import("@react-navigation/native"), + "CommonActions" | "StackActions" | "StackRouter" +>; + function setup(branch: string | null = null) { (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; state.refName = null; @@ -80,24 +106,48 @@ function setup(branch: string | null = null) { state.inputs.clear(); state.actions.clear(); state.worktrees = []; + state.gitResult = Promise.resolve(null); + + const router = StackRouter({}); + const options = { + routeNames: ["Home", "Thread", "GitBranches"], + routeParamList: {}, + routeGetIdList: {}, + }; + let stack = router.getInitialState(options); + function dispatch(action: Parameters[1]) { + const next = router.getStateForAction(stack, action, options); + if (next) stack = router.getRehydratedState(next, options); + } + const thread = { environmentId: "environment", threadId: "thread" }; + const open = () => dispatch(StackActions.push("GitBranches", thread)); + dispatch(StackActions.push("Thread", thread)); + open(); + const sheetKey = stack.routes[stack.index]!.key; + state.navigation = { + goBack: () => dispatch({ ...CommonActions.goBack(), source: sheetKey }), + isFocused: () => stack.routes[stack.index]?.key === sheetKey, + }; const root = createRoot(document.createElement("div")); const render = () => act(() => { - root.render( - , - ); + root.render(); }); render(); return { render, + open, + dismiss() { + dispatch(StackActions.pop()); + act(() => root.render(null)); + }, + activeRoute: () => stack.routes[stack.index]?.name, + press(label: string) { + act(() => state.actions.get(label)!.onPress()); + }, edit(label: string, value: string) { act(() => state.inputs.get(label)!.onChangeText(value)); }, - create() { - act(() => state.actions.get("Create worktree")!.onPress()); - }, [Symbol.dispose]() { act(() => root.unmount()); }, @@ -111,7 +161,7 @@ it.each([null, "stale-branch"])( screen.edit("Worktree branch name", "feature/new-worktree"); state.refName = "release/stable"; screen.render(); - screen.create(); + screen.press("Create worktree"); expect(state.worktrees).toEqual([ { baseBranch: "release/stable", newBranch: "feature/new-worktree" }, ]); @@ -126,8 +176,33 @@ it.each(["custom-base", ""])("preserves an edited base %j when status arrives", screen.render(); expect(state.inputs.get("Worktree base branch")!.value).toBe(base); expect(state.actions.get("Create worktree")!.disabled).toBe(base === ""); - screen.create(); + screen.press("Create worktree"); expect(state.worktrees).toEqual( base === "" ? [] : [{ baseBranch: base, newBranch: "feature/new-worktree" }], ); }); + +it.each([ + { action: "Create worktree", reopen: false, activeRoute: "Thread" }, + { action: "Create worktree", reopen: true, activeRoute: "GitBranches" }, + { action: "Create & checkout", reopen: false, activeRoute: "Thread" }, + { action: "release/stable", reopen: false, activeRoute: "Thread" }, +])( + "keeps $activeRoute open when $action finishes after dismissal", + async ({ action, reopen, activeRoute }) => { + using screen = setup(); + const gitResult = Promise.withResolvers(); + state.gitResult = gitResult.promise; + screen.edit("New branch name", "feature/new-branch"); + screen.edit("Worktree branch name", "feature/new-worktree"); + screen.press(action); + screen.dismiss(); + if (reopen) screen.open(); + + await act(async () => { + gitResult.resolve({ refName: "feature/ready" }); + await gitResult.promise; + }); + expect(screen.activeRoute()).toBe(activeRoute); + }, +); From 3879bed82213e43370b060b51fee26946c12f6b5 Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Fri, 20 Nov 2026 08:02:33 +0100 Subject: [PATCH 2/4] fix(mobile): keep chat open when branch actions finish after dismissal Co-authored-by: GPT 6.2 Sol --- apps/mobile/src/features/threads/git/GitBranchesSheet.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/mobile/src/features/threads/git/GitBranchesSheet.tsx b/apps/mobile/src/features/threads/git/GitBranchesSheet.tsx index 7c610a4456675..df1ad6649c8f7 100644 --- a/apps/mobile/src/features/threads/git/GitBranchesSheet.tsx +++ b/apps/mobile/src/features/threads/git/GitBranchesSheet.tsx @@ -83,7 +83,7 @@ export function GitBranchesSheet(_props: GitBranchesSheetProps) { const branch = sanitizeFeatureBranchName(newBranchName.trim()); if (branch.length === 0) return; void gitActions.onCreateSelectedThreadBranch(branch).then((result) => { - if (result === null) return; + if (result === null || !navigation.isFocused()) return; setNewBranchName(""); navigation.goBack(); }); @@ -123,7 +123,7 @@ export function GitBranchesSheet(_props: GitBranchesSheetProps) { void gitActions .onCreateSelectedThreadWorktree({ baseBranch, newBranch }) .then((result) => { - if (result === null) return; + if (result === null || !navigation.isFocused()) return; setWorktreeBranchName(""); navigation.goBack(); }); @@ -161,7 +161,7 @@ export function GitBranchesSheet(_props: GitBranchesSheetProps) { disabled={!canChangeThreadBranch || busy || disabled} onPress={() => { void gitActions.onCheckoutSelectedThreadBranch(branch.name).then((result) => { - if (result === null) return; + if (result === null || !navigation.isFocused()) return; navigation.goBack(); }); }} From 5b6426c734718e3d9c72a19fe5d2041b6a39f8fc Mon Sep 17 00:00:00 2001 From: Wout Stiens <71498452+StiensWout@users.noreply.github.com> Date: Fri, 20 Nov 2026 08:09:56 +0100 Subject: [PATCH 3/4] chore: credit the models that worked on this change Co-authored-by: Claude Opus 5.5 From ca090fb9306f9bbde0bec52c3f071045bf71b72c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 07:23:57 +0000 Subject: [PATCH 4/4] test(mobile): fake focus instead of driving a real stack router The guard only reads navigation.isFocused(), so a focus flag proves it without reaching into @react-navigation/routers through createRequire. Co-Authored-By: Claude Opus 5.5 --- .../threads/git/GitBranchesSheet.test.tsx | 76 +++++-------------- 1 file changed, 20 insertions(+), 56 deletions(-) diff --git a/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx b/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx index bfb46719d040f..084dd7880fba1 100644 --- a/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx +++ b/apps/mobile/src/features/threads/git/GitBranchesSheet.test.tsx @@ -1,4 +1,3 @@ -import * as NodeModule from "node:module"; import { act, type ReactNode } from "react"; import { createRoot } from "react-dom/client"; import { expect, it, vi } from "vite-plus/test"; @@ -9,21 +8,23 @@ type InputProps = { onChangeText: (value: string) => void; }; type ActionProps = { label: string; disabled: boolean; onPress: () => void }; -type Navigation = Pick< - import("@react-navigation/native").NavigationProp, - "goBack" | "isFocused" ->; const state = vi.hoisted(() => ({ refName: null as string | null, branch: null as string | null, inputs: new Map(), actions: new Map(), worktrees: [] as Array<{ baseBranch: string; newBranch: string }>, - navigation: null as Navigation | null, + focused: true, + goBackCalls: 0, gitResult: Promise.resolve(null), })); -vi.mock("@react-navigation/native", () => ({ useNavigation: () => state.navigation })); +vi.mock("@react-navigation/native", () => ({ + useNavigation: () => ({ + goBack: () => state.goBackCalls++, + isFocused: () => state.focused, + }), +})); vi.mock("../../../components/ui/SheetHeader", () => ({ SheetHeader: () => null })); vi.mock("../../../components/ui/FitSheet", () => ({ FitSheet: ({ children }: { children: ReactNode }) => children, @@ -89,16 +90,6 @@ vi.mock("./gitSheetComponents", () => ({ import { GitBranchesSheet } from "./GitBranchesSheet"; -const requireNavigation = NodeModule.createRequire( - NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"), -); -const { CommonActions, StackActions, StackRouter } = requireNavigation( - "@react-navigation/routers", -) as Pick< - typeof import("@react-navigation/native"), - "CommonActions" | "StackActions" | "StackRouter" ->; - function setup(branch: string | null = null) { (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; state.refName = null; @@ -107,41 +98,20 @@ function setup(branch: string | null = null) { state.actions.clear(); state.worktrees = []; state.gitResult = Promise.resolve(null); - - const router = StackRouter({}); - const options = { - routeNames: ["Home", "Thread", "GitBranches"], - routeParamList: {}, - routeGetIdList: {}, - }; - let stack = router.getInitialState(options); - function dispatch(action: Parameters[1]) { - const next = router.getStateForAction(stack, action, options); - if (next) stack = router.getRehydratedState(next, options); - } - const thread = { environmentId: "environment", threadId: "thread" }; - const open = () => dispatch(StackActions.push("GitBranches", thread)); - dispatch(StackActions.push("Thread", thread)); - open(); - const sheetKey = stack.routes[stack.index]!.key; - state.navigation = { - goBack: () => dispatch({ ...CommonActions.goBack(), source: sheetKey }), - isFocused: () => stack.routes[stack.index]?.key === sheetKey, - }; + state.focused = true; + state.goBackCalls = 0; const root = createRoot(document.createElement("div")); const render = () => act(() => { - root.render(); + root.render( + , + ); }); render(); return { render, - open, - dismiss() { - dispatch(StackActions.pop()); - act(() => root.render(null)); - }, - activeRoute: () => stack.routes[stack.index]?.name, press(label: string) { act(() => state.actions.get(label)!.onPress()); }, @@ -182,27 +152,21 @@ it.each(["custom-base", ""])("preserves an edited base %j when status arrives", ); }); -it.each([ - { action: "Create worktree", reopen: false, activeRoute: "Thread" }, - { action: "Create worktree", reopen: true, activeRoute: "GitBranches" }, - { action: "Create & checkout", reopen: false, activeRoute: "Thread" }, - { action: "release/stable", reopen: false, activeRoute: "Thread" }, -])( - "keeps $activeRoute open when $action finishes after dismissal", - async ({ action, reopen, activeRoute }) => { +it.each(["Create worktree", "Create & checkout", "release/stable"])( + "does not navigate back when %s finishes after the sheet lost focus", + async (action) => { using screen = setup(); const gitResult = Promise.withResolvers(); state.gitResult = gitResult.promise; screen.edit("New branch name", "feature/new-branch"); screen.edit("Worktree branch name", "feature/new-worktree"); screen.press(action); - screen.dismiss(); - if (reopen) screen.open(); + state.focused = false; await act(async () => { gitResult.resolve({ refName: "feature/ready" }); await gitResult.promise; }); - expect(screen.activeRoute()).toBe(activeRoute); + expect(state.goBackCalls).toBe(0); }, );