1import { decodeStagedBatch, encodeStagedBatch } from "@/Git/PushWire.ts";
2import { ObjectType } from "@/Git/Protocol/ObjectCodec.ts";
3import { describe, expect, test } from "alchemy-test";
4
5describe("push batch codec", { tags: ["unit", "local"] }, () => {
6 test("inline and promoted rows round-trip", () => {
7 const rows = [
8 {
9 oid: "a".repeat(40),
10 type: ObjectType.blob,
11 size: 10,
12 zdata: new Uint8Array([1, 2, 3]),
13 zsize: 3,
14 },
15 {
16 oid: "b".repeat(40),
17 type: ObjectType.blob,
18 size: 4096,
19 zdata: new Uint8Array(0),
20 zsize: 900,
21 pack: { packId: "wire-01H", offset: 12345 },
22 },
23 {
24 oid: "c".repeat(40),
25 type: ObjectType.tree,
26 size: 60,
27 zdata: new Uint8Array([9, 9, 9, 9]),
28 zsize: undefined,
29 },
30 ];
31 const decoded = decodeStagedBatch(encodeStagedBatch(rows));
32 expect(decoded.length).toBe(3);
33 expect(decoded.map((r) => r.oid)).toEqual(rows.map((r) => r.oid));
34 expect(Array.from(decoded[0]!.zdata)).toEqual([1, 2, 3]);
35 expect(decoded[1]!.zdata.byteLength).toBe(0);
36 expect(decoded[1]!.pack).toEqual({ packId: "wire-01H", offset: 12345 });
37 expect(decoded[1]!.zsize).toBe(900);
38 expect(Array.from(decoded[2]!.zdata)).toEqual([9, 9, 9, 9]);
39 expect(decoded[2]!.type).toBe(ObjectType.tree);
40 });
41 test("an empty batch encodes to a header only", () => {
42 expect(decodeStagedBatch(encodeStagedBatch([])).length).toBe(0);
43 });
44});