Open History to see the last commit
1import {
2 EnvironmentId,
3 type GitActionProgressEvent,
4 type GitRunStackedActionResult,
5 type VcsCreateRefResult,
6 type VcsCreateWorktreeResult,
7 type VcsPullResult,
8 type VcsStatusResult,
9 type VcsSwitchRefResult,
10} from "@t3tools/contracts";
11import { AtomRegistry } from "effect/unstable/reactivity";
12import { afterEach, describe, expect, it, vi } from "vite-plus/test";
13
14import {
15 type VcsActionClient,
16 createVcsActionManager,
17 EMPTY_VCS_ACTION_STATE,
18} from "./vcsActionState.ts";
19
20let atomRegistry = AtomRegistry.make();
21
22function resetAtomRegistry() {
23 atomRegistry.dispose();
24 atomRegistry = AtomRegistry.make();
25}
26
27function createDeferred<T>() {
28 let resolve!: (value: T) => void;
29 let reject!: (reason?: unknown) => void;
30 const promise = new Promise<T>((res, rej) => {
31 resolve = res;
32 reject = rej;
33 });
34 return { promise, resolve, reject };
35}
36
37const TARGET = { environmentId: EnvironmentId.make("env-local"), cwd: "/repo" } as const;
38
39const BASE_STATUS: VcsStatusResult = {
40 isRepo: true,
41 hasPrimaryRemote: true,
42 isDefaultRef: false,
43 refName: "feature/test",
44 hasWorkingTreeChanges: true,
45 workingTree: { files: [], insertions: 0, deletions: 0 },
46 hasUpstream: true,
47 aheadCount: 0,
48 behindCount: 0,
49 pr: null,
50};
51
52function createPhaseStartedEvent(): Extract<GitActionProgressEvent, { kind: "phase_started" }> {
53 return {
54 actionId: "action-123",
55 cwd: "/repo",
56 action: "commit_push",
57 kind: "phase_started",
58 phase: "commit",
59 label: "Committing...",
60 };
61}
62
63function createHookStartedEvent(): Extract<GitActionProgressEvent, { kind: "hook_started" }> {
64 return {
65 actionId: "action-123",
66 cwd: "/repo",
67 action: "commit_push",
68 kind: "hook_started",
69 hookName: "post-commit",
70 };
71}
72
73function createHookOutputEvent(): Extract<GitActionProgressEvent, { kind: "hook_output" }> {
74 return {
75 actionId: "action-123",
76 cwd: "/repo",
77 action: "commit_push",
78 kind: "hook_output",
79 hookName: "post-commit",
80 stream: "stdout",
81 text: "hook output",
82 };
83}
84
85function createHookFinishedEvent(): Extract<GitActionProgressEvent, { kind: "hook_finished" }> {
86 return {
87 actionId: "action-123",
88 cwd: "/repo",
89 action: "commit_push",
90 kind: "hook_finished",
91 hookName: "post-commit",
92 exitCode: 0,
93 durationMs: 12,
94 };
95}
96
97function createActionFinishedEvent(): Extract<GitActionProgressEvent, { kind: "action_finished" }> {
98 return {
99 actionId: "action-123",
100 cwd: "/repo",
101 action: "commit_push",
102 kind: "action_finished",
103 result: {
104 action: "commit_push",
105 branch: { status: "skipped_not_requested" },
106 commit: { status: "created", commitSha: "abc123", subject: "Test commit" },
107 push: {
108 status: "pushed",
109 branch: "feature/test",
110 upstreamBranch: "origin/feature/test",
111 },
112 pr: { status: "skipped_not_requested" },
113 toast: {
114 title: "Done",
115 description: "Action finished",
116 cta: { kind: "none" },
117 },
118 } satisfies GitRunStackedActionResult,
119 };
120}
121
122function createMockClient() {
123 const refreshDeferred = createDeferred<VcsStatusResult>();
124 const pullDeferred = createDeferred<VcsPullResult>();
125 const switchRefDeferred = createDeferred<VcsSwitchRefResult>();
126 const createRefDeferred = createDeferred<VcsCreateRefResult>();
127 const createWorktreeDeferred = createDeferred<VcsCreateWorktreeResult>();
128 const initDeferred = createDeferred<void>();
129 const runChangeRequestDeferred = createDeferred<GitRunStackedActionResult>();
130 let runChangeRequestProgressListener: ((event: GitActionProgressEvent) => void) | null = null;
131
132 const client: VcsActionClient = {
133 refreshStatus: vi.fn(() => refreshDeferred.promise),
134 pull: vi.fn(() => pullDeferred.promise),
135 switchRef: vi.fn(() => switchRefDeferred.promise),
136 createRef: vi.fn(() => createRefDeferred.promise),
137 createWorktree: vi.fn(() => createWorktreeDeferred.promise),
138 init: vi.fn(() => initDeferred.promise),
139 runChangeRequest: vi.fn((_, options) => {
140 runChangeRequestProgressListener = options?.onProgress ?? null;
141 return runChangeRequestDeferred.promise;
142 }),
143 };
144
145 return {
146 client,
147 refreshDeferred,
148 pullDeferred,
149 switchRefDeferred,
150 createRefDeferred,
151 createWorktreeDeferred,
152 initDeferred,
153 runChangeRequestDeferred,
154 emitProgress(event: GitActionProgressEvent) {
155 runChangeRequestProgressListener?.(event);
156 },
157 };
158}
159
160describe("createVcsActionManager", () => {
161 afterEach(() => {
162 resetAtomRegistry();
163 });
164
165 it("tracks refreshStatus progress and clears state on success", async () => {
166 const mock = createMockClient();
167 const manager = createVcsActionManager({
168 getRegistry: () => atomRegistry,
169 getClient: () => mock.client,
170 });
171
172 const promise = manager.refreshStatus(TARGET, mock.client);
173
174 expect(manager.getSnapshot(TARGET)).toMatchObject({
175 isRunning: true,
176 operation: "refresh_status",
177 currentLabel: "Refreshing source control status",
178 error: null,
179 });
180
181 mock.refreshDeferred.resolve(BASE_STATUS);
182
183 await expect(promise).resolves.toEqual(BASE_STATUS);
184 expect(manager.getSnapshot(TARGET)).toEqual(EMPTY_VCS_ACTION_STATE);
185 });
186
187 it("tracks runChangeRequest progress events", async () => {
188 const mock = createMockClient();
189 const onProgress = vi.fn();
190 const manager = createVcsActionManager({
191 getRegistry: () => atomRegistry,
192 getClient: () => mock.client,
193 getActionId: () => "action-123",
194 });
195
196 const promise = manager.runChangeRequest(
197 TARGET,
198 { action: "commit_push", commitMessage: "Test commit" },
199 { client: mock.client, gitStatus: BASE_STATUS, onProgress },
200 );
201
202 expect(manager.getSnapshot(TARGET)).toMatchObject({
203 isRunning: true,
204 operation: "run_change_request",
205 actionId: "action-123",
206 currentLabel: "Committing...",
207 error: null,
208 });
209
210 mock.emitProgress(createPhaseStartedEvent());
211 expect(manager.getSnapshot(TARGET).currentLabel).toBe("Committing...");
212 expect(onProgress).toHaveBeenLastCalledWith(createPhaseStartedEvent());
213
214 mock.emitProgress(createHookStartedEvent());
215 expect(manager.getSnapshot(TARGET)).toMatchObject({
216 currentLabel: "Running post-commit...",
217 hookName: "post-commit",
218 isRunning: true,
219 });
220
221 mock.emitProgress(createHookOutputEvent());
222 expect(manager.getSnapshot(TARGET).lastOutputLine).toBe("hook output");
223
224 mock.emitProgress(createHookFinishedEvent());
225 expect(manager.getSnapshot(TARGET)).toMatchObject({
226 currentLabel: "Committing...",
227 hookName: null,
228 lastOutputLine: null,
229 });
230
231 const result = createActionFinishedEvent().result;
232 mock.runChangeRequestDeferred.resolve(result);
233
234 await expect(promise).resolves.toEqual(result);
235 expect(manager.getSnapshot(TARGET)).toEqual(EMPTY_VCS_ACTION_STATE);
236 });
237
238 it("stores the error when an operation fails", async () => {
239 const mock = createMockClient();
240 const manager = createVcsActionManager({
241 getRegistry: () => atomRegistry,
242 getClient: () => mock.client,
243 });
244
245 const promise = manager.pull(TARGET, mock.client);
246
247 mock.pullDeferred.reject(new Error("Pull failed."));
248
249 await expect(promise).rejects.toThrow("Pull failed.");
250 expect(manager.getSnapshot(TARGET)).toMatchObject({
251 isRunning: false,
252 operation: "pull",
253 currentLabel: null,
254 error: "Pull failed.",
255 });
256 });
257
258 it("invalidates after successful mutations but not refreshStatus", async () => {
259 const mock = createMockClient();
260 const onInvalidate = vi.fn();
261 const manager = createVcsActionManager({
262 getRegistry: () => atomRegistry,
263 getClient: () => mock.client,
264 onInvalidate,
265 });
266
267 const refreshPromise = manager.refreshStatus(TARGET, mock.client);
268 mock.refreshDeferred.resolve(BASE_STATUS);
269 await expect(refreshPromise).resolves.toEqual(BASE_STATUS);
270 expect(onInvalidate).not.toHaveBeenCalled();
271
272 const pullPromise = manager.pull(TARGET, mock.client);
273 const pullResult: VcsPullResult = {
274 status: "skipped_up_to_date",
275 refName: "main",
276 upstreamRef: null,
277 };
278 mock.pullDeferred.resolve(pullResult);
279 await expect(pullPromise).resolves.toEqual(pullResult);
280 expect(onInvalidate).toHaveBeenCalledWith(TARGET);
281 });
282
283 it("returns null when no client is available", async () => {
284 const manager = createVcsActionManager({
285 getRegistry: () => atomRegistry,
286 getClient: () => null,
287 });
288
289 await expect(manager.switchRef(TARGET, { refName: "main" })).resolves.toBeNull();
290 expect(manager.getSnapshot(TARGET)).toEqual(EMPTY_VCS_ACTION_STATE);
291 });
292});