| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
from pathlib import Path |
| 5 |
from typing import Awaitable, Callable |
| 6 |
|
| 7 |
import pytest |
| 8 |
from textual.widgets import Input |
| 9 |
|
| 10 |
from harlequin import HarlequinKeys |
| 11 |
from harlequin.keys_app import QuitModal |
| 12 |
from tests.waiting import wait_for, wait_for_value |
| 13 |
|
| 14 |
USER_CONFIG_PATH = Path("/tmp") / "harlequin" |
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture(autouse=True) |
| 18 |
def mock_config_loader(monkeypatch: pytest.MonkeyPatch) -> None: |
| 19 |
monkeypatch.setattr( |
| 20 |
"harlequin.keys_app.load_profile_and_keymaps", lambda **_: (dict(), []) |
| 21 |
) |
| 22 |
monkeypatch.setattr( |
| 23 |
"harlequin.keys_app.get_highest_priority_existing_config_file", lambda **_: None |
| 24 |
) |
| 25 |
monkeypatch.setattr( |
| 26 |
"harlequin.keys_app.user_config_path", lambda **_: USER_CONFIG_PATH |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
@pytest.fixture |
| 31 |
def keys_app() -> HarlequinKeys: |
| 32 |
return HarlequinKeys() |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.asyncio |
| 36 |
async def test_keys_app( |
| 37 |
keys_app: HarlequinKeys, |
| 38 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 39 |
) -> None: |
| 40 |
target_path = USER_CONFIG_PATH / "config.toml" |
| 41 |
target_path.unlink(missing_ok=True) |
| 42 |
app = keys_app |
| 43 |
snap_results: list[bool] = [] |
| 44 |
async with app.run_test(size=(120, 36)) as pilot: |
| 45 |
|
| 46 |
def the_bindings_are_loaded() -> bool: |
| 47 |
return None not in ( |
| 48 |
app.active_keymap_names, |
| 49 |
app.bindings, |
| 50 |
app.unmodifed_bindings, |
| 51 |
app.table, |
| 52 |
) |
| 53 |
|
| 54 |
await wait_for( |
| 55 |
pilot, |
| 56 |
the_bindings_are_loaded, |
| 57 |
description="the keymap editor to load the bindings it edits", |
| 58 |
) |
| 59 |
snap_results.append(await app_snapshot(app, "Initialization")) |
| 60 |
|
| 61 |
await pilot.press("down", "down", "down", "down", "enter") |
| 62 |
await pilot.pause() |
| 63 |
snap_results.append(await app_snapshot(app, "Edit Modal")) |
| 64 |
|
| 65 |
await pilot.press("enter") |
| 66 |
await pilot.pause() |
| 67 |
snap_results.append(await app_snapshot(app, "Enter Key Modal")) |
| 68 |
|
| 69 |
await pilot.press("f3") |
| 70 |
await pilot.wait_for_scheduled_animations() |
| 71 |
snap_results.append(await app_snapshot(app, "Edit Modal: f3")) |
| 72 |
|
| 73 |
await pilot.press("tab", "tab", "enter", "f4") |
| 74 |
await pilot.wait_for_scheduled_animations() |
| 75 |
snap_results.append(await app_snapshot(app, "Edit Modal: f3 and f4")) |
| 76 |
|
| 77 |
await pilot.press("shift+tab", "enter") |
| 78 |
await pilot.pause() |
| 79 |
await pilot.wait_for_animation() |
| 80 |
snap_results.append(await app_snapshot(app, "Edit Modal: f4 removed")) |
| 81 |
|
| 82 |
await pilot.press("tab", "tab", "tab", "tab", "enter") |
| 83 |
await pilot.pause() |
| 84 |
await pilot.wait_for_animation() |
| 85 |
snap_results.append(await app_snapshot(app, "Main modal, Focus F3")) |
| 86 |
|
| 87 |
await pilot.press("ctrl+q") |
| 88 |
# the quit modal should now be visible. |
| 89 |
quit_modal = await wait_for_value( |
| 90 |
pilot, |
| 91 |
lambda: app.screen if isinstance(app.screen, QuitModal) else None, |
| 92 |
description="the Quit modal", |
| 93 |
) |
| 94 |
path_input = quit_modal.query_one( |
| 95 |
"#path_input", |
| 96 |
expect_type=Input, |
| 97 |
) |
| 98 |
assert path_input.value == str(target_path) |
| 99 |
if sys.platform == "win32": |
| 100 |
# on windows the path will use backslashes, which makes |
| 101 |
# the snapshot fail. Use forward slashes instead. |
| 102 |
path_input.value = target_path.as_posix() |
| 103 |
await pilot.wait_for_animation() |
| 104 |
snap_results.append(await app_snapshot(app, "Quit modal")) |
| 105 |
|
| 106 |
await pilot.press("f", "o", "o", "tab", "tab", "tab", "enter") |
| 107 |
assert app.return_code == 0 |
| 108 |
assert target_path.exists() |
| 109 |
|
| 110 |
assert all(snap_results) |