| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import pickle |
| 5 |
import time |
| 6 |
from pathlib import Path |
| 7 |
|
| 8 |
import pytest |
| 9 |
from textual.widgets.text_area import Selection |
| 10 |
|
| 11 |
from harlequin.editor_cache import ( |
| 12 |
RECOVERY_STALE_SECONDS, |
| 13 |
BufferState, |
| 14 |
Cache, |
| 15 |
adopt_recovery, |
| 16 |
clear_recovery, |
| 17 |
get_cache_file, |
| 18 |
get_recovery_file, |
| 19 |
load_cache, |
| 20 |
write_cache, |
| 21 |
write_recovery, |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
@pytest.fixture |
| 26 |
def cache() -> Cache: |
| 27 |
return Cache( |
| 28 |
focus_index=0, |
| 29 |
buffers=[BufferState(selection=Selection((0, 3), (0, 3)), text="select 1\n")], |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
@pytest.fixture(autouse=True) |
| 34 |
def mock_user_cache_dir(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path: |
| 35 |
cache_dir = tmp_path / "cache" |
| 36 |
monkeypatch.setattr("harlequin.editor_cache.user_cache_dir", lambda **_: cache_dir) |
| 37 |
return cache_dir |
| 38 |
|
| 39 |
|
| 40 |
@pytest.fixture |
| 41 |
def failing_dump(monkeypatch: pytest.MonkeyPatch) -> None: |
| 42 |
def _dump(*_args: object, **_kwargs: object) -> None: |
| 43 |
raise pickle.PicklingError("no") |
| 44 |
|
| 45 |
monkeypatch.setattr("harlequin.editor_cache.pickle.dump", _dump) |
| 46 |
|
| 47 |
|
| 48 |
def test_write_cache_round_trips(mock_user_cache_dir: Path, cache: Cache) -> None: |
| 49 |
assert write_cache(cache) is True |
| 50 |
assert load_cache() == cache |
| 51 |
|
| 52 |
|
| 53 |
def test_write_cache_leaves_no_temp_file( |
| 54 |
mock_user_cache_dir: Path, cache: Cache |
| 55 |
) -> None: |
| 56 |
assert write_cache(cache) is True |
| 57 |
assert [path.name for path in mock_user_cache_dir.iterdir()] == [ |
| 58 |
get_cache_file().name |
| 59 |
] |
| 60 |
|
| 61 |
|
| 62 |
@pytest.mark.usefixtures("failing_dump") |
| 63 |
def test_a_failed_write_leaves_the_last_cache_alone( |
| 64 |
mock_user_cache_dir: Path, cache: Cache |
| 65 |
) -> None: |
| 66 |
"""The point of the temp file: a write that dies cannot destroy what is there.""" |
| 67 |
good_cache = get_cache_file() |
| 68 |
good_cache.parent.mkdir(parents=True, exist_ok=True) |
| 69 |
good_cache.write_bytes(pickle.dumps(cache)) |
| 70 |
before = good_cache.read_bytes() |
| 71 |
|
| 72 |
assert write_cache(Cache(focus_index=0, buffers=[])) is False |
| 73 |
|
| 74 |
assert good_cache.read_bytes() == before |
| 75 |
assert [path.name for path in mock_user_cache_dir.iterdir()] == [good_cache.name] |
| 76 |
|
| 77 |
|
| 78 |
def test_write_cache_returns_false_when_the_cache_dir_is_unwritable( |
| 79 |
mock_user_cache_dir: Path, cache: Cache |
| 80 |
) -> None: |
| 81 |
mock_user_cache_dir.parent.mkdir(parents=True, exist_ok=True) |
| 82 |
mock_user_cache_dir.write_text("not a directory") |
| 83 |
assert write_cache(cache) is False |
| 84 |
|
| 85 |
|
| 86 |
def test_write_recovery_does_not_touch_the_shared_cache( |
| 87 |
mock_user_cache_dir: Path, cache: Cache |
| 88 |
) -> None: |
| 89 |
assert write_cache(cache) is True |
| 90 |
before = get_cache_file().read_bytes() |
| 91 |
|
| 92 |
assert write_recovery(Cache(focus_index=0, buffers=[])) is True |
| 93 |
|
| 94 |
assert get_recovery_file().exists() |
| 95 |
assert get_recovery_file() != get_cache_file() |
| 96 |
assert get_cache_file().read_bytes() == before |
| 97 |
|
| 98 |
clear_recovery() |
| 99 |
assert not get_recovery_file().exists() |
| 100 |
assert get_cache_file().read_bytes() == before |
| 101 |
|
| 102 |
|
| 103 |
def test_clear_recovery_when_there_is_nothing_to_clear( |
| 104 |
mock_user_cache_dir: Path, |
| 105 |
) -> None: |
| 106 |
clear_recovery() |
| 107 |
|
| 108 |
|
| 109 |
def test_adopt_recovery_finds_nothing(mock_user_cache_dir: Path, cache: Cache) -> None: |
| 110 |
assert write_cache(cache) is True |
| 111 |
assert adopt_recovery() == (None, None) |
| 112 |
|
| 113 |
|
| 114 |
def test_adopt_recovery_replays_a_crash_handlers_file( |
| 115 |
mock_user_cache_dir: Path, cache: Cache |
| 116 |
) -> None: |
| 117 |
planted = _plant(mock_user_cache_dir, "recovered-20260904T120000Z-99.pickle", cache) |
| 118 |
|
| 119 |
recovered, replayed = adopt_recovery() |
| 120 |
|
| 121 |
assert recovered == cache |
| 122 |
assert replayed == planted.with_suffix(".replayed") |
| 123 |
assert not planted.exists() |
| 124 |
|
| 125 |
|
| 126 |
def test_adopt_recovery_replays_each_file_once( |
| 127 |
mock_user_cache_dir: Path, cache: Cache |
| 128 |
) -> None: |
| 129 |
"""The crash-loop guard: a file is renamed before it is unpickled.""" |
| 130 |
_plant(mock_user_cache_dir, "recovered-20260904T120000Z-99.pickle", cache) |
| 131 |
|
| 132 |
assert adopt_recovery()[0] == cache |
| 133 |
assert adopt_recovery() == (None, None) |
| 134 |
|
| 135 |
|
| 136 |
def test_adopt_recovery_consumes_a_file_it_cannot_unpickle( |
| 137 |
mock_user_cache_dir: Path, |
| 138 |
) -> None: |
| 139 |
"""A file that poisons the replay is spent by the attempt, not left to repeat.""" |
| 140 |
planted = mock_user_cache_dir / "recovered-20260904T120000Z-99.pickle" |
| 141 |
planted.parent.mkdir(parents=True, exist_ok=True) |
| 142 |
planted.write_bytes(b"not a pickle") |
| 143 |
|
| 144 |
assert adopt_recovery() == (None, None) |
| 145 |
|
| 146 |
assert not planted.exists() |
| 147 |
assert planted.with_suffix(".replayed").exists() |
| 148 |
|
| 149 |
|
| 150 |
def test_adopt_recovery_leaves_a_live_sessions_file_alone( |
| 151 |
mock_user_cache_dir: Path, cache: Cache |
| 152 |
) -> None: |
| 153 |
_plant(mock_user_cache_dir, "recovery-99.pickle", cache) |
| 154 |
|
| 155 |
assert adopt_recovery() == (None, None) |
| 156 |
|
| 157 |
assert (mock_user_cache_dir / "recovery-99.pickle").exists() |
| 158 |
|
| 159 |
|
| 160 |
def test_adopt_recovery_takes_over_a_stale_file( |
| 161 |
mock_user_cache_dir: Path, cache: Cache |
| 162 |
) -> None: |
| 163 |
planted = _plant( |
| 164 |
mock_user_cache_dir, |
| 165 |
"recovery-99.pickle", |
| 166 |
cache, |
| 167 |
age_seconds=RECOVERY_STALE_SECONDS + 1, |
| 168 |
) |
| 169 |
|
| 170 |
recovered, replayed = adopt_recovery() |
| 171 |
|
| 172 |
assert recovered == cache |
| 173 |
assert replayed == planted.with_suffix(".replayed") |
| 174 |
|
| 175 |
|
| 176 |
def test_adopt_recovery_prefers_the_newest_crash( |
| 177 |
mock_user_cache_dir: Path, cache: Cache |
| 178 |
) -> None: |
| 179 |
newest = Cache(focus_index=0, buffers=[BufferState(Selection(), "select 2\n")]) |
| 180 |
_plant( |
| 181 |
mock_user_cache_dir, |
| 182 |
"recovered-20260904T120000Z-98.pickle", |
| 183 |
cache, |
| 184 |
age_seconds=60, |
| 185 |
) |
| 186 |
_plant(mock_user_cache_dir, "recovered-20260904T130000Z-99.pickle", newest) |
| 187 |
_plant( |
| 188 |
mock_user_cache_dir, |
| 189 |
"recovery-97.pickle", |
| 190 |
cache, |
| 191 |
age_seconds=RECOVERY_STALE_SECONDS + 1, |
| 192 |
) |
| 193 |
|
| 194 |
assert adopt_recovery()[0] == newest |
| 195 |
|
| 196 |
|
| 197 |
def _plant(cache_dir: Path, name: str, cache: Cache, age_seconds: float = 0.0) -> Path: |
| 198 |
"""Write a recovery file as another process would have left it.""" |
| 199 |
cache_dir.mkdir(parents=True, exist_ok=True) |
| 200 |
path = cache_dir / name |
| 201 |
path.write_bytes(pickle.dumps(cache)) |
| 202 |
if age_seconds: |
| 203 |
mtime = time.time() - age_seconds |
| 204 |
os.utime(path, (mtime, mtime)) |
| 205 |
return path |