| 1 |
import pickle |
| 2 |
from pathlib import Path |
| 3 |
from typing import List |
| 4 |
|
| 5 |
import pytest |
| 6 |
from textual.widgets.text_area import Selection |
| 7 |
|
| 8 |
from harlequin import Harlequin |
| 9 |
from harlequin.editor_cache import ( |
| 10 |
BufferState, |
| 11 |
Cache, |
| 12 |
get_cache_file, |
| 13 |
load_cache, |
| 14 |
write_cache, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture |
| 19 |
def buffer_states() -> List[BufferState]: |
| 20 |
return [ |
| 21 |
BufferState( |
| 22 |
selection=Selection((0, 3), (0, 3)), |
| 23 |
text="select 1\n", |
| 24 |
), |
| 25 |
BufferState( |
| 26 |
selection=Selection((0, 0), (0, 0)), |
| 27 |
text="", |
| 28 |
), |
| 29 |
BufferState( |
| 30 |
selection=Selection((0, 0), (1, 0)), |
| 31 |
text="select\n*\nfrom\nfoo\n", |
| 32 |
), |
| 33 |
] |
| 34 |
|
| 35 |
|
| 36 |
@pytest.fixture |
| 37 |
def cache(buffer_states: List[BufferState]) -> Cache: |
| 38 |
return Cache(focus_index=1, buffers=buffer_states) |
| 39 |
|
| 40 |
|
| 41 |
@pytest.fixture(autouse=True) |
| 42 |
def mock_user_cache_dir(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path: |
| 43 |
monkeypatch.setattr("harlequin.editor_cache.user_cache_dir", lambda **_: tmp_path) |
| 44 |
return tmp_path |
| 45 |
|
| 46 |
|
| 47 |
@pytest.mark.use_cache |
| 48 |
def test_cache_ops(mock_user_cache_dir: Path, cache: Cache) -> None: |
| 49 |
assert mock_user_cache_dir.exists() |
| 50 |
assert len(list(mock_user_cache_dir.iterdir())) == 0 |
| 51 |
write_cache(cache) |
| 52 |
children = list(mock_user_cache_dir.iterdir()) |
| 53 |
assert len(children) == 1 |
| 54 |
assert children[0].suffix == ".pickle" |
| 55 |
assert get_cache_file() == children[0] |
| 56 |
loaded_cache = load_cache() |
| 57 |
assert loaded_cache == cache |
| 58 |
|
| 59 |
|
| 60 |
@pytest.mark.use_cache |
| 61 |
@pytest.mark.asyncio |
| 62 |
async def test_harlequin_loads_cache(cache: Cache, app: Harlequin) -> None: |
| 63 |
write_cache(cache) |
| 64 |
async with app.run_test() as pilot: |
| 65 |
while app.editor is None: |
| 66 |
await pilot.pause() |
| 67 |
assert app.editor_collection is not None |
| 68 |
assert app.editor is not None |
| 69 |
assert app.editor_collection.tab_count == len(cache.buffers) |
| 70 |
assert [buffer.text for buffer in app.editor_collection.buffers] == [ |
| 71 |
buffer.text for buffer in cache.buffers |
| 72 |
] |
| 73 |
# the buffer that was active when the cache was written is active again |
| 74 |
assert app.editor_collection.active_buffer_index == cache.focus_index |
| 75 |
assert app.editor.text == cache.buffers[cache.focus_index].text |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.use_cache |
| 79 |
@pytest.mark.asyncio |
| 80 |
async def test_harlequin_writes_cache(app: Harlequin) -> None: |
| 81 |
cache_path = get_cache_file() |
| 82 |
assert not cache_path.exists() |
| 83 |
async with app.run_test() as pilot: |
| 84 |
while app.editor is None: |
| 85 |
await pilot.pause() |
| 86 |
assert app.editor_collection is not None |
| 87 |
assert app.editor_collection.tab_count == 1 |
| 88 |
app.editor.text = "first" |
| 89 |
await pilot.press("ctrl+n") |
| 90 |
await pilot.pause() |
| 91 |
app.editor.text = "second" |
| 92 |
await pilot.press("ctrl+q") |
| 93 |
assert cache_path.exists() |
| 94 |
with open(cache_path, "rb") as f: |
| 95 |
cache = pickle.load(f) |
| 96 |
assert isinstance(cache, Cache) |
| 97 |
assert [buffer.text for buffer in cache.buffers] == ["first", "second"] |
| 98 |
assert cache.focus_index == 1 |