|
| 1 | +// Session store load tests cover startup sweep of orphaned atomic-write .tmp files. |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import { withTempDir } from "../../test-helpers/temp-dir.js"; |
| 6 | +import { sweepOrphanSessionStoreTemps } from "./store-load.js"; |
| 7 | + |
| 8 | +describe("sweepOrphanSessionStoreTemps", () => { |
| 9 | + const uuid = "0f9c1a2b-3c4d-4e5f-8a9b-0c1d2e3f4a5b"; |
| 10 | + |
| 11 | + it("deletes stale orphan temp files matching the store basename", async () => { |
| 12 | + await withTempDir({ prefix: "sweep-test" }, async (tmpDir) => { |
| 13 | + const storePath = path.join(tmpDir, "sessions.json"); |
| 14 | + |
| 15 | + // Create a stale orphan (older than 5 min) |
| 16 | + const staleOrphan = path.join(tmpDir, `sessions.json.12345.${uuid}.tmp`); |
| 17 | + fs.writeFileSync(staleOrphan, "stale", "utf-8"); |
| 18 | + // Backdate mtime to 10 minutes ago |
| 19 | + const tenMinAgo = Date.now() - 10 * 60 * 1000; |
| 20 | + fs.utimesSync(staleOrphan, tenMinAgo / 1000, tenMinAgo / 1000); |
| 21 | + |
| 22 | + // Create a fresh orphan (younger than 5 min — should be preserved) |
| 23 | + const freshOrphan = path.join(tmpDir, `sessions.json.99999.${uuid}.tmp`); |
| 24 | + fs.writeFileSync(freshOrphan, "fresh", "utf-8"); |
| 25 | + |
| 26 | + // Create an unrelated temp file (should not be touched) |
| 27 | + const unrelated = path.join(tmpDir, "other.tmp"); |
| 28 | + fs.writeFileSync(unrelated, "unrelated", "utf-8"); |
| 29 | + fs.utimesSync(unrelated, tenMinAgo / 1000, tenMinAgo / 1000); |
| 30 | + |
| 31 | + sweepOrphanSessionStoreTemps(storePath); |
| 32 | + |
| 33 | + // Stale orphan matching the store basename should be deleted |
| 34 | + expect(fs.existsSync(staleOrphan)).toBe(false); |
| 35 | + // Fresh orphan should remain (not old enough) |
| 36 | + expect(fs.existsSync(freshOrphan)).toBe(true); |
| 37 | + // Unrelated temp files should not be touched |
| 38 | + expect(fs.existsSync(unrelated)).toBe(true); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it("does nothing when no orphan temp files exist", async () => { |
| 43 | + await withTempDir({ prefix: "sweep-empty" }, async (tmpDir) => { |
| 44 | + const storePath = path.join(tmpDir, "sessions.json"); |
| 45 | + fs.writeFileSync(storePath, "{}", "utf-8"); |
| 46 | + |
| 47 | + expect(() => sweepOrphanSessionStoreTemps(storePath)).not.toThrow(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("handles non-existent store directory gracefully", () => { |
| 52 | + const storePath = "/tmp/nonexistent-dir-89520/sessions.json"; |
| 53 | + expect(() => sweepOrphanSessionStoreTemps(storePath)).not.toThrow(); |
| 54 | + }); |
| 55 | + |
| 56 | + it("only deletes temp files for the matching store basename", async () => { |
| 57 | + await withTempDir({ prefix: "sweep-multi" }, async (tmpDir) => { |
| 58 | + const storePath = path.join(tmpDir, "my-sessions.json"); |
| 59 | + const tenMinAgo = Date.now() - 10 * 60 * 1000; |
| 60 | + |
| 61 | + const matchingOrphan = path.join(tmpDir, `my-sessions.json.12345.${uuid}.tmp`); |
| 62 | + fs.writeFileSync(matchingOrphan, "match", "utf-8"); |
| 63 | + fs.utimesSync(matchingOrphan, tenMinAgo / 1000, tenMinAgo / 1000); |
| 64 | + |
| 65 | + const otherStoreOrphan = path.join(tmpDir, `other-sessions.json.12345.${uuid}.tmp`); |
| 66 | + fs.writeFileSync(otherStoreOrphan, "other", "utf-8"); |
| 67 | + fs.utimesSync(otherStoreOrphan, tenMinAgo / 1000, tenMinAgo / 1000); |
| 68 | + |
| 69 | + sweepOrphanSessionStoreTemps(storePath); |
| 70 | + |
| 71 | + expect(fs.existsSync(matchingOrphan)).toBe(false); |
| 72 | + expect(fs.existsSync(otherStoreOrphan)).toBe(true); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments