|
| 1 | +""" |
| 2 | +Tests for the capture_prompts_enabled() / _capture_prompts_enabled() config gate. |
| 3 | +
|
| 4 | +Covers both capture-claude.py (capture side) and finalize-ledger.py (trace side). |
| 5 | +""" |
| 6 | +import importlib.util |
| 7 | +import os |
| 8 | +import tempfile |
| 9 | +import unittest |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 13 | +CAPTURE_CLAUDE = REPO_ROOT / "scripts" / "capture-claude.py" |
| 14 | +FINALIZE_LEDGER = REPO_ROOT / "scripts" / "finalize-ledger.py" |
| 15 | + |
| 16 | + |
| 17 | +def load_module(path: Path, name: str): |
| 18 | + spec = importlib.util.spec_from_file_location(name, path) |
| 19 | + mod = importlib.util.module_from_spec(spec) |
| 20 | + assert spec and spec.loader |
| 21 | + spec.loader.exec_module(mod) |
| 22 | + return mod |
| 23 | + |
| 24 | + |
| 25 | +class CaptureCapturePromptsEnabledTests(unittest.TestCase): |
| 26 | + """Tests for capture_prompts_enabled() in capture-claude.py.""" |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def setUpClass(cls): |
| 30 | + cls.mod = load_module(CAPTURE_CLAUDE, "capture_claude") |
| 31 | + |
| 32 | + def _write_config(self, tmp_dir: str, content: str) -> str: |
| 33 | + path = os.path.join(tmp_dir, "config.toml") |
| 34 | + with open(path, "w") as f: |
| 35 | + f.write(content) |
| 36 | + return path |
| 37 | + |
| 38 | + def test_returns_true_when_config_missing(self): |
| 39 | + original = os.environ.get("HOME") |
| 40 | + try: |
| 41 | + with tempfile.TemporaryDirectory() as tmp: |
| 42 | + # Point HOME to an empty directory with no config |
| 43 | + os.environ["HOME"] = tmp |
| 44 | + result = self.mod.capture_prompts_enabled() |
| 45 | + self.assertTrue(result, "Should default to True when config is absent") |
| 46 | + finally: |
| 47 | + if original is not None: |
| 48 | + os.environ["HOME"] = original |
| 49 | + |
| 50 | + def test_returns_false_when_explicitly_disabled(self): |
| 51 | + with tempfile.TemporaryDirectory() as tmp: |
| 52 | + agentdiff_dir = os.path.join(tmp, ".agentdiff") |
| 53 | + os.makedirs(agentdiff_dir) |
| 54 | + config_path = os.path.join(agentdiff_dir, "config.toml") |
| 55 | + with open(config_path, "w") as f: |
| 56 | + f.write('schema_version = "1.0"\ncapture_prompts = false\n') |
| 57 | + original = os.environ.get("HOME") |
| 58 | + try: |
| 59 | + os.environ["HOME"] = tmp |
| 60 | + result = self.mod.capture_prompts_enabled() |
| 61 | + self.assertFalse(result, "Should return False when capture_prompts = false") |
| 62 | + finally: |
| 63 | + if original is not None: |
| 64 | + os.environ["HOME"] = original |
| 65 | + |
| 66 | + def test_returns_true_when_explicitly_enabled(self): |
| 67 | + with tempfile.TemporaryDirectory() as tmp: |
| 68 | + agentdiff_dir = os.path.join(tmp, ".agentdiff") |
| 69 | + os.makedirs(agentdiff_dir) |
| 70 | + config_path = os.path.join(agentdiff_dir, "config.toml") |
| 71 | + with open(config_path, "w") as f: |
| 72 | + f.write('schema_version = "1.0"\ncapture_prompts = true\n') |
| 73 | + original = os.environ.get("HOME") |
| 74 | + try: |
| 75 | + os.environ["HOME"] = tmp |
| 76 | + result = self.mod.capture_prompts_enabled() |
| 77 | + self.assertTrue(result, "Should return True when capture_prompts = true") |
| 78 | + finally: |
| 79 | + if original is not None: |
| 80 | + os.environ["HOME"] = original |
| 81 | + |
| 82 | + def test_accepts_off_as_false(self): |
| 83 | + with tempfile.TemporaryDirectory() as tmp: |
| 84 | + agentdiff_dir = os.path.join(tmp, ".agentdiff") |
| 85 | + os.makedirs(agentdiff_dir) |
| 86 | + config_path = os.path.join(agentdiff_dir, "config.toml") |
| 87 | + with open(config_path, "w") as f: |
| 88 | + f.write("capture_prompts = off\n") |
| 89 | + original = os.environ.get("HOME") |
| 90 | + try: |
| 91 | + os.environ["HOME"] = tmp |
| 92 | + result = self.mod.capture_prompts_enabled() |
| 93 | + self.assertFalse(result, "Should treat 'off' as disabled") |
| 94 | + finally: |
| 95 | + if original is not None: |
| 96 | + os.environ["HOME"] = original |
| 97 | + |
| 98 | + def test_inline_comment_does_not_break_false_detection(self): |
| 99 | + """Regression: capture_prompts = false # comment was previously misread as enabled.""" |
| 100 | + with tempfile.TemporaryDirectory() as tmp: |
| 101 | + agentdiff_dir = os.path.join(tmp, ".agentdiff") |
| 102 | + os.makedirs(agentdiff_dir) |
| 103 | + config_path = os.path.join(agentdiff_dir, "config.toml") |
| 104 | + with open(config_path, "w") as f: |
| 105 | + f.write("capture_prompts = false # prod default\n") |
| 106 | + original = os.environ.get("HOME") |
| 107 | + try: |
| 108 | + os.environ["HOME"] = tmp |
| 109 | + result = self.mod.capture_prompts_enabled() |
| 110 | + self.assertFalse(result, "Inline comment must not prevent false detection") |
| 111 | + finally: |
| 112 | + if original is not None: |
| 113 | + os.environ["HOME"] = original |
| 114 | + |
| 115 | + |
| 116 | +class FinalizeCapturePropmptsEnabledTests(unittest.TestCase): |
| 117 | + """Tests for _capture_prompts_enabled() in finalize-ledger.py.""" |
| 118 | + |
| 119 | + @classmethod |
| 120 | + def setUpClass(cls): |
| 121 | + cls.mod = load_module(FINALIZE_LEDGER, "finalize_ledger") |
| 122 | + |
| 123 | + def test_returns_true_when_config_missing(self): |
| 124 | + original = os.environ.get("HOME") |
| 125 | + try: |
| 126 | + with tempfile.TemporaryDirectory() as tmp: |
| 127 | + os.environ["HOME"] = tmp |
| 128 | + result = self.mod._capture_prompts_enabled() |
| 129 | + self.assertTrue(result) |
| 130 | + finally: |
| 131 | + if original is not None: |
| 132 | + os.environ["HOME"] = original |
| 133 | + |
| 134 | + def test_returns_false_when_disabled(self): |
| 135 | + with tempfile.TemporaryDirectory() as tmp: |
| 136 | + agentdiff_dir = os.path.join(tmp, ".agentdiff") |
| 137 | + os.makedirs(agentdiff_dir) |
| 138 | + with open(os.path.join(agentdiff_dir, "config.toml"), "w") as f: |
| 139 | + f.write("capture_prompts = false\n") |
| 140 | + original = os.environ.get("HOME") |
| 141 | + try: |
| 142 | + os.environ["HOME"] = tmp |
| 143 | + result = self.mod._capture_prompts_enabled() |
| 144 | + self.assertFalse(result) |
| 145 | + finally: |
| 146 | + if original is not None: |
| 147 | + os.environ["HOME"] = original |
| 148 | + |
| 149 | + def test_returns_true_when_enabled(self): |
| 150 | + with tempfile.TemporaryDirectory() as tmp: |
| 151 | + agentdiff_dir = os.path.join(tmp, ".agentdiff") |
| 152 | + os.makedirs(agentdiff_dir) |
| 153 | + with open(os.path.join(agentdiff_dir, "config.toml"), "w") as f: |
| 154 | + f.write("capture_prompts = true\n") |
| 155 | + original = os.environ.get("HOME") |
| 156 | + try: |
| 157 | + os.environ["HOME"] = tmp |
| 158 | + result = self.mod._capture_prompts_enabled() |
| 159 | + self.assertTrue(result) |
| 160 | + finally: |
| 161 | + if original is not None: |
| 162 | + os.environ["HOME"] = original |
| 163 | + |
| 164 | + |
| 165 | +if __name__ == "__main__": |
| 166 | + unittest.main() |
0 commit comments