The best way to control Claude Code, Codex and more from any computer or phone

main
ts 849 lines 34.9 KB
Raw
1 /**
2 * Tier-3 — the money suite (DESIGN.md §9 steps 1–14): the real `git` binary
3 * driven via the Effect `ChildProcess` service against a deployed
4 * git-service stack, work-trees under `FileSystem.makeTempDirectory`.
5 *
6 * Every git invocation is bounded (60s command timeout inside ≤120s tests);
7 * slow cases are `skipIf(process.env.FAST)`. `NO_DESTROY=1` keeps the
8 * deployment between local iterations.
9 */
10 import * as Cloudflare from "@alchemy.run/cloudflare";
11 import { GitApi, type Oid } from "@alchemy.run/git/Api";
12 import { expect } from "@alchemy.run/test";
13 import * as Test from "@alchemy.run/test/Alchemy";
14 import * as Data from "effect/Data";
15 import * as Effect from "effect/Effect";
16 import * as FileSystem from "effect/FileSystem";
17 import * as HttpApiClient from "effect/http-api/HttpApiClient";
18 import * as HttpClient from "effect/http/HttpClient";
19 import * as HttpClientRequest from "effect/http/HttpClientRequest";
20 import * as Path from "effect/Path";
21 import * as ChildProcess from "effect/process/ChildProcess";
22 import { MinimumLogLevel } from "effect/References";
23 import * as Schedule from "effect/Schedule";
24 import * as Stream from "effect/Stream";
25 import { makeTestStack, TEST_SECRET } from "./fixtures/stack.ts";
26
27 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
28 providers: Cloudflare.providers(),
29 });
30
31 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
32
33 const Stack = makeTestStack("GitServiceE2EStack");
34
35 // ── REST plumbing ───────────────────────────────────────────────────────────
36
37 const makeClient = (url: string, token: string) =>
38 HttpApiClient.make(GitApi, {
39 baseUrl: url,
40 transformClient: HttpClient.mapRequest((request) =>
41 request.pipe(HttpClientRequest.bearerToken(token)),
42 ),
43 });
44
45 const asOid = (oid: string): Oid => oid as Oid;
46
47 const edgeRetry = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
48 effect.pipe(
49 Effect.timeout("10 seconds"),
50 Effect.retry({
51 while: (error: unknown) =>
52 (error as { _tag?: string })._tag === "TimeoutError" ||
53 ((error as { _tag?: string })._tag === "HttpClientError" &&
54 (!(error as { response?: { status: number } }).response ||
55 (error as { response: { status: number } }).response.status === 404 ||
56 (error as { response: { status: number } }).response.status >= 500)),
57 schedule: Schedule.spaced("1500 millis"),
58 times: 40,
59 }),
60 );
61
62 /** Delete-if-exists + wait until the deterministic name is free again. */
63 const purgeRepo = Effect.fn(function* (url: string, owner: string, repo: string) {
64 const admin = yield* makeClient(url, TEST_SECRET);
65 // edgeRetry on every step: a freshly deployed workers.dev route serves
66 // transient 5xx/1042s for a few seconds (typed 404s decode fine and are
67 // NOT retried — they end the poll).
68 yield* admin.repos.delete({ params: { owner, repo } }).pipe(
69 Effect.catchTag("RepoNotFound", () => Effect.void),
70 edgeRetry,
71 );
72 yield* admin.repos.get({ params: { owner, repo } }).pipe(
73 edgeRetry,
74 Effect.as(false),
75 Effect.catchTag("RepoNotFound", () => Effect.succeed(true)),
76 Effect.repeat({
77 schedule: Schedule.spaced("1 second"),
78 until: (gone) => gone,
79 times: 60,
80 }),
81 );
82 });
83
84 // ── git CLI plumbing ────────────────────────────────────────────────────────
85
86 class GitError extends Data.TaggedError("GitError")<{
87 readonly args: ReadonlyArray<string>;
88 readonly exitCode: number;
89 readonly stdout: string;
90 readonly stderr: string;
91 }> {
92 override get message(): string {
93 return `git ${this.args.join(" ")} exited ${this.exitCode}\n--- stdout ---\n${this.stdout}\n--- stderr ---\n${this.stderr}`;
94 }
95 }
96
97 const git = Effect.fn(function* (cwd: string, ...args: Array<string>) {
98 const handle = yield* ChildProcess.make("git", args, {
99 cwd,
100 env: {
101 GIT_TERMINAL_PROMPT: "0",
102 GIT_CONFIG_NOSYSTEM: "1",
103 GIT_CONFIG_GLOBAL: "/dev/null",
104 GIT_AUTHOR_NAME: "Test User",
105 GIT_AUTHOR_EMAIL: "test@example.com",
106 GIT_COMMITTER_NAME: "Test User",
107 GIT_COMMITTER_EMAIL: "test@example.com",
108 },
109 extendEnv: true,
110 });
111 const [exitCode, stdout, stderr] = yield* Effect.all(
112 [
113 handle.exitCode,
114 Stream.mkString(Stream.decodeText(handle.stdout)),
115 Stream.mkString(Stream.decodeText(handle.stderr)),
116 ],
117 { concurrency: 3 },
118 );
119 return { args, exitCode, stdout: stdout.trim(), stderr };
120 }, Effect.timeout("60 seconds"));
121
122 const mustGit = Effect.fn(function* (cwd: string, ...args: Array<string>) {
123 const result = yield* git(cwd, ...args);
124 if (result.exitCode !== 0) {
125 return yield* new GitError(result);
126 }
127 return result;
128 });
129
130 const mustFailGit = Effect.fn(function* (cwd: string, ...args: Array<string>) {
131 const result = yield* git(cwd, ...args);
132 if (result.exitCode === 0) {
133 return yield* new GitError(result);
134 }
135 return result;
136 });
137
138 /** Bounded retry of a git command through transient edge/contention windows. */
139 const retryGit = (cwd: string, ...args: Array<string>) =>
140 mustGit(cwd, ...args).pipe(
141 Effect.retry({
142 while: (error) => error._tag === "GitError" || error._tag === "TimeoutError",
143 schedule: Schedule.spaced("3 seconds"),
144 times: 5,
145 }),
146 );
147
148 const authRemote = (url: string, token: string, owner: string, repo: string) =>
149 Effect.sync(() => {
150 const parsed = new URL(url);
151 return `${parsed.protocol}//x:${token}@${parsed.host}/${owner}/${repo}.git`;
152 });
153
154 const tempDir = Effect.gen(function* () {
155 const fs = yield* FileSystem.FileSystem;
156 return yield* fs.makeTempDirectory({ prefix: "git-service-e2e-" });
157 });
158
159 /** Purge + create a repo, returning the admin client and an authed remote. */
160 const freshRepo = Effect.fn(function* (url: string, owner: string, name: string) {
161 const admin = yield* makeClient(url, TEST_SECRET);
162 // Retry the whole purge -> create CYCLE, never the bare POST: a create
163 // that commits server-side but loses its response (edge 5xx mid-rollout)
164 // leaves the name taken, so retrying just the POST would 409 forever.
165 const created = yield* Effect.gen(function* () {
166 yield* purgeRepo(url, owner, name);
167 return yield* admin.repos.create({ payload: { owner, name } }).pipe(edgeRetry);
168 }).pipe(
169 Effect.retry({
170 while: (error: { readonly _tag?: string }) => error._tag === "RepoAlreadyExists",
171 schedule: Schedule.spaced("1 second"),
172 times: 3,
173 }),
174 );
175 return {
176 admin,
177 created,
178 token: TEST_SECRET,
179 remote: yield* authRemote(url, TEST_SECRET, owner, name),
180 };
181 });
182
183 /** Deterministic pseudo-random bytes (xorshift32) — stable across runs. */
184 const deterministicBytes = (length: number, seed: number) =>
185 Effect.sync(() => {
186 const bytes = new Uint8Array(length);
187 let state = seed >>> 0 || 1;
188 for (let i = 0; i < length; i++) {
189 state ^= state << 13;
190 state ^= state >>> 17;
191 state ^= state << 5;
192 bytes[i] = state & 0xff;
193 }
194 return bytes;
195 });
196
197 const stack = beforeAll(
198 deploy(Stack).pipe(
199 Effect.tap(({ url }) =>
200 Effect.gen(function* () {
201 // Printed so a failing live run can be probed by hand (curl/git).
202 yield* Effect.logInfo(`git-service deployed at ${url}`);
203 const admin = yield* makeClient(url, TEST_SECRET);
204 // Readiness probe: retry on ANY failure, not just transport errors.
205 // Right after a rollout the edge briefly serves the PREVIOUS worker
206 // version, whose responses decode against the old schema — that
207 // surfaces as a SchemaError on a 200, which `edgeRetry` ignores by
208 // design. Waiting here means "the deployed version answers the
209 // schema this test binary was built against".
210 yield* admin.repos.list({ query: {} }).pipe(
211 edgeRetry,
212 Effect.retry({
213 schedule: Schedule.spaced("1500 millis"),
214 times: 40,
215 }),
216 );
217 }),
218 ),
219 logLevel,
220 ),
221 );
222 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
223
224 // ── steps 1 + 2: empty clone, first push, REST agreement ────────────────────
225
226 test(
227 "empty-repo clone, first push (exec bit, symlink, subdir), REST agreement",
228 Effect.gen(function* () {
229 const { url } = yield* stack;
230 const { admin, remote } = yield* freshRepo(url, "e2e", "proto-basic");
231 const fs = yield* FileSystem.FileSystem;
232 const path = yield* Path.Path;
233 const tmp = yield* tempDir;
234 const params = { owner: "e2e", repo: "proto-basic" };
235
236 // step 1: empty clone succeeds with the empty-repo warning
237 const clone = yield* retryGit(tmp, "-c", "init.defaultBranch=main", "clone", remote, "work");
238 expect(`${clone.stdout}\n${clone.stderr}`).toContain("empty repository");
239 const work = path.join(tmp, "work");
240
241 // step 2: three commits — plain file, subdir, executable + symlink
242 yield* fs.writeFileString(path.join(work, "hello.txt"), "hello e2e\n");
243 yield* mustGit(work, "add", "-A");
244 yield* mustGit(work, "commit", "-m", "c1: hello");
245 yield* fs.makeDirectory(path.join(work, "sub"), { recursive: true });
246 yield* fs.writeFileString(path.join(work, "sub", "nested.txt"), "nested\n");
247 yield* mustGit(work, "add", "-A");
248 yield* mustGit(work, "commit", "-m", "c2: subdir");
249 yield* fs.writeFileString(path.join(work, "run.sh"), "#!/bin/sh\necho ok\n");
250 yield* fs.chmod(path.join(work, "run.sh"), 0o755);
251 yield* fs.symlink("hello.txt", path.join(work, "link.txt"));
252 yield* mustGit(work, "add", "-A");
253 yield* mustGit(work, "commit", "-m", "c3: modes");
254 // A fresh workers.dev rollout can still return its HTML 404 on
255 // receive-pack discovery after upload-pack (the empty clone) succeeds.
256 yield* retryGit(work, "push", "origin", "main");
257 const head = (yield* mustGit(work, "rev-parse", "HEAD")).stdout;
258
259 // REST agrees byte-for-byte with the CLI's view
260 const refs = yield* admin.refs.list({ params, query: {} });
261 expect(refs.refs.find((r) => r.name === "refs/heads/main")?.oid).toBe(head);
262 const commit = yield* admin.objects.commit({
263 params: { ...params, oid: asOid(head) },
264 });
265 expect(commit.message).toContain("c3: modes");
266 const tree = yield* admin.objects.tree({
267 params: { ...params, oid: commit.tree },
268 });
269 expect(tree.entries.find((e) => e.name === "run.sh")?.mode).toBe("100755");
270 expect(tree.entries.find((e) => e.name === "link.txt")?.mode).toBe("120000");
271 expect(tree.entries.find((e) => e.name === "sub")?.type).toBe("tree");
272
273 // the /file raw route serves the working-tree bytes
274 const http = yield* HttpClient.HttpClient;
275 const file = yield* http.get(
276 `${url}/api/v1/repos/e2e/proto-basic/file?ref=refs/heads/main&path=sub/nested.txt`,
277 { headers: { authorization: `Bearer ${TEST_SECRET}` } },
278 );
279 expect(file.status).toBe(200);
280 expect(yield* file.text).toBe("nested\n");
281 }).pipe(logLevel),
282 {
283 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
284 timeout: 120_000,
285 },
286 );
287
288 // ── steps 3 + 4: clone-back + fsck, incremental fetch ───────────────────────
289
290 test(
291 "clone-back is fsck-clean; incremental fetch rides the haves/ACK path",
292 Effect.gen(function* () {
293 const { url } = yield* stack;
294 const { remote } = yield* freshRepo(url, "e2e", "proto-fetch");
295 const fs = yield* FileSystem.FileSystem;
296 const path = yield* Path.Path;
297 const tmp = yield* tempDir;
298
299 yield* retryGit(tmp, "-c", "init.defaultBranch=main", "clone", remote, "a");
300 const a = path.join(tmp, "a");
301 yield* fs.writeFileString(path.join(a, "file.txt"), "v1\n");
302 yield* mustGit(a, "add", "-A");
303 yield* mustGit(a, "commit", "-m", "c1");
304 yield* fs.writeFileString(path.join(a, "file.txt"), "v2\n");
305 yield* mustGit(a, "add", "-A");
306 yield* mustGit(a, "commit", "-m", "c2");
307 yield* retryGit(a, "push", "origin", "main");
308
309 // step 3: fresh clone, strict fsck, identical log
310 yield* retryGit(tmp, "clone", remote, "b");
311 const b = path.join(tmp, "b");
312 yield* mustGit(b, "fsck", "--strict");
313 const logA = (yield* mustGit(a, "log", "--format=%H %s")).stdout;
314 const logB = (yield* mustGit(b, "log", "--format=%H %s")).stdout;
315 expect(logB).toBe(logA);
316
317 // step 4: two more commits from A; B fetches incrementally + ff-merges
318 yield* fs.writeFileString(path.join(a, "file.txt"), "v3\n");
319 yield* mustGit(a, "add", "-A");
320 yield* mustGit(a, "commit", "-m", "c3");
321 yield* fs.writeFileString(path.join(a, "extra.txt"), "extra\n");
322 yield* mustGit(a, "add", "-A");
323 yield* mustGit(a, "commit", "-m", "c4");
324 yield* mustGit(a, "push", "origin", "main");
325 const headA = (yield* mustGit(a, "rev-parse", "HEAD")).stdout;
326
327 yield* mustGit(b, "fetch", "origin");
328 yield* mustGit(b, "merge", "--ff-only", "origin/main");
329 expect((yield* mustGit(b, "rev-parse", "HEAD")).stdout).toBe(headA);
330 expect(yield* fs.readFileString(path.join(b, "file.txt"))).toBe("v3\n");
331 }).pipe(logLevel),
332 {
333 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
334 timeout: 120_000,
335 },
336 );
337
338 // ── step 5: CAS, force push, concurrent CAS race, --atomic ──────────────────
339
340 test(
341 "non-FF push rejected, force push wins, concurrent CAS race has one winner",
342 Effect.gen(function* () {
343 const { url } = yield* stack;
344 const { remote } = yield* freshRepo(url, "e2e", "proto-cas");
345 const fs = yield* FileSystem.FileSystem;
346 const path = yield* Path.Path;
347 const tmp = yield* tempDir;
348
349 yield* retryGit(tmp, "-c", "init.defaultBranch=main", "clone", remote, "a");
350 const a = path.join(tmp, "a");
351 yield* fs.writeFileString(path.join(a, "f.txt"), "base\n");
352 yield* mustGit(a, "add", "-A");
353 yield* mustGit(a, "commit", "-m", "base");
354 yield* mustGit(a, "push", "origin", "main");
355
356 // B clones at base; A advances; B's stale push gets the server-side ng
357 yield* retryGit(tmp, "clone", remote, "b");
358 const b = path.join(tmp, "b");
359 yield* fs.writeFileString(path.join(a, "f.txt"), "a2\n");
360 yield* mustGit(a, "add", "-A");
361 yield* mustGit(a, "commit", "-m", "a2");
362 yield* mustGit(a, "push", "origin", "main");
363
364 yield* fs.writeFileString(path.join(b, "g.txt"), "b2\n");
365 yield* mustGit(b, "add", "-A");
366 yield* mustGit(b, "commit", "-m", "b2");
367 const rejected = yield* mustFailGit(b, "push", "origin", "main");
368 expect(`${rejected.stdout}\n${rejected.stderr}`).toContain("rejected");
369
370 // force push after fetch overwrites; the other clone fetches it cleanly
371 yield* mustGit(b, "fetch", "origin");
372 yield* mustGit(b, "push", "--force", "origin", "main");
373 const headB = (yield* mustGit(b, "rev-parse", "HEAD")).stdout;
374 yield* mustGit(a, "fetch", "origin");
375 expect((yield* mustGit(a, "rev-parse", "origin/main")).stdout).toBe(headB);
376
377 // CAS race: both clones at the same base push divergent tips concurrently;
378 // the Repo DO serializes — exactly one wins, the other reports ng/rejected
379 yield* mustGit(a, "reset", "--hard", "origin/main");
380 yield* fs.writeFileString(path.join(a, "race.txt"), "from-a\n");
381 yield* mustGit(a, "add", "-A");
382 yield* mustGit(a, "commit", "-m", "race-a");
383 yield* fs.writeFileString(path.join(b, "race.txt"), "from-b\n");
384 yield* mustGit(b, "add", "-A");
385 yield* mustGit(b, "commit", "-m", "race-b");
386 const [pushA, pushB] = yield* Effect.all(
387 [git(a, "push", "origin", "main"), git(b, "push", "origin", "main")],
388 { concurrency: 2 },
389 );
390 const succeeded = [pushA, pushB].filter((r) => r.exitCode === 0);
391 expect(succeeded.length).toBe(1);
392
393 // --atomic: a batch with one stale CAS is all-or-nothing
394 const loser = pushA.exitCode === 0 ? b : a;
395 yield* mustFailGit(loser, "push", "--atomic", "origin", "main", "HEAD:refs/heads/atomic-side");
396 const admin = yield* makeClient(url, TEST_SECRET);
397 const refs = yield* admin.refs.list({
398 params: { owner: "e2e", repo: "proto-cas" },
399 query: {},
400 });
401 // the side branch must NOT exist — the failed main CAS aborted the batch
402 expect(refs.refs.some((r) => r.name === "refs/heads/atomic-side")).toBe(false);
403 }).pipe(logLevel),
404 {
405 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
406 timeout: 120_000,
407 },
408 );
409
410 // ── step 6: concurrent push serialization ───────────────────────────────────
411
412 test(
413 "two concurrent pushes to different branches both land (semaphore, no corruption)",
414 Effect.gen(function* () {
415 const { url } = yield* stack;
416 const { admin, remote } = yield* freshRepo(url, "e2e", "proto-concurrent");
417 const fs = yield* FileSystem.FileSystem;
418 const path = yield* Path.Path;
419 const tmp = yield* tempDir;
420
421 yield* retryGit(tmp, "-c", "init.defaultBranch=main", "clone", remote, "a");
422 const a = path.join(tmp, "a");
423 yield* fs.writeFileString(path.join(a, "f.txt"), "base\n");
424 yield* mustGit(a, "add", "-A");
425 yield* mustGit(a, "commit", "-m", "base");
426 yield* mustGit(a, "push", "origin", "main");
427
428 yield* retryGit(tmp, "clone", remote, "b");
429 const b = path.join(tmp, "b");
430 yield* fs.writeFileString(path.join(a, "a.txt"), "a\n");
431 yield* mustGit(a, "add", "-A");
432 yield* mustGit(a, "commit", "-m", "branch-a");
433 yield* fs.writeFileString(path.join(b, "b.txt"), "b\n");
434 yield* mustGit(b, "add", "-A");
435 yield* mustGit(b, "commit", "-m", "branch-b");
436
437 // different target refs: no CAS conflict — both must eventually succeed
438 // (second may wait on the push semaphore or see a 503 + clean retry)
439 yield* Effect.all(
440 [
441 retryGit(a, "push", "origin", "HEAD:refs/heads/side-a"),
442 retryGit(b, "push", "origin", "HEAD:refs/heads/side-b"),
443 ],
444 { concurrency: 2 },
445 );
446 const refs = yield* admin.refs.list({
447 params: { owner: "e2e", repo: "proto-concurrent" },
448 query: { prefix: "refs/heads/" },
449 });
450 const names = refs.refs.map((r) => r.name);
451 expect(names).toContain("refs/heads/side-a");
452 expect(names).toContain("refs/heads/side-b");
453
454 // and the repo is not corrupted
455 yield* retryGit(tmp, "clone", remote, "check");
456 yield* mustGit(path.join(tmp, "check"), "fsck", "--strict");
457 }).pipe(logLevel),
458 {
459 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
460 timeout: 120_000,
461 },
462 );
463
464 // ── step 7: branch + annotated tag lifecycle ────────────────────────────────
465
466 test(
467 "branch and annotated tag round-trip (peeled ^{} advertisement) and delete",
468 Effect.gen(function* () {
469 const { url } = yield* stack;
470 const { admin, remote } = yield* freshRepo(url, "e2e", "proto-tags");
471 const fs = yield* FileSystem.FileSystem;
472 const path = yield* Path.Path;
473 const tmp = yield* tempDir;
474 const params = { owner: "e2e", repo: "proto-tags" };
475
476 yield* retryGit(tmp, "-c", "init.defaultBranch=main", "clone", remote, "work");
477 const work = path.join(tmp, "work");
478 yield* fs.writeFileString(path.join(work, "f.txt"), "one\n");
479 yield* mustGit(work, "add", "-A");
480 yield* mustGit(work, "commit", "-m", "c1");
481 yield* mustGit(work, "push", "origin", "main");
482 const head = (yield* mustGit(work, "rev-parse", "HEAD")).stdout;
483
484 // annotated tag is stored and served as a real tag object (the ripgit
485 // silent-drop bug is this named regression)
486 yield* mustGit(work, "tag", "-a", "v1", "-m", "release v1");
487 yield* mustGit(work, "push", "origin", "v1");
488 const tagOid = (yield* mustGit(work, "rev-parse", "v1")).stdout;
489 expect(tagOid).not.toBe(head);
490 const tagRef = yield* admin.refs.get({
491 params,
492 query: { name: "refs/tags/v1" },
493 });
494 expect(tagRef.oid).toBe(tagOid);
495 expect(tagRef.peeled).toBe(head);
496 const lsRemote = (yield* mustGit(work, "ls-remote", "--tags", "origin")).stdout;
497 expect(lsRemote).toContain(`${tagOid}\trefs/tags/v1`);
498 expect(lsRemote).toContain(`${head}\trefs/tags/v1^{}`);
499
500 // a fresh clone materializes the annotated tag object intact
501 yield* retryGit(tmp, "clone", remote, "verify");
502 const verify = path.join(tmp, "verify");
503 expect((yield* mustGit(verify, "cat-file", "-t", tagOid)).stdout).toBe("tag");
504 expect((yield* mustGit(verify, "tag", "-l", "v1")).stdout).toBe("v1");
505
506 // branch push + delete-refs on both kinds
507 yield* mustGit(work, "push", "origin", "main:feature");
508 yield* mustGit(work, "push", "origin", "--delete", "feature");
509 yield* mustGit(work, "push", "origin", "--delete", "v1");
510 const refs = yield* admin.refs.list({ params, query: {} });
511 expect(refs.refs.map((r) => r.name)).toEqual(["refs/heads/main"]);
512 }).pipe(logLevel),
513 {
514 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
515 timeout: 120_000,
516 },
517 );
518
519 // ── step 8: shallow clone + deepen ──────────────────────────────────────────
520
521 test(
522 "shallow clone --depth 1 truncates history; fetch --depth 2 extends it",
523 Effect.gen(function* () {
524 const { url } = yield* stack;
525 const { remote } = yield* freshRepo(url, "e2e", "proto-shallow");
526 const fs = yield* FileSystem.FileSystem;
527 const path = yield* Path.Path;
528 const tmp = yield* tempDir;
529
530 yield* retryGit(tmp, "-c", "init.defaultBranch=main", "clone", remote, "full");
531 const full = path.join(tmp, "full");
532 for (const n of ["c1", "c2", "c3"]) {
533 yield* fs.writeFileString(path.join(full, "f.txt"), `${n}\n`);
534 yield* mustGit(full, "add", "-A");
535 yield* mustGit(full, "commit", "-m", n);
536 }
537 yield* mustGit(full, "push", "origin", "main");
538
539 // depth-1 clone sees exactly one commit and is marked shallow
540 yield* retryGit(tmp, "clone", "--depth", "1", remote, "shallow");
541 const shallow = path.join(tmp, "shallow");
542 const countShallow = (yield* mustGit(shallow, "rev-list", "--count", "HEAD")).stdout;
543 expect(countShallow).toBe("1");
544 expect(yield* fs.exists(path.join(shallow, ".git", "shallow"))).toBe(true);
545 expect(yield* fs.readFileString(path.join(shallow, "f.txt"))).toBe("c3\n");
546
547 // deepen by one commit — absolute --depth (v1 supports `deepen <n>`
548 // only; `--deepen` needs the deepen-relative capability, cut in v1)
549 yield* mustGit(shallow, "fetch", "--depth", "2", "origin");
550 const deepened = (yield* mustGit(shallow, "rev-list", "--count", "HEAD")).stdout;
551 expect(deepened).toBe("2");
552 }).pipe(logLevel),
553 {
554 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
555 timeout: 120_000,
556 },
557 );
558
559 // ── step 9: large blob through R2 ───────────────────────────────────────────
560
561 test.skipIf(!!process.env.FAST)(
562 "a 3 MiB deterministic binary round-trips byte-identically (location='r2')",
563 Effect.gen(function* () {
564 const { url } = yield* stack;
565 const { remote } = yield* freshRepo(url, "e2e", "proto-bigblob");
566 const fs = yield* FileSystem.FileSystem;
567 const path = yield* Path.Path;
568 const tmp = yield* tempDir;
569
570 yield* retryGit(tmp, "-c", "init.defaultBranch=main", "clone", remote, "a");
571 const a = path.join(tmp, "a");
572 // deterministic (seeded) noise: > 1 MiB compressed, forcing the R2 path
573 const noise = yield* deterministicBytes(3 * 1024 * 1024, 0xa1c4e57);
574 yield* fs.writeFile(path.join(a, "big.bin"), noise);
575 yield* mustGit(a, "add", "-A");
576 yield* mustGit(a, "commit", "-m", "big blob");
577 yield* mustGit(a, "push", "origin", "main");
578 const blobOid = (yield* mustGit(a, "rev-parse", "HEAD:big.bin")).stdout;
579
580 yield* retryGit(tmp, "clone", remote, "b");
581 const b = path.join(tmp, "b");
582 yield* mustGit(b, "fsck", "--strict");
583 expect((yield* mustGit(b, "rev-parse", "HEAD:big.bin")).stdout).toBe(blobOid);
584 const roundTripped = yield* fs.readFile(path.join(b, "big.bin"));
585 expect(roundTripped.length).toBe(noise.length);
586 expect(Buffer.compare(Buffer.from(roundTripped), Buffer.from(noise))).toBe(0);
587 }).pipe(logLevel),
588 {
589 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
590 timeout: 120_000,
591 },
592 );
593
594 // ── step 10: big-ish push (deep delta chains) ───────────────────────────────
595
596 test.skipIf(!!process.env.FAST)(
597 "a ~500-commit history pushes and clones back within budget",
598 Effect.gen(function* () {
599 const { url } = yield* stack;
600 const { remote } = yield* freshRepo(url, "e2e", "proto-many");
601 const path = yield* Path.Path;
602 const tmp = yield* tempDir;
603
604 yield* retryGit(tmp, "-c", "init.defaultBranch=main", "clone", remote, "a");
605 const a = path.join(tmp, "a");
606 // one sh child builds the history (500 spawns of `git commit` inside a
607 // single shell — deterministic content, growing file → deep delta chains)
608 const script = [
609 "set -e",
610 "for i in $(seq 1 500); do",
611 ' echo "line $i of the growing fixture payload" >> grow.txt',
612 " git add grow.txt",
613 ' git commit -q -m "c$i"',
614 "done",
615 ].join("\n");
616 const handle = yield* ChildProcess.make("sh", ["-c", script], {
617 cwd: a,
618 env: {
619 GIT_AUTHOR_NAME: "Test User",
620 GIT_AUTHOR_EMAIL: "test@example.com",
621 GIT_COMMITTER_NAME: "Test User",
622 GIT_COMMITTER_EMAIL: "test@example.com",
623 GIT_CONFIG_NOSYSTEM: "1",
624 GIT_CONFIG_GLOBAL: "/dev/null",
625 },
626 extendEnv: true,
627 });
628 expect(yield* handle.exitCode.pipe(Effect.timeout("60 seconds"))).toBe(0);
629
630 yield* mustGit(a, "push", "origin", "main");
631 const head = (yield* mustGit(a, "rev-parse", "HEAD")).stdout;
632
633 yield* retryGit(tmp, "clone", remote, "b");
634 const b = path.join(tmp, "b");
635 yield* mustGit(b, "fsck", "--strict");
636 expect((yield* mustGit(b, "rev-parse", "HEAD")).stdout).toBe(head);
637 expect((yield* mustGit(b, "rev-list", "--count", "HEAD")).stdout).toBe("500");
638 }).pipe(logLevel),
639 {
640 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
641 timeout: 120_000,
642 },
643 );
644
645 // ── step 11: readOnly ───────────────────────────────────────────────────────
646
647 test(
648 "readOnly repos reject pushes per-ref; unflag restores writes",
649 Effect.gen(function* () {
650 const { url } = yield* stack;
651 const { admin, remote } = yield* freshRepo(url, "e2e", "proto-readonly");
652 const fs = yield* FileSystem.FileSystem;
653 const path = yield* Path.Path;
654 const tmp = yield* tempDir;
655 const params = { owner: "e2e", repo: "proto-readonly" };
656
657 yield* retryGit(tmp, "-c", "init.defaultBranch=main", "clone", remote, "work");
658 const work = path.join(tmp, "work");
659 yield* fs.writeFileString(path.join(work, "f.txt"), "one\n");
660 yield* mustGit(work, "add", "-A");
661 yield* mustGit(work, "commit", "-m", "c1");
662 yield* mustGit(work, "push", "origin", "main");
663
664 yield* admin.repos.update({ params, payload: { readOnly: true } });
665 yield* fs.writeFileString(path.join(work, "f.txt"), "two\n");
666 yield* mustGit(work, "add", "-A");
667 yield* mustGit(work, "commit", "-m", "c2");
668 const rejected = yield* mustFailGit(work, "push", "origin", "main");
669 expect(`${rejected.stdout}\n${rejected.stderr}`).toContain("rejected");
670 // reads still work while readOnly
671 yield* mustGit(work, "fetch", "origin");
672
673 yield* admin.repos.update({ params, payload: { readOnly: false } });
674 yield* mustGit(work, "push", "origin", "main");
675 }).pipe(logLevel),
676 {
677 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
678 timeout: 120_000,
679 },
680 );
681
682 // ── step 12: auth matrix ────────────────────────────────────────────────────
683
684 test(
685 "auth matrix: an unknown credential is anonymous and 401s on a private repo",
686 Effect.gen(function* () {
687 const { url } = yield* stack;
688 const { admin, remote } = yield* freshRepo(url, "e2e", "proto-auth");
689 const fs = yield* FileSystem.FileSystem;
690 const path = yield* Path.Path;
691 const tmp = yield* tempDir;
692 const params = { owner: "e2e", repo: "proto-auth" };
693
694 yield* retryGit(tmp, "-c", "init.defaultBranch=main", "clone", remote, "seed");
695 const seed = path.join(tmp, "seed");
696 yield* fs.writeFileString(path.join(seed, "f.txt"), "seed\n");
697 yield* mustGit(seed, "add", "-A");
698 yield* mustGit(seed, "commit", "-m", "c1");
699 yield* mustGit(seed, "push", "origin", "main");
700
701 // garbage token → 401 (git surfaces the auth failure on stderr)
702 const badRemote = yield* authRemote(url, "gs_garbage", "e2e", "proto-auth");
703 const unauthorized = yield* mustFailGit(tmp, "clone", badRemote, "nope");
704 expect(unauthorized.stderr.toLowerCase()).toMatch(/401|authentication/);
705
706 // token embedded in the remote URL: read scope clones, cannot push
707 }).pipe(logLevel),
708 {
709 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
710 timeout: 120_000,
711 },
712 );
713
714 // ── step 13: fork + import ──────────────────────────────────────────────────
715
716 test.skipIf(!!process.env.FAST)(
717 "fork: identical history, divergent push isolation, parent delete keeps the fork alive",
718 Effect.gen(function* () {
719 const { url } = yield* stack;
720 const { admin, remote } = yield* freshRepo(url, "e2e", "proto-fork-src");
721 const fs = yield* FileSystem.FileSystem;
722 const path = yield* Path.Path;
723 const tmp = yield* tempDir;
724 yield* purgeRepo(url, "e2e", "proto-fork-dst");
725
726 // seed the parent
727 yield* retryGit(tmp, "-c", "init.defaultBranch=main", "clone", remote, "src");
728 const src = path.join(tmp, "src");
729 yield* fs.writeFileString(path.join(src, "f.txt"), "one\n");
730 yield* mustGit(src, "add", "-A");
731 yield* mustGit(src, "commit", "-m", "c1");
732 yield* fs.writeFileString(path.join(src, "f.txt"), "two\n");
733 yield* mustGit(src, "add", "-A");
734 yield* mustGit(src, "commit", "-m", "c2");
735 yield* mustGit(src, "push", "origin", "main");
736 const parentHead = (yield* mustGit(src, "rev-parse", "HEAD")).stdout;
737
738 // fork via REST, poll until the alarm job flips it ready
739 const forked = yield* admin.repos.fork({
740 params: { owner: "e2e", repo: "proto-fork-src" },
741 payload: { targetOwner: "e2e", targetName: "proto-fork-dst" },
742 });
743 expect(forked.repo.status).toBe("forking");
744 expect(forked.repo.forkOf).toBeTruthy();
745 const ready = yield* admin.repos.get({ params: { owner: "e2e", repo: "proto-fork-dst" } }).pipe(
746 Effect.repeat({
747 schedule: Schedule.spaced("2 seconds"),
748 until: (repo) => repo.status === "ready",
749 times: 45,
750 }),
751 );
752 expect(ready.status).toBe("ready");
753
754 // the fork clones with its own bootstrap token and matches the parent
755 const forkRemote = yield* authRemote(url, TEST_SECRET, "e2e", "proto-fork-dst");
756 yield* retryGit(tmp, "clone", forkRemote, "dst");
757 const dst = path.join(tmp, "dst");
758 yield* mustGit(dst, "fsck", "--strict");
759 expect((yield* mustGit(dst, "rev-parse", "HEAD")).stdout).toBe(parentHead);
760
761 // divergent push to the fork leaves the parent untouched
762 yield* fs.writeFileString(path.join(dst, "fork-only.txt"), "fork\n");
763 yield* mustGit(dst, "add", "-A");
764 yield* mustGit(dst, "commit", "-m", "fork-only");
765 yield* mustGit(dst, "push", "origin", "main");
766 const parentRefs = yield* admin.refs.list({
767 params: { owner: "e2e", repo: "proto-fork-src" },
768 query: {},
769 });
770 expect(parentRefs.refs.find((r) => r.name === "refs/heads/main")?.oid).toBe(parentHead);
771
772 // delete the parent → the fork still clones (R2 fork-retention pin)
773 yield* admin.repos.delete({
774 params: { owner: "e2e", repo: "proto-fork-src" },
775 });
776 yield* admin.repos.get({ params: { owner: "e2e", repo: "proto-fork-src" } }).pipe(
777 Effect.as(false),
778 Effect.catchTag("RepoNotFound", () => Effect.succeed(true)),
779 Effect.repeat({
780 schedule: Schedule.spaced("2 seconds"),
781 until: (gone) => gone,
782 times: 45,
783 }),
784 );
785 yield* retryGit(tmp, "clone", forkRemote, "dst2");
786 yield* mustGit(path.join(tmp, "dst2"), "fsck", "--strict");
787
788 yield* purgeRepo(url, "e2e", "proto-fork-dst");
789 }).pipe(logLevel),
790 {
791 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
792 timeout: 180_000,
793 },
794 );
795
796 test.skipIf(!!process.env.FAST)(
797 "import: depth-limited import from a public smart-HTTP source reaches ready",
798 Effect.gen(function* () {
799 const { url } = yield* stack;
800 const admin = yield* makeClient(url, TEST_SECRET);
801 yield* purgeRepo(url, "e2e", "proto-import");
802
803 // a tiny, stable public repo; depth-limited to stay under the 50 MiB cap.
804 // (Importing a sibling repo of the SAME deployment would make the worker
805 // fetch its own hostname, which Cloudflare blocks as worker recursion.)
806 const imported = yield* admin.repos.import({
807 payload: {
808 owner: "e2e",
809 name: "proto-import",
810 source: { url: "https://github.com/octocat/Hello-World.git", depth: 1 },
811 },
812 });
813 expect(imported.repo.status).toBe("importing");
814
815 const ready = yield* admin.repos.get({ params: { owner: "e2e", repo: "proto-import" } }).pipe(
816 Effect.repeat({
817 schedule: Schedule.spaced("2 seconds"),
818 until: (repo) => repo.status === "ready",
819 times: 45,
820 }),
821 );
822 expect(ready.status).toBe("ready");
823
824 const refs = yield* admin.refs.list({
825 params: { owner: "e2e", repo: "proto-import" },
826 query: {},
827 });
828 expect(refs.refs.length).toBeGreaterThan(0);
829
830 // and the imported history clones with a fresh token
831 const path = yield* Path.Path;
832 const tmp = yield* tempDir;
833 const importRemote = yield* authRemote(url, TEST_SECRET, "e2e", "proto-import");
834 // the import was depth-limited, so the repo's history is shallow —
835 // v0 can only serve that to a shallow-aware client (see handleUploadPack)
836 yield* retryGit(tmp, "clone", "--depth", "1", importRemote, "imported");
837 yield* mustGit(path.join(tmp, "imported"), "fsck", "--strict");
838
839 yield* purgeRepo(url, "e2e", "proto-import");
840 }).pipe(logLevel),
841 {
842 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
843 timeout: 180_000,
844 },
845 );
846
847 // step 14 (cleanup pin) is the afterAll destroy above; out-of-band distilled
848 // verification of the R2 prefix + Registry rows is a coordinator-level check
849 // (this package does not depend on distilled).