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