storage.test.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // tests/lib/storage.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 {
  7. listBranches,
  8. listYears,
  9. listMonths,
  10. listDays,
  11. listFiles,
  12. } from "@/lib/storage";
  13. let tmpRoot;
  14. beforeAll(async () => {
  15. // Create a unique temporary directory for the tests
  16. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "storage-test-"));
  17. // Point NAS_ROOT_PATH to our temp directory
  18. process.env.NAS_ROOT_PATH = tmpRoot;
  19. // Create a fake directory tree:
  20. // tmpRoot/NL01/2024/10/23/test.pdf
  21. await fs.mkdir(path.join(tmpRoot, "NL01", "2024", "10", "23"), {
  22. recursive: true,
  23. });
  24. await fs.writeFile(
  25. path.join(tmpRoot, "NL01", "2024", "10", "23", "test.pdf"),
  26. "dummy-pdf-content"
  27. );
  28. // Add snapshot folder which should be ignored
  29. await fs.mkdir(path.join(tmpRoot, "@Recently-Snapshot"));
  30. });
  31. afterAll(async () => {
  32. // Clean up the temporary directory after tests
  33. await fs.rm(tmpRoot, { recursive: true, force: true });
  34. });
  35. describe("storage: listBranches", () => {
  36. it("returns branch names and filters snapshots", async () => {
  37. const branches = await listBranches();
  38. expect(branches).toEqual(["NL01"]);
  39. });
  40. });
  41. describe("storage: year/month/day helpers", () => {
  42. it("returns years for a branch", async () => {
  43. const years = await listYears("NL01");
  44. expect(years).toEqual(["2024"]);
  45. });
  46. it("returns months for a branch/year", async () => {
  47. const months = await listMonths("NL01", "2024");
  48. expect(months).toEqual(["10"]);
  49. });
  50. it("returns days for a branch/year/month", async () => {
  51. const days = await listDays("NL01", "2024", "10");
  52. expect(days).toEqual(["23"]);
  53. });
  54. });
  55. describe("storage: listFiles", () => {
  56. it("returns PDF files with relativePath", async () => {
  57. const files = await listFiles("NL01", "2024", "10", "23");
  58. expect(files).toEqual([
  59. {
  60. name: "test.pdf",
  61. relativePath: "NL01/2024/10/23/test.pdf",
  62. },
  63. ]);
  64. });
  65. });