@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 488 lines 17.6 KB
Raw
1 /**
2 * Conformance workout — the real `git` CLI driven through the SHELL against
3 * a deployed git-service stack.
4 *
5 * There is no official conformance suite for git servers; the de-facto
6 * oracle is the git client itself plus its strict integrity checkers. This
7 * suite therefore treats conformance as: every everyday workflow a
8 * developer throws at a remote — clone, push, rebase, amend, merge,
9 * cherry-pick, revert, tags (both kinds), branch churn, prune, atomic
10 * multi-ref pushes, client-side gc — round-trips against the deployed
11 * service with `git fsck --strict` clean at every checkpoint, and the REST
12 * plane (token flow, refs) agrees with what the wire reports.
13 *
14 * Each phase is a literal `sh -c` script (bounded by a hard timeout), so a
15 * failure prints the exact shell block + stderr that broke.
16 */
17 import * as Cloudflare from "@alchemy.run/cloudflare";
18 import { GitApi } from "@alchemy.run/git/Api";
19 import { expect } from "@alchemy.run/test";
20 import * as Test from "@alchemy.run/test/Alchemy";
21 import * as Data from "effect/Data";
22 import * as Effect from "effect/Effect";
23 import * as FileSystem from "effect/FileSystem";
24 import * as HttpApiClient from "effect/http-api/HttpApiClient";
25 import * as HttpClient from "effect/http/HttpClient";
26 import * as HttpClientRequest from "effect/http/HttpClientRequest";
27 import * as ChildProcess from "effect/process/ChildProcess";
28 import { MinimumLogLevel } from "effect/References";
29 import * as Schedule from "effect/Schedule";
30 import * as Stream from "effect/Stream";
31 import { makeTestStack, TEST_SECRET } from "./fixtures/stack.ts";
32
33 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
34 providers: Cloudflare.providers(),
35 });
36
37 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
38
39 const Stack = makeTestStack("GitConformanceStack");
40
41 // ── REST plumbing ───────────────────────────────────────────────────────────
42
43 const makeClient = (url: string, token: string) =>
44 HttpApiClient.make(GitApi, {
45 baseUrl: url,
46 transformClient: HttpClient.mapRequest((request) =>
47 request.pipe(HttpClientRequest.bearerToken(token)),
48 ),
49 });
50
51 const edgeRetry = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
52 effect.pipe(
53 Effect.timeout("10 seconds"),
54 Effect.retry({
55 while: (error: unknown) =>
56 (error as { _tag?: string })._tag === "TimeoutError" ||
57 ((error as { _tag?: string })._tag === "HttpClientError" &&
58 (!(error as { response?: { status: number } }).response ||
59 (error as { response: { status: number } }).response.status === 404 ||
60 (error as { response: { status: number } }).response.status >= 500)),
61 schedule: Schedule.spaced("1500 millis"),
62 times: 40,
63 }),
64 );
65
66 /** Delete-if-exists + wait until the deterministic name is free again. */
67 const purgeRepo = Effect.fn(function* (url: string, owner: string, repo: string) {
68 const admin = yield* makeClient(url, TEST_SECRET);
69 // edgeRetry on every step: a freshly deployed workers.dev route serves
70 // transient 5xx/1042s for a few seconds (typed 404s decode fine and are
71 // NOT retried — they end the poll).
72 yield* admin.repos.delete({ params: { owner, repo } }).pipe(
73 Effect.catchTag("RepoNotFound", () => Effect.void),
74 edgeRetry,
75 );
76 yield* admin.repos.get({ params: { owner, repo } }).pipe(
77 edgeRetry,
78 Effect.as(false),
79 Effect.catchTag("RepoNotFound", () => Effect.succeed(true)),
80 Effect.repeat({
81 schedule: Schedule.spaced("1 second"),
82 until: (gone) => gone,
83 times: 60,
84 }),
85 );
86 });
87
88 /** Purge + create a repo, returning the admin client and an authed remote. */
89 const freshRepo = Effect.fn(function* (url: string, owner: string, name: string) {
90 const admin = yield* makeClient(url, TEST_SECRET);
91 // Retry the whole purge -> create CYCLE, never the bare POST: a create
92 // that commits server-side but loses its response (edge 5xx mid-rollout)
93 // leaves the name taken, so retrying just the POST would 409 forever.
94 const created = yield* Effect.gen(function* () {
95 yield* purgeRepo(url, owner, name);
96 return yield* admin.repos.create({ payload: { owner, name } }).pipe(edgeRetry);
97 }).pipe(
98 Effect.retry({
99 while: (error: { readonly _tag?: string }) => error._tag === "RepoAlreadyExists",
100 schedule: Schedule.spaced("1 second"),
101 times: 3,
102 }),
103 );
104 const parsed = new URL(url);
105 return {
106 admin,
107 created,
108 token: TEST_SECRET,
109 host: parsed.host,
110 remoteFor: (token: string) =>
111 `${parsed.protocol}//x:${token}@${parsed.host}/${owner}/${name}.git`,
112 remote: `${parsed.protocol}//x:${TEST_SECRET}@${parsed.host}/${owner}/${name}.git`,
113 };
114 });
115
116 // ── shell plumbing ──────────────────────────────────────────────────────────
117
118 class ShellError extends Data.TaggedError("ShellError")<{
119 readonly script: string;
120 readonly exitCode: number;
121 readonly stdout: string;
122 readonly stderr: string;
123 }> {
124 override get message(): string {
125 return `shell script exited ${this.exitCode}\n--- script ---\n${this.script}\n--- stdout ---\n${this.stdout}\n--- stderr ---\n${this.stderr}`;
126 }
127 }
128
129 /**
130 * Runs one bounded `sh -c` script with hermetic git identity/config. The
131 * script's cwd is the given work dir; `set -eu` makes any failing command
132 * fail the whole block.
133 */
134 const sh = Effect.fn(function* (cwd: string, script: string) {
135 const handle = yield* ChildProcess.make("sh", ["-ec", script], {
136 cwd,
137 env: {
138 GIT_TERMINAL_PROMPT: "0",
139 GIT_CONFIG_NOSYSTEM: "1",
140 GIT_CONFIG_GLOBAL: "/dev/null",
141 GIT_AUTHOR_NAME: "Conformance Bot",
142 GIT_AUTHOR_EMAIL: "conformance@example.com",
143 GIT_COMMITTER_NAME: "Conformance Bot",
144 GIT_COMMITTER_EMAIL: "conformance@example.com",
145 // deterministic commit timestamps make re-runs reproducible
146 GIT_AUTHOR_DATE: "2026-01-01T00:00:00Z",
147 GIT_COMMITTER_DATE: "2026-01-01T00:00:00Z",
148 },
149 extendEnv: true,
150 });
151 const [exitCode, stdout, stderr] = yield* Effect.all(
152 [
153 handle.exitCode,
154 Stream.mkString(Stream.decodeText(handle.stdout)),
155 Stream.mkString(Stream.decodeText(handle.stderr)),
156 ],
157 { concurrency: 3 },
158 );
159 return { script, exitCode, stdout: stdout.trim(), stderr };
160 }, Effect.timeout("90 seconds"));
161
162 /** Runs the script; non-zero exit is a typed failure carrying all output. */
163 const mustSh = Effect.fn(function* (cwd: string, script: string) {
164 const result = yield* sh(cwd, script);
165 if (result.exitCode !== 0) {
166 return yield* new ShellError(result);
167 }
168 return result;
169 });
170
171 /** Runs the script; a ZERO exit is the failure (expected-to-fail blocks). */
172 const mustFailSh = Effect.fn(function* (cwd: string, script: string) {
173 const result = yield* sh(cwd, script);
174 if (result.exitCode === 0) {
175 return yield* new ShellError(result);
176 }
177 return result;
178 });
179
180 /** First-contact clone with bounded retries through edge propagation. */
181 const retrySh = (cwd: string, script: string) =>
182 mustSh(cwd, script).pipe(
183 Effect.retry({
184 while: (error) => error._tag === "ShellError" || error._tag === "TimeoutError",
185 schedule: Schedule.spaced("3 seconds"),
186 times: 5,
187 }),
188 );
189
190 const tempDir = Effect.gen(function* () {
191 const fs = yield* FileSystem.FileSystem;
192 return yield* fs.makeTempDirectory({ prefix: "git-conformance-" });
193 });
194
195 // ── stack ───────────────────────────────────────────────────────────────────
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).pipe(logLevel));
223
224 // ═══════════════════════════════════════════════════════════════════════════
225 // 1. Token flow: bootstrap → scoped tokens → revocation, end to end
226 // ═══════════════════════════════════════════════════════════════════════════
227
228 // ═══════════════════════════════════════════════════════════════════════════
229 // 2. History surgery: rebase, amend, merge, cherry-pick, revert, force-push
230 // ═══════════════════════════════════════════════════════════════════════════
231
232 test(
233 "history surgery: rebase + amend + merge --no-ff + cherry-pick + revert round-trip",
234 Effect.gen(function* () {
235 const { url } = yield* stack;
236 const repo = yield* freshRepo(url, "conf", "surgery");
237 const tmp = yield* tempDir;
238
239 yield* retrySh(
240 tmp,
241 `
242 rm -rf work
243 git -c init.defaultBranch=main clone '${repo.remote}' work
244 cd work
245 echo base > base.txt
246 git add -A && git commit -m base
247 git push origin main
248 `,
249 );
250
251 // build a feature branch, advance main underneath it, rebase, force-push
252 yield* mustSh(
253 tmp,
254 `
255 cd work
256 git checkout -b feature
257 echo f1 > f1.txt && git add -A && git commit -m feat-1
258 echo f2 > f2.txt && git add -A && git commit -m feat-2
259 git push origin feature
260
261 git checkout main
262 echo m1 > m1.txt && git add -A && git commit -m main-1
263 git push origin main
264
265 git checkout feature
266 git rebase main
267 git push --force origin feature
268 `,
269 );
270
271 // amend the feature tip and force-push again
272 yield* mustSh(
273 tmp,
274 `
275 cd work
276 git checkout feature
277 echo f2-amended > f2.txt
278 git add -A && git commit --amend -m feat-2-amended
279 git push --force origin feature
280 `,
281 );
282
283 // merge --no-ff (a real merge commit), then cherry-pick + revert on main
284 yield* mustSh(
285 tmp,
286 `
287 cd work
288 git checkout main
289 git merge --no-ff feature -m merge-feature
290 echo hot > hotfix.txt && git add -A && git commit -m hotfix
291 git revert --no-edit HEAD
292 git push origin main
293
294 git checkout -b picks main~2
295 git cherry-pick $(git rev-parse main~1)
296 git push origin picks
297 `,
298 );
299
300 // conformance oracle: a fresh clone is fsck-clean and histories agree
301 const check = yield* mustSh(
302 tmp,
303 `
304 git clone '${repo.remote}' verify
305 cd verify
306 git fsck --strict
307 git fetch origin '+refs/heads/*:refs/remotes/origin/*'
308 git rev-parse origin/main origin/feature origin/picks >/dev/null
309 git log --format=%s origin/main | tr '\\n' ' '
310 `,
311 );
312 expect(check.stdout).toContain("merge-feature");
313 expect(check.stdout).toContain("Revert");
314
315 // REST agrees with the wire: refs list carries all three branches
316 const refs = yield* repo.admin.refs.list({
317 params: { owner: "conf", repo: "surgery" },
318 query: { prefix: "refs/heads/" },
319 });
320 expect(refs.refs.map((ref) => ref.name).sort()).toEqual([
321 "refs/heads/feature",
322 "refs/heads/main",
323 "refs/heads/picks",
324 ]);
325 }).pipe(logLevel),
326 {
327 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
328 timeout: 180_000,
329 },
330 );
331
332 // ═══════════════════════════════════════════════════════════════════════════
333 // 3. Branch/tag churn: both tag kinds, deletes, prune, atomic multi-ref
334 // ═══════════════════════════════════════════════════════════════════════════
335
336 test(
337 "branch/tag churn: annotated + lightweight tags, deletes, fetch --prune, --atomic",
338 Effect.gen(function* () {
339 const { url } = yield* stack;
340 const repo = yield* freshRepo(url, "conf", "churn");
341 const tmp = yield* tempDir;
342
343 yield* retrySh(
344 tmp,
345 `
346 rm -rf a
347 git -c init.defaultBranch=main clone '${repo.remote}' a
348 cd a
349 echo one > f.txt && git add -A && git commit -m c1
350 git push origin main
351 `,
352 );
353
354 // one annotated + one lightweight tag; both round-trip with types intact
355 yield* mustSh(
356 tmp,
357 `
358 cd a
359 git tag -a v1.0.0 -m 'release v1'
360 git tag lightweight
361 git push origin v1.0.0 lightweight
362 `,
363 );
364 const tagTypes = yield* mustSh(
365 tmp,
366 `
367 git clone '${repo.remote}' b
368 cd b
369 git fetch --tags origin
370 printf '%s %s' $(git cat-file -t v1.0.0^{}) $(git cat-file -t $(git rev-parse v1.0.0))
371 `,
372 );
373 // peeled target is a commit; the annotated ref itself is a tag object
374 expect(tagTypes.stdout).toBe("commit tag");
375
376 // atomic multi-ref push: two branches land together
377 yield* mustSh(
378 tmp,
379 `
380 cd a
381 git branch side1 && git branch side2
382 git push --atomic origin side1 side2
383 `,
384 );
385
386 // remote deletes; a pruning fetch in clone b drops them
387 yield* mustSh(
388 tmp,
389 `
390 cd a
391 git push --delete origin side1 side2 lightweight v1.0.0
392 `,
393 );
394 const pruned = yield* mustSh(
395 tmp,
396 `
397 cd b
398 git fetch --prune origin
399 git fetch --prune --prune-tags origin '+refs/tags/*:refs/tags/*'
400 git for-each-ref refs/remotes/origin refs/tags | wc -l
401 `,
402 );
403 // only origin/main (HEAD alias may or may not be present; count real refs)
404 expect(Number(pruned.stdout.trim())).toBeLessThanOrEqual(2);
405
406 // REST agrees: only main remains
407 const refs = yield* repo.admin.refs.list({
408 params: { owner: "conf", repo: "churn" },
409 query: {},
410 });
411 expect(refs.refs.map((ref) => ref.name)).toEqual(["refs/heads/main"]);
412 }).pipe(logLevel),
413 {
414 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
415 timeout: 180_000,
416 },
417 );
418
419 // ═══════════════════════════════════════════════════════════════════════════
420 // 4. Client gc / repack interop + odd paths (unicode, exec bit, binary)
421 // ═══════════════════════════════════════════════════════════════════════════
422
423 test(
424 "client gc interop: aggressive repack then push; unicode/exec/binary survive byte-identically",
425 Effect.gen(function* () {
426 const { url } = yield* stack;
427 const repo = yield* freshRepo(url, "conf", "gc-interop");
428 const tmp = yield* tempDir;
429
430 // odd content: unicode filename, executable, raw bytes incl. NUL
431 yield* retrySh(
432 tmp,
433 `
434 rm -rf work
435 git -c init.defaultBranch=main clone '${repo.remote}' work
436 cd work
437 printf 'こんにちは' > 'naïve-файл.txt'
438 printf '#!/bin/sh\\necho ok\\n' > run.sh && chmod +x run.sh
439 head -c 4096 /dev/zero | tr '\\0' '\\377' > solid.bin
440 printf 'a\\0b\\0c' > nul.bin
441 git add -A && git commit -m odd-paths
442 git push origin main
443 `,
444 );
445
446 // grow some history, aggressively gc the client (max delta chains),
447 // then keep pushing — thin packs now delta against gc'd bases
448 yield* mustSh(
449 tmp,
450 `
451 cd work
452 i=1
453 while [ $i -le 30 ]; do
454 printf 'line %s\\n' $i >> grow.txt
455 git add grow.txt && git commit -q -m grow-$i
456 i=$((i+1))
457 done
458 git gc --aggressive --prune=now
459 git push origin main
460
461 echo after-gc >> grow.txt
462 git add grow.txt && git commit -q -m after-gc
463 git push origin main
464 `,
465 );
466
467 // oracle: fresh clone, strict fsck, and every odd file byte-identical
468 const verify = yield* mustSh(
469 tmp,
470 `
471 git clone '${repo.remote}' verify
472 cd verify
473 git fsck --strict
474 cmp 'naïve-файл.txt' '../work/naïve-файл.txt'
475 cmp run.sh ../work/run.sh
476 cmp solid.bin ../work/solid.bin
477 cmp nul.bin ../work/nul.bin
478 test -x run.sh
479 git log --oneline | wc -l
480 `,
481 );
482 expect(Number(verify.stdout.trim())).toBe(32);
483 }).pipe(logLevel),
484 {
485 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
486 timeout: 180_000,
487 },
488 );