|
| 1 | +"""The post-commit cooldown file: its encoding, its window and its pruning (#692). |
| 2 | +
|
| 3 | +``gated_files.write_json`` encodes the cooldown JSON as UTF-8, and the hook read it |
| 4 | +back with a bare ``read_text()``, which uses the locale code page (cp1252 on |
| 5 | +Windows). ``-X warn_default_encoding`` turns each default-encoding file open into an |
| 6 | +``EncodingWarning``, made an error in the subprocess test, so that proof holds on |
| 7 | +any platform. The in-process tests pin the rest of ``_check_cooldown`` and |
| 8 | +``_update_cooldown``, which the diff-scoped mutation gate (ADR-1122) mutates whole. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import json |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from mcp_server.hooks import post_commit_reindex as hook |
| 21 | + |
| 22 | +ROOT = Path(__file__).resolve().parents[2] |
| 23 | +REPO = "C:/Users/Clément/projets/日本語" |
| 24 | +CHILD = f""" |
| 25 | +import sys |
| 26 | +from pathlib import Path |
| 27 | +from mcp_server.hooks import post_commit_reindex as hook |
| 28 | +
|
| 29 | +hook._COOLDOWN_FILE = Path(sys.argv[1]) / "cooldown.json" |
| 30 | +hook._update_cooldown({REPO!r}) |
| 31 | +hook._update_cooldown("/second/repo") |
| 32 | +assert hook._check_cooldown({REPO!r}), "the cooldown entry was not read back" |
| 33 | +""" |
| 34 | +# source: CORTEX_REINDEX_COOLDOWN_SECONDS, set by each test to a round window. |
| 35 | +WINDOW = 10 |
| 36 | + |
| 37 | + |
| 38 | +class _RecordingPath(type(Path())): |
| 39 | + """A real path that records the encoding of every ``read_text`` call.""" |
| 40 | + |
| 41 | + encodings: list[str | None] = [] |
| 42 | + |
| 43 | + def read_text(self, encoding=None, errors=None, **kwargs): |
| 44 | + type(self).encodings.append(encoding) |
| 45 | + return super().read_text(encoding=encoding, errors=errors, **kwargs) |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def cooldown(tmp_path, monkeypatch): |
| 50 | + path = _RecordingPath(tmp_path / "cooldown.json") |
| 51 | + _RecordingPath.encodings = [] |
| 52 | + monkeypatch.setattr(hook, "_COOLDOWN_FILE", path) |
| 53 | + monkeypatch.setenv("CORTEX_REINDEX_COOLDOWN_SECONDS", str(WINDOW)) |
| 54 | + return path |
| 55 | + |
| 56 | + |
| 57 | +def _at(monkeypatch, now: float) -> None: |
| 58 | + monkeypatch.setattr(hook.time, "time", lambda: now) |
| 59 | + |
| 60 | + |
| 61 | +def test_cooldown_round_trip_does_no_default_encoding_io(tmp_path: Path) -> None: |
| 62 | + done = subprocess.run( |
| 63 | + [ |
| 64 | + sys.executable, |
| 65 | + "-X", |
| 66 | + "warn_default_encoding", |
| 67 | + "-W", |
| 68 | + "error::EncodingWarning", |
| 69 | + "-c", |
| 70 | + CHILD, |
| 71 | + str(tmp_path), |
| 72 | + ], |
| 73 | + capture_output=True, |
| 74 | + env={"CORTEX_CLAUDE_DIR": str(tmp_path / "claude"), "PYTHONPATH": str(ROOT)}, |
| 75 | + cwd=ROOT, |
| 76 | + ) |
| 77 | + assert done.returncode == 0, done.stderr.decode("utf-8", "backslashreplace") |
| 78 | + stored = (tmp_path / "cooldown.json").read_bytes().decode("utf-8") |
| 79 | + assert "/second/repo" in stored |
| 80 | + |
| 81 | + |
| 82 | +def test_both_reads_name_utf8(cooldown, monkeypatch) -> None: |
| 83 | + _at(monkeypatch, 100.0) |
| 84 | + cooldown.write_text(json.dumps({"/a": 100.0}), encoding="utf-8") |
| 85 | + hook._update_cooldown("/b") |
| 86 | + assert hook._check_cooldown("/a") |
| 87 | + assert _RecordingPath.encodings == ["utf-8", "utf-8"] |
| 88 | + |
| 89 | + |
| 90 | +def test_an_unknown_root_counts_from_the_epoch(cooldown, monkeypatch) -> None: |
| 91 | + cooldown.write_text(json.dumps({"/other": 1.0}), encoding="utf-8") |
| 92 | + _at(monkeypatch, WINDOW - 5) |
| 93 | + assert hook._check_cooldown("/unknown") |
| 94 | + _at(monkeypatch, WINDOW) |
| 95 | + assert not hook._check_cooldown("/unknown") |
| 96 | + |
| 97 | + |
| 98 | +def test_the_window_end_is_outside_the_cooldown(cooldown, monkeypatch) -> None: |
| 99 | + cooldown.write_text(json.dumps({"/a": 50.0}), encoding="utf-8") |
| 100 | + _at(monkeypatch, 50.0 + WINDOW - 1) |
| 101 | + assert hook._check_cooldown("/a") |
| 102 | + _at(monkeypatch, 50.0 + WINDOW) |
| 103 | + assert not hook._check_cooldown("/a") |
| 104 | + |
| 105 | + |
| 106 | +def test_an_update_keeps_the_other_repositories(cooldown, monkeypatch) -> None: |
| 107 | + cooldown.write_text(json.dumps({"/a": 1.0}), encoding="utf-8") |
| 108 | + _at(monkeypatch, 2.0) |
| 109 | + hook._update_cooldown("/b") |
| 110 | + assert json.loads(cooldown.read_text(encoding="utf-8")) == {"/a": 1.0, "/b": 2.0} |
| 111 | + |
| 112 | + |
| 113 | +def _full_history(count: int) -> dict[str, float]: |
| 114 | + # The oldest entry has the lexically largest name, so neither a sort by name |
| 115 | + # nor insertion order agrees with a sort by time. |
| 116 | + return {f"/r{count - 1 - i:02d}": float(i + 1) for i in range(count)} |
| 117 | + |
| 118 | + |
| 119 | +def test_a_full_file_drops_only_the_oldest_entry(cooldown, monkeypatch) -> None: |
| 120 | + cap = hook._MAX_COOLDOWN_ENTRIES |
| 121 | + history = _full_history(cap) |
| 122 | + cooldown.write_text(json.dumps(history), encoding="utf-8") |
| 123 | + _at(monkeypatch, 1000.0) |
| 124 | + hook._update_cooldown("/new") |
| 125 | + stored = json.loads(cooldown.read_text(encoding="utf-8")) |
| 126 | + oldest = min(history, key=history.__getitem__) |
| 127 | + assert len(stored) == cap |
| 128 | + assert "/new" in stored and oldest not in stored |
| 129 | + |
| 130 | + |
| 131 | +def test_a_file_at_the_cap_is_not_pruned(cooldown, monkeypatch) -> None: |
| 132 | + history = _full_history(hook._MAX_COOLDOWN_ENTRIES - 1) |
| 133 | + cooldown.write_text(json.dumps(history), encoding="utf-8") |
| 134 | + _at(monkeypatch, 1000.0) |
| 135 | + hook._update_cooldown("/new") |
| 136 | + stored = json.loads(cooldown.read_text(encoding="utf-8")) |
| 137 | + assert list(stored) == [*history, "/new"] |
0 commit comments