|
| 1 | +"""Unit tests for utils.session_cache.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import shutil |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from utils.jsonl_parser import parse_session |
| 12 | +from utils.session_cache import clear_cache, get_cached_session, set_max_entries |
| 13 | + |
| 14 | +FIXTURES = Path(__file__).resolve().parent / "fixtures" |
| 15 | +SAMPLE_SESSION = FIXTURES / "session_with_tools.jsonl" |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def sample_session(tmp_path: Path) -> Path: |
| 20 | + dest = tmp_path / "session.jsonl" |
| 21 | + shutil.copy(SAMPLE_SESSION, dest) |
| 22 | + return dest |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(autouse=True) |
| 26 | +def _reset_cache() -> None: |
| 27 | + clear_cache() |
| 28 | + set_max_entries(200) |
| 29 | + |
| 30 | + |
| 31 | +def test_cache_returns_same_data_as_direct_parse(sample_session: Path) -> None: |
| 32 | + path = str(sample_session) |
| 33 | + assert get_cached_session(path) == parse_session(path) |
| 34 | + |
| 35 | + |
| 36 | +def test_cache_hit_avoids_reparse(sample_session: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 37 | + path = str(sample_session) |
| 38 | + get_cached_session(path) |
| 39 | + calls = 0 |
| 40 | + |
| 41 | + def counting_parse(p: str): |
| 42 | + nonlocal calls |
| 43 | + calls += 1 |
| 44 | + return parse_session(p) |
| 45 | + |
| 46 | + monkeypatch.setattr("utils.session_cache.parse_session", counting_parse) |
| 47 | + get_cached_session(path) |
| 48 | + assert calls == 0 |
| 49 | + |
| 50 | + |
| 51 | +def test_cache_invalidates_on_mtime_change( |
| 52 | + sample_session: Path, monkeypatch: pytest.MonkeyPatch |
| 53 | +) -> None: |
| 54 | + path = str(sample_session) |
| 55 | + get_cached_session(path) |
| 56 | + |
| 57 | + calls = 0 |
| 58 | + |
| 59 | + def counting_parse(p: str): |
| 60 | + nonlocal calls |
| 61 | + calls += 1 |
| 62 | + return parse_session(p) |
| 63 | + |
| 64 | + monkeypatch.setattr("utils.session_cache.parse_session", counting_parse) |
| 65 | + |
| 66 | + stat = sample_session.stat() |
| 67 | + os.utime(sample_session, (stat.st_mtime + 1, stat.st_mtime + 1)) |
| 68 | + get_cached_session(path) |
| 69 | + assert calls == 1 |
| 70 | + |
| 71 | + |
| 72 | +def test_cache_normalizes_relative_and_absolute_paths( |
| 73 | + sample_session: Path, |
| 74 | + tmp_path: Path, |
| 75 | + monkeypatch: pytest.MonkeyPatch, |
| 76 | +) -> None: |
| 77 | + calls = 0 |
| 78 | + |
| 79 | + def counting_parse(p: str): |
| 80 | + nonlocal calls |
| 81 | + calls += 1 |
| 82 | + return parse_session(p) |
| 83 | + |
| 84 | + monkeypatch.setattr("utils.session_cache.parse_session", counting_parse) |
| 85 | + rel = "session.jsonl" |
| 86 | + abs_path = str(sample_session.resolve()) |
| 87 | + original_cwd = os.getcwd() |
| 88 | + try: |
| 89 | + os.chdir(tmp_path) |
| 90 | + get_cached_session(rel) |
| 91 | + assert calls == 1 |
| 92 | + get_cached_session(abs_path) |
| 93 | + assert calls == 1 |
| 94 | + finally: |
| 95 | + os.chdir(original_cwd) |
| 96 | + |
| 97 | + |
| 98 | +def test_lru_eviction( |
| 99 | + sample_session: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 100 | +) -> None: |
| 101 | + set_max_entries(2) |
| 102 | + content = sample_session.read_text(encoding="utf-8") |
| 103 | + paths = [] |
| 104 | + for name in ("a.jsonl", "b.jsonl", "c.jsonl"): |
| 105 | + p = tmp_path / name |
| 106 | + p.write_text(content, encoding="utf-8") |
| 107 | + paths.append(p) |
| 108 | + |
| 109 | + for p in paths: |
| 110 | + get_cached_session(str(p)) |
| 111 | + |
| 112 | + calls = 0 |
| 113 | + |
| 114 | + def counting_parse(p: str): |
| 115 | + nonlocal calls |
| 116 | + calls += 1 |
| 117 | + return parse_session(p) |
| 118 | + |
| 119 | + monkeypatch.setattr("utils.session_cache.parse_session", counting_parse) |
| 120 | + get_cached_session(str(paths[2])) |
| 121 | + assert calls == 0 |
| 122 | + get_cached_session(str(paths[1])) |
| 123 | + assert calls == 0 |
| 124 | + get_cached_session(str(paths[0])) |
| 125 | + assert calls == 1 |
| 126 | + |
| 127 | + |
| 128 | +def test_set_max_entries_rejects_negative() -> None: |
| 129 | + with pytest.raises(ValueError, match="non-negative"): |
| 130 | + set_max_entries(-1) |
| 131 | + |
| 132 | + |
| 133 | +def test_returns_parsed_when_mtime_after_parse_raises( |
| 134 | + sample_session: Path, monkeypatch: pytest.MonkeyPatch |
| 135 | +) -> None: |
| 136 | + path = str(sample_session) |
| 137 | + real_getmtime = os.path.getmtime |
| 138 | + calls = 0 |
| 139 | + |
| 140 | + def getmtime_side_effect(p: str) -> float: |
| 141 | + nonlocal calls |
| 142 | + calls += 1 |
| 143 | + if calls == 2: |
| 144 | + raise OSError("file removed after parse") |
| 145 | + return real_getmtime(p) |
| 146 | + |
| 147 | + monkeypatch.setattr(os.path, "getmtime", getmtime_side_effect) |
| 148 | + result = get_cached_session(path) |
| 149 | + assert result == parse_session(path) |
0 commit comments