storageCatalog.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. classifyStoragePath,
  4. getStorageCategoryCleanability,
  5. getStorageCleanScopes,
  6. isProtectedStoragePath,
  7. type StorageCatalogContext,
  8. } from "../src/storage/domain/storageCatalog.js";
  9. import { STORAGE_CATEGORY_IDS } from "@zcode/shared";
  10. const home: StorageCatalogContext = { rootId: "home", hasCustomDataBaseDir: false };
  11. describe("classifyStoragePath", () => {
  12. it.each([
  13. ["v2/tasks-index.sqlite", "sessionStore", "v2/tasks-index.sqlite"],
  14. ["v2/tasks-index.sqlite-wal", "sessionStore", "v2/tasks-index.sqlite-wal"],
  15. ["cli/db/db.sqlite", "sessionStore", "cli/db/db.sqlite"],
  16. ["cli/db/db.sqlite-shm", "sessionStore", "cli/db/db.sqlite-shm"],
  17. ["v2/sessions/abc/task.json", "sessionStore", "v2/sessions/abc"],
  18. ["v2/checkpoints/ws/a/pending/x.bin", "toolOutputs", "v2/checkpoints/ws/a/pending/x.bin"],
  19. ["cli/artifacts/sess_1/call.json", "toolOutputs", "cli/artifacts/sess_1"],
  20. ["cli/agents/sess_1/agent_1/transcript.jsonl", "subagentTranscripts", "cli/agents/sess_1"],
  21. ["cli/agents/sess_1/agent_1/output.txt", "toolOutputs", "cli/agents/sess_1"],
  22. ["cli/agents/sess_1/agent_1/metadata.json", "toolOutputs", "cli/agents/sess_1"],
  23. ["cli/debug/model-io-sess_x.jsonl", "modelTrajectory", "cli/debug/model-io-sess_x.jsonl"],
  24. ["cli/rollout/model-io-sess_x.jsonl", "modelTrajectory", "cli/rollout/model-io-sess_x.jsonl"],
  25. ["v2/dev/stdio-traffic/abc/x.ndjson", "devTraces", "v2/dev/stdio-traffic"],
  26. ["v2/acp-traffic-proxy/captures/t.ndjson", "devTraces", "v2/acp-traffic-proxy/captures"],
  27. ["v2/logs/2026-09-04.log", "logs", "v2/logs/2026-09-04.log"],
  28. ["cli/log/zcode-2026-09-04.jsonl", "logs", "cli/log/zcode-2026-09-04.jsonl"],
  29. ["v2/crash/archive/x.dmp", "logs", "v2/crash/archive"],
  30. ["feedback/logs/a.zip", "logs", "feedback/logs/a.zip"],
  31. ["feedback/attachments/a.png", "exports", "feedback/attachments"],
  32. ["computer-use/run/helper.exit.log", "logs", "computer-use/run/helper.exit.log"],
  33. ["backup/session-projects-1/v2/x", "backups", "backup/session-projects-1"],
  34. ["cli/db/backup/db.pre-projects.1.sqlite", "backups", "cli/db/backup/db.pre-projects.1.sqlite"],
  35. ["cli/db/db.sqlite.bak-agent-control", "backups", "cli/db/db.sqlite.bak-agent-control"],
  36. [
  37. "cli/db/db.sqlite.backup-before-drop-shm",
  38. "backups",
  39. "cli/db/db.sqlite.backup-before-drop-shm",
  40. ],
  41. ["v2/migrations/x/journal.json", "backups", "v2/migrations/x"],
  42. ["v2/model-providers.json.bak", "backups", "v2/model-providers.json.bak"],
  43. ["v2/model-providers.v1.backup.json", "backups", "v2/model-providers.v1.backup.json"],
  44. ["v2/setting.json.corrupt-2026", "backups", "v2/setting.json.corrupt-2026"],
  45. ["v2/setting.json.codex-perf-backup-1", "backups", "v2/setting.json.codex-perf-backup-1"],
  46. ["v2/config.json.pre-provider-v1-abc.bak", "backups", "v2/config.json.pre-provider-v1-abc.bak"],
  47. ["cli/config.json.bak-2026", "backups", "cli/config.json.bak-2026"],
  48. ["export-log/zcode-logs-1/a.zip", "exports", "export-log/zcode-logs-1"],
  49. ["export-log-stage/stage-x/a", "exports", "export-log-stage/stage-x"],
  50. ["cli/exec/sess_1/out.txt", "toolOutputs", "cli/exec/sess_1"],
  51. ["cli/image-cache/sess_1/a.png", "toolOutputs", "cli/image-cache/sess_1"],
  52. ["cli/plugins/cache/m/x", "runtimes", "cli/plugins/cache"],
  53. ["cli/plugins/data/x", "runtimes", "cli/plugins/data"],
  54. ["v2/coding-plan-cache.json", "toolOutputs", "v2/coding-plan-cache.json"],
  55. ["v2/bots-model-cache.v2.json", "toolOutputs", "v2/bots-model-cache.v2.json"],
  56. ["tmp/paste-attachments/2026/a.txt", "toolOutputs", "tmp/paste-attachments"],
  57. ["agents/claude-code/bin/x", "runtimes", "agents/claude-code"],
  58. ["agents/code-reviewer.md", "config", "agents/code-reviewer.md"],
  59. ["lite/releases/3.6.0/x", "runtimes", "lite/releases"],
  60. ["computer-use/dev/App.app/Contents/x", "runtimes", "computer-use/dev"],
  61. ["bundled-agents/opencode.tgz", "runtimes", "bundled-agents/opencode.tgz"],
  62. ["v2/setting.json", "config", "v2/setting.json"],
  63. ["v2/credentials.json", "config", "v2/credentials.json"],
  64. ["v2/acp-config/codex/x", "config", "v2/acp-config/codex"],
  65. ["cli/config.json", "config", "cli/config.json"],
  66. ["cli/memories/projects/x.md", "config", "cli/memories/projects"],
  67. ["security/workspace-hook-trust-v1.json", "config", "security/workspace-hook-trust-v1.json"],
  68. ["workspace/default/a.html", "config", "workspace/default"],
  69. ["AGENTS.md", "config", "AGENTS.md"],
  70. ["agent/sessions/x.json", "other", "agent"],
  71. [".DS_Store", "other", ".DS_Store"],
  72. ["unknown-dir/deep/file", "other", "unknown-dir"],
  73. ])("%s → %s (%s)", (path, categoryId, entryKey) => {
  74. expect(classifyStoragePath(path, home)).toEqual({ categoryId, entryKey });
  75. });
  76. it("treats the stale home v2 copy as other only when a custom data dir is active", () => {
  77. const custom: StorageCatalogContext = { rootId: "home", hasCustomDataBaseDir: true };
  78. expect(classifyStoragePath("v2/logs/a.log", custom)).toEqual({
  79. categoryId: "other",
  80. entryKey: "v2",
  81. });
  82. expect(classifyStoragePath("v2/tasks-index.sqlite", custom).categoryId).toBe("other");
  83. expect(classifyStoragePath("cli/debug/x.jsonl", custom).categoryId).toBe("modelTrajectory");
  84. const dataBaseDir: StorageCatalogContext = {
  85. rootId: "dataBaseDir",
  86. hasCustomDataBaseDir: true,
  87. };
  88. expect(classifyStoragePath("v2/logs/a.log", dataBaseDir).categoryId).toBe("logs");
  89. });
  90. it("normalizes windows separators and surrounding slashes", () => {
  91. expect(classifyStoragePath("\\cli\\debug\\x.jsonl", home).categoryId).toBe("modelTrajectory");
  92. expect(classifyStoragePath("/v2/logs/x.log/", home).entryKey).toBe("v2/logs/x.log");
  93. });
  94. });
  95. describe("cleanability, protection and clean scopes", () => {
  96. it("declares every category and only allows cleaning of cleanable ones", () => {
  97. for (const id of STORAGE_CATEGORY_IDS) {
  98. expect(["none", "safe", "confirm"]).toContain(getStorageCategoryCleanability(id));
  99. }
  100. expect(getStorageCategoryCleanability("modelTrajectory")).toBe("safe");
  101. expect(getStorageCategoryCleanability("backups")).toBe("confirm");
  102. expect(getStorageCategoryCleanability("sessionStore")).toBe("none");
  103. expect(getStorageCategoryCleanability("toolOutputs")).toBe("none");
  104. expect(getStorageCategoryCleanability("subagentTranscripts")).toBe("safe");
  105. expect(getStorageCleanScopes("subagentTranscripts")).toEqual([
  106. { prefix: "cli/agents", recursive: true },
  107. ]);
  108. expect(getStorageCleanScopes("toolOutputs")).toEqual([]);
  109. expect(getStorageCleanScopes("sessionStore")).toEqual([]);
  110. expect(getStorageCleanScopes("other")).toEqual([]);
  111. });
  112. it("protects bootstrap, credential, broker token, diagnostics toggle and live crash files", () => {
  113. for (const path of [
  114. "v2/setting.json",
  115. "v2/setting.json.lock",
  116. "v2/credentials.json",
  117. "computer-use/run/.tokens",
  118. "v2/dev/zcode-stdio-tap.json",
  119. "v2/crash/live/current.dmp",
  120. ]) {
  121. expect(isProtectedStoragePath(path), path).toBe(true);
  122. }
  123. for (const path of ["v2/setting.json.corrupt-1", "v2/logs/a.log", "v2/crash/archive/x"]) {
  124. expect(isProtectedStoragePath(path), path).toBe(false);
  125. }
  126. });
  127. it("lists recursive prefix scopes plus shallow directories for file rules", () => {
  128. expect(getStorageCleanScopes("backups")).toEqual([
  129. { prefix: "backup", recursive: true },
  130. { prefix: "v2/backup", recursive: true },
  131. { prefix: "v2/migrations", recursive: true },
  132. { prefix: "cli/db/backup", recursive: true },
  133. { prefix: "cli/db/backups", recursive: true },
  134. { prefix: "cli/db", recursive: false },
  135. { prefix: "cli", recursive: false },
  136. { prefix: "v2", recursive: false },
  137. ]);
  138. expect(getStorageCleanScopes("modelTrajectory")).toEqual([
  139. { prefix: "cli/debug", recursive: true },
  140. { prefix: "cli/rollout", recursive: true },
  141. ]);
  142. });
  143. });