import { describe, expect, it, vi } from "vitest"; vi.mock("@/lib/db", () => ({ db: {} })); vi.mock("@/lib/storage", () => ({ deleteFromHetzner: vi.fn() })); import { highResStorageKeys, shouldKeepHighRes } from "./high-res"; const now = new Date("2026-09-22T12:00:00Z"); const daysAgo = (d: number) => new Date(now.getTime() - d * 24 * 3600 * 1000); const old = { isLatest: false, isClientVisible: false, approvalStatus: "PENDING_REVIEW", createdAt: daysAgo(30) }; describe("shouldKeepHighRes", () => { it("prunes an old, superseded, unshared, unapproved version", () => { expect(shouldKeepHighRes(old, now, 14)).toBe(false); }); it("keeps the latest version however old", () => { expect(shouldKeepHighRes({ ...old, isLatest: true }, now, 14)).toBe(true); }); it("keeps anything shared with the client — they may download it", () => { expect(shouldKeepHighRes({ ...old, isClientVisible: true }, now, 14)).toBe(true); }); it("keeps the approved version of record", () => { expect(shouldKeepHighRes({ ...old, approvalStatus: "APPROVED" }, now, 14)).toBe(true); }); it("keeps recent superseded versions inside the retention window", () => { expect(shouldKeepHighRes({ ...old, createdAt: daysAgo(13) }, now, 14)).toBe(true); expect(shouldKeepHighRes({ ...old, createdAt: daysAgo(15) }, now, 14)).toBe(false); }); it("treats needs-changes and rejected versions as prunable", () => { expect(shouldKeepHighRes({ ...old, approvalStatus: "NEEDS_CHANGES" }, now, 14)).toBe(false); expect(shouldKeepHighRes({ ...old, approvalStatus: "REJECTED" }, now, 14)).toBe(false); }); it("prunes superseded versions straight away with a zero-day window", () => { expect(shouldKeepHighRes({ ...old, createdAt: daysAgo(0.01) }, now, 0)).toBe(false); }); }); describe("highResStorageKeys", () => { it("returns the ProRes key and the HQ MP4's storage key", () => { expect( highResStorageKeys({ highResKey: "highres/a.mov", hqFileUrl: "/api/files/videos/b_HQ.mp4" }) ).toEqual(["highres/a.mov", "videos/b_HQ.mp4"]); }); it("is empty when there is nothing to delete", () => { expect(highResStorageKeys({ highResKey: null, hqFileUrl: null })).toEqual([]); }); });