import { afterAll, beforeAll, describe, expect, test } from "bun:test"; import { mkdirSync, readdirSync, rmSync } from "node:fs"; import path from "node:path"; import config from "../src/config.ts"; const TMP = path.resolve(`./data-test-archive-${process.pid}`); config.DATA_DIR = TMP; const { archiveRepo } = await import("../src/services/git.ts"); const REPO = "archivetest"; beforeAll(() => { rmSync(TMP, { recursive: true, force: true }); mkdirSync(path.join(TMP, "repos"), { recursive: true }); const work = path.join(TMP, "work"); mkdirSync(work, { recursive: true }); const run = (...args: string[]) => Bun.spawnSync(args, { cwd: work, stdio: ["ignore", "ignore", "ignore"] }); run("git", "init", "-q", "-b", "main"); run("git", "config", "user.email", "t@example.com"); run("git", "config", "user.name", "Test"); Bun.write(path.join(work, "hello.txt"), "hello archive\n"); run("git", "add", "-A"); run("git", "commit", "-q", "-m", "init"); Bun.spawnSync( ["git", "clone", "-q", "--bare", work, path.join(TMP, "repos", `${REPO}.git`)], { stdio: ["ignore", "ignore", "ignore"] }, ); }); afterAll(() => { rmSync(TMP, { recursive: true, force: true }); }); describe("archiveRepo", () => { // The .tar.zst used to be produced by piping git into a `zstd` binary. // It is now compressed in-process, so the container ships without zstd. test("writes all three source archive formats", async () => { const out = path.join(TMP, "out"); mkdirSync(out, { recursive: true }); await archiveRepo(REPO, "main", "slug", out); expect(readdirSync(out).sort()).toEqual([ "slug-main.tar.gz", "slug-main.tar.zst", "slug-main.zip", ]); }); test("the .tar.zst is real zstd and round-trips to the source tar", async () => { const out = path.join(TMP, "out2"); mkdirSync(out, { recursive: true }); await archiveRepo(REPO, "main", "slug", out); const compressed = await Bun.file( path.join(out, "slug-main.tar.zst"), ).bytes(); // Zstandard magic number. expect(Array.from(compressed.slice(0, 4))).toEqual([0x28, 0xb5, 0x2f, 0xfd]); const tar = Bun.zstdDecompressSync(compressed); // tar stores filenames as plain text in each 512-byte header block. expect(new TextDecoder().decode(tar)).toContain("hello.txt"); expect(tar.length % 512).toBe(0); }); test("a bad ref fails before writing anything", async () => { const out = path.join(TMP, "out3"); mkdirSync(out, { recursive: true }); // The zip stage throws first, so this never reaches the zst path — // that one is covered separately below. await expect( archiveRepo(REPO, "no-such-ref", "slug", out), ).rejects.toThrow(); expect(readdirSync(out)).not.toContain("slug-no-such-ref.tar.zst"); }); test("an abort during the zst stage leaves no partial file", async () => { const out = path.join(TMP, "out4"); mkdirSync(out, { recursive: true }); const ac = new AbortController(); // Abort once the two earlier formats exist, i.e. while the zst stage // is the one in flight. This is the path the inner finally guards: // the sink must be closed before the catch unlinks the file. const done = archiveRepo(REPO, "main", "slug", out, ac.signal); await Bun.sleep(1); ac.abort(); await done.catch(() => {}); expect(readdirSync(out)).not.toContain("slug-main.tar.zst"); }); });