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

ProjectCloneTracker.test.ts

298 lines11.7 KB

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