import subprocess import tempfile import unittest import zipfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] class ContextTests(unittest.TestCase): def test_collision_preserves_legacy_ids(self): with tempfile.TemporaryDirectory() as temp: first, second = Path(temp) / "new.md", Path(temp) / "legacy.md" first.write_text("## PI-042 — New\n## TASK-001 — Historical\n") second.write_text("## PI-042 — Existing\n## FORGE-F001 — Forge\n") result = subprocess.run(["python3", str(ROOT / "scripts/context_audit.py"), str(first), str(second)], capture_output=True, text=True) self.assertEqual(result.returncode, 1) self.assertIn("COLLISION PI-042", result.stdout) self.assertNotIn("TASK-001", result.stdout) self.assertNotIn("FORGE-F001", result.stdout) def test_snapshot_includes_edits_and_excludes_local_data(self): with tempfile.TemporaryDirectory() as temp: repo = Path(temp) subprocess.run(["git", "init", "-q", str(repo)], check=True) (repo / "tracked.txt").write_text("old") subprocess.run(["git", "-C", str(repo), "add", "tracked.txt"], check=True) subprocess.run(["git", "-C", str(repo), "-c", "user.name=Test", "-c", "user.email=test@example.org", "commit", "-qm", "base"], check=True) (repo / "tracked.txt").write_text("new") (repo / "untracked.txt").write_text("safe") (repo / ".env").write_text("private") (repo / "snapshots").mkdir() (repo / "snapshots/old.zip").write_text("old") subprocess.run(["python3", str(ROOT / "scripts/create-chatgpt-snapshot.py"), str(repo)], check=True, capture_output=True) with zipfile.ZipFile(next((repo / "snapshots").glob("*.zip"))) as archive: self.assertEqual(archive.read("tracked.txt"), b"new") self.assertEqual(archive.read("untracked.txt"), b"safe") self.assertNotIn(".env", archive.namelist()) self.assertNotIn("snapshots/old.zip", archive.namelist()) if __name__ == "__main__": unittest.main()