Open History to see the last commit
1import {
2 AuthOrchestrationOperateScope,
3 AuthSourceControlWriteScope,
4 EnvironmentId,
5 ThreadId,
6} from "@t3tools/contracts";
7import * as Cause from "effect/Cause";
8import { AsyncResult } from "effect/reactivity";
9import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
10
11const state = vi.hoisted(() => ({
12 scopes: new Set<string>(),
13 primaryScopes: new Set<string>(),
14 shell: { branch: "main" } as { branch: string } | null,
15 detail: null as { branch: string } | null,
16 draft: null as { branch: string; worktreePath: null; envMode: "local" } | null,
17 branch: "main",
18 commits: 0,
19 metadataRequests: [] as { environmentId: string; branch: string }[],
20 afterGitAction: undefined as (() => void) | undefined,
21 run: null as ((input: { action: "commit"; featureBranch?: boolean }) => Promise<void>) | null,
22}));
23
24vi.mock("react", async (importOriginal) => ({
25 ...(await importOriginal<typeof import("react")>()),
26 useCallback: (callback: unknown) => callback,
27 useMemo: (factory: () => unknown) => factory(),
28 useState: (initial: unknown) => [typeof initial === "function" ? initial() : initial, () => {}],
29 useRef: (current: unknown) => ({ current }),
30 useEffect: () => {},
31 useEffectEvent: (callback: typeof state.run) => {
32 state.run = callback;
33 return callback;
34 },
35}));
36vi.mock("@effect/atom-react", () => ({
37 useAtomValue: (atom: unknown) => (atom === "vcs-state" ? { isRunning: false } : null),
38}));
39vi.mock("~/state/entities", () => ({
40 useThreadProjection: (ref: unknown) =>
41 ref === null || state.detail === null ? null : { projection: { thread: state.detail } },
42 useThreadShell: () => state.shell,
43}));
44vi.mock("~/state/session", () => ({
45 useEnvironmentScope: (environmentId: unknown, scope: string) =>
46 (environmentId === "environment" ? state.scopes : state.primaryScopes).has(scope),
47 readEnvironmentScope: (environmentId: unknown, scope: string) =>
48 (environmentId === "environment" ? state.scopes : state.primaryScopes).has(scope),
49}));
50vi.mock("~/state/use-atom-command", () => ({ useAtomCommand: (command: unknown) => command }));
51vi.mock("~/state/server", () => ({ serverEnvironment: { configValueAtom: () => null } }));
52vi.mock("~/state/sourceControl", () => ({ sourceControlEnvironment: {} }));
53vi.mock("~/state/vcs", () => ({
54 vcsEnvironment: { status: () => null },
55 vcsActionManager: { stateAtom: () => "vcs-state" },
56}));
57vi.mock("~/state/threads", () => ({
58 threadEnvironment: {
59 updateMetadata: async ({
60 environmentId,
61 input,
62 }: {
63 environmentId: string;
64 input: { branch: string };
65 }) => {
66 state.metadataRequests.push({ environmentId, branch: input.branch });
67 if (!state.scopes.has(AuthOrchestrationOperateScope))
68 return AsyncResult.failure(Cause.fail(new Error("Task denied")));
69 if (state.shell) state.shell.branch = input.branch;
70 if (state.detail) state.detail.branch = input.branch;
71 return AsyncResult.success(undefined);
72 },
73 },
74}));
75vi.mock("~/state/query", () => ({
76 useEnvironmentQuery: () => ({
77 data: {
78 isRepo: true,
79 refName: "main",
80 isDefaultRef: false,
81 hasPrimaryRemote: true,
82 hasWorkingTreeChanges: true,
83 workingTree: {
84 files: [{ path: "file.ts", status: "modified", insertions: 1, deletions: 0 }],
85 },
86 },
87 error: null,
88 }),
89}));
90vi.mock("~/composerDraftStore", () => ({
91 useComposerDraftStore: (
92 select: (store: {
93 getDraftSession: () => typeof state.draft;
94 getDraftThreadByRef: () => typeof state.draft;
95 setDraftThreadContext: (_target: unknown, input: { branch: string }) => void;
96 }) => unknown,
97 ) =>
98 select({
99 getDraftSession: () => state.draft,
100 getDraftThreadByRef: () => state.draft,
101 setDraftThreadContext: (_target, input) => {
102 if (state.draft) state.draft.branch = input.branch;
103 },
104 }),
105}));
106vi.mock("~/lib/sourceControlActions", () => ({
107 useSourceControlActionRunning: () => false,
108 useVcsInitAction: () => ({}),
109 useVcsPullAction: () => ({}),
110 useSourceControlPublishRepositoryAction: () => ({}),
111 useGitStackedAction: () => ({
112 run: async ({ featureBranch }: { featureBranch?: boolean }) => {
113 state.commits += 1;
114 if (featureBranch) state.branch = "feature";
115 state.afterGitAction?.();
116 return {
117 _tag: "Success",
118 value: {
119 branch: featureBranch
120 ? { status: "created", name: "feature" }
121 : { status: "skipped_not_requested" },
122 toast: { title: "Committed", description: "Committed", cta: { kind: "none" } },
123 },
124 };
125 },
126 }),
127}));
128vi.mock("~/lib/utils", () => ({ cn: () => "", randomUUID: () => "action" }));
129vi.mock("~/editorPreferences", () => ({ useOpenInPreferredEditor: () => () => {} }));
130vi.mock("~/browser/useOpenLink", () => ({ useOpenLink: () => () => {} }));
131vi.mock("~/lib/openPullRequestLink", () => ({ useOpenPrLink: () => () => {} }));
132vi.mock("~/components/ui/toast", () => ({
133 stackedThreadToast: (input: unknown) => input,
134 toastManager: { add: () => "toast", update: () => {}, close: () => {} },
135}));
136vi.mock("~/components/ui/dialog", () => ({
137 Dialog: "Dialog",
138 DialogDescription: "DialogDescription",
139 DialogFooter: "DialogFooter",
140 DialogHeader: "DialogHeader",
141 DialogPanel: "DialogPanel",
142 DialogPopup: "DialogPopup",
143 DialogTitle: "DialogTitle",
144}));
145vi.mock("~/components/ui/group", () => ({ Group: "Group", GroupSeparator: "GroupSeparator" }));
146vi.mock("~/components/ui/menu", () => ({
147 Menu: "Menu",
148 MenuItem: "MenuItem",
149 MenuItemLabel: "MenuItemLabel",
150 MenuPopup: "MenuPopup",
151 MenuTrigger: "MenuTrigger",
152}));
153vi.mock("~/components/ui/popover", () => ({
154 Popover: "Popover",
155 PopoverPopup: "PopoverPopup",
156 PopoverTrigger: "PopoverTrigger",
157}));
158vi.mock("~/components/ui/tooltip", () => ({
159 Tooltip: "Tooltip",
160 TooltipPopup: "TooltipPopup",
161 TooltipTrigger: "TooltipTrigger",
162}));
163vi.mock("~/components/ui/button", () => ({ Button: "Button" }));
164vi.mock("~/components/ui/checkbox", () => ({ Checkbox: "Checkbox" }));
165vi.mock("~/components/ui/input", () => ({ Input: "Input" }));
166vi.mock("~/components/ui/radio-group", () => ({ RadioGroup: "RadioGroup" }));
167vi.mock("~/components/ui/scroll-area", () => ({ ScrollArea: "ScrollArea" }));
168vi.mock("~/components/ui/spinner", () => ({ Spinner: "Spinner" }));
169vi.mock("~/components/ui/textarea", () => ({ Textarea: "Textarea" }));
170vi.mock("~/components/ui/toggle", () => ({ toggleVariants: () => "" }));
171vi.mock("./AnimatedHeight", () => ({ AnimatedHeight: "AnimatedHeight" }));
172
173import GitActionsControl from "./GitActionsControl";
174
175function renderActions() {
176 GitActionsControl({
177 gitCwd: "/repo",
178 activeThreadRef: {
179 environmentId: EnvironmentId.make("environment"),
180 threadId: ThreadId.make("thread"),
181 },
182 });
183 if (!state.run) throw new Error("Git action missing");
184 return state.run;
185}
186
187describe("Git actions while thread details load", () => {
188 beforeEach(() => {
189 state.scopes = new Set([AuthSourceControlWriteScope]);
190 state.primaryScopes = new Set([AuthSourceControlWriteScope, AuthOrchestrationOperateScope]);
191 state.shell = { branch: "main" };
192 state.detail = null;
193 state.draft = null;
194 state.branch = "main";
195 state.commits = 0;
196 state.metadataRequests = [];
197 state.afterGitAction = undefined;
198 state.run = null;
199 });
200
201 it("does not create a feature branch for a server thread without task permission", async () => {
202 await renderActions()({ action: "commit", featureBranch: true });
203
204 expect(state.branch).toBe("main");
205 expect(state.commits).toBe(0);
206 expect(state.shell?.branch).toBe("main");
207 });
208
209 it("commits and synchronizes the server thread before details finish loading", async () => {
210 state.primaryScopes.clear();
211 state.scopes.add(AuthOrchestrationOperateScope);
212 await renderActions()({ action: "commit", featureBranch: true });
213
214 expect(state.branch).toBe("feature");
215 expect(state.commits).toBe(1);
216 expect(state.shell?.branch).toBe("feature");
217 expect(state.metadataRequests).toEqual([{ environmentId: "environment", branch: "feature" }]);
218 });
219
220 it("synchronizes archived thread detail after its shell disappears", async () => {
221 state.scopes.add(AuthOrchestrationOperateScope);
222 state.detail = { branch: "main" };
223 renderActions();
224 state.shell = null;
225
226 await renderActions()({ action: "commit", featureBranch: true });
227
228 expect(state.commits).toBe(1);
229 expect(state.branch).toBe("feature");
230 expect(state.detail.branch).toBe("feature");
231 expect(state.metadataRequests).toEqual([{ environmentId: "environment", branch: "feature" }]);
232 });
233
234 it("requires task permission before changing an archived thread's branch", async () => {
235 state.shell = null;
236 state.detail = { branch: "main" };
237
238 await renderActions()({ action: "commit", featureBranch: true });
239
240 expect(state.commits).toBe(0);
241 expect(state.detail.branch).toBe("main");
242 expect(state.metadataRequests).toEqual([]);
243 });
244
245 it("uses the current shell branch when cached detail names the new branch", async () => {
246 state.scopes.add(AuthOrchestrationOperateScope);
247 state.detail = { branch: "feature" };
248
249 await renderActions()({ action: "commit", featureBranch: true });
250
251 expect(state.commits).toBe(1);
252 expect(state.shell?.branch).toBe("feature");
253 expect(state.metadataRequests).toEqual([{ environmentId: "environment", branch: "feature" }]);
254 });
255
256 it("synchronizes a retained callback after the thread grant is gained", async () => {
257 const run = renderActions();
258 state.scopes.add(AuthOrchestrationOperateScope);
259 await run({ action: "commit", featureBranch: true });
260 expect(state.commits).toBe(1);
261 expect(state.shell?.branch).toBe("feature");
262 expect(state.metadataRequests).toHaveLength(1);
263 });
264
265 it.each([
266 [AuthOrchestrationOperateScope, false],
267 [AuthSourceControlWriteScope, true],
268 ] as const)(
269 "uses the fresh task grant after Git finishes, revoking %s",
270 async (revokedScope, syncsThread) => {
271 state.scopes.add(AuthOrchestrationOperateScope);
272 state.afterGitAction = () => state.scopes.delete(revokedScope);
273 await renderActions()({ action: "commit", featureBranch: true });
274 expect(state.commits).toBe(1);
275 expect(state.branch).toBe("feature");
276 expect(state.shell?.branch).toBe(syncsThread ? "feature" : "main");
277 expect(state.metadataRequests).toHaveLength(syncsThread ? 1 : 0);
278 },
279 );
280
281 it("keeps ordinary commits available while details load", async () => {
282 await renderActions()({ action: "commit" });
283
284 expect(state.commits).toBe(1);
285 expect(state.branch).toBe("main");
286 });
287
288 it("keeps feature-branch commits available for a local draft", async () => {
289 state.shell = null;
290 state.draft = { branch: "main", worktreePath: null, envMode: "local" };
291 await renderActions()({ action: "commit", featureBranch: true });
292
293 expect(state.commits).toBe(1);
294 expect(state.draft.branch).toBe("feature");
295 expect(state.metadataRequests).toEqual([]);
296 });
297});