// tests/lib/storage.test.js import { describe, it, expect, beforeAll, afterAll } from "vitest"; import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { listBranches, listYears, listMonths, listDays, listFiles, } from "@/lib/storage"; let tmpRoot; beforeAll(async () => { // Create a unique temporary directory for the tests tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "storage-test-")); // Point NAS_ROOT_PATH to our temp directory process.env.NAS_ROOT_PATH = tmpRoot; // Create a fake directory tree: // tmpRoot/NL01/2024/10/23/test.pdf await fs.mkdir(path.join(tmpRoot, "NL01", "2024", "10", "23"), { recursive: true, }); await fs.writeFile( path.join(tmpRoot, "NL01", "2024", "10", "23", "test.pdf"), "dummy-pdf-content" ); // Add snapshot folder which should be ignored await fs.mkdir(path.join(tmpRoot, "@Recently-Snapshot")); }); afterAll(async () => { // Clean up the temporary directory after tests await fs.rm(tmpRoot, { recursive: true, force: true }); }); describe("storage: listBranches", () => { it("returns branch names and filters snapshots", async () => { const branches = await listBranches(); expect(branches).toEqual(["NL01"]); }); }); describe("storage: year/month/day helpers", () => { it("returns years for a branch", async () => { const years = await listYears("NL01"); expect(years).toEqual(["2024"]); }); it("returns months for a branch/year", async () => { const months = await listMonths("NL01", "2024"); expect(months).toEqual(["10"]); }); it("returns days for a branch/year/month", async () => { const days = await listDays("NL01", "2024", "10"); expect(days).toEqual(["23"]); }); }); describe("storage: listFiles", () => { it("returns PDF files with relativePath", async () => { const files = await listFiles("NL01", "2024", "10", "23"); expect(files).toEqual([ { name: "test.pdf", relativePath: "NL01/2024/10/23/test.pdf", }, ]); }); });