Skip to content
File

Blob: test/parse-helpers.worker.test.ts

typescript83 lines
1import { it, expect, describe } from "vitest";
2import { parseCommitRefs, parseTreeChildOids, inflateAndParseHeader } from "@/worker/git/core";
3import { deflate } from "@/worker/common";
4 
5describe("shared parse helpers", () => {
6 it("parseCommitRefs extracts tree and parents correctly", () => {
7 const commitContent = new TextEncoder().encode(
8 `tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
9parent 1234567890abcdef1234567890abcdef12345678
10parent abcdef1234567890abcdef1234567890abcdef12
11author Test User <test@example.com> 1234567890 +0000
12committer Test User <test@example.com> 1234567890 +0000
13
14Test commit message`
15 );
16 
17 const refs = parseCommitRefs(commitContent);
18 expect(refs.tree).toBe("4b825dc642cb6eb9a060e54bf8d69288fbee4904");
19 expect(refs.parents).toEqual([
20 "1234567890abcdef1234567890abcdef12345678",
21 "abcdef1234567890abcdef1234567890abcdef12",
22 ]);
23 });
24 
25 it("parseTreeChildOids extracts child OIDs from tree content", () => {
26 // Tree entry format: mode<space>name<null>20-byte-sha
27 const entries: Uint8Array[] = [];
28 
29 // Add a file entry
30 entries.push(new TextEncoder().encode("100644 file.txt"));
31 entries.push(new Uint8Array([0])); // null terminator
32 entries.push(
33 new Uint8Array([
34 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd,
35 0xef, 0x12, 0x34, 0x56, 0x78,
36 ])
37 );
38 
39 // Add a directory entry
40 entries.push(new TextEncoder().encode("40000 dir"));
41 entries.push(new Uint8Array([0])); // null terminator
42 entries.push(
43 new Uint8Array([
44 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78,
45 0x90, 0xab, 0xcd, 0xef, 0x12,
46 ])
47 );
48 
49 // Concatenate all entries
50 let totalLength = 0;
51 for (const entry of entries) {
52 totalLength += entry.length;
53 }
54 const treeContent = new Uint8Array(totalLength);
55 let offset = 0;
56 for (const entry of entries) {
57 treeContent.set(entry, offset);
58 offset += entry.length;
59 }
60 
61 const childOids = parseTreeChildOids(treeContent);
62 expect(childOids).toEqual([
63 "1234567890abcdef1234567890abcdef12345678",
64 "abcdef1234567890abcdef1234567890abcdef12",
65 ]);
66 });
67 
68 it("inflateAndParseHeader correctly parses compressed git objects", async () => {
69 const payload = new TextEncoder().encode("Hello, World!");
70 const header = new TextEncoder().encode(`blob ${payload.length}\0`);
71 const raw = new Uint8Array(header.length + payload.length);
72 raw.set(header, 0);
73 raw.set(payload, header.length);
74 
75 const compressed = await deflate(raw);
76 
77 const result = await inflateAndParseHeader(compressed);
78 expect(result).toBeDefined();
79 expect(result?.type).toBe("blob");
80 expect(new TextDecoder().decode(result?.payload)).toBe("Hello, World!");
81 });
82});