route.test.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // app/api/branches/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 getBranches } from "./route.js";
  7. let tmpRoot;
  8. beforeAll(async () => {
  9. // Create a dedicated temporary root for this test suite
  10. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-branches-"));
  11. // Make this temp root the NAS root for the duration of these tests
  12. process.env.NAS_ROOT_PATH = tmpRoot;
  13. // Create a branch and a snapshot folder
  14. await fs.mkdir(path.join(tmpRoot, "NL01"), { recursive: true });
  15. await fs.mkdir(path.join(tmpRoot, "@Recently-Snapshot"));
  16. });
  17. afterAll(async () => {
  18. await fs.rm(tmpRoot, { recursive: true, force: true });
  19. });
  20. describe("GET /api/branches", () => {
  21. it("returns branch names and filters snapshot folders", async () => {
  22. const req = new Request("http://localhost/api/branches");
  23. const res = await getBranches(req);
  24. expect(res.status).toBe(200);
  25. const body = await res.json();
  26. expect(body).toEqual({ branches: ["NL01"] });
  27. });
  28. it("returns 500 when NAS_ROOT_PATH is invalid", async () => {
  29. const originalRoot = process.env.NAS_ROOT_PATH;
  30. process.env.NAS_ROOT_PATH = path.join(tmpRoot, "does-not-exist");
  31. const req = new Request("http://localhost/api/branches");
  32. const res = await getBranches(req);
  33. expect(res.status).toBe(500);
  34. const body = await res.json();
  35. expect(body.error).toContain("Fehler beim Lesen der Niederlassungen");
  36. // Restore valid root for subsequent tests
  37. process.env.NAS_ROOT_PATH = originalRoot;
  38. });
  39. });