// app/api/files/route.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 { GET as getFiles } from "./route.js"; let tmpRoot; beforeAll(async () => { tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-files-")); process.env.NAS_ROOT_PATH = tmpRoot; // 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"), "content" ); }); afterAll(async () => { await fs.rm(tmpRoot, { recursive: true, force: true }); }); describe("GET /api/files", () => { it("returns files for a valid query", async () => { const req = new Request( "http://localhost/api/files?branch=NL01&year=2024&month=10&day=23" ); const res = await getFiles(req); expect(res.status).toBe(200); const body = await res.json(); expect(body.branch).toBe("NL01"); expect(body.year).toBe("2024"); expect(body.month).toBe("10"); expect(body.day).toBe("23"); expect(body.files).toEqual([ { name: "test.pdf", relativePath: "NL01/2024/10/23/test.pdf", }, ]); }); it("returns 400 when query params are missing", async () => { const req = new Request("http://localhost/api/files"); // no params const res = await getFiles(req); expect(res.status).toBe(400); const body = await res.json(); expect(body.error).toBe("branch, year, month, day sind erforderlich"); }); });