Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitActionsControl.test.ts

295 lines10.8 KB

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: { files: [{ path: "file.ts", status: "modified" }] },
84 },
85 error: null,
86 }),
87}));
88vi.mock("~/composerDraftStore", () => ({
89 useComposerDraftStore: (
90 select: (store: {
91 getDraftSession: () => typeof state.draft;
92 getDraftThreadByRef: () => typeof state.draft;
93 setDraftThreadContext: (_target: unknown, input: { branch: string }) => void;
94 }) => unknown,
95 ) =>
96 select({
97 getDraftSession: () => state.draft,
98 getDraftThreadByRef: () => state.draft,
99 setDraftThreadContext: (_target, input) => {
100 if (state.draft) state.draft.branch = input.branch;
101 },
102 }),
103}));
104vi.mock("~/lib/sourceControlActions", () => ({
105 useSourceControlActionRunning: () => false,
106 useVcsInitAction: () => ({}),
107 useVcsPullAction: () => ({}),
108 useSourceControlPublishRepositoryAction: () => ({}),
109 useGitStackedAction: () => ({
110 run: async ({ featureBranch }: { featureBranch?: boolean }) => {
111 state.commits += 1;
112 if (featureBranch) state.branch = "feature";
113 state.afterGitAction?.();
114 return {
115 _tag: "Success",
116 value: {
117 branch: featureBranch
118 ? { status: "created", name: "feature" }
119 : { status: "skipped_not_requested" },
120 toast: { title: "Committed", description: "Committed", cta: { kind: "none" } },
121 },
122 };
123 },
124 }),
125}));
126vi.mock("~/lib/utils", () => ({ cn: () => "", randomUUID: () => "action" }));
127vi.mock("~/editorPreferences", () => ({ useOpenInPreferredEditor: () => () => {} }));
128vi.mock("~/browser/useOpenLink", () => ({ useOpenLink: () => () => {} }));
129vi.mock("~/lib/openPullRequestLink", () => ({ useOpenPrLink: () => () => {} }));
130vi.mock("~/components/ui/toast", () => ({
131 stackedThreadToast: (input: unknown) => input,
132 toastManager: { add: () => "toast", update: () => {}, close: () => {} },
133}));
134vi.mock("~/components/ui/dialog", () => ({
135 Dialog: "Dialog",
136 DialogDescription: "DialogDescription",
137 DialogFooter: "DialogFooter",
138 DialogHeader: "DialogHeader",
139 DialogPanel: "DialogPanel",
140 DialogPopup: "DialogPopup",
141 DialogTitle: "DialogTitle",
142}));
143vi.mock("~/components/ui/group", () => ({ Group: "Group", GroupSeparator: "GroupSeparator" }));
144vi.mock("~/components/ui/menu", () => ({
145 Menu: "Menu",
146 MenuItem: "MenuItem",
147 MenuItemLabel: "MenuItemLabel",
148 MenuPopup: "MenuPopup",
149 MenuTrigger: "MenuTrigger",
150}));
151vi.mock("~/components/ui/popover", () => ({
152 Popover: "Popover",
153 PopoverPopup: "PopoverPopup",
154 PopoverTrigger: "PopoverTrigger",
155}));
156vi.mock("~/components/ui/tooltip", () => ({
157 Tooltip: "Tooltip",
158 TooltipPopup: "TooltipPopup",
159 TooltipTrigger: "TooltipTrigger",
160}));
161vi.mock("~/components/ui/button", () => ({ Button: "Button" }));
162vi.mock("~/components/ui/checkbox", () => ({ Checkbox: "Checkbox" }));
163vi.mock("~/components/ui/input", () => ({ Input: "Input" }));
164vi.mock("~/components/ui/radio-group", () => ({ RadioGroup: "RadioGroup" }));
165vi.mock("~/components/ui/scroll-area", () => ({ ScrollArea: "ScrollArea" }));
166vi.mock("~/components/ui/spinner", () => ({ Spinner: "Spinner" }));
167vi.mock("~/components/ui/textarea", () => ({ Textarea: "Textarea" }));
168vi.mock("~/components/ui/toggle", () => ({ toggleVariants: () => "" }));
169vi.mock("./AnimatedHeight", () => ({ AnimatedHeight: "AnimatedHeight" }));
170​
171import GitActionsControl from "./GitActionsControl";
172​
173function renderActions() {
174 GitActionsControl({
175 gitCwd: "/repo",
176 activeThreadRef: {
177 environmentId: EnvironmentId.make("environment"),
178 threadId: ThreadId.make("thread"),
179 },
180 });
181 if (!state.run) throw new Error("Git action missing");
182 return state.run;
184​
185describe("Git actions while thread details load", () => {
186 beforeEach(() => {
187 state.scopes = new Set([AuthSourceControlWriteScope]);
188 state.primaryScopes = new Set([AuthSourceControlWriteScope, AuthOrchestrationOperateScope]);
189 state.shell = { branch: "main" };
190 state.detail = null;
191 state.draft = null;
192 state.branch = "main";
193 state.commits = 0;
194 state.metadataRequests = [];
195 state.afterGitAction = undefined;
196 state.run = null;
197 });
198​
199 it("does not create a feature branch for a server thread without task permission", async () => {
200 await renderActions()({ action: "commit", featureBranch: true });
201​
202 expect(state.branch).toBe("main");
203 expect(state.commits).toBe(0);
204 expect(state.shell?.branch).toBe("main");
205 });
206​
207 it("commits and synchronizes the server thread before details finish loading", async () => {
208 state.primaryScopes.clear();
209 state.scopes.add(AuthOrchestrationOperateScope);
210 await renderActions()({ action: "commit", featureBranch: true });
211​
212 expect(state.branch).toBe("feature");
213 expect(state.commits).toBe(1);
214 expect(state.shell?.branch).toBe("feature");
215 expect(state.metadataRequests).toEqual([{ environmentId: "environment", branch: "feature" }]);
216 });
217​
218 it("synchronizes archived thread detail after its shell disappears", async () => {
219 state.scopes.add(AuthOrchestrationOperateScope);
220 state.detail = { branch: "main" };
221 renderActions();
222 state.shell = null;
223​
224 await renderActions()({ action: "commit", featureBranch: true });
225​
226 expect(state.commits).toBe(1);
227 expect(state.branch).toBe("feature");
228 expect(state.detail.branch).toBe("feature");
229 expect(state.metadataRequests).toEqual([{ environmentId: "environment", branch: "feature" }]);
230 });
231​
232 it("requires task permission before changing an archived thread's branch", async () => {
233 state.shell = null;
234 state.detail = { branch: "main" };
235​
236 await renderActions()({ action: "commit", featureBranch: true });
237​
238 expect(state.commits).toBe(0);
239 expect(state.detail.branch).toBe("main");
240 expect(state.metadataRequests).toEqual([]);
241 });
242​
243 it("uses the current shell branch when cached detail names the new branch", async () => {
244 state.scopes.add(AuthOrchestrationOperateScope);
245 state.detail = { branch: "feature" };
246​
247 await renderActions()({ action: "commit", featureBranch: true });
248​
249 expect(state.commits).toBe(1);
250 expect(state.shell?.branch).toBe("feature");
251 expect(state.metadataRequests).toEqual([{ environmentId: "environment", branch: "feature" }]);
252 });
253​
254 it("synchronizes a retained callback after the thread grant is gained", async () => {
255 const run = renderActions();
256 state.scopes.add(AuthOrchestrationOperateScope);
257 await run({ action: "commit", featureBranch: true });
258 expect(state.commits).toBe(1);
259 expect(state.shell?.branch).toBe("feature");
260 expect(state.metadataRequests).toHaveLength(1);
261 });
262​
263 it.each([
264 [AuthOrchestrationOperateScope, false],
265 [AuthSourceControlWriteScope, true],
266 ] as const)(
267 "uses the fresh task grant after Git finishes, revoking %s",
268 async (revokedScope, syncsThread) => {
269 state.scopes.add(AuthOrchestrationOperateScope);
270 state.afterGitAction = () => state.scopes.delete(revokedScope);
271 await renderActions()({ action: "commit", featureBranch: true });
272 expect(state.commits).toBe(1);
273 expect(state.branch).toBe("feature");
274 expect(state.shell?.branch).toBe(syncsThread ? "feature" : "main");
275 expect(state.metadataRequests).toHaveLength(syncsThread ? 1 : 0);
276 },
277 );
278​
279 it("keeps ordinary commits available while details load", async () => {
280 await renderActions()({ action: "commit" });
281​
282 expect(state.commits).toBe(1);
283 expect(state.branch).toBe("main");
284 });
285​
286 it("keeps feature-branch commits available for a local draft", async () => {
287 state.shell = null;
288 state.draft = { branch: "main", worktreePath: null, envMode: "local" };
289 await renderActions()({ action: "commit", featureBranch: true });
290​
291 expect(state.commits).toBe(1);
292 expect(state.draft.branch).toBe("feature");
293 expect(state.metadataRequests).toEqual([]);
294 });
295});