@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 172 lines 5.62 KB
Raw
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 type InputProps = {
6 accessibilityLabel: string;
7 value: string;
8 onChangeText: (value: string) => void;
9 };
10 type ActionProps = { label: string; disabled: boolean; onPress: () => void };
11 const state = vi.hoisted(() => ({
12 refName: null as string | null,
13 branch: null as string | null,
14 inputs: new Map<string, InputProps>(),
15 actions: new Map<string, ActionProps>(),
16 worktrees: [] as Array<{ baseBranch: string; newBranch: string }>,
17 focused: true,
18 goBackCalls: 0,
19 gitResult: Promise.resolve<object | null>(null),
20 }));
21
22 vi.mock("@react-navigation/native", () => ({
23 useNavigation: () => ({
24 goBack: () => state.goBackCalls++,
25 isFocused: () => state.focused,
26 }),
27 }));
28 vi.mock("../../../components/ui/SheetHeader", () => ({ SheetHeader: () => null }));
29 vi.mock("../../../components/ui/FitSheet", () => ({
30 FitSheet: ({ children }: { children: ReactNode }) => children,
31 }));
32 vi.mock("../../../native/StackHeader", () => ({ NativeStackScreenOptions: () => null }));
33 vi.mock("../../../components/ui/FormSection", () => ({
34 FormSection: ({ children }: { children: ReactNode }) => children,
35 FormField: (props: InputProps) => {
36 state.inputs.set(props.accessibilityLabel, props);
37 return null;
38 },
39 }));
40 vi.mock("../../../components/ui/ListSection", () => ({
41 ListSection: ({ children }: { children: ReactNode }) => children,
42 }));
43 vi.mock("../../../components/ui/ListChoiceRow", () => ({
44 ListChoiceRow: (props: ActionProps) => {
45 state.actions.set(props.label, props);
46 return null;
47 },
48 }));
49 vi.mock("../../../state/query", () => ({
50 useEnvironmentQuery: () => ({
51 data: state.refName === null ? undefined : { refName: state.refName },
52 }),
53 }));
54 vi.mock("../../../state/use-thread-selection", () => ({
55 useThreadSelection: () => ({
56 selectedThread: { environmentId: "environment", branch: state.branch },
57 }),
58 }));
59 vi.mock("../../../state/use-selected-thread-worktree", () => ({
60 useSelectedThreadWorktree: () => ({
61 selectedThreadCwd: "/repo",
62 selectedThreadWorktreePath: null,
63 }),
64 }));
65 vi.mock("../../../state/use-selected-thread-git-state", () => ({
66 useSelectedThreadGitState: () => ({
67 selectedThreadBranches: [{ name: "release/stable", worktreePath: null }],
68 selectedThreadBranchesLoading: false,
69 gitOperationLabel: null,
70 }),
71 }));
72 vi.mock("../../../state/use-selected-thread-git-actions", () => ({
73 useSelectedThreadGitActions: () => ({
74 canChangeThreadBranch: true,
75 onCreateSelectedThreadBranch: () => state.gitResult,
76 onCheckoutSelectedThreadBranch: () => state.gitResult,
77 onCreateSelectedThreadWorktree: (input: { baseBranch: string; newBranch: string }) => {
78 state.worktrees.push(input);
79 return state.gitResult;
80 },
81 }),
82 }));
83 vi.mock("../../../state/vcs", () => ({ vcsEnvironment: { branch: () => null } }));
84 vi.mock("./gitSheetComponents", () => ({
85 SheetActionButton: (props: ActionProps) => {
86 state.actions.set(props.label, props);
87 return null;
88 },
89 }));
90
91 import { GitBranchesSheet } from "./GitBranchesSheet";
92
93 function setup(branch: string | null = null) {
94 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
95 state.refName = null;
96 state.branch = branch;
97 state.inputs.clear();
98 state.actions.clear();
99 state.worktrees = [];
100 state.gitResult = Promise.resolve(null);
101 state.focused = true;
102 state.goBackCalls = 0;
103 const root = createRoot(document.createElement("div"));
104 const render = () =>
105 act(() => {
106 root.render(
107 <GitBranchesSheet
108 route={{ params: { environmentId: "environment", threadId: "thread" } }}
109 />,
110 );
111 });
112 render();
113 return {
114 render,
115 press(label: string) {
116 act(() => state.actions.get(label)!.onPress());
117 },
118 edit(label: string, value: string) {
119 act(() => state.inputs.get(label)!.onChangeText(value));
120 },
121 [Symbol.dispose]() {
122 act(() => root.unmount());
123 },
124 };
125 }
126
127 it.each([null, "stale-branch"])(
128 "uses the loaded checkout branch when thread metadata is %s",
129 (branch) => {
130 using screen = setup(branch);
131 screen.edit("Worktree branch name", "feature/new-worktree");
132 state.refName = "release/stable";
133 screen.render();
134 screen.press("Create worktree");
135 expect(state.worktrees).toEqual([
136 { baseBranch: "release/stable", newBranch: "feature/new-worktree" },
137 ]);
138 },
139 );
140
141 it.each(["custom-base", ""])("preserves an edited base %j when status arrives", (base) => {
142 using screen = setup();
143 screen.edit("Worktree base branch", base);
144 screen.edit("Worktree branch name", "feature/new-worktree");
145 state.refName = "release/stable";
146 screen.render();
147 expect(state.inputs.get("Worktree base branch")!.value).toBe(base);
148 expect(state.actions.get("Create worktree")!.disabled).toBe(base === "");
149 screen.press("Create worktree");
150 expect(state.worktrees).toEqual(
151 base === "" ? [] : [{ baseBranch: base, newBranch: "feature/new-worktree" }],
152 );
153 });
154
155 it.each(["Create worktree", "Create & checkout", "release/stable"])(
156 "does not navigate back when %s finishes after the sheet lost focus",
157 async (action) => {
158 using screen = setup();
159 const gitResult = Promise.withResolvers<object>();
160 state.gitResult = gitResult.promise;
161 screen.edit("New branch name", "feature/new-branch");
162 screen.edit("Worktree branch name", "feature/new-worktree");
163 screen.press(action);
164 state.focused = false;
165
166 await act(async () => {
167 gitResult.resolve({ refName: "feature/ready" });
168 await gitResult.promise;
169 });
170 expect(state.goBackCalls).toBe(0);
171 },
172 );