route.test.js 1.5 KB

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