| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- // app/api/branches/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 getBranches } from "./route.js";
- let tmpRoot;
- beforeAll(async () => {
- // Create a dedicated temporary root for this test suite
- tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-branches-"));
- // Make this temp root the NAS root for the duration of these tests
- process.env.NAS_ROOT_PATH = tmpRoot;
- // Create a branch and a snapshot folder
- await fs.mkdir(path.join(tmpRoot, "NL01"), { recursive: true });
- await fs.mkdir(path.join(tmpRoot, "@Recently-Snapshot"));
- });
- afterAll(async () => {
- await fs.rm(tmpRoot, { recursive: true, force: true });
- });
- describe("GET /api/branches", () => {
- it("returns branch names and filters snapshot folders", async () => {
- const req = new Request("http://localhost/api/branches");
- const res = await getBranches(req);
- expect(res.status).toBe(200);
- const body = await res.json();
- expect(body).toEqual({ branches: ["NL01"] });
- });
- it("returns 500 when NAS_ROOT_PATH is invalid", async () => {
- const originalRoot = process.env.NAS_ROOT_PATH;
- process.env.NAS_ROOT_PATH = path.join(tmpRoot, "does-not-exist");
- const req = new Request("http://localhost/api/branches");
- const res = await getBranches(req);
- expect(res.status).toBe(500);
- const body = await res.json();
- expect(body.error).toContain("Fehler beim Lesen der Niederlassungen");
- // Restore valid root for subsequent tests
- process.env.NAS_ROOT_PATH = originalRoot;
- });
- });
|