| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from pathlib import Path |
| 4 |
from textwrap import dedent |
| 5 |
from typing import Awaitable, Callable |
| 6 |
|
| 7 |
import pytest |
| 8 |
from textual import events |
| 9 |
|
| 10 |
from harlequin import Harlequin, HarlequinAdapter |
| 11 |
from harlequin.config import load_profile_and_keymaps |
| 12 |
from tests.functional_tests.helpers import wait_for_any_table, wait_for_editor |
| 13 |
|
| 14 |
QUERY = dedent( |
| 15 |
""" |
| 16 |
select * |
| 17 |
from |
| 18 |
( |
| 19 |
values |
| 20 |
(1, 2, 3), |
| 21 |
(4, 5, 6), |
| 22 |
(7, 8, 9), |
| 23 |
(10, 11, 12), |
| 24 |
(13, 14, 15), |
| 25 |
(16, 17, 18), |
| 26 |
(19, 20, 21) |
| 27 |
) foo(a, b, c) |
| 28 |
""" |
| 29 |
).strip() |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.asyncio |
| 33 |
async def test_results_viewer_bindings( |
| 34 |
duckdb_adapter: type[HarlequinAdapter], |
| 35 |
data_dir: Path, |
| 36 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 37 |
) -> None: |
| 38 |
config_path = ( |
| 39 |
data_dir / "functional_tests" / "test_keymap_from_config" / "config.toml" |
| 40 |
) |
| 41 |
profile, my_keymaps = load_profile_and_keymaps( |
| 42 |
config_path=config_path, profile_name=None |
| 43 |
) |
| 44 |
app = Harlequin( |
| 45 |
duckdb_adapter([":memory:"], no_init=True), |
| 46 |
keymap_names=profile["keymap_name"], |
| 47 |
user_defined_keymaps=my_keymaps, |
| 48 |
) |
| 49 |
async with app.run_test() as pilot: |
| 50 |
await wait_for_workers(app) |
| 51 |
editor = await wait_for_editor(pilot, app) |
| 52 |
|
| 53 |
q = QUERY |
| 54 |
editor.text = q |
| 55 |
await pilot.press("ctrl+j") |
| 56 |
|
| 57 |
table = await wait_for_any_table(pilot, app) |
| 58 |
|
| 59 |
assert table is not None |
| 60 |
assert table.cursor_coordinate == (0, 0) |
| 61 |
assert table.selection_anchor_coordinate is None |
| 62 |
|
| 63 |
# simple navigation |
| 64 |
await pilot.press("s") |
| 65 |
assert table.cursor_coordinate == (1, 0) |
| 66 |
assert table.selection_anchor_coordinate is None |
| 67 |
await pilot.press("d") |
| 68 |
assert table.cursor_coordinate == (1, 1) |
| 69 |
assert table.selection_anchor_coordinate is None |
| 70 |
await pilot.press("d") |
| 71 |
assert table.cursor_coordinate == (1, 2) |
| 72 |
assert table.selection_anchor_coordinate is None |
| 73 |
await pilot.press("a") |
| 74 |
assert table.cursor_coordinate == (1, 1) |
| 75 |
assert table.selection_anchor_coordinate is None |
| 76 |
await pilot.press("w") |
| 77 |
assert table.cursor_coordinate == (0, 1) |
| 78 |
assert table.selection_anchor_coordinate is None |
| 79 |
|
| 80 |
|
| 81 |
def press_alt(app: Harlequin, character: str) -> None: |
| 82 |
"""Sends alt+<character> the way a terminal does. |
| 83 |
|
| 84 |
ESC + n parses to the key `alt+n` carrying the character "n", which |
| 85 |
`pilot.press()` does not send and a focused TextArea would insert. |
| 86 |
""" |
| 87 |
key_event = events.Key(f"alt+{character}", character) |
| 88 |
key_event.set_sender(app) |
| 89 |
assert app._driver is not None |
| 90 |
app._driver.send_message(key_event) |
| 91 |
|
| 92 |
|
| 93 |
@pytest.mark.asyncio |
| 94 |
async def test_alt_letter_binding_beats_the_focused_editor( |
| 95 |
duckdb_adapter: type[HarlequinAdapter], |
| 96 |
data_dir: Path, |
| 97 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 98 |
) -> None: |
| 99 |
"""alt+n opens a buffer instead of typing an "n" into the focused one.""" |
| 100 |
config_path = ( |
| 101 |
data_dir / "functional_tests" / "test_keymap_from_config" / "config.toml" |
| 102 |
) |
| 103 |
profile, my_keymaps = load_profile_and_keymaps( |
| 104 |
config_path=config_path, profile_name="alt_keys" |
| 105 |
) |
| 106 |
app = Harlequin( |
| 107 |
duckdb_adapter([":memory:"], no_init=True), |
| 108 |
keymap_names=profile["keymap_name"], |
| 109 |
user_defined_keymaps=my_keymaps, |
| 110 |
) |
| 111 |
async with app.run_test() as pilot: |
| 112 |
await wait_for_workers(app) |
| 113 |
editor = await wait_for_editor(pilot, app) |
| 114 |
|
| 115 |
editor.text = "select 1" |
| 116 |
editor.focus() |
| 117 |
await pilot.press("ctrl+end") |
| 118 |
assert app.editor_collection.tab_count == 1 |
| 119 |
|
| 120 |
press_alt(app, "n") |
| 121 |
await pilot.pause() |
| 122 |
await pilot.wait_for_scheduled_animations() |
| 123 |
|
| 124 |
assert app.editor_collection.tab_count == 2 |
| 125 |
assert editor.text == "" |
| 126 |
|
| 127 |
# the buffer the editor was on did not get an "n" typed into it |
| 128 |
await pilot.press("ctrl+k") |
| 129 |
await pilot.pause() |
| 130 |
await pilot.wait_for_scheduled_animations() |
| 131 |
assert app.editor_collection.active == "tab-1" |
| 132 |
assert editor.text == "select 1" |