import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { mkdtemp, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; const ORIGINAL_ENV = { ...process.env }; describe("storage fallback", () => { let tempDir: string; beforeEach(async () => { vi.resetModules(); process.env = { ...ORIGINAL_ENV }; delete process.env.BUILT_IN_FORGE_API_URL; delete process.env.BUILT_IN_FORGE_API_KEY; tempDir = await mkdtemp(path.join(os.tmpdir(), "tennis-storage-")); process.env.LOCAL_STORAGE_DIR = tempDir; }); afterEach(async () => { process.env = { ...ORIGINAL_ENV }; await rm(tempDir, { recursive: true, force: true }); }); it("stores files locally when remote storage is not configured", async () => { const { storagePut, storageGet } = await import("./storage"); const stored = await storagePut("videos/test/sample.webm", Buffer.from("demo")); const loaded = await storageGet("videos/test/sample.webm"); expect(stored).toEqual({ key: "videos/test/sample.webm", url: "/uploads/videos/test/sample.webm", }); expect(loaded).toEqual({ key: "videos/test/sample.webm", url: "/uploads/videos/test/sample.webm", }); }); it("builds externally accessible URLs for local assets", async () => { process.env.APP_PUBLIC_BASE_URL = "https://te.hao.work/"; const { toExternalAssetUrl } = await import("./storage"); expect(toExternalAssetUrl("/uploads/videos/test/sample.webm")).toBe( "https://te.hao.work/uploads/videos/test/sample.webm" ); expect(toExternalAssetUrl("https://cdn.example.com/demo.jpg")).toBe( "https://cdn.example.com/demo.jpg" ); }); });