shell-init-snapshot-cleanup.test.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { mkdir, mkdtemp, readdir, rm, stat, utimes, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import {
  6. DEFAULT_SHELL_INIT_SNAPSHOT_RETENTION_DAYS,
  7. ShellInitSnapshotCleanupRegistry,
  8. cleanupStaleShellInitSnapshots,
  9. shellInitSnapshotsDir,
  10. } from "../src/exec/shell-init-snapshot.js";
  11. const DAY_MS = 24 * 60 * 60 * 1000;
  12. describe("shell init snapshot cleanup", () => {
  13. it("unlinks snapshots registered by the current adapter process", async () => {
  14. const rootDir = await mkdtemp(join(tmpdir(), "zcode-shell-init-cleanup-"));
  15. const registry = new ShellInitSnapshotCleanupRegistry();
  16. const snapshotPath = join(shellInitSnapshotsDir(rootDir), "snapshot-bash.sh");
  17. try {
  18. await mkdir(shellInitSnapshotsDir(rootDir), { recursive: true });
  19. await writeFile(snapshotPath, "# snapshot\n");
  20. registry.register(snapshotPath);
  21. const result = await registry.cleanupAll();
  22. expect(result).toEqual({ deleted: 1, errors: 0 });
  23. await expect(stat(snapshotPath)).rejects.toHaveProperty("code", "ENOENT");
  24. } finally {
  25. await rm(rootDir, { recursive: true, force: true });
  26. }
  27. });
  28. it("treats already-missing registered snapshots as cleaned", async () => {
  29. const rootDir = await mkdtemp(join(tmpdir(), "zcode-shell-init-cleanup-"));
  30. const registry = new ShellInitSnapshotCleanupRegistry();
  31. const snapshotPath = join(shellInitSnapshotsDir(rootDir), "missing.sh");
  32. try {
  33. registry.register(snapshotPath);
  34. const first = await registry.cleanupAll();
  35. const second = await registry.cleanupAll();
  36. expect(first).toEqual({ deleted: 0, errors: 0 });
  37. expect(second).toEqual({ deleted: 0, errors: 0 });
  38. } finally {
  39. await rm(rootDir, { recursive: true, force: true });
  40. }
  41. });
  42. it("removes stale top-level .sh files under shell-snapshots and keeps fresh or non-sh files", async () => {
  43. const rootDir = await mkdtemp(join(tmpdir(), "zcode-shell-init-retention-"));
  44. const snapshotsDir = shellInitSnapshotsDir(rootDir);
  45. const staleSnapshot = join(snapshotsDir, "stale.sh");
  46. const freshSnapshot = join(snapshotsDir, "fresh.sh");
  47. const staleTextFile = join(snapshotsDir, "stale.txt");
  48. const staleNestedSnapshot = join(snapshotsDir, "nested", "stale.sh");
  49. const now = new Date("2026-06-26T00:00:00.000Z");
  50. const staleTime = new Date(
  51. now.getTime() - (DEFAULT_SHELL_INIT_SNAPSHOT_RETENTION_DAYS + 1) * DAY_MS,
  52. );
  53. try {
  54. await mkdir(snapshotsDir, { recursive: true });
  55. await mkdir(join(snapshotsDir, "nested"), { recursive: true });
  56. await writeFile(staleSnapshot, "# stale\n");
  57. await writeFile(freshSnapshot, "# fresh\n");
  58. await writeFile(staleTextFile, "not a shell snapshot\n");
  59. await writeFile(staleNestedSnapshot, "# nested stale\n");
  60. await utimes(staleSnapshot, staleTime, staleTime);
  61. await utimes(staleTextFile, staleTime, staleTime);
  62. await utimes(staleNestedSnapshot, staleTime, staleTime);
  63. const result = await cleanupStaleShellInitSnapshots({ now, rootDir });
  64. expect(result).toEqual({ deleted: 1, errors: 0 });
  65. await expect(stat(staleSnapshot)).rejects.toHaveProperty("code", "ENOENT");
  66. await expect(stat(freshSnapshot)).resolves.toBeDefined();
  67. await expect(stat(staleTextFile)).resolves.toBeDefined();
  68. await expect(stat(staleNestedSnapshot)).resolves.toBeDefined();
  69. await expect(readdir(snapshotsDir)).resolves.toEqual([
  70. "fresh.sh",
  71. "nested",
  72. "stale.txt",
  73. ]);
  74. } finally {
  75. await rm(rootDir, { recursive: true, force: true });
  76. }
  77. });
  78. });