@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 311 lines 12.6 KB
Raw
1 /**
2 * Real-world load: push **this repository** to a deployed git-service.
3 *
4 * Every other suite pushes toy histories. This one takes the actual alchemy
5 * monorepo — ~13.6k objects, ~36 MiB, 12k+ files in one tree — and drives
6 * the whole pipeline with it: thin-pack ingest at scale, the object store,
7 * compaction into R2 packs, clone bundles, and byte-for-byte clone
8 * fidelity verified by `git fsck --strict` plus a tree comparison.
9 *
10 * The source is the repo the tests are running inside (`git rev-parse
11 * --show-toplevel`), so it works on any checkout, and the suite skips
12 * itself when that is not a git repo.
13 *
14 * Shape of the fixture: a **depth-1 clone re-committed as a single root
15 * commit**. That keeps the object count and byte volume of the real repo
16 * (which is the point) while staying a fully connected history — pushing
17 * from a shallow clone would send commits whose parents are missing, which
18 * receive-pack correctly refuses.
19 *
20 * Slow by construction; skipped under `--fast`.
21 */
22 import * as Cloudflare from "@alchemy.run/cloudflare";
23 import { GitApi } from "@alchemy.run/git/Api";
24 import { expect } from "@alchemy.run/test";
25 import * as Test from "@alchemy.run/test/Alchemy";
26 import * as Data from "effect/Data";
27 import * as Effect from "effect/Effect";
28 import * as FileSystem from "effect/FileSystem";
29 import * as HttpApiClient from "effect/http-api/HttpApiClient";
30 import * as HttpClient from "effect/http/HttpClient";
31 import * as HttpClientRequest from "effect/http/HttpClientRequest";
32 import * as ChildProcess from "effect/process/ChildProcess";
33 import { MinimumLogLevel } from "effect/References";
34 import * as Schedule from "effect/Schedule";
35 import * as Stream from "effect/Stream";
36 import { makeTestStack, TEST_SECRET } from "./fixtures/stack.ts";
37
38 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
39 providers: Cloudflare.providers(),
40 });
41
42 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
43
44 const Stack = makeTestStack("GitRealWorldStack");
45
46 const skipHuge = !!process.env.FAST;
47
48 // ── plumbing ────────────────────────────────────────────────────────────────
49
50 const makeClient = (url: string, token: string) =>
51 HttpApiClient.make(GitApi, {
52 baseUrl: url,
53 transformClient: HttpClient.mapRequest((request) =>
54 request.pipe(HttpClientRequest.bearerToken(token)),
55 ),
56 });
57
58 class ShellError extends Data.TaggedError("ShellError")<{
59 readonly script: string;
60 readonly exitCode: number;
61 readonly stdout: string;
62 readonly stderr: string;
63 }> {
64 override get message(): string {
65 return `shell exited ${this.exitCode}\n--- script ---\n${this.script}\n--- stderr ---\n${this.stderr}`;
66 }
67 }
68
69 /** Bounded `sh -c` with hermetic git identity; 10 min covers a 36 MiB push. */
70 const sh = Effect.fn(function* (cwd: string, script: string) {
71 const handle = yield* ChildProcess.make("sh", ["-ec", script], {
72 cwd,
73 env: {
74 GIT_TERMINAL_PROMPT: "0",
75 GIT_CONFIG_NOSYSTEM: "1",
76 GIT_CONFIG_GLOBAL: "/dev/null",
77 GIT_AUTHOR_NAME: "Real World Bot",
78 GIT_AUTHOR_EMAIL: "realworld@example.com",
79 GIT_COMMITTER_NAME: "Real World Bot",
80 GIT_COMMITTER_EMAIL: "realworld@example.com",
81 GIT_AUTHOR_DATE: "2026-01-01T00:00:00Z",
82 GIT_COMMITTER_DATE: "2026-01-01T00:00:00Z",
83 },
84 extendEnv: true,
85 });
86 const [exitCode, stdout, stderr] = yield* Effect.all(
87 [
88 handle.exitCode,
89 Stream.mkString(Stream.decodeText(handle.stdout)),
90 Stream.mkString(Stream.decodeText(handle.stderr)),
91 ],
92 { concurrency: 3 },
93 );
94 return { script, exitCode, stdout: stdout.trim(), stderr };
95 }, Effect.timeout("600 seconds"));
96
97 const mustSh = Effect.fn(function* (cwd: string, script: string) {
98 const result = yield* sh(cwd, script);
99 if (result.exitCode !== 0) return yield* new ShellError(result);
100 return result;
101 });
102
103 const tempDir = Effect.gen(function* () {
104 const fs = yield* FileSystem.FileSystem;
105 return yield* fs.makeTempDirectory({ prefix: "git-realworld-" });
106 });
107
108 const edgeRetry = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
109 effect.pipe(
110 Effect.timeout("15 seconds"),
111 Effect.retry({ schedule: Schedule.spaced("1500 millis"), times: 40 }),
112 );
113
114 const freshRepo = Effect.fn(function* (url: string, owner: string, name: string) {
115 const admin = yield* makeClient(url, TEST_SECRET);
116 const created = yield* Effect.gen(function* () {
117 yield* admin.repos.delete({ params: { owner, repo: name } }).pipe(
118 Effect.catchTag("RepoNotFound", () => Effect.void),
119 edgeRetry,
120 );
121 yield* admin.repos.get({ params: { owner, repo: name } }).pipe(
122 edgeRetry,
123 Effect.as(false),
124 Effect.catchTag("RepoNotFound", () => Effect.succeed(true)),
125 Effect.repeat({
126 schedule: Schedule.spaced("1 second"),
127 until: (gone) => gone,
128 times: 60,
129 }),
130 );
131 return yield* admin.repos.create({ payload: { owner, name } }).pipe(edgeRetry);
132 }).pipe(
133 Effect.retry({
134 while: (error: { readonly _tag?: string }) => error._tag === "RepoAlreadyExists",
135 schedule: Schedule.spaced("1 second"),
136 times: 3,
137 }),
138 );
139 const parsed = new URL(url);
140 return {
141 admin,
142 token: TEST_SECRET,
143 remote: `${parsed.protocol}//x:${TEST_SECRET}@${parsed.host}/${owner}/${name}.git`,
144 };
145 });
146
147 const stack = beforeAll(
148 deploy(Stack).pipe(
149 Effect.tap(({ url }) =>
150 Effect.gen(function* () {
151 yield* Effect.logInfo(`git-service deployed at ${url}`);
152 const admin = yield* makeClient(url, TEST_SECRET);
153 yield* admin.repos.list({ query: {} }).pipe(
154 edgeRetry,
155 Effect.retry({
156 schedule: Schedule.spaced("1500 millis"),
157 times: 40,
158 }),
159 );
160 }),
161 ),
162 logLevel,
163 ),
164 );
165 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
166
167 // ═══════════════════════════════════════════════════════════════════════════
168
169 test.skipIf(skipHuge)(
170 "real world: push this entire repository, clone it back, compact it",
171 Effect.gen(function* () {
172 const { url } = yield* stack;
173 const tmp = yield* tempDir;
174
175 // The repo under test is the one these tests live in.
176 const root = (yield* mustSh(process.cwd(), `git rev-parse --show-toplevel`)).stdout;
177 expect(root.length).toBeGreaterThan(0);
178
179 // Depth-1 clone, re-committed as a single root commit: keeps the real
180 // object count and byte volume, but fully connected (a shallow push
181 // would reference parents the server does not have).
182 yield* mustSh(
183 tmp,
184 `
185 rm -rf src
186 git clone -q --depth 1 "file://${root}" src
187 cd src
188 rm -rf .git
189 git init -q -b main
190 git add -A
191 git commit -q -m "alchemy snapshot"
192 `,
193 );
194
195 const stats = (yield* mustSh(tmp, `cd src && git count-objects -v | tr '\\n' ' '`)).stdout;
196 const files = Number.parseInt(
197 (yield* mustSh(tmp, `cd src && git ls-files | wc -l`)).stdout,
198 10,
199 );
200 const head = (yield* mustSh(tmp, `cd src && git rev-parse HEAD`)).stdout;
201 const tree = (yield* mustSh(tmp, `cd src && git rev-parse HEAD^{tree}`)).stdout;
202 yield* Effect.logInfo(`[real-world] source: ${files} files, ${stats.trim()}`);
203 expect(files).toBeGreaterThan(5_000);
204
205 const repo = yield* freshRepo(url, "real", "alchemy");
206
207 // ── the push ─────────────────────────────────────────────────────────
208 const pushStarted = yield* Effect.sync(() => performance.now());
209 yield* mustSh(tmp, `cd src && git remote add origin '${repo.remote}' && git push origin main`);
210 const pushMs = yield* Effect.sync(() => performance.now() - pushStarted);
211 yield* Effect.logInfo(`[real-world] pushed ${files} files in ${(pushMs / 1000).toFixed(1)}s`);
212
213 // The server's view must match the client's exactly.
214 const remoteRef = yield* repo.admin.refs.get({
215 params: { owner: "real", repo: "alchemy" },
216 query: { name: "refs/heads/main" },
217 });
218 expect(remoteRef.oid).toBe(head);
219
220 const meta = yield* repo.admin.repos.get({
221 params: { owner: "real", repo: "alchemy" },
222 });
223 yield* Effect.logInfo(
224 `[real-world] stored ${meta.objects.loose + meta.objects.packed + meta.objects.r2} objects, ` +
225 `${(meta.objects.bytes / 1024 / 1024).toFixed(1)} MiB`,
226 );
227 // Server-side timing (DESIGN.md §19 phase 0): the wall clock above also
228 // covers client packing and the upload, so this is what is actually ours.
229 if (meta.lastPush !== null) {
230 const p = meta.lastPush;
231 yield* Effect.logInfo(
232 `[real-world] SERVER ingest ${p.ingestMs}ms (sql ${p.stageMs}ms / cpu ${p.ingestMs - p.stageMs}ms) + connectivity ${p.connectivityMs}ms + ` +
233 `finalize ${p.finalizeMs}ms = ${p.totalMs}ms for ${p.objects} objects ` +
234 `[phases: ${Object.entries(p.phases ?? {})
235 .sort((a, b) => b[1] - a[1])
236 .map(([k, v]) => `${k} ${v}ms`)
237 .join(", ")}] ` +
238 `(${(p.totalMs / Math.max(p.objects, 1)).toFixed(2)}ms/object); ` +
239 `client+network = ${(pushMs - p.totalMs).toFixed(0)}ms of the ${(pushMs / 1000).toFixed(1)}s wall clock`,
240 );
241 expect(p.objects).toBeGreaterThan(5_000);
242 }
243 expect(meta.objects.loose + meta.objects.packed + meta.objects.r2).toBeGreaterThan(5_000);
244
245 // ── clone it back and prove it is identical ──────────────────────────
246 const cloneStarted = yield* Effect.sync(() => performance.now());
247 yield* mustSh(tmp, `rm -rf back && git clone -q '${repo.remote}' back`);
248 const cloneMs = yield* Effect.sync(() => performance.now() - cloneStarted);
249 yield* Effect.logInfo(`[real-world] cloned back in ${(cloneMs / 1000).toFixed(1)}s`);
250
251 yield* mustSh(tmp, `cd back && git fsck --strict`);
252 expect((yield* mustSh(tmp, `cd back && git rev-parse HEAD`)).stdout).toBe(head);
253 // The tree oid matching means every path, mode and blob matches.
254 expect((yield* mustSh(tmp, `cd back && git rev-parse HEAD^{tree}`)).stdout).toBe(tree);
255 expect(
256 Number.parseInt((yield* mustSh(tmp, `cd back && git ls-files | wc -l`)).stdout, 10),
257 ).toBe(files);
258 // NOTE: no `diff -r` here. The tree oid equality above is the stronger
259 // and *correct* assertion — identical tree oids mean every path, mode
260 // and blob content matches. A directory diff also compares things git
261 // deliberately does not track (empty directories, e.g. the checked-out
262 // submodule mount points), which differ legitimately between a working
263 // tree and a fresh clone of it.
264
265 // ── compact the real repo into R2 packs, then clone again ────────────
266 yield* repo.admin.repos.compact({
267 params: { owner: "real", repo: "alchemy" },
268 });
269 const packed = yield* repo.admin.repos.get({ params: { owner: "real", repo: "alchemy" } }).pipe(
270 Effect.map((found) => found.objects),
271 Effect.repeat({
272 schedule: Schedule.spaced("2 seconds"),
273 until: (objects: { loose: number; packed: number }) =>
274 objects.packed > 0 && objects.loose === 0,
275 times: 150,
276 }),
277 );
278 yield* Effect.logInfo(`[real-world] compacted: ${packed.packed} objects in R2 packs`);
279 expect(packed.packed).toBeGreaterThan(5_000);
280
281 const packedCloneStarted = yield* Effect.sync(() => performance.now());
282 yield* mustSh(tmp, `rm -rf packed && git clone -q '${repo.remote}' packed`);
283 yield* Effect.logInfo(
284 `[real-world] cloned from R2 packs in ${(
285 (yield* Effect.sync(() => performance.now())) - packedCloneStarted
286 ).toFixed(0)}ms`,
287 );
288 yield* mustSh(tmp, `cd packed && git fsck --strict`);
289 expect((yield* mustSh(tmp, `cd packed && git rev-parse HEAD^{tree}`)).stdout).toBe(tree);
290
291 // ── an incremental push on top of the real repo still works ──────────
292 yield* mustSh(
293 tmp,
294 `
295 cd src
296 echo "real world" > REALWORLD.md
297 git add REALWORLD.md
298 git commit -q -m "incremental on top of a real repo"
299 git push origin main
300 `,
301 );
302 yield* mustSh(tmp, `cd back && git pull -q origin main`);
303 expect((yield* mustSh(tmp, `cd back && git rev-parse HEAD`)).stdout).toBe(
304 (yield* mustSh(tmp, `cd src && git rev-parse HEAD`)).stdout,
305 );
306 }).pipe(logLevel),
307 {
308 tags: ["provider:cloudflare", "provider:cloudflare:r2", "provider:cloudflare:worker", "live"],
309 timeout: 900_000,
310 },
311 );