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

ProjectCloneTracker.test.ts

338 lines13.6 KB

1import { SourceControlProviderKind } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import {
4 OrchestrationDispatchCommandError,
5 ProjectId,
6 projectCloneProgressSummary,
7 SourceControlRepositoryError,
8} from "@t3tools/contracts";
9import * as Deferred from "effect/Deferred";
10import * as Effect from "effect/Effect";
11import * as Fiber from "effect/Fiber";
12import * as Layer from "effect/Layer";
13import * as Stream from "effect/Stream";
14import * as TestClock from "effect/testing/TestClock";
15​
16import * as SourceControlRepositoryService from "../sourceControl/SourceControlRepositoryService.ts";
17import * as ProjectCloneTracker from "./ProjectCloneTracker.ts";
18import { parseGitCloneProgressLine } from "./gitCloneProgress.ts";
19​
20const projectId = ProjectId.make("project-1");
21const startInput = {
22 projectId,
23 title: "t3code",
24 createdAt: "2026-01-01T00:00:00.000Z",
25 remoteUrl: "git@github.com:octocat/t3code.git",
26 destinationPath: "/workspace/t3code",
27};
28​
29function makeHarness(options?: {
30 readonly clone?: SourceControlRepositoryService.SourceControlRepositoryService["Service"]["cloneRepository"];
31}) {
32 const created: Array<{ projectId: ProjectId; workspaceRoot: string }> = [];
33 const cloned: Array<ProjectId> = [];
34 const discarded: Array<string> = [];
35 const hooks: ProjectCloneTracker.ProjectCloneHooks = {
36 createProject: (input) =>
37 Effect.sync(() => {
38 created.push({ projectId: input.projectId, workspaceRoot: input.workspaceRoot });
39 }),
40 onCloned: (input) => Effect.sync(() => void cloned.push(input.projectId)),
41 };
42 const layer = ProjectCloneTracker.layer.pipe(
43 Layer.provide(
44 Layer.mock(SourceControlRepositoryService.SourceControlRepositoryService)({
45 prepareClone: (input) =>
46 Effect.succeed({
47 destinationPath: input.destinationPath,
48 remoteUrl: input.remoteUrl ?? "",
49 cloneUrl: input.remoteUrl ?? "",
50 repository: null,
51 }),
52 cloneRepository:
53 options?.clone ??
54 ((input) =>
55 Effect.succeed({
56 cwd: input.destinationPath,
57 remoteUrl: input.remoteUrl ?? "",
58 repository: null,
59 })),
60 discardClone: (destination) => Effect.sync(() => void discarded.push(destination)),
61 }),
62 ),
63 );
64 return { layer, hooks, created, cloned, discarded };
66​
67describe("ProjectCloneTracker", () => {
68 it.effect("creates the project first and reports the clone through the stream", () => {
69 const release = Deferred.makeUnsafe<void>();
70 const harness = makeHarness({
71 clone: (input, options) =>
72 Effect.gen(function* () {
73 yield* (
74 options?.onProgress?.({ stage: "receiving", percent: 40, detail: "1 MiB" }) ??
75 Effect.void
76 );
77 yield* Deferred.await(release);
78 return { cwd: input.destinationPath, remoteUrl: input.remoteUrl ?? "", repository: null };
79 }),
80 });
81 return Effect.gen(function* () {
82 const tracker = yield* ProjectCloneTracker.ProjectCloneTracker;
83 const collected = yield* tracker.stream.pipe(
84 Stream.takeUntil((clones) => clones[0]?.phase === "done"),
85 Stream.runCollect,
86 Effect.forkChild,
87 );
88 yield* Effect.yieldNow;
89​
90 const result = yield* tracker.start(startInput, harness.hooks);
91 expect(result.cwd).toBe("/workspace/t3code");
92 expect(harness.created).toEqual([{ projectId, workspaceRoot: "/workspace/t3code" }]);
93​
94 yield* Effect.yieldNow;
95 const running = yield* tracker.get(projectId);
96 expect(running).toMatchObject({ phase: "running", stage: "receiving", percent: 40 });
97​
98 yield* Deferred.succeed(release, undefined);
99 const lists = yield* Fiber.join(collected);
100 const final = lists.at(-1)?.[0];
101 expect(final).toMatchObject({ phase: "done", percent: 100 });
102 expect(harness.cloned).toEqual([projectId]);
103​
104 yield* TestClock.adjust("31 seconds");
105 expect(yield* tracker.get(projectId)).toBeNull();
106 }).pipe(Effect.provide(harness.layer));
107 });
108​
109 it.effect("flags a clone stalled when git goes quiet and clears it when git speaks", () => {
110 const release = Deferred.makeUnsafe<void>();
111 const resumed = Deferred.makeUnsafe<void>();
112 const harness = makeHarness({
113 clone: (input, options) =>
114 Effect.gen(function* () {
115 const report = options?.onProgress ?? (() => Effect.void);
116 yield* report({ stage: "receiving", percent: 24, detail: "168.80 MiB | 20.69 MiB/s" });
117 yield* Deferred.await(resumed);
118 yield* report({ stage: "receiving", percent: 25, detail: "170.00 MiB | 1.00 MiB/s" });
119 yield* Deferred.await(release);
120 return { cwd: input.destinationPath, remoteUrl: input.remoteUrl ?? "", repository: null };
121 }),
122 });
123 return Effect.gen(function* () {
124 const tracker = yield* ProjectCloneTracker.ProjectCloneTracker;
125 yield* tracker.start(startInput, harness.hooks);
126 yield* Effect.yieldNow;
127 expect((yield* tracker.get(projectId))?.stalled).toBe(false);
128​
129 yield* TestClock.adjust("19 seconds");
130 expect((yield* tracker.get(projectId))?.stalled).toBe(false);
131 yield* TestClock.adjust("1 second");
132 const stalled = yield* tracker.get(projectId);
133 expect(stalled).toMatchObject({ stalled: true, percent: 24 });
134 expect(projectCloneProgressSummary(stalled!)).toBe("Receiving objects · 24% · Stalled");
135​
136 yield* Deferred.succeed(resumed, undefined);
137 yield* Effect.yieldNow;
138 expect(yield* tracker.get(projectId)).toMatchObject({ stalled: false, percent: 25 });
139​
140 yield* TestClock.adjust("20 seconds");
141 expect((yield* tracker.get(projectId))?.stalled).toBe(true);
142 yield* Deferred.succeed(release, undefined);
143 yield* Effect.yieldNow;
144 expect(yield* tracker.get(projectId)).toMatchObject({ phase: "done", stalled: false });
145 }).pipe(Effect.provide(harness.layer));
146 });
147​
148 it.effect("keeps a failed clone with git's own explanation and retries it", () => {
149 let attempts = 0;
150 const harness = makeHarness({
151 clone: (input) =>
152 Effect.suspend(() => {
153 attempts += 1;
154 return attempts === 1
155 ? Effect.fail(
156 new SourceControlRepositoryError({
157 operation: "cloneRepository",
158 provider: SourceControlProviderKind.make("unknown"),
159 detail: "fatal: repository not found",
160 }),
161 )
162 : Effect.succeed({
163 cwd: input.destinationPath,
164 remoteUrl: input.remoteUrl ?? "",
165 repository: null,
166 });
167 }),
168 });
169 return Effect.gen(function* () {
170 const tracker = yield* ProjectCloneTracker.ProjectCloneTracker;
171 yield* tracker.start(startInput, harness.hooks);
172 yield* Effect.yieldNow;
173 const failed = yield* tracker.get(projectId);
174 expect(failed).toMatchObject({ phase: "failed", error: "fatal: repository not found" });
175​
176 expect(yield* tracker.retry(projectId)).toBe(true);
177 expect(harness.discarded).toEqual(["/workspace/t3code"]);
178 yield* Effect.yieldNow;
179 expect((yield* tracker.get(projectId))?.phase).toBe("done");
180 expect(attempts).toBe(2);
181 }).pipe(Effect.provide(harness.layer));
182 });
183​
184 it.effect("cancel interrupts the clone and removes the partial checkout", () => {
185 const harness = makeHarness({ clone: () => Effect.never });
186 return Effect.gen(function* () {
187 const tracker = yield* ProjectCloneTracker.ProjectCloneTracker;
188 yield* tracker.start(startInput, harness.hooks);
189 yield* Effect.yieldNow;
190​
191 expect(yield* tracker.cancel(projectId)).toBe(true);
192 expect((yield* tracker.get(projectId))?.phase).toBe("cancelled");
193 expect(harness.discarded).toEqual(["/workspace/t3code"]);
194 expect(yield* tracker.cancel(projectId)).toBe(false);
195 expect(yield* tracker.retry(projectId)).toBe(true);
196 }).pipe(Effect.provide(harness.layer));
197 });
198​
199 it.effect("a cancel that lands after git finished keeps the checkout", () => {
200 const gate = Deferred.makeUnsafe<void>();
201 const harness = makeHarness();
202 const hooks: ProjectCloneTracker.ProjectCloneHooks = {
203 ...harness.hooks,
204 onCloned: () => Deferred.await(gate),
205 };
206 return Effect.gen(function* () {
207 const tracker = yield* ProjectCloneTracker.ProjectCloneTracker;
208 yield* tracker.start(startInput, hooks);
209 yield* Effect.yieldNow;
210 expect((yield* tracker.get(projectId))?.phase).toBe("done");
211 expect(yield* tracker.cancel(projectId)).toBe(false);
212 expect(harness.discarded).toEqual([]);
213 yield* Deferred.succeed(gate, undefined);
214 }).pipe(Effect.provide(harness.layer));
215 });
216​
217 it.effect("hands git the credential-bearing URL while snapshots carry the redacted one", () => {
218 const cloneUrls: Array<string> = [];
219 const harness = makeHarness({
220 clone: (input) =>
221 Effect.sync(() => {
222 cloneUrls.push(input.remoteUrl ?? "");
223 return { cwd: input.destinationPath, remoteUrl: "", repository: null };
224 }),
225 });
226 const layer = ProjectCloneTracker.layer.pipe(
227 Layer.provide(
228 Layer.mock(SourceControlRepositoryService.SourceControlRepositoryService)({
229 prepareClone: (input) =>
230 Effect.succeed({
231 destinationPath: input.destinationPath,
232 remoteUrl: "https://github.com/octocat/t3code.git",
233 cloneUrl: "https://user:s3cret@github.com/octocat/t3code.git",
234 repository: null,
235 }),
236 cloneRepository: (input) =>
237 Effect.sync(() => {
238 cloneUrls.push(input.remoteUrl ?? "");
239 return { cwd: input.destinationPath, remoteUrl: "", repository: null };
240 }),
241 discardClone: () => Effect.void,
242 }),
243 ),
244 );
245 return Effect.gen(function* () {
246 const tracker = yield* ProjectCloneTracker.ProjectCloneTracker;
247 const result = yield* tracker.start(startInput, harness.hooks);
248 yield* Effect.yieldNow;
249 expect(result.remoteUrl).toBe("https://github.com/octocat/t3code.git");
250 expect(cloneUrls).toEqual(["https://user:s3cret@github.com/octocat/t3code.git"]);
251 }).pipe(Effect.provide(layer));
252 });
253​
254 it.effect("discard forgets a project's clone when the project is deleted", () => {
255 const harness = makeHarness({ clone: () => Effect.never });
256 return Effect.gen(function* () {
257 const tracker = yield* ProjectCloneTracker.ProjectCloneTracker;
258 yield* tracker.start(startInput, harness.hooks);
259 yield* Effect.yieldNow;
260 yield* tracker.discard(projectId);
261 expect(yield* tracker.get(projectId)).toBeNull();
262 expect(harness.discarded).toEqual(["/workspace/t3code"]);
263 }).pipe(Effect.provide(harness.layer));
264 });
265​
266 it.effect("releases the claim when project creation fails", () => {
267 const harness = makeHarness();
268 const hooks: ProjectCloneTracker.ProjectCloneHooks = {
269 ...harness.hooks,
270 createProject: () =>
271 Effect.fail(new OrchestrationDispatchCommandError({ message: "workspace root exists" })),
272 };
273 return Effect.gen(function* () {
274 const tracker = yield* ProjectCloneTracker.ProjectCloneTracker;
275 const error = yield* Effect.flip(tracker.start(startInput, hooks));
276 expect(error.message).toContain("workspace root exists");
277 expect(yield* tracker.get(projectId)).toBeNull();
278 yield* tracker.start(startInput, harness.hooks);
279 yield* Effect.yieldNow;
280 expect((yield* tracker.get(projectId))?.phase).toBe("done");
281 }).pipe(Effect.provide(harness.layer));
282 });
283​
284 it.effect("does not create a project when the clone cannot be prepared", () => {
285 const harness = makeHarness();
286 const layer = ProjectCloneTracker.layer.pipe(
287 Layer.provide(
288 Layer.mock(SourceControlRepositoryService.SourceControlRepositoryService)({
289 prepareClone: () =>
290 Effect.fail(
291 new SourceControlRepositoryError({
292 operation: "cloneRepository",
293 provider: SourceControlProviderKind.make("unknown"),
294 detail: "Destination path already exists and is not empty.",
295 }),
296 ),
297 }),
298 ),
299 );
300 return Effect.gen(function* () {
301 const tracker = yield* ProjectCloneTracker.ProjectCloneTracker;
302 const error = yield* Effect.flip(tracker.start(startInput, harness.hooks));
303 expect(error.message).toContain("not empty");
304 expect(harness.created).toEqual([]);
305 expect(yield* tracker.get(projectId)).toBeNull();
306 }).pipe(Effect.provide(layer));
307 });
308});
309​
310describe("parseGitCloneProgressLine", () => {
311 it("parses git's transfer counters and ignores other output", () => {
312 expect(
313 parseGitCloneProgressLine("Receiving objects: 45% (4500/10000), 12.30 MiB | 5.00 MiB/s"),
314 ).toEqual({ stage: "receiving", percent: 45, detail: "12.30 MiB | 5.00 MiB/s" });
315 expect(parseGitCloneProgressLine("Resolving deltas: 100% (700/700), done.")).toEqual({
316 stage: "resolving",
317 percent: 100,
318 detail: null,
319 });
320 expect(parseGitCloneProgressLine("remote: Compressing objects: 12% (3/25)")).toEqual({
321 stage: "counting",
322 percent: 12,
323 detail: null,
324 });
325 expect(parseGitCloneProgressLine("Updating files: 78% (2104/2700)")).toEqual({
326 stage: "checkout",
327 percent: 78,
328 detail: null,
329 });
330 expect(parseGitCloneProgressLine("remote: Enumerating objects: 10, done.")).toEqual({
331 stage: "counting",
332 percent: null,
333 detail: null,
334 });
335 expect(parseGitCloneProgressLine("Cloning into 't3code'...")).toBeNull();
336 expect(parseGitCloneProgressLine("fatal: repository not found")).toBeNull();
337 });
338});