| 1 |
import sys |
| 2 |
from contextlib import suppress |
| 3 |
from typing import Awaitable, Callable |
| 4 |
from unittest.mock import MagicMock |
| 5 |
|
| 6 |
import pytest |
| 7 |
from textual.pilot import Pilot |
| 8 |
from textual.widgets import Input |
| 9 |
from textual.widgets._tree import TreeNode |
| 10 |
from textual.worker import WorkerCancelled |
| 11 |
|
| 12 |
from harlequin.app import Harlequin |
| 13 |
from harlequin.autocomplete import HarlequinCompletion |
| 14 |
from harlequin.catalog import CatalogItem |
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture(autouse=True) |
| 18 |
def no_cursor_blink(monkeypatch: pytest.MonkeyPatch) -> None: |
| 19 |
"""Freeze every Input's cursor in the visible state. |
| 20 |
|
| 21 |
Snapshots are captured after a settle delay that is longer than the blink |
| 22 |
interval, so a blinking cursor makes snapshot tests nondeterministic. |
| 23 |
Neutering the toggle leaves the pause/restart behavior (and so the blurred |
| 24 |
state) intact. |
| 25 |
""" |
| 26 |
monkeypatch.setattr(Input, "_toggle_cursor", lambda _self: None) |
| 27 |
|
| 28 |
|
| 29 |
@pytest.fixture(autouse=True) |
| 30 |
def no_use_buffer_cache( |
| 31 |
monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest |
| 32 |
) -> None: |
| 33 |
if "use_cache" in request.keywords: |
| 34 |
return |
| 35 |
monkeypatch.setattr("harlequin.components.code_editor.load_cache", lambda: None) |
| 36 |
monkeypatch.setattr("harlequin.app.write_editor_cache", lambda *_: None) |
| 37 |
|
| 38 |
|
| 39 |
@pytest.fixture(autouse=True) |
| 40 |
def no_use_catalog_cache( |
| 41 |
monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest |
| 42 |
) -> None: |
| 43 |
if "use_cache" in request.keywords: |
| 44 |
return |
| 45 |
monkeypatch.setattr("harlequin.app.get_catalog_cache", lambda *_: None) |
| 46 |
monkeypatch.setattr("harlequin.app.update_catalog_cache", lambda *_: None) |
| 47 |
|
| 48 |
|
| 49 |
@pytest.fixture(autouse=True) |
| 50 |
def mock_config_loader(no_discovered_config: None) -> None: |
| 51 |
"""Autouse wrapper over the shared fixture. test_keys_app overrides it.""" |
| 52 |
|
| 53 |
|
| 54 |
@pytest.fixture(autouse=True) |
| 55 |
def mock_completions(monkeypatch: pytest.MonkeyPatch) -> None: |
| 56 |
KEYWORDS = [ |
| 57 |
"abort", |
| 58 |
"all", |
| 59 |
"alter", |
| 60 |
"always", |
| 61 |
"analyze", |
| 62 |
"and", |
| 63 |
"as", |
| 64 |
"asc", |
| 65 |
"begin", |
| 66 |
"between", |
| 67 |
"by", |
| 68 |
"cascade", |
| 69 |
"case", |
| 70 |
"column", |
| 71 |
"commit", |
| 72 |
"create", |
| 73 |
"database", |
| 74 |
"delete", |
| 75 |
"desc", |
| 76 |
"distinct", |
| 77 |
"drop", |
| 78 |
"else", |
| 79 |
"end", |
| 80 |
"explain", |
| 81 |
"from", |
| 82 |
"group", |
| 83 |
"groups", |
| 84 |
"having", |
| 85 |
"in", |
| 86 |
"inner", |
| 87 |
"insert", |
| 88 |
"intersect", |
| 89 |
"into", |
| 90 |
"is", |
| 91 |
"join", |
| 92 |
"left", |
| 93 |
"like", |
| 94 |
"limit", |
| 95 |
"null", |
| 96 |
"on", |
| 97 |
"order", |
| 98 |
"outer", |
| 99 |
"over", |
| 100 |
"partition", |
| 101 |
"row", |
| 102 |
"savepoint", |
| 103 |
"select", |
| 104 |
"set", |
| 105 |
"table", |
| 106 |
"temp", |
| 107 |
"temporary", |
| 108 |
"then", |
| 109 |
"union", |
| 110 |
"update", |
| 111 |
"using", |
| 112 |
"values", |
| 113 |
"view", |
| 114 |
"when", |
| 115 |
"where", |
| 116 |
"window", |
| 117 |
] |
| 118 |
|
| 119 |
FUNCTIONS = [ |
| 120 |
("array_select", "fn"), |
| 121 |
("count", "agg"), |
| 122 |
("greatest", "fn"), |
| 123 |
("least", "fn"), |
| 124 |
("list_select", "fn"), |
| 125 |
("sqrt", "fn"), |
| 126 |
("sum", "agg"), |
| 127 |
] |
| 128 |
|
| 129 |
keyword_completions = [ |
| 130 |
HarlequinCompletion( |
| 131 |
label=kw_name, type_label="kw", value=kw_name, priority=100, context=None |
| 132 |
) |
| 133 |
for kw_name in KEYWORDS |
| 134 |
] |
| 135 |
|
| 136 |
function_completions = [ |
| 137 |
HarlequinCompletion( |
| 138 |
label=label, type_label=type_label, value=label, priority=1000, context=None |
| 139 |
) |
| 140 |
for label, type_label in FUNCTIONS |
| 141 |
] |
| 142 |
|
| 143 |
completions = [*keyword_completions, *function_completions] |
| 144 |
duckdb_completions = [ |
| 145 |
( |
| 146 |
completion.label, |
| 147 |
completion.type_label, |
| 148 |
completion.priority, |
| 149 |
completion.context, |
| 150 |
) |
| 151 |
for completion in completions |
| 152 |
] |
| 153 |
monkeypatch.setattr( |
| 154 |
"harlequin_sqlite.adapter.get_completion_data", |
| 155 |
lambda *_: completions, |
| 156 |
raising=True, |
| 157 |
) |
| 158 |
monkeypatch.setattr( |
| 159 |
"harlequin_duckdb.adapter.get_completion_data", |
| 160 |
lambda *_: duckdb_completions, |
| 161 |
raising=True, |
| 162 |
) |
| 163 |
|
| 164 |
|
| 165 |
@pytest.fixture |
| 166 |
def mock_pyperclip(monkeypatch: pytest.MonkeyPatch) -> MagicMock: |
| 167 |
mock = MagicMock() |
| 168 |
mock.determine_clipboard.return_value = mock.copy, mock.paste |
| 169 |
|
| 170 |
def set_paste(x: str) -> None: |
| 171 |
mock.paste.return_value = x |
| 172 |
|
| 173 |
mock.copy.side_effect = set_paste |
| 174 |
monkeypatch.setattr("textual_textarea.text_editor.pyperclip", mock) |
| 175 |
|
| 176 |
return mock |
| 177 |
|
| 178 |
|
| 179 |
@pytest.fixture |
| 180 |
def wait_for_workers() -> Callable[[Harlequin], Awaitable[None]]: |
| 181 |
async def wait_for_filtered_workers(app: Harlequin) -> None: |
| 182 |
filtered_workers = [ |
| 183 |
w for w in app.workers if w.name != "_database_tree_background_loader" |
| 184 |
] |
| 185 |
if filtered_workers: |
| 186 |
with suppress(WorkerCancelled): |
| 187 |
await app.workers.wait_for_complete(filtered_workers) |
| 188 |
|
| 189 |
return wait_for_filtered_workers |
| 190 |
|
| 191 |
|
| 192 |
@pytest.fixture |
| 193 |
def expand_catalog_node() -> Callable[[Pilot, TreeNode[CatalogItem]], Awaitable[None]]: |
| 194 |
"""Expand a catalog node and wait until its real children are rendered. |
| 195 |
|
| 196 |
The catalog shows a "loading…" placeholder child as soon as an unloaded node |
| 197 |
is expanded, so waiting on `node.children` alone returns before the adapter |
| 198 |
has answered. |
| 199 |
""" |
| 200 |
|
| 201 |
async def expand(pilot: Pilot, node: TreeNode[CatalogItem]) -> None: |
| 202 |
node.expand() |
| 203 |
while True: |
| 204 |
data = node.data |
| 205 |
if data is None or ( |
| 206 |
getattr(data, "loaded", True) |
| 207 |
and len(node.children) == len(data.children) |
| 208 |
): |
| 209 |
return |
| 210 |
await pilot.pause() |
| 211 |
|
| 212 |
return expand |
| 213 |
|
| 214 |
|
| 215 |
@pytest.fixture |
| 216 |
def transaction_button_visible() -> Callable[[Harlequin], bool]: |
| 217 |
def fn(app: Harlequin) -> bool: |
| 218 |
""" |
| 219 |
Skip snapshot checks for versions of that app showing the autocommit button. |
| 220 |
""" |
| 221 |
return ( |
| 222 |
sys.version_info >= (3, 12) and "Sqlite" in app.adapter.__class__.__name__ |
| 223 |
) |
| 224 |
|
| 225 |
return fn |