|
| 1 | +// CHANGE: add regression coverage for session backup tmp filtering |
| 2 | +// WHY: session snapshots must ignore transient tmp directories while preserving persistent files |
| 3 | +// REF: issue-198 |
| 4 | +// PURITY: SHELL (tests filesystem traversal against committed backup script) |
| 5 | + |
| 6 | +import { describe, expect, it } from "@effect/vitest" |
| 7 | +import { Effect } from "effect" |
| 8 | +import fs from "node:fs" |
| 9 | +import path from "node:path" |
| 10 | + |
| 11 | +import sessionBackupGist from "../../../../scripts/session-backup-gist.js" |
| 12 | + |
| 13 | +const { collectSessionFiles, shouldIgnoreSessionPath } = sessionBackupGist |
| 14 | +const tmpDirPrefix = path.join(process.cwd(), ".tmp-session-backup-gist-") |
| 15 | + |
| 16 | +const withTempDir = Effect.acquireRelease( |
| 17 | + Effect.sync(() => fs.mkdtempSync(tmpDirPrefix)), |
| 18 | + (tmpDir) => |
| 19 | + Effect.sync(() => { |
| 20 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 21 | + }) |
| 22 | +) |
| 23 | + |
| 24 | +describe("session-backup-gist tmp filtering", () => { |
| 25 | + it.effect("ignores tmp directories while keeping persistent session files", () => |
| 26 | + Effect.scoped( |
| 27 | + Effect.gen(function*(_) { |
| 28 | + const tmpDir = yield* _(withTempDir) |
| 29 | + const codexDir = path.join(tmpDir, ".codex") |
| 30 | + const claudeDir = path.join(tmpDir, ".claude") |
| 31 | + |
| 32 | + yield* _( |
| 33 | + Effect.sync(() => { |
| 34 | + fs.mkdirSync(path.join(codexDir, "tmp", "run"), { recursive: true }) |
| 35 | + fs.mkdirSync(path.join(codexDir, "memory"), { recursive: true }) |
| 36 | + fs.mkdirSync(path.join(claudeDir, "tmp"), { recursive: true }) |
| 37 | + fs.mkdirSync(path.join(claudeDir, "profiles"), { recursive: true }) |
| 38 | + |
| 39 | + fs.writeFileSync(path.join(codexDir, "tmp", "run", ".lock"), "lock") |
| 40 | + fs.writeFileSync(path.join(codexDir, "history.jsonl"), "{\"event\":1}\n") |
| 41 | + fs.writeFileSync(path.join(codexDir, "memory", "notes.md"), "# notes\n") |
| 42 | + fs.writeFileSync(path.join(claudeDir, "tmp", "session.lock"), "lock") |
| 43 | + fs.writeFileSync(path.join(claudeDir, "profiles", "default.json"), "{}\n") |
| 44 | + }) |
| 45 | + ) |
| 46 | + |
| 47 | + const files = [ |
| 48 | + ...collectSessionFiles(codexDir, ".codex", false), |
| 49 | + ...collectSessionFiles(claudeDir, ".claude", false) |
| 50 | + ] |
| 51 | + const logicalNames = files |
| 52 | + .map((file) => file.logicalName) |
| 53 | + .toSorted((left, right) => left.localeCompare(right)) |
| 54 | + |
| 55 | + yield* _( |
| 56 | + Effect.sync(() => { |
| 57 | + expect(logicalNames).toContain(".codex/history.jsonl") |
| 58 | + expect(logicalNames).toContain(".codex/memory/notes.md") |
| 59 | + expect(logicalNames).toContain(".claude/profiles/default.json") |
| 60 | + expect(logicalNames.some((name) => name.split("/").includes("tmp"))).toBe(false) |
| 61 | + }) |
| 62 | + ) |
| 63 | + }) |
| 64 | + )) |
| 65 | + |
| 66 | + it.effect("marks tmp paths for exclusion", () => |
| 67 | + Effect.sync(() => { |
| 68 | + expect(shouldIgnoreSessionPath("tmp")).toBe(true) |
| 69 | + expect(shouldIgnoreSessionPath("tmp/run/.lock")).toBe(true) |
| 70 | + expect(shouldIgnoreSessionPath("memory/tmp/run/.lock")).toBe(true) |
| 71 | + expect(shouldIgnoreSessionPath("history.jsonl")).toBe(false) |
| 72 | + expect(shouldIgnoreSessionPath("memory/notes.md")).toBe(false) |
| 73 | + })) |
| 74 | +}) |
0 commit comments