resourceManagerStoragePresentation.test.ts 2.4 KB

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