import { describe, expect, it, vi } from "vitest"; import { parseBuildCommit, readBuildCommit } from "../build-commit.js"; describe("parseBuildCommit", () => { it("normalizes a full deployment commit", () => { expect(parseBuildCommit(" ABCDEF0123456789ABCDEF0123456789ABCDEF01\n")).toBe( "abcdef0123456789abcdef0123456789abcdef01", ); }); it("rejects truncated and malformed commits", () => { expect(parseBuildCommit("abcdef0")).toBeNull(); expect(parseBuildCommit("not-a-commit")).toBeNull(); }); }); describe("readBuildCommit", () => { it("reads the built server stamp in npm packages without a deployment marker", () => { const commit = "0123456789abcdef0123456789abcdef01234567"; expect(readBuildCommit({ environmentCommit: null, buildCommitPath: "/app/.paperclip-build-commit", buildInfoPath: "/app/server/dist/build-info.json", readTextFile: (path) => { if (path.endsWith(".paperclip-build-commit")) throw new Error("ENOENT"); return JSON.stringify({ commit }); }, })).toBe(commit); }); it.each(["invalid json", "null", '{"commit":"short"}', '{"commit":42}'])( "fails open on an invalid server stamp: %s", (stamp) => { expect(readBuildCommit({ environmentCommit: null, readTextFile: () => stamp, })).toBeNull(); }, ); it("prefers an explicit environment commit", () => { const readTextFile = vi.fn(() => "ffffffffffffffffffffffffffffffffffffffff"); expect( readBuildCommit({ environmentCommit: "0123456789abcdef0123456789abcdef01234567", readTextFile, }), ).toBe("0123456789abcdef0123456789abcdef01234567"); expect(readTextFile).not.toHaveBeenCalled(); }); it("reads the deployment marker when no environment commit is set", () => { expect( readBuildCommit({ environmentCommit: null, buildCommitPath: "/app/.paperclip-build-commit", readTextFile: (path) => { expect(path).toBe("/app/.paperclip-build-commit"); return "0123456789abcdef0123456789abcdef01234567\n"; }, }), ).toBe("0123456789abcdef0123456789abcdef01234567"); }); });