resourceManagerStoragePresentation.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { describe, expect, it } from "vitest";
  2. import type { StorageRootUsage } from "@zcode/shared";
  3. import {
  4. buildStorageLegend,
  5. joinStoragePath,
  6. STORAGE_LEGEND_MAX_ITEMS,
  7. sumCategoriesAcrossRoots,
  8. } from "@/resource-manager/storage/storageCategoryPresentation.js";
  9. function root(
  10. id: StorageRootUsage["id"],
  11. sizes: Partial<Record<string, number>>,
  12. ): StorageRootUsage {
  13. const categories = Object.entries(sizes).map(([categoryId, bytes]) => ({
  14. id: categoryId as StorageRootUsage["categories"][number]["id"],
  15. bytes: bytes ?? 0,
  16. fileCount: bytes ? 1 : 0,
  17. cleanability: "none" as const,
  18. entries: [],
  19. }));
  20. return { id, path: `/${id}/.zcode`, volume: null, bytes: 0, fileCount: 0, categories };
  21. }
  22. describe("sumCategoriesAcrossRoots", () => {
  23. it("merges the same category across roots and sorts by bytes", () => {
  24. const totals = sumCategoriesAcrossRoots([
  25. root("home", { logs: 5, backups: 100 }),
  26. root("dataBaseDir", { logs: 7, other: 1 }),
  27. ]);
  28. expect(totals.map((item) => [item.id, item.bytes, item.fileCount])).toEqual([
  29. ["backups", 100, 1],
  30. ["logs", 12, 2],
  31. ["other", 1, 1],
  32. ]);
  33. });
  34. });
  35. describe("buildStorageLegend", () => {
  36. it("keeps distinct colors for the largest categories and folds remaining bytes", () => {
  37. const sizes = Object.fromEntries(
  38. ["a", "b", "c", "d", "e", "f", "g", "h"].map((id, index) => [id, 100 - index]),
  39. );
  40. const legend = buildStorageLegend(sumCategoriesAcrossRoots([root("home", sizes)]));
  41. expect(legend).toHaveLength(STORAGE_LEGEND_MAX_ITEMS + 1);
  42. expect(legend.at(-1)).toMatchObject({
  43. id: "rest",
  44. restCount: 2,
  45. bytes: 94 + 93,
  46. });
  47. expect(new Set(legend.slice(0, -1).map((item) => item.color)).size).toBe(
  48. STORAGE_LEGEND_MAX_ITEMS,
  49. );
  50. });
  51. it("skips empty categories", () => {
  52. expect(
  53. buildStorageLegend(sumCategoriesAcrossRoots([root("home", { logs: 0, backups: 3 })])),
  54. ).toEqual([expect.objectContaining({ id: "backups", bytes: 3 })]);
  55. });
  56. });
  57. describe("joinStoragePath", () => {
  58. it("joins with the root's own separator", () => {
  59. expect(joinStoragePath("/Users/u/.zcode/", "cli/debug")).toBe("/Users/u/.zcode/cli/debug");
  60. expect(joinStoragePath("C:\\Users\\u\\.zcode", "cli/debug/x.jsonl")).toBe(
  61. "C:\\Users\\u\\.zcode\\cli\\debug\\x.jsonl",
  62. );
  63. });
  64. });