storageUsageAggregate.test.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { describe, expect, it } from "vitest";
  2. import { createStorageUsageAccumulator } from "../src/storage/domain/usageAggregate.js";
  3. import { STORAGE_CATEGORY_IDS, STORAGE_MORE_ENTRIES_PATH } from "@zcode/shared";
  4. const spec = { id: "home" as const, path: "/h/.zcode", hasCustomDataBaseDir: false };
  5. describe("createStorageUsageAccumulator", () => {
  6. it("folds entries into every category with stable ordering and totals", () => {
  7. const acc = createStorageUsageAccumulator(spec);
  8. acc.add({ relativePath: "cli/debug/model-io-a.jsonl", bytes: 30, mtimeMs: 0 });
  9. acc.add({ relativePath: "cli/debug/model-io-b.jsonl", bytes: 70, mtimeMs: 0 });
  10. acc.add({ relativePath: "backup/one/x", bytes: 5, mtimeMs: 0 });
  11. acc.add({ relativePath: "backup/one/y", bytes: 5, mtimeMs: 0 });
  12. const usage = acc.snapshot(null);
  13. expect(usage.bytes).toBe(110);
  14. expect(usage.fileCount).toBe(4);
  15. expect(usage.categories.map((category) => category.id)).toEqual([...STORAGE_CATEGORY_IDS]);
  16. const trajectory = usage.categories.find((category) => category.id === "modelTrajectory");
  17. expect(trajectory).toMatchObject({ bytes: 100, fileCount: 2, cleanability: "safe" });
  18. expect(trajectory?.entries).toEqual([
  19. { relativePath: "cli/debug/model-io-b.jsonl", bytes: 70, fileCount: 1 },
  20. { relativePath: "cli/debug/model-io-a.jsonl", bytes: 30, fileCount: 1 },
  21. ]);
  22. const backups = usage.categories.find((category) => category.id === "backups");
  23. expect(backups?.entries).toEqual([{ relativePath: "backup/one", bytes: 10, fileCount: 2 }]);
  24. expect(usage.categories.find((category) => category.id === "logs")).toMatchObject({
  25. bytes: 0,
  26. fileCount: 0,
  27. entries: [],
  28. });
  29. });
  30. it("caps entries per category and folds the remainder", () => {
  31. const acc = createStorageUsageAccumulator(spec, { maxEntriesPerCategory: 2 });
  32. for (let index = 0; index < 5; index += 1) {
  33. acc.add({ relativePath: `cli/debug/model-io-${index}.jsonl`, bytes: index + 1, mtimeMs: 0 });
  34. }
  35. const entries = acc.snapshot(null).categories.find((c) => c.id === "modelTrajectory")?.entries;
  36. expect(entries).toEqual([
  37. { relativePath: "cli/debug/model-io-4.jsonl", bytes: 5, fileCount: 1 },
  38. { relativePath: "cli/debug/model-io-3.jsonl", bytes: 4, fileCount: 1 },
  39. { relativePath: STORAGE_MORE_ENTRIES_PATH, bytes: 6, fileCount: 3 },
  40. ]);
  41. });
  42. it("carries root spec and volume into the snapshot", () => {
  43. const volume = { deviceId: "9", mountPoint: "/", totalBytes: 10, freeBytes: 1 };
  44. expect(createStorageUsageAccumulator(spec).snapshot(volume)).toMatchObject({
  45. id: "home",
  46. path: "/h/.zcode",
  47. volume,
  48. bytes: 0,
  49. });
  50. });
  51. });