Pārlūkot izejas kodu

RHL-002-feat:implement tests for storage module functions using vitest

Code_Uwe 1 nedēļu atpakaļ
vecāks
revīzija
081432b174
2 mainītis faili ar 90 papildinājumiem un 11 dzēšanām
  1. 12 11
      lib/storage.js
  2. 78 0
      lib/storage.test.js

+ 12 - 11
lib/storage.js

@@ -15,21 +15,22 @@ import path from "node:path"; // Safe path utilities (handles separators)
 
 // Root directory of the NAS share, injected via environment variable.
 // On the Linux app server, this is typically `/mnt/niederlassungen`.
-// Locally you can point this to any folder (or leave it unset for now).
-const ROOT = process.env.NAS_ROOT_PATH;
+// Do NOT cache process.env.NAS_ROOT_PATH at module load time.
+// Instead, resolve it on demand so tests (and runtime) can change it.
+function getRoot() {
+	const root = process.env.NAS_ROOT_PATH;
 
-// Build an absolute path below the NAS root from a list of segments.
-// Example: fullPath("NL01", "2024", "10", "23")
-//        → "/mnt/niederlassungen/NL01/2024/10/23"
-function fullPath(...segments) {
-	// Failing fast if ROOT is missing makes configuration errors obvious.
-	if (!ROOT) {
+	if (!root) {
 		throw new Error("NAS_ROOT_PATH environment variable is not set");
 	}
 
-	// Always use `path.join` instead of manual string concatenation to avoid
-	// issues with slashes on different platforms.
-	return path.join(ROOT, ...segments.map(String));
+	return root;
+}
+
+// Build an absolute path below the NAS root from a list of segments.
+function fullPath(...segments) {
+	const root = getRoot();
+	return path.join(root, ...segments.map(String));
 }
 
 // Compare strings that represent numbers in a numeric way.

+ 78 - 0
lib/storage.test.js

@@ -0,0 +1,78 @@
+// tests/lib/storage.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 {
+	listBranches,
+	listYears,
+	listMonths,
+	listDays,
+	listFiles,
+} from "@/lib/storage";
+
+let tmpRoot;
+
+beforeAll(async () => {
+	// Create a unique temporary directory for the tests
+	tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "storage-test-"));
+
+	// Point NAS_ROOT_PATH to our temp directory
+	process.env.NAS_ROOT_PATH = tmpRoot;
+
+	// Create a fake directory tree:
+	// 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"),
+		"dummy-pdf-content"
+	);
+
+	// Add snapshot folder which should be ignored
+	await fs.mkdir(path.join(tmpRoot, "@Recently-Snapshot"));
+});
+
+afterAll(async () => {
+	// Clean up the temporary directory after tests
+	await fs.rm(tmpRoot, { recursive: true, force: true });
+});
+
+describe("storage: listBranches", () => {
+	it("returns branch names and filters snapshots", async () => {
+		const branches = await listBranches();
+		expect(branches).toEqual(["NL01"]);
+	});
+});
+
+describe("storage: year/month/day helpers", () => {
+	it("returns years for a branch", async () => {
+		const years = await listYears("NL01");
+		expect(years).toEqual(["2024"]);
+	});
+
+	it("returns months for a branch/year", async () => {
+		const months = await listMonths("NL01", "2024");
+		expect(months).toEqual(["10"]);
+	});
+
+	it("returns days for a branch/year/month", async () => {
+		const days = await listDays("NL01", "2024", "10");
+		expect(days).toEqual(["23"]);
+	});
+});
+
+describe("storage: listFiles", () => {
+	it("returns PDF files with relativePath", async () => {
+		const files = await listFiles("NL01", "2024", "10", "23");
+		expect(files).toEqual([
+			{
+				name: "test.pdf",
+				relativePath: "NL01/2024/10/23/test.pdf",
+			},
+		]);
+	});
+});