|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import os |
5 | 6 | import shutil |
6 | | -import time |
7 | 7 | from pathlib import Path |
8 | 8 |
|
9 | 9 | import pytest |
@@ -51,12 +51,38 @@ def counting_parse(p: str): |
51 | 51 | def test_cache_invalidates_on_mtime_change(sample_session: Path) -> None: |
52 | 52 | path = str(sample_session) |
53 | 53 | first = get_cached_session(path) |
54 | | - time.sleep(0.05) |
55 | | - sample_session.touch() |
| 54 | + stat = sample_session.stat() |
| 55 | + os.utime(sample_session, (stat.st_mtime + 1, stat.st_mtime + 1)) |
56 | 56 | second = get_cached_session(path) |
57 | 57 | assert first is not second |
58 | 58 |
|
59 | 59 |
|
| 60 | +def test_cache_normalizes_relative_and_absolute_paths( |
| 61 | + sample_session: Path, |
| 62 | + tmp_path: Path, |
| 63 | + monkeypatch: pytest.MonkeyPatch, |
| 64 | +) -> None: |
| 65 | + calls = 0 |
| 66 | + |
| 67 | + def counting_parse(p: str): |
| 68 | + nonlocal calls |
| 69 | + calls += 1 |
| 70 | + return parse_session(p) |
| 71 | + |
| 72 | + monkeypatch.setattr("utils.session_cache.parse_session", counting_parse) |
| 73 | + rel = "session.jsonl" |
| 74 | + abs_path = str(sample_session.resolve()) |
| 75 | + original_cwd = os.getcwd() |
| 76 | + try: |
| 77 | + os.chdir(tmp_path) |
| 78 | + get_cached_session(rel) |
| 79 | + assert calls == 1 |
| 80 | + get_cached_session(abs_path) |
| 81 | + assert calls == 1 |
| 82 | + finally: |
| 83 | + os.chdir(original_cwd) |
| 84 | + |
| 85 | + |
60 | 86 | def test_lru_eviction(sample_session: Path, tmp_path: Path) -> None: |
61 | 87 | set_max_entries(2) |
62 | 88 | content = sample_session.read_text(encoding="utf-8") |
@@ -87,3 +113,8 @@ def counting_parse(p: str): |
87 | 113 | assert calls == 1 |
88 | 114 | finally: |
89 | 115 | monkeypatch.undo() |
| 116 | + |
| 117 | + |
| 118 | +def test_set_max_entries_rejects_negative() -> None: |
| 119 | + with pytest.raises(ValueError, match="non-negative"): |
| 120 | + set_max_entries(-1) |
0 commit comments