|
| 1 | +"""Unit tests for utils.session_summary_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, quick_session_info |
| 12 | +from utils.session_summary_cache import ( |
| 13 | + clear_cache, |
| 14 | + get_summary, |
| 15 | + put_summary, |
| 16 | + reset_connection_for_tests, |
| 17 | + rules_fingerprint, |
| 18 | + summary_from_peek, |
| 19 | + summary_from_session, |
| 20 | +) |
| 21 | + |
| 22 | +FIXTURES = Path(__file__).resolve().parent / "fixtures" |
| 23 | +SAMPLE_SESSION = FIXTURES / "session_with_tools.jsonl" |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def sample_session(tmp_path: Path) -> Path: |
| 28 | + dest = tmp_path / "session.jsonl" |
| 29 | + shutil.copy(SAMPLE_SESSION, dest) |
| 30 | + return dest |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def cache_db(tmp_path: Path) -> Path: |
| 35 | + db = tmp_path / "summary.sqlite" |
| 36 | + reset_connection_for_tests(db) |
| 37 | + yield db |
| 38 | + clear_cache() |
| 39 | + |
| 40 | + |
| 41 | +def test_rules_fingerprint_empty() -> None: |
| 42 | + assert rules_fingerprint([]) == "none" |
| 43 | + |
| 44 | + |
| 45 | +def test_rules_fingerprint_stable() -> None: |
| 46 | + rules = [[("word", "secret")]] |
| 47 | + assert rules_fingerprint(rules) == rules_fingerprint(rules) |
| 48 | + |
| 49 | + |
| 50 | +def test_cache_miss_returns_none(sample_session: Path, cache_db: Path) -> None: |
| 51 | + path = str(sample_session) |
| 52 | + mtime = sample_session.stat().st_mtime |
| 53 | + assert get_summary(path, mtime, "none") is None |
| 54 | + |
| 55 | + |
| 56 | +def test_cache_hit_round_trip(sample_session: Path, cache_db: Path) -> None: |
| 57 | + path = str(sample_session) |
| 58 | + mtime = sample_session.stat().st_mtime |
| 59 | + parsed = parse_session(path) |
| 60 | + row = summary_from_session(parsed, is_excluded=False) |
| 61 | + put_summary(path, mtime, "none", row) |
| 62 | + hit = get_summary(path, mtime, "none") |
| 63 | + assert hit is not None |
| 64 | + assert hit["title"] == row["title"] |
| 65 | + assert hit["tokens"] == row["tokens"] |
| 66 | + assert hit["is_complete"] is True |
| 67 | + |
| 68 | + |
| 69 | +def test_cache_invalidates_on_mtime_change(sample_session: Path, cache_db: Path) -> None: |
| 70 | + path = str(sample_session) |
| 71 | + mtime = sample_session.stat().st_mtime |
| 72 | + parsed = parse_session(path) |
| 73 | + put_summary(path, mtime, "none", summary_from_session(parsed, is_excluded=False)) |
| 74 | + stat = sample_session.stat() |
| 75 | + os.utime(sample_session, (stat.st_mtime + 10, stat.st_mtime + 10)) |
| 76 | + new_mtime = sample_session.stat().st_mtime |
| 77 | + assert new_mtime != mtime |
| 78 | + assert get_summary(path, new_mtime, "none") is None |
| 79 | + |
| 80 | + |
| 81 | +def test_exclusion_key_separation(sample_session: Path, cache_db: Path) -> None: |
| 82 | + path = str(sample_session) |
| 83 | + mtime = sample_session.stat().st_mtime |
| 84 | + parsed = parse_session(path) |
| 85 | + put_summary(path, mtime, "none", summary_from_session(parsed, is_excluded=False)) |
| 86 | + put_summary(path, mtime, "rules_a", summary_from_session(parsed, is_excluded=True)) |
| 87 | + hit_none = get_summary(path, mtime, "none") |
| 88 | + hit_rules = get_summary(path, mtime, "rules_a") |
| 89 | + assert hit_none is not None and hit_none["is_excluded"] is False |
| 90 | + assert hit_rules is not None and hit_rules["is_excluded"] is True |
| 91 | + |
| 92 | + |
| 93 | +def test_peek_partial_row(sample_session: Path, cache_db: Path) -> None: |
| 94 | + path = str(sample_session) |
| 95 | + mtime = sample_session.stat().st_mtime |
| 96 | + row = summary_from_peek(quick_session_info(path)) |
| 97 | + put_summary(path, mtime, "none", row) |
| 98 | + hit = get_summary(path, mtime, "none") |
| 99 | + assert hit is not None |
| 100 | + assert hit["is_complete"] is False |
| 101 | + assert hit["tokens"] == 0 |
| 102 | + |
| 103 | + |
| 104 | +def test_lru_eviction( |
| 105 | + sample_session: Path, cache_db: Path, monkeypatch: pytest.MonkeyPatch |
| 106 | +) -> None: |
| 107 | + monkeypatch.setattr("utils.session_summary_cache.DEFAULT_MAX_ROWS", 2) |
| 108 | + content = sample_session.read_text(encoding="utf-8") |
| 109 | + paths = [] |
| 110 | + for name in ("a.jsonl", "b.jsonl", "c.jsonl"): |
| 111 | + p = sample_session.parent / name |
| 112 | + p.write_text(content, encoding="utf-8") |
| 113 | + paths.append(p) |
| 114 | + |
| 115 | + for p in paths[:2]: |
| 116 | + mtime = p.stat().st_mtime |
| 117 | + put_summary( |
| 118 | + str(p), |
| 119 | + mtime, |
| 120 | + "none", |
| 121 | + summary_from_peek(quick_session_info(str(p))), |
| 122 | + ) |
| 123 | + get_summary(str(p), mtime, "none") |
| 124 | + |
| 125 | + third = paths[2] |
| 126 | + third_mtime = third.stat().st_mtime |
| 127 | + put_summary( |
| 128 | + str(third), |
| 129 | + third_mtime, |
| 130 | + "none", |
| 131 | + summary_from_peek(quick_session_info(str(third))), |
| 132 | + ) |
| 133 | + |
| 134 | + first = paths[0] |
| 135 | + assert get_summary(str(first), first.stat().st_mtime, "none") is None |
| 136 | + assert get_summary(str(third), third_mtime, "none") is not None |
| 137 | + |
| 138 | + |
| 139 | +def test_clear_cache(sample_session: Path, cache_db: Path) -> None: |
| 140 | + path = str(sample_session) |
| 141 | + mtime = sample_session.stat().st_mtime |
| 142 | + parsed = parse_session(path) |
| 143 | + put_summary(path, mtime, "none", summary_from_session(parsed, is_excluded=False)) |
| 144 | + clear_cache() |
| 145 | + reset_connection_for_tests(cache_db) |
| 146 | + assert get_summary(path, mtime, "none") is None |
0 commit comments