Open History to see the last commit
1import type {
2 GitActionRequestInput,
3 RunVcsStackedActionInput,
4} from "@t3tools/client-runtime/state/vcs";
5import { act, type ReactNode } from "react";
6import { createRoot } from "react-dom/client";
7import { expect, it, vi } from "vite-plus/test";
8
9type PressProps = {
10 label?: string;
11 accessibilityLabel?: string;
12 accessibilityState?: { checked?: boolean };
13 onPress: () => void;
14};
15type InputProps = {
16 value?: string;
17 onChangeText?: (text: string) => void;
18};
19const state = vi.hoisted(() => ({
20 refName: "main" as string | null,
21 cwd: "/repo",
22 files: [
23 { path: "include.ts", insertions: 1, deletions: 0 },
24 { path: "later.ts", insertions: 1, deletions: 0 },
25 ],
26 result: null as object | null,
27 pressables: [] as PressProps[],
28 buttons: [] as PressProps[],
29 inputs: [] as InputProps[],
30 ran: [] as object[],
31 createdBranch: undefined as string | undefined,
32 waitBeforeProgress: null as Promise<void> | null,
33}));
34
35vi.mock("react-native", () => ({
36 View: ({ children }: { children: ReactNode }) => children,
37 Pressable: (props: PressProps & { children: ReactNode }) => {
38 state.pressables.push(props);
39 return null;
40 },
41}));
42vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({ goBack: () => {} }) }));
43vi.mock("../../../components/ui/SheetHeader", () => ({ SheetHeader: () => null }));
44vi.mock("../../../components/ui/FitSheet", () => ({
45 FitSheet: ({ children }: { children: ReactNode }) => children,
46}));
47vi.mock("../../../components/ui/AppSymbol", () => ({ SymbolView: () => null }));
48vi.mock("../../../components/ui/AppText", () => ({
49 AppText: () => null,
50 AppTextInput: (props: InputProps) => {
51 state.inputs.push(props);
52 return null;
53 },
54}));
55vi.mock("../../../components/ui/Button", () => ({
56 Button: (props: PressProps) => {
57 state.buttons.push(props);
58 return null;
59 },
60}));
61vi.mock("../../../state/query", () => ({
62 useEnvironmentQuery: () => ({
63 data: {
64 refName: state.refName,
65 isDefaultRef: false,
66 workingTree: {
67 files: [...state.files],
68 },
69 },
70 }),
71}));
72vi.mock("../../../state/use-thread-selection", () => ({
73 useThreadSelection: () => ({ selectedThread: { environmentId: "environment" } }),
74}));
75vi.mock("../../../state/use-selected-thread-worktree", () => ({
76 useSelectedThreadWorktree: () => ({ selectedThreadCwd: state.cwd }),
77}));
78vi.mock("../../../state/use-selected-thread-git-state", () => ({
79 useSelectedThreadGitState: () => ({ gitOperationLabel: null }),
80}));
81vi.mock("../../../state/use-selected-thread-git-actions", () => ({
82 useSelectedThreadGitActions: () => ({
83 canWriteSourceControl: true,
84 canCreateFeatureBranch: true,
85 onRunSelectedThreadGitAction: async ({
86 onProgress,
87 ...input
88 }: GitActionRequestInput & Pick<RunVcsStackedActionInput, "onProgress">) => {
89 state.ran.push(input);
90 if (state.waitBeforeProgress) await state.waitBeforeProgress;
91 if (state.createdBranch) {
92 onProgress?.({
93 actionId: "commit",
94 cwd: state.cwd,
95 action: "commit",
96 kind: "action_failed",
97 phase: "commit",
98 message: "Commit rejected",
99 createdBranch: state.createdBranch,
100 });
101 }
102 return state.result;
103 },
104 }),
105}));
106vi.mock("../../../state/vcs", () => ({ vcsEnvironment: { status: () => null } }));
107
108import { GitCommitSheet } from "./GitCommitSheet";
109
110function openSheet() {
111 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
112 const root = createRoot(document.createElement("div"));
113 const render = () => {
114 state.pressables = [];
115 state.buttons = [];
116 state.inputs = [];
117 root.render(
118 <GitCommitSheet route={{ params: { environmentId: "environment", threadId: "thread" } }} />,
119 );
120 };
121 act(render);
122 const row = (path: string) => state.pressables.findLast((p) => p.accessibilityLabel === path)!;
123 const toggleAllButton = () =>
124 state.buttons.findLast((b) => b.label === "Select all" || b.label === "Deselect all");
125 return {
126 isIncluded: (path: string) => row(path).accessibilityState?.checked,
127 toggle: (path: string) => act(() => row(path).onPress()),
128 toggleAllLabel: () => toggleAllButton()?.label,
129 toggleAll: () => act(() => toggleAllButton()!.onPress()),
130 message: () => state.inputs.at(-1)?.value,
131 setMessage: (text: string) => act(() => state.inputs.at(-1)?.onChangeText?.(text)),
132 refresh: () => act(render),
133 // The sheet closes on submit, so it unmounts once the commit settles.
134 commit: async (label = "Commit changes") => {
135 await act(async () => state.buttons.findLast((b) => b.label === label)!.onPress());
136 act(() => root.unmount());
137 },
138 close: () => act(() => root.unmount()),
139 };
140}
141
142it("keeps excluded files and commit message after a failed commit until a commit succeeds", async () => {
143 state.refName = "main";
144 state.result = null;
145 state.ran = [];
146
147 const first = openSheet();
148 first.toggle("later.ts");
149 first.setMessage(" fix: preserve commit message ");
150 await first.commit();
151
152 const retry = openSheet();
153 expect(retry.isIncluded("later.ts")).toBe(false);
154 expect(retry.isIncluded("include.ts")).toBe(true);
155 expect(retry.message()).toBe(" fix: preserve commit message ");
156 state.result = { commit: { status: "created" } };
157 await retry.commit();
158 expect(state.ran).toEqual([
159 {
160 action: "commit",
161 featureBranch: false,
162 commitMessage: "fix: preserve commit message",
163 filePaths: ["include.ts"],
164 },
165 {
166 action: "commit",
167 featureBranch: false,
168 commitMessage: "fix: preserve commit message",
169 filePaths: ["include.ts"],
170 },
171 ]);
172
173 const afterSuccess = openSheet();
174 expect(afterSuccess.isIncluded("later.ts")).toBe(true);
175 expect(afterSuccess.message()).toBe("");
176 afterSuccess.close();
177});
178
179it("resets excluded files when moving from detached HEAD to a named branch", async () => {
180 state.refName = null;
181 state.result = null;
182 state.ran = [];
183
184 const detached = openSheet();
185 detached.toggle("later.ts");
186 await detached.commit();
187
188 const retryDetached = openSheet();
189 expect(retryDetached.isIncluded("later.ts")).toBe(false);
190 expect(retryDetached.isIncluded("include.ts")).toBe(true);
191 retryDetached.close();
192
193 state.refName = "feature";
194 const onBranch = openSheet();
195 expect(onBranch.isIncluded("include.ts")).toBe(true);
196 expect(onBranch.isIncluded("later.ts")).toBe(true);
197
198 state.result = { commit: { status: "created" } };
199 await onBranch.commit();
200 expect(state.ran).toEqual([
201 { action: "commit", featureBranch: false, filePaths: ["include.ts"] },
202 { action: "commit", featureBranch: false },
203 ]);
204});
205
206it.each(["before reopening", "after reopening"])(
207 "keeps retry exclusions when new branch status arrives %s",
208 async (arrival) => {
209 state.cwd = `/repo/${arrival}`;
210 state.refName = "main";
211 state.result = null;
212 state.ran = [];
213 state.createdBranch = "feature/retry";
214
215 const first = openSheet();
216 first.toggle("later.ts");
217 await first.commit("Commit on a new branch");
218 state.createdBranch = undefined;
219
220 if (arrival === "before reopening") state.refName = "feature/retry";
221 const retry = openSheet();
222 expect(retry.isIncluded("later.ts")).toBe(false);
223 state.refName = "feature/retry";
224 retry.refresh();
225 expect(retry.isIncluded("later.ts")).toBe(false);
226 state.result = { commit: { status: "created" } };
227 await retry.commit();
228 expect(state.ran).toEqual([
229 { action: "commit", featureBranch: true, filePaths: ["include.ts"] },
230 { action: "commit", featureBranch: false, filePaths: ["include.ts"] },
231 ]);
232 },
233);
234
235it.each([
236 { checkedOut: "feature/retry", includedAfterFailure: false },
237 { checkedOut: "feature/other", includedAfterFailure: true },
238])("reconciles an open retry sheet when the action settles on $checkedOut", async (scenario) => {
239 state.cwd = `/repo/pending/${scenario.checkedOut}`;
240 state.refName = "main";
241 state.result = null;
242 state.ran = [];
243 state.createdBranch = "feature/retry";
244 const failure = Promise.withResolvers<void>();
245 state.waitBeforeProgress = failure.promise;
246
247 const first = openSheet();
248 first.toggle("later.ts");
249 await first.commit("Commit on a new branch");
250
251 const retry = openSheet();
252 state.refName = scenario.checkedOut;
253 retry.refresh();
254 const includedWhileRunning = retry.isIncluded("later.ts");
255 await act(async () => failure.resolve());
256 state.waitBeforeProgress = null;
257 state.createdBranch = undefined;
258
259 expect(includedWhileRunning).toBe(false);
260 expect(retry.isIncluded("later.ts")).toBe(scenario.includedAfterFailure);
261 state.result = { commit: { status: "created" } };
262 await retry.commit();
263 expect(state.ran.at(-1)).toEqual({
264 action: "commit",
265 featureBranch: false,
266 ...(scenario.includedAfterFailure ? {} : { filePaths: ["include.ts"] }),
267 });
268});
269
270it("clears inherited exclusions and message on success with stale branch status", async () => {
271 state.cwd = "/repo/pending/success";
272 state.refName = "main";
273 state.result = { commit: { status: "created" } };
274 state.createdBranch = undefined;
275 const success = Promise.withResolvers<void>();
276 state.waitBeforeProgress = success.promise;
277
278 const first = openSheet();
279 first.toggle("later.ts");
280 first.setMessage("fix: stale branch commit");
281 await first.commit("Commit on a new branch");
282
283 const reopened = openSheet();
284 expect(reopened.isIncluded("later.ts")).toBe(false);
285 expect(reopened.message()).toBe("fix: stale branch commit");
286 await act(async () => success.resolve());
287 state.waitBeforeProgress = null;
288
289 expect(reopened.isIncluded("later.ts")).toBe(true);
290 expect(reopened.message()).toBe("");
291 reopened.close();
292});
293
294it("treats remaining files as all selected when an excluded file disappears", async () => {
295 const initialFiles = state.files;
296 state.cwd = "/repo/stale-excluded";
297 state.refName = "main";
298 state.result = { commit: { status: "created" } };
299 state.ran = [];
300 state.createdBranch = undefined;
301 state.waitBeforeProgress = null;
302
303 const sheet = openSheet();
304 try {
305 sheet.toggle("later.ts");
306 state.files = initialFiles.slice(0, 1);
307 sheet.refresh();
308
309 expect(sheet.toggleAllLabel()).toBe("Deselect all");
310 sheet.toggleAll();
311 state.files = initialFiles;
312 sheet.refresh();
313 expect(sheet.isIncluded("later.ts")).toBe(false);
314 expect(sheet.isIncluded("include.ts")).toBe(false);
315
316 sheet.toggle("include.ts");
317 state.files = initialFiles.slice(0, 1);
318 sheet.refresh();
319 await sheet.commit();
320 expect(state.ran).toEqual([
321 { action: "commit", featureBranch: false, filePaths: ["include.ts"] },
322 ]);
323 } finally {
324 state.files = initialFiles;
325 }
326});