|
| 1 | +"""Unit tests for utils.session_cache.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import shutil |
| 6 | +import time |
| 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(sample_session: Path) -> None: |
| 52 | + path = str(sample_session) |
| 53 | + first = get_cached_session(path) |
| 54 | + time.sleep(0.05) |
| 55 | + sample_session.touch() |
| 56 | + second = get_cached_session(path) |
| 57 | + assert first is not second |
| 58 | + |
| 59 | + |
| 60 | +def test_lru_eviction(sample_session: Path, tmp_path: Path) -> None: |
| 61 | + set_max_entries(2) |
| 62 | + content = sample_session.read_text(encoding="utf-8") |
| 63 | + paths = [] |
| 64 | + for name in ("a.jsonl", "b.jsonl", "c.jsonl"): |
| 65 | + p = tmp_path / name |
| 66 | + p.write_text(content, encoding="utf-8") |
| 67 | + paths.append(p) |
| 68 | + |
| 69 | + for p in paths: |
| 70 | + get_cached_session(str(p)) |
| 71 | + |
| 72 | + calls = 0 |
| 73 | + |
| 74 | + def counting_parse(p: str): |
| 75 | + nonlocal calls |
| 76 | + calls += 1 |
| 77 | + return parse_session(p) |
| 78 | + |
| 79 | + monkeypatch = pytest.MonkeyPatch() |
| 80 | + monkeypatch.setattr("utils.session_cache.parse_session", counting_parse) |
| 81 | + try: |
| 82 | + get_cached_session(str(paths[2])) |
| 83 | + assert calls == 0 |
| 84 | + get_cached_session(str(paths[1])) |
| 85 | + assert calls == 0 |
| 86 | + get_cached_session(str(paths[0])) |
| 87 | + assert calls == 1 |
| 88 | + finally: |
| 89 | + monkeypatch.undo() |
0 commit comments