The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | concatBytes, |
| 3 | decodeOfsDeltaOffset, |
| 4 | decodeTypeSize, |
| 5 | hashObject, |
| 6 | type ObjectType, |
| 7 | } from "@alchemy.run/git/Protocol/ObjectCodec"; |
| 8 | import { |
| 9 | bufferRandomAccess, |
| 10 | ingestPack, |
| 11 | readPackHeader, |
| 12 | type ResolvedEntry, |
| 13 | type ThinBaseSource, |
| 14 | } from "@alchemy.run/git/Protocol/PackParser"; |
| 15 | import { writePackBytes } from "@alchemy.run/git/Protocol/PackWriter"; |
| 16 | import type { ManifestEntry, ObjectSource } from "@alchemy.run/git/Protocol/Store"; |
| 17 | import { inflate, inflateEntry } from "@alchemy.run/git/Protocol/Zlib"; |
| 18 | import { makeStreamingSource } from "@alchemy.run/git/Store/StreamingSource"; |
| 19 | import { describe, expect, it } from "@alchemy.run/test"; |
| 20 | /** |
| 21 | * Tier-1 pack parse/write round-trip tests (DESIGN.md §9) against the |
| 22 | * checked-in fixture packs in `test/fixtures/packs/` (generated once by the |
| 23 | * real `git` CLI — see the README there; never regenerated at test time). |
| 24 | * |
| 25 | * Covers: header/count validation, no-delta ingestion with byte-verbatim |
| 26 | * zdata (re-hash proof), OFS_DELTA and REF_DELTA resolution, thin-pack |
| 27 | * resolution against a seeded object store (and the MissingDeltaBaseError |
| 28 | * without it), trailer-checksum rejection, PackWriter → PackParser |
| 29 | * round-trips, and `git index-pack --strict` as the oracle for writer output. |
| 30 | */ |
| 31 | import * as BunServices from "@effect/platform-bun/BunServices"; |
| 32 | import * as Effect from "effect/Effect"; |
| 33 | import * as Fiber from "effect/Fiber"; |
| 34 | import * as FileSystem from "effect/FileSystem"; |
| 35 | import * as Path from "effect/Path"; |
| 36 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 37 | import * as Result from "effect/Result"; |
| 38 | import * as Stream from "effect/Stream"; |
| 39 | |
| 40 | // ── manifest shape (test/fixtures/packs/manifest.json) ────────────────────── |
| 41 | |
| 42 | interface PackManifest { |
| 43 | readonly commits: { readonly c1: string; readonly c2: string; readonly c3: string }; |
| 44 | readonly tag: { readonly oid: string; readonly target: string; readonly name: string }; |
| 45 | readonly tree1: string; |
| 46 | readonly blobs: { readonly data1: string; readonly data2: string; readonly hello1: string }; |
| 47 | readonly packs: Record< |
| 48 | string, |
| 49 | { readonly count: number; readonly oids: ReadonlyArray<string>; readonly thin?: boolean } |
| 50 | >; |
| 51 | } |
| 52 | |
| 53 | const fixturesDir = Effect.gen(function* () { |
| 54 | const path = yield* Path.Path; |
| 55 | return path.join(import.meta.dirname, "fixtures", "packs"); |
| 56 | }); |
| 57 | |
| 58 | const readFixture = Effect.fn(function* (name: string) { |
| 59 | const fs = yield* FileSystem.FileSystem; |
| 60 | const path = yield* Path.Path; |
| 61 | const dir = yield* fixturesDir; |
| 62 | return yield* fs.readFile(path.join(dir, name)); |
| 63 | }); |
| 64 | |
| 65 | const readManifest = Effect.gen(function* () { |
| 66 | const fs = yield* FileSystem.FileSystem; |
| 67 | const path = yield* Path.Path; |
| 68 | const dir = yield* fixturesDir; |
| 69 | const text = yield* fs.readFileString(path.join(dir, "manifest.json")); |
| 70 | return yield* Effect.try(() => JSON.parse(text) as PackManifest); |
| 71 | }); |
| 72 | |
| 73 | // ── in-memory ObjectSource fed by the ingest sink ─────────────────────────── |
| 74 | |
| 75 | interface StoredObject { |
| 76 | readonly type: ObjectType; |
| 77 | readonly content: Uint8Array; |
| 78 | readonly zdata: Uint8Array; |
| 79 | } |
| 80 | |
| 81 | const makeMemoryStore = () => { |
| 82 | const objects = new Map<string, StoredObject>(); |
| 83 | const source: ObjectSource & ThinBaseSource = { |
| 84 | has: (oid) => Effect.sync(() => objects.has(oid)), |
| 85 | filterExisting: (oids) => Effect.sync(() => oids.filter((oid) => objects.has(oid))), |
| 86 | getMeta: (oid) => |
| 87 | Effect.sync(() => { |
| 88 | const stored = objects.get(oid); |
| 89 | return stored === undefined |
| 90 | ? undefined |
| 91 | : { |
| 92 | oid, |
| 93 | type: stored.type, |
| 94 | size: stored.content.length, |
| 95 | zsize: stored.zdata.length, |
| 96 | location: "row" as const, |
| 97 | }; |
| 98 | }), |
| 99 | readZData: (oid) => Stream.fromEffect(Effect.sync(() => objects.get(oid)!.zdata)), |
| 100 | readBase: (oid) => |
| 101 | Effect.sync(() => { |
| 102 | const stored = objects.get(oid); |
| 103 | return stored === undefined ? undefined : { type: stored.type, content: stored.content }; |
| 104 | }), |
| 105 | readContent: (oid) => Effect.sync(() => objects.get(oid)!.content), |
| 106 | }; |
| 107 | /** Ingest sink: stage the resolved entry (zdata is already a safe copy). */ |
| 108 | const sink = (entry: ResolvedEntry) => |
| 109 | Effect.gen(function* () { |
| 110 | const content = yield* inflate(entry.zdata); |
| 111 | objects.set(entry.oid, { type: entry.type, content, zdata: entry.zdata }); |
| 112 | }); |
| 113 | const manifestEntries = (): ReadonlyArray<ManifestEntry> => |
| 114 | Array.from(objects.entries(), ([oid, stored]) => ({ |
| 115 | oid, |
| 116 | type: stored.type, |
| 117 | size: stored.content.length, |
| 118 | zsize: stored.zdata.length, |
| 119 | location: "row" as const, |
| 120 | })); |
| 121 | return { objects, source, sink, manifestEntries }; |
| 122 | }; |
| 123 | |
| 124 | const ingestInto = Effect.fn(function* ( |
| 125 | pack: Uint8Array, |
| 126 | store: ReturnType<typeof makeMemoryStore>, |
| 127 | ) { |
| 128 | return yield* ingestPack({ |
| 129 | source: bufferRandomAccess(pack), |
| 130 | store: store.source, |
| 131 | sink: store.sink, |
| 132 | }); |
| 133 | }); |
| 134 | |
| 135 | /** Scan the raw entry types of a pack without resolving anything. */ |
| 136 | const scanEntryTypes = Effect.fn(function* (pack: Uint8Array) { |
| 137 | const view = new DataView(pack.buffer, pack.byteOffset, pack.byteLength); |
| 138 | const count = view.getUint32(8); |
| 139 | const types: Array<number> = []; |
| 140 | let offset = 12; |
| 141 | for (let i = 0; i < count; i++) { |
| 142 | const header = decodeTypeSize(pack, offset); |
| 143 | offset = header.next; |
| 144 | if (header.type === 6) offset = decodeOfsDeltaOffset(pack, offset).next; |
| 145 | if (header.type === 7) offset += 20; |
| 146 | const entry = yield* inflateEntry(pack, offset); |
| 147 | offset += entry.bytesConsumed; |
| 148 | types.push(header.type); |
| 149 | } |
| 150 | return types; |
| 151 | }); |
| 152 | |
| 153 | const sortedOids = (oids: ReadonlyArray<string>) => [...oids].sort(); |
| 154 | |
| 155 | const platform = <A, E, R>( |
| 156 | effect: Effect.Effect<A, E, R>, |
| 157 | ): Effect.Effect<A, E, Exclude<R, BunServices.BunServices>> => |
| 158 | Effect.provide(effect, BunServices.layer) as Effect.Effect< |
| 159 | A, |
| 160 | E, |
| 161 | Exclude<R, BunServices.BunServices> |
| 162 | >; |
| 163 | |
| 164 | describe("fixture pack parsing", { tags: ["unit", "local"] }, () => { |
| 165 | it.live("empty pack: count 0, valid trailer, zero objects", () => |
| 166 | Effect.gen(function* () { |
| 167 | const pack = yield* readFixture("empty.pack"); |
| 168 | const header = yield* readPackHeader(bufferRandomAccess(pack)); |
| 169 | expect(header.version).toBe(2); |
| 170 | expect(header.count).toBe(0); |
| 171 | const store = makeMemoryStore(); |
| 172 | const summary = yield* ingestInto(pack, store); |
| 173 | expect(summary.count).toBe(0); |
| 174 | expect(summary.oids).toEqual([]); |
| 175 | }).pipe(platform), |
| 176 | ); |
| 177 | |
| 178 | it.live( |
| 179 | "simple pack (window=0): no deltas, byte-verbatim zdata re-hashes to the manifest oids", |
| 180 | () => |
| 181 | Effect.gen(function* () { |
| 182 | const manifest = yield* readManifest; |
| 183 | const pack = yield* readFixture("simple.pack"); |
| 184 | const types = yield* scanEntryTypes(pack); |
| 185 | expect(types).not.toContain(6); |
| 186 | expect(types).not.toContain(7); |
| 187 | |
| 188 | const store = makeMemoryStore(); |
| 189 | const summary = yield* ingestInto(pack, store); |
| 190 | expect(sortedOids(summary.oids)).toEqual(sortedOids(manifest.packs["simple"]!.oids)); |
| 191 | // the ingest stores the pack's compressed span verbatim; re-hashing the |
| 192 | // inflated content must reproduce every oid |
| 193 | for (const [oid, stored] of store.objects) { |
| 194 | expect(yield* hashObject(stored.type, stored.content)).toBe(oid); |
| 195 | } |
| 196 | // the notable objects landed with the right kinds |
| 197 | expect(store.objects.get(manifest.commits.c1)?.type).toBe(1); |
| 198 | expect(store.objects.get(manifest.tree1)?.type).toBe(2); |
| 199 | expect(store.objects.get(manifest.blobs.data1)?.type).toBe(3); |
| 200 | expect(store.objects.get(manifest.tag.oid)?.type).toBe(4); |
| 201 | }).pipe(platform), |
| 202 | ); |
| 203 | |
| 204 | it.live( |
| 205 | "streaming: delta bases evicted before the body ends are deferred and resolved through the fallback (DESIGN §22.6)", |
| 206 | () => |
| 207 | Effect.gen(function* () { |
| 208 | const manifest = yield* readManifest; |
| 209 | const pack = yield* readFixture("ofs-delta.pack"); |
| 210 | // Retain almost nothing, so every base is evicted before its |
| 211 | // delta arrives; a parser that blocked on evicted bytes would |
| 212 | // deadlock against backpressure here. |
| 213 | const feeder = makeStreamingSource({ |
| 214 | slabBytes: 256, |
| 215 | retainBytes: 512, |
| 216 | backpressureBytes: 1024, |
| 217 | }); |
| 218 | const store = makeMemoryStore(); |
| 219 | const parse = yield* Effect.forkChild( |
| 220 | ingestPack({ source: feeder.source, store: store.source, sink: store.sink }), |
| 221 | ); |
| 222 | for (let at = 0; at < pack.length; at += 300) { |
| 223 | yield* feeder.push(pack.subarray(at, Math.min(at + 300, pack.length))); |
| 224 | } |
| 225 | feeder.setFallback(bufferRandomAccess(pack)); |
| 226 | feeder.end(); |
| 227 | const summary = yield* Fiber.join(parse); |
| 228 | expect(sortedOids(summary.oids)).toEqual(sortedOids(manifest.packs["ofs-delta"]!.oids)); |
| 229 | }).pipe(platform), |
| 230 | ); |
| 231 | |
| 232 | it.live("ofs-delta pack: OFS_DELTA entries resolve to the same object set", () => |
| 233 | Effect.gen(function* () { |
| 234 | const manifest = yield* readManifest; |
| 235 | const pack = yield* readFixture("ofs-delta.pack"); |
| 236 | const types = yield* scanEntryTypes(pack); |
| 237 | expect(types).toContain(6); |
| 238 | |
| 239 | const store = makeMemoryStore(); |
| 240 | const resolvedFromDelta: Array<string> = []; |
| 241 | const summary = yield* ingestPack({ |
| 242 | source: bufferRandomAccess(pack), |
| 243 | store: store.source, |
| 244 | sink: (entry) => |
| 245 | Effect.gen(function* () { |
| 246 | if (entry.fromDelta) resolvedFromDelta.push(entry.oid); |
| 247 | yield* store.sink(entry); |
| 248 | }), |
| 249 | }); |
| 250 | expect(sortedOids(summary.oids)).toEqual(sortedOids(manifest.packs["ofs-delta"]!.oids)); |
| 251 | expect(resolvedFromDelta.length).toBeGreaterThan(0); |
| 252 | for (const [oid, stored] of store.objects) { |
| 253 | expect(yield* hashObject(stored.type, stored.content)).toBe(oid); |
| 254 | } |
| 255 | }).pipe(platform), |
| 256 | ); |
| 257 | |
| 258 | it.live("ref-delta pack: REF_DELTA entries resolve to the same object set", () => |
| 259 | Effect.gen(function* () { |
| 260 | const manifest = yield* readManifest; |
| 261 | const pack = yield* readFixture("ref-delta.pack"); |
| 262 | const types = yield* scanEntryTypes(pack); |
| 263 | expect(types).toContain(7); |
| 264 | |
| 265 | const store = makeMemoryStore(); |
| 266 | const summary = yield* ingestInto(pack, store); |
| 267 | expect(sortedOids(summary.oids)).toEqual(sortedOids(manifest.packs["ref-delta"]!.oids)); |
| 268 | }).pipe(platform), |
| 269 | ); |
| 270 | |
| 271 | it.live("thin pack: resolves REF_DELTA bases from the seeded store", () => |
| 272 | Effect.gen(function* () { |
| 273 | const manifest = yield* readManifest; |
| 274 | const store = makeMemoryStore(); |
| 275 | // seed with c1's closure (base.pack), then ingest the thin pack |
| 276 | yield* ingestInto(yield* readFixture("base.pack"), store); |
| 277 | const thin = yield* readFixture("thin.pack"); |
| 278 | const types = yield* scanEntryTypes(thin); |
| 279 | expect(types).toContain(7); |
| 280 | const summary = yield* ingestInto(thin, store); |
| 281 | expect(sortedOids(summary.oids)).toEqual(sortedOids(manifest.packs["thin"]!.oids)); |
| 282 | // the extended blob resolved against the excluded base blob |
| 283 | expect(summary.oids).toContain(manifest.blobs.data2); |
| 284 | const data2 = store.objects.get(manifest.blobs.data2)!; |
| 285 | expect(yield* hashObject(data2.type, data2.content)).toBe(manifest.blobs.data2); |
| 286 | }).pipe(platform), |
| 287 | ); |
| 288 | |
| 289 | it.live("thin pack without its bases fails with MissingDeltaBaseError", () => |
| 290 | Effect.gen(function* () { |
| 291 | const thin = yield* readFixture("thin.pack"); |
| 292 | const empty = makeMemoryStore(); |
| 293 | const result = yield* Effect.result(ingestInto(thin, empty)); |
| 294 | expect(Result.isFailure(result)).toBe(true); |
| 295 | if (Result.isFailure(result)) { |
| 296 | expect(result.failure._tag).toBe("MissingDeltaBaseError"); |
| 297 | } |
| 298 | }).pipe(platform), |
| 299 | ); |
| 300 | |
| 301 | it.live("corrupted trailer is rejected with PackChecksumMismatch", () => |
| 302 | Effect.gen(function* () { |
| 303 | const pack = Uint8Array.from(yield* readFixture("simple.pack")); |
| 304 | pack[pack.length - 1]! ^= 0xff; |
| 305 | const result = yield* Effect.result(ingestInto(pack, makeMemoryStore())); |
| 306 | expect(Result.isFailure(result)).toBe(true); |
| 307 | if (Result.isFailure(result)) { |
| 308 | expect(result.failure._tag).toBe("PackChecksumMismatch"); |
| 309 | } |
| 310 | }).pipe(platform), |
| 311 | ); |
| 312 | }); |
| 313 | |
| 314 | describe("PackWriter round-trips", { tags: ["local"] }, () => { |
| 315 | it.live( |
| 316 | "rewrite of an ingested delta pack re-parses to an identical object set", |
| 317 | () => |
| 318 | Effect.gen(function* () { |
| 319 | const manifest = yield* readManifest; |
| 320 | const store = makeMemoryStore(); |
| 321 | yield* ingestInto(yield* readFixture("ofs-delta.pack"), store); |
| 322 | |
| 323 | const rewritten = concatBytes( |
| 324 | yield* Stream.runCollect(writePackBytes(store.manifestEntries(), store.source)), |
| 325 | ); |
| 326 | const header = yield* readPackHeader(bufferRandomAccess(rewritten)); |
| 327 | expect(header.version).toBe(2); |
| 328 | expect(header.count).toBe(manifest.packs["ofs-delta"]!.count); |
| 329 | |
| 330 | const reparsed = makeMemoryStore(); |
| 331 | const summary = yield* ingestInto(rewritten, reparsed); |
| 332 | expect(sortedOids(summary.oids)).toEqual(sortedOids(manifest.packs["ofs-delta"]!.oids)); |
| 333 | for (const [oid, stored] of reparsed.objects) { |
| 334 | expect(yield* hashObject(stored.type, stored.content)).toBe(oid); |
| 335 | } |
| 336 | }).pipe(platform), |
| 337 | { tags: ["unit"] }, |
| 338 | ); |
| 339 | |
| 340 | it.live( |
| 341 | "writer output is accepted by git index-pack --strict", |
| 342 | () => |
| 343 | Effect.gen(function* () { |
| 344 | const fs = yield* FileSystem.FileSystem; |
| 345 | const path = yield* Path.Path; |
| 346 | const store = makeMemoryStore(); |
| 347 | yield* ingestInto(yield* readFixture("simple.pack"), store); |
| 348 | const rewritten = concatBytes( |
| 349 | yield* Stream.runCollect(writePackBytes(store.manifestEntries(), store.source)), |
| 350 | ); |
| 351 | const dir = yield* fs.makeTempDirectory({ prefix: "git-service-pack-" }); |
| 352 | const packPath = path.join(dir, "writer-output.pack"); |
| 353 | yield* fs.writeFile(packPath, rewritten); |
| 354 | |
| 355 | const handle = yield* ChildProcess.make("git", ["index-pack", "--strict", packPath], { |
| 356 | cwd: dir, |
| 357 | extendEnv: true, |
| 358 | }); |
| 359 | const [exitCode, stderr] = yield* Effect.all( |
| 360 | [handle.exitCode, Stream.mkString(Stream.decodeText(handle.stderr))], |
| 361 | { concurrency: 2 }, |
| 362 | ); |
| 363 | expect(`${exitCode}:${stderr}`).toBe(`0:${stderr}`); |
| 364 | expect(exitCode).toBe(0); |
| 365 | }).pipe(platform, Effect.timeout("30 seconds")), |
| 366 | { timeout: 60_000 }, |
| 367 | ); |
| 368 | }); |