storageCleanPlan.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { describe, expect, it } from "vitest";
  2. import { planStorageClean, SUBAGENT_ACTIVE_WINDOW_MS } from "../src/storage/domain/cleanPlan.js";
  3. const context = { rootId: "home" as const, hasCustomDataBaseDir: false };
  4. const now = new Date(2026, 8, 10, 15, 0, 0).getTime();
  5. const yesterday = now - 24 * 60 * 60 * 1000;
  6. const candidate = (relativePath: string, mtimeMs = yesterday, bytes = 1) => ({
  7. relativePath,
  8. bytes,
  9. mtimeMs,
  10. });
  11. describe("planStorageClean", () => {
  12. it("refuses non-cleanable categories", () => {
  13. const plan = planStorageClean({
  14. categoryId: "sessionStore",
  15. candidates: [candidate("v2/tasks-index.sqlite")],
  16. context,
  17. now,
  18. });
  19. expect(plan).toEqual({ targets: [], skippedCount: 1 });
  20. });
  21. it("only deletes candidates classified into the requested category and never protected files", () => {
  22. const plan = planStorageClean({
  23. categoryId: "devTraces",
  24. candidates: [
  25. candidate("v2/dev/stdio-traffic/a/x.ndjson"),
  26. candidate("v2/dev/zcode-stdio-tap.json"),
  27. candidate("v2/logs/a.log"),
  28. ],
  29. context,
  30. now,
  31. });
  32. expect(plan.targets.map((t) => t.relativePath)).toEqual(["v2/dev/stdio-traffic/a/x.ndjson"]);
  33. expect(plan.skippedCount).toBe(2);
  34. });
  35. it("keeps today's log files and the live crash directory", () => {
  36. const plan = planStorageClean({
  37. categoryId: "logs",
  38. candidates: [
  39. candidate("v2/logs/2026-09-09.log", yesterday),
  40. candidate("v2/logs/2026-09-10.log", now - 60_000),
  41. candidate("v2/crash/live/current.dmp", yesterday),
  42. candidate("cli/log/zcode-2026-09-01.jsonl", yesterday - 8 * 24 * 3600 * 1000),
  43. ],
  44. context,
  45. now,
  46. });
  47. expect(plan.targets.map((t) => t.relativePath)).toEqual([
  48. "v2/logs/2026-09-09.log",
  49. "cli/log/zcode-2026-09-01.jsonl",
  50. ]);
  51. });
  52. it("only deletes subagent transcripts and skips session directories touched within 24h", () => {
  53. const recent = now - SUBAGENT_ACTIVE_WINDOW_MS + 1;
  54. const old = yesterday - SUBAGENT_ACTIVE_WINDOW_MS;
  55. const plan = planStorageClean({
  56. categoryId: "subagentTranscripts",
  57. candidates: [
  58. candidate("cli/agents/sess_old/agent_1/transcript.jsonl", old),
  59. candidate("cli/agents/sess_old/agent_1/output.txt", old),
  60. candidate("cli/agents/sess_hot/agent_2/transcript.jsonl", old),
  61. candidate("cli/agents/sess_hot/agent_2/metadata.json", recent),
  62. ],
  63. context,
  64. now,
  65. });
  66. expect(plan.targets.map((t) => t.relativePath)).toEqual([
  67. "cli/agents/sess_old/agent_1/transcript.jsonl",
  68. ]);
  69. expect(plan.skippedCount).toBe(3);
  70. });
  71. it("refuses to clean tool outputs and temporary caches", () => {
  72. const plan = planStorageClean({
  73. categoryId: "toolOutputs",
  74. candidates: [candidate("cli/exec/sess_1/out.log"), candidate("cli/artifacts/sess_1/a.json")],
  75. context,
  76. now,
  77. });
  78. expect(plan).toEqual({ targets: [], skippedCount: 2 });
  79. });
  80. it("does not let backups cleaning touch live databases listed by the shallow scope", () => {
  81. const plan = planStorageClean({
  82. categoryId: "backups",
  83. candidates: [
  84. candidate("cli/db/db.sqlite"),
  85. candidate("cli/db/db.sqlite-wal"),
  86. candidate("cli/db/db.sqlite.bak-1"),
  87. candidate("v2/setting.json"),
  88. candidate("v2/setting.json.corrupt-1"),
  89. candidate("v2/tasks-index.sqlite"),
  90. ],
  91. context,
  92. now,
  93. });
  94. expect(plan.targets.map((t) => t.relativePath)).toEqual([
  95. "cli/db/db.sqlite.bak-1",
  96. "v2/setting.json.corrupt-1",
  97. ]);
  98. });
  99. });