route.test.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // app/api/files/route.test.js
  2. import { describe, it, expect, beforeAll, afterAll } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. import { GET as getFiles } from "./route.js";
  7. let tmpRoot;
  8. beforeAll(async () => {
  9. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-files-"));
  10. process.env.NAS_ROOT_PATH = tmpRoot;
  11. // tmpRoot/NL01/2024/10/23/test.pdf
  12. await fs.mkdir(path.join(tmpRoot, "NL01", "2024", "10", "23"), {
  13. recursive: true,
  14. });
  15. await fs.writeFile(
  16. path.join(tmpRoot, "NL01", "2024", "10", "23", "test.pdf"),
  17. "content"
  18. );
  19. });
  20. afterAll(async () => {
  21. await fs.rm(tmpRoot, { recursive: true, force: true });
  22. });
  23. describe("GET /api/files", () => {
  24. it("returns files for a valid query", async () => {
  25. const req = new Request(
  26. "http://localhost/api/files?branch=NL01&year=2024&month=10&day=23"
  27. );
  28. const res = await getFiles(req);
  29. expect(res.status).toBe(200);
  30. const body = await res.json();
  31. expect(body.branch).toBe("NL01");
  32. expect(body.year).toBe("2024");
  33. expect(body.month).toBe("10");
  34. expect(body.day).toBe("23");
  35. expect(body.files).toEqual([
  36. {
  37. name: "test.pdf",
  38. relativePath: "NL01/2024/10/23/test.pdf",
  39. },
  40. ]);
  41. });
  42. it("returns 400 when query params are missing", async () => {
  43. const req = new Request("http://localhost/api/files"); // no params
  44. const res = await getFiles(req);
  45. expect(res.status).toBe(400);
  46. const body = await res.json();
  47. expect(body.error).toBe("branch, year, month, day sind erforderlich");
  48. });
  49. });