| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import sqlite3 |
| 5 |
import threading |
| 6 |
from datetime import datetime, timedelta |
| 7 |
from pathlib import Path |
| 8 |
from typing import Any, Awaitable, Callable |
| 9 |
from unittest.mock import MagicMock, patch |
| 10 |
|
| 11 |
import pytest |
| 12 |
from rich.console import COLOR_SYSTEMS |
| 13 |
from textual.pilot import Pilot |
| 14 |
from textual.widgets._select import SelectOverlay |
| 15 |
from textual_textarea.text_editor import TextAreaPlus |
| 16 |
|
| 17 |
import harlequin.app |
| 18 |
from harlequin import Harlequin |
| 19 |
from harlequin.adapter import HarlequinAdapter |
| 20 |
from harlequin.app import QueryHistoryLoaded, QuerySubmitted |
| 21 |
from harlequin.components import HistoryScreen |
| 22 |
from harlequin.components.help_screen import HelpScreen |
| 23 |
from harlequin.components.history_screen import FILTER_INTERVAL, Filters |
| 24 |
from harlequin.history import DEFAULT_ROWS, History |
| 25 |
from harlequin.query import fetch |
| 26 |
from harlequin.query_log import QueryLog |
| 27 |
from tests.functional_tests.helpers import wait_for_editor |
| 28 |
from tests.waiting import wait_for_value |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture |
| 32 |
def mock_time(monkeypatch: pytest.MonkeyPatch) -> None: |
| 33 |
"""Fix what the store records, so the screen that reads it back snapshots. |
| 34 |
|
| 35 |
The timestamps are built from a local wall-clock time and written in UTC, |
| 36 |
which is how a record made anywhere renders as the same row: the screen |
| 37 |
shows the reader's own time zone. |
| 38 |
""" |
| 39 |
base = datetime(2024, 1, 26, hour=10).astimezone() |
| 40 |
mock_datetime = MagicMock() |
| 41 |
mock_datetime.now.side_effect = (base + timedelta(minutes=i) for i in range(1000)) |
| 42 |
monkeypatch.setattr("harlequin.query_log.datetime", mock_datetime) |
| 43 |
|
| 44 |
for module in ("harlequin.app", "harlequin.query"): |
| 45 |
mock_time = MagicMock() |
| 46 |
mock_time.monotonic.side_effect = (float(i) for i in range(1000)) |
| 47 |
monkeypatch.setattr(f"{module}.time", mock_time) |
| 48 |
|
| 49 |
|
| 50 |
async def open_history( |
| 51 |
pilot: Pilot, |
| 52 |
app: Harlequin, |
| 53 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 54 |
) -> HistoryScreen: |
| 55 |
"""Press the key that opens the History screen, and return the screen.""" |
| 56 |
await pilot.press("f8") |
| 57 |
await wait_for_workers(app) |
| 58 |
return await wait_for_value( |
| 59 |
pilot, |
| 60 |
lambda: app.screen if isinstance(app.screen, HistoryScreen) else None, |
| 61 |
description="the History screen", |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
async def settle_filter( |
| 66 |
pilot: Pilot, |
| 67 |
app: Harlequin, |
| 68 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 69 |
*, |
| 70 |
search: str = "", |
| 71 |
program: str | None = None, |
| 72 |
status: str | None = None, |
| 73 |
) -> None: |
| 74 |
"""Wait until the screen is listing what these filters match. |
| 75 |
|
| 76 |
A poll rather than a sleep: the debounce arms a timer, so how long the read |
| 77 |
takes to start is not something a test can time. |
| 78 |
""" |
| 79 |
screen = app.screen |
| 80 |
assert isinstance(screen, HistoryScreen) |
| 81 |
wanted = Filters(search=search, program=program, status=status) |
| 82 |
for _ in range(int(FILTER_INTERVAL * 100)): |
| 83 |
await pilot.pause(FILTER_INTERVAL / 4) |
| 84 |
await wait_for_workers(app) |
| 85 |
await pilot.pause() |
| 86 |
if screen.applied_filters == wanted: |
| 87 |
return |
| 88 |
raise AssertionError(f"the filter never settled on {wanted}") |
| 89 |
|
| 90 |
|
| 91 |
async def show_filters(pilot: Pilot, app: Harlequin) -> HistoryScreen: |
| 92 |
"""Press the key that reveals the filter controls, and return the screen.""" |
| 93 |
screen = app.screen |
| 94 |
assert isinstance(screen, HistoryScreen) |
| 95 |
if not screen.filter_controls.display: |
| 96 |
await pilot.press("ctrl+f") |
| 97 |
await pilot.pause() |
| 98 |
assert screen.filter_controls.display |
| 99 |
return screen |
| 100 |
|
| 101 |
|
| 102 |
async def type_filter( |
| 103 |
pilot: Pilot, |
| 104 |
app: Harlequin, |
| 105 |
term: str, |
| 106 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 107 |
) -> None: |
| 108 |
"""Type into the filter and wait for the read it debounces to land.""" |
| 109 |
screen = await show_filters(pilot, app) |
| 110 |
screen.filter_input.focus() |
| 111 |
await pilot.pause() |
| 112 |
await pilot.press(*term) |
| 113 |
await settle_filter(pilot, app, wait_for_workers, search=term) |
| 114 |
|
| 115 |
|
| 116 |
def preview_area(screen: HistoryScreen) -> TextAreaPlus: |
| 117 |
"""The text area inside the preview, which is what focus and scroll act on.""" |
| 118 |
area = screen.preview.text_input |
| 119 |
assert area is not None |
| 120 |
return area |
| 121 |
|
| 122 |
|
| 123 |
@pytest.mark.asyncio |
| 124 |
async def test_history_screen( |
| 125 |
app: Harlequin, |
| 126 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 127 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 128 |
mock_time: None, |
| 129 |
) -> None: |
| 130 |
snap_results: list[bool] = [] |
| 131 |
async with app.run_test(size=(120, 36)) as pilot: |
| 132 |
# rich.Syntax calculates different colors for the line numbers, depending |
| 133 |
# on the color system of the rich.console, which is different across different |
| 134 |
# GitHub action runners. Here we force everything to truecolor. |
| 135 |
app.console._color_system = COLOR_SYSTEMS["truecolor"] |
| 136 |
q = [f"select {i};" for i in range(15)] |
| 137 |
await wait_for_editor(pilot, app) |
| 138 |
app.post_message(QuerySubmitted(queries=q, limit=None)) |
| 139 |
await pilot.pause() |
| 140 |
await wait_for_workers(app) |
| 141 |
await pilot.pause() |
| 142 |
# run a bad query |
| 143 |
app.post_message(QuerySubmitted(queries=["sel;"], limit=None)) |
| 144 |
await pilot.pause() |
| 145 |
await wait_for_workers(app) |
| 146 |
await pilot.pause() |
| 147 |
await pilot.press("space") |
| 148 |
|
| 149 |
await open_history(pilot, app, wait_for_workers) |
| 150 |
await pilot.press("down") |
| 151 |
snap_results.append(await app_snapshot(app, "History Viewer")) |
| 152 |
|
| 153 |
await type_filter(pilot, app, "select 1", wait_for_workers) |
| 154 |
snap_results.append(await app_snapshot(app, "Filtered History Viewer")) |
| 155 |
|
| 156 |
await pilot.press("escape") |
| 157 |
await settle_filter(pilot, app, wait_for_workers) |
| 158 |
await pilot.press("escape") |
| 159 |
await pilot.pause() |
| 160 |
await pilot.press("down") |
| 161 |
await pilot.press("enter") |
| 162 |
await pilot.pause() |
| 163 |
snap_results.append(await app_snapshot(app, "New buffer with select 14")) |
| 164 |
|
| 165 |
assert all(snap_results) |
| 166 |
|
| 167 |
|
| 168 |
@pytest.mark.asyncio |
| 169 |
async def test_a_second_request_does_not_stack_history_screens( |
| 170 |
app: Harlequin, |
| 171 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 172 |
mock_time: None, |
| 173 |
) -> None: |
| 174 |
"""https://github.com/tconbeer/harlequin/issues/485""" |
| 175 |
async with app.run_test() as pilot: |
| 176 |
q = [f"select {i};" for i in range(15)] |
| 177 |
await wait_for_editor(pilot, app) |
| 178 |
app.post_message(QuerySubmitted(queries=q, limit=None)) |
| 179 |
await pilot.pause() |
| 180 |
await wait_for_workers(app) |
| 181 |
await pilot.pause() |
| 182 |
|
| 183 |
screen = await open_history(pilot, app, wait_for_workers) |
| 184 |
await pilot.press("f8") |
| 185 |
await wait_for_workers(app) |
| 186 |
await pilot.pause() |
| 187 |
|
| 188 |
# and a read that lands after the screen is already up is dropped too, |
| 189 |
# which is the half a keypress cannot reach |
| 190 |
app.post_message(QueryHistoryLoaded(history=History(queries=[]))) |
| 191 |
await pilot.pause() |
| 192 |
|
| 193 |
assert app.is_running |
| 194 |
assert app.screen is screen |
| 195 |
assert [s for s in app.screen_stack if isinstance(s, HistoryScreen)] == [screen] |
| 196 |
|
| 197 |
|
| 198 |
def logged(store: Path) -> list[dict[str, Any]]: |
| 199 |
"""Every row the IDE wrote to the query log, oldest first.""" |
| 200 |
db = sqlite3.connect(store) |
| 201 |
db.row_factory = sqlite3.Row |
| 202 |
try: |
| 203 |
return [dict(row) for row in db.execute("select * from queries order by id")] |
| 204 |
finally: |
| 205 |
db.close() |
| 206 |
|
| 207 |
|
| 208 |
@pytest.mark.asyncio |
| 209 |
async def test_a_query_is_recorded_as_it_runs( |
| 210 |
app: Harlequin, |
| 211 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 212 |
query_log_path: Path, |
| 213 |
) -> None: |
| 214 |
"""As each statement runs, not at quit.""" |
| 215 |
async with app.run_test() as pilot: |
| 216 |
await wait_for_editor(pilot, app) |
| 217 |
app.post_message(QuerySubmitted(queries=["select 1 as a;", "sel;"], limit=None)) |
| 218 |
await pilot.pause() |
| 219 |
await wait_for_workers(app) |
| 220 |
await pilot.pause() |
| 221 |
|
| 222 |
recorded = {row["sql"]: row for row in logged(query_log_path)} |
| 223 |
assert set(recorded) == {"select 1 as a;", "sel;"} |
| 224 |
assert all(row["program"] == "harlequin" for row in recorded.values()) |
| 225 |
assert all(row["connection"] == "foo" for row in recorded.values()) |
| 226 |
assert recorded["select 1 as a;"]["status"] == "ok" |
| 227 |
assert recorded["select 1 as a;"]["rows"] == 1 |
| 228 |
assert recorded["sel;"]["status"] == "error" |
| 229 |
assert recorded["sel;"]["error"] |
| 230 |
|
| 231 |
|
| 232 |
@pytest.mark.asyncio |
| 233 |
async def test_a_row_exists_before_its_rows_are_known( |
| 234 |
app: Harlequin, |
| 235 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 236 |
query_log_path: Path, |
| 237 |
) -> None: |
| 238 |
"""The two-phase write: a session that dies mid-fetch keeps the query. |
| 239 |
|
| 240 |
Asserted by holding the fetch open, which is the window a crash lands in. |
| 241 |
""" |
| 242 |
fetching = threading.Event() |
| 243 |
release = threading.Event() |
| 244 |
real_fetch = fetch |
| 245 |
|
| 246 |
def slow_fetch(*args: Any, **kwargs: Any) -> Any: |
| 247 |
fetching.set() |
| 248 |
release.wait(timeout=10) |
| 249 |
return real_fetch(*args, **kwargs) |
| 250 |
|
| 251 |
async with app.run_test() as pilot: |
| 252 |
await wait_for_editor(pilot, app) |
| 253 |
with patch.object(harlequin.app, "fetch", slow_fetch): |
| 254 |
app.post_message(QuerySubmitted(queries=["select 1 as a;"], limit=None)) |
| 255 |
assert await asyncio.get_running_loop().run_in_executor( |
| 256 |
None, fetching.wait, 10 |
| 257 |
), "the fetch never started" |
| 258 |
|
| 259 |
# mid-fetch: the row is already there, with its rows not yet known |
| 260 |
(pending,) = logged(query_log_path) |
| 261 |
assert pending["sql"] == "select 1 as a;" |
| 262 |
assert pending["status"] == "ok" |
| 263 |
assert pending["rows"] is None |
| 264 |
|
| 265 |
release.set() |
| 266 |
await wait_for_workers(app) |
| 267 |
await pilot.pause() |
| 268 |
|
| 269 |
# and the same row is completed rather than a second one written |
| 270 |
(final,) = logged(query_log_path) |
| 271 |
assert final["id"] == pending["id"] |
| 272 |
assert final["rows"] == 1 |
| 273 |
assert final["elapsed_ms"] is not None |
| 274 |
|
| 275 |
|
| 276 |
@pytest.mark.asyncio |
| 277 |
async def test_a_query_cancelled_mid_fetch_is_recorded_as_canceled( |
| 278 |
app: Harlequin, |
| 279 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 280 |
query_log_path: Path, |
| 281 |
) -> None: |
| 282 |
"""The cancel has to mark the rows before it cancels the cursor. |
| 283 |
|
| 284 |
A cancelled cursor comes back empty and error-free, so a fetch that |
| 285 |
completes in the window between `cancel()` returning and the marking would |
| 286 |
record `ok` with no rows -- indistinguishable from a query that matched |
| 287 |
nothing, which is the ambiguity `canceled` exists to remove. The fake |
| 288 |
`cancel()` here puts the fetch's completion inside that window on purpose, |
| 289 |
rather than racing for it. |
| 290 |
""" |
| 291 |
fetching = threading.Event() |
| 292 |
release = threading.Event() |
| 293 |
completed = threading.Event() |
| 294 |
|
| 295 |
def slow_fetch(*args: Any, **kwargs: Any) -> Any: |
| 296 |
fetching.set() |
| 297 |
release.wait(timeout=10) |
| 298 |
return fetch(*args, **kwargs) |
| 299 |
|
| 300 |
real_update = app.query_log.update |
| 301 |
|
| 302 |
def watched_update(row: int | None, **kwargs: Any) -> None: |
| 303 |
real_update(row, **kwargs) |
| 304 |
completed.set() |
| 305 |
|
| 306 |
def fake_cancel() -> None: |
| 307 |
# let the fetch run to completion, inside the cancel |
| 308 |
release.set() |
| 309 |
completed.wait(timeout=10) |
| 310 |
|
| 311 |
async with app.run_test() as pilot: |
| 312 |
await wait_for_editor(pilot, app) |
| 313 |
connection = await wait_for_value( |
| 314 |
pilot, lambda: app.connection, description="the app to connect" |
| 315 |
) |
| 316 |
app.query_log.update = watched_update # type: ignore[method-assign] |
| 317 |
with ( |
| 318 |
patch.object(harlequin.app, "fetch", slow_fetch), |
| 319 |
patch.object(app.adapter, "IMPLEMENTS_CANCEL", True), |
| 320 |
patch.object(connection, "cancel", fake_cancel), |
| 321 |
): |
| 322 |
app.post_message(QuerySubmitted(queries=["select 1 as a;"], limit=None)) |
| 323 |
assert await asyncio.get_running_loop().run_in_executor( |
| 324 |
None, fetching.wait, 10 |
| 325 |
), "the fetch never started" |
| 326 |
|
| 327 |
app._cancel_query() |
| 328 |
await wait_for_workers(app) |
| 329 |
await pilot.pause() |
| 330 |
|
| 331 |
(record,) = logged(query_log_path) |
| 332 |
assert record["status"] == "canceled", ( |
| 333 |
"the fetch completed the row before the cancel marked it" |
| 334 |
) |
| 335 |
|
| 336 |
|
| 337 |
@pytest.mark.asyncio |
| 338 |
async def test_a_cancel_leaves_a_finished_query_alone( |
| 339 |
app: Harlequin, |
| 340 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 341 |
query_log_path: Path, |
| 342 |
) -> None: |
| 343 |
"""Only what the fetch never completed is `canceled`.""" |
| 344 |
async with app.run_test() as pilot: |
| 345 |
await wait_for_editor(pilot, app) |
| 346 |
app.post_message(QuerySubmitted(queries=["select 1 as a;"], limit=None)) |
| 347 |
await pilot.pause() |
| 348 |
await wait_for_workers(app) |
| 349 |
await pilot.pause() |
| 350 |
assert logged(query_log_path)[0]["status"] == "ok" |
| 351 |
|
| 352 |
app._cancel_query() |
| 353 |
await wait_for_workers(app) |
| 354 |
await pilot.pause() |
| 355 |
|
| 356 |
(record,) = logged(query_log_path) |
| 357 |
assert record["status"] == "ok", "a cancel rewrote a query that had finished" |
| 358 |
assert record["rows"] == 1 |
| 359 |
|
| 360 |
|
| 361 |
@pytest.mark.asyncio |
| 362 |
async def test_a_session_that_records_nothing_still_runs_queries( |
| 363 |
duckdb_adapter: type[HarlequinAdapter], |
| 364 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 365 |
query_log_path: Path, |
| 366 |
) -> None: |
| 367 |
"""`--no-write-history`, or the key of that name in the profile.""" |
| 368 |
app = Harlequin( |
| 369 |
duckdb_adapter([":memory:"], no_init=True), |
| 370 |
connection_hash="foo", |
| 371 |
record_history=False, |
| 372 |
) |
| 373 |
async with app.run_test() as pilot: |
| 374 |
await wait_for_editor(pilot, app) |
| 375 |
app.post_message(QuerySubmitted(queries=["select 1;"], limit=None)) |
| 376 |
await pilot.pause() |
| 377 |
await wait_for_workers(app) |
| 378 |
await pilot.pause() |
| 379 |
|
| 380 |
assert not query_log_path.exists() |
| 381 |
|
| 382 |
|
| 383 |
@pytest.mark.asyncio |
| 384 |
async def test_the_screen_shows_what_another_command_ran( |
| 385 |
app: Harlequin, |
| 386 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 387 |
) -> None: |
| 388 |
"""One store, so an agent's queries reach the human's History screen.""" |
| 389 |
agent = QueryLog(program="hsql", connection="foo") |
| 390 |
agent.write("select * from line_items", rows=3, elapsed_ms=12.5) |
| 391 |
agent.close() |
| 392 |
|
| 393 |
async with app.run_test() as pilot: |
| 394 |
await wait_for_editor(pilot, app) |
| 395 |
app.post_message(QuerySubmitted(queries=["select 1;"], limit=None)) |
| 396 |
await pilot.pause() |
| 397 |
await wait_for_workers(app) |
| 398 |
await pilot.pause() |
| 399 |
|
| 400 |
screen = await open_history(pilot, app, wait_for_workers) |
| 401 |
assert [record.query_text for record in screen.history] == [ |
| 402 |
"select 1;", |
| 403 |
"select * from line_items", |
| 404 |
] |
| 405 |
|
| 406 |
|
| 407 |
@pytest.mark.asyncio |
| 408 |
async def test_the_screen_shows_only_this_connections_queries( |
| 409 |
app: Harlequin, |
| 410 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 411 |
) -> None: |
| 412 |
elsewhere = QueryLog(program="hsql", connection="another-database") |
| 413 |
elsewhere.write("select * from orders") |
| 414 |
elsewhere.close() |
| 415 |
|
| 416 |
async with app.run_test() as pilot: |
| 417 |
await wait_for_editor(pilot, app) |
| 418 |
screen = await open_history(pilot, app, wait_for_workers) |
| 419 |
assert len(screen.history) == 0 |
| 420 |
|
| 421 |
|
| 422 |
@pytest.mark.asyncio |
| 423 |
async def test_the_screen_opens_before_anything_has_been_run( |
| 424 |
app: Harlequin, |
| 425 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 426 |
) -> None: |
| 427 |
"""An empty store is an empty screen rather than an error.""" |
| 428 |
async with app.run_test() as pilot: |
| 429 |
await wait_for_editor(pilot, app) |
| 430 |
screen = await open_history(pilot, app, wait_for_workers) |
| 431 |
assert len(screen.history) == 0 |
| 432 |
|
| 433 |
|
| 434 |
@pytest.mark.asyncio |
| 435 |
async def test_a_pickled_history_arrives_in_the_screen( |
| 436 |
app: Harlequin, |
| 437 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 438 |
legacy_history_cache: Path, |
| 439 |
) -> None: |
| 440 |
"""The one-time move: what an older Harlequin saved is still there.""" |
| 441 |
async with app.run_test() as pilot: |
| 442 |
await wait_for_editor(pilot, app) |
| 443 |
screen = await open_history(pilot, app, wait_for_workers) |
| 444 |
assert [record.query_text for record in screen.history] == [ |
| 445 |
"select * from line_items" |
| 446 |
] |
| 447 |
|
| 448 |
|
| 449 |
@pytest.mark.asyncio |
| 450 |
async def test_a_session_that_records_nothing_moves_no_pickle( |
| 451 |
duckdb_adapter: type[HarlequinAdapter], |
| 452 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 453 |
legacy_history_cache: Path, |
| 454 |
query_log_path: Path, |
| 455 |
) -> None: |
| 456 |
"""`--no-write-history` is a refusal to write, and the move is a write.""" |
| 457 |
app = Harlequin( |
| 458 |
duckdb_adapter([":memory:"], no_init=True), |
| 459 |
connection_hash="foo", |
| 460 |
record_history=False, |
| 461 |
) |
| 462 |
async with app.run_test() as pilot: |
| 463 |
await wait_for_editor(pilot, app) |
| 464 |
screen = await open_history(pilot, app, wait_for_workers) |
| 465 |
assert len(screen.history) == 0 |
| 466 |
assert not query_log_path.exists() |
| 467 |
|
| 468 |
|
| 469 |
@pytest.mark.asyncio |
| 470 |
async def test_an_app_binding_does_not_reach_through_the_screen( |
| 471 |
app: Harlequin, |
| 472 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 473 |
) -> None: |
| 474 |
"""https://github.com/tconbeer/harlequin/issues/850""" |
| 475 |
async with app.run_test() as pilot: |
| 476 |
while app.editor is None: |
| 477 |
await pilot.pause() |
| 478 |
screen = await open_history(pilot, app, wait_for_workers) |
| 479 |
assert not app.full_screen |
| 480 |
|
| 481 |
for key in ("f1", "f10", "f12"): |
| 482 |
await pilot.press(key) |
| 483 |
await pilot.pause() |
| 484 |
assert app.screen is screen, f"{key} pushed a screen" |
| 485 |
assert not [s for s in app.screen_stack if isinstance(s, HelpScreen)] |
| 486 |
assert not app.full_screen, "f10 reached the app" |
| 487 |
|
| 488 |
|
| 489 |
LONG_QUERY = "select\n" + ",\n".join(f" {i} as column_{i}" for i in range(60)) + "\n;" |
| 490 |
|
| 491 |
|
| 492 |
async def focus_preview( |
| 493 |
pilot: Pilot, |
| 494 |
app: Harlequin, |
| 495 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 496 |
query: str = LONG_QUERY, |
| 497 |
) -> HistoryScreen: |
| 498 |
"""Open the History screen over one query and tab into the preview.""" |
| 499 |
app.post_message(QuerySubmitted(queries=[query], limit=None)) |
| 500 |
await pilot.pause() |
| 501 |
await wait_for_workers(app) |
| 502 |
await pilot.pause() |
| 503 |
screen = await open_history(pilot, app, wait_for_workers) |
| 504 |
await pilot.press("tab") |
| 505 |
await pilot.pause() |
| 506 |
assert preview_area(screen).has_focus, "tab did not reach the preview" |
| 507 |
return screen |
| 508 |
|
| 509 |
|
| 510 |
@pytest.mark.asyncio |
| 511 |
async def test_the_preview_shows_no_cursor( |
| 512 |
app: Harlequin, |
| 513 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 514 |
) -> None: |
| 515 |
"""https://github.com/tconbeer/harlequin/issues/850 |
| 516 |
|
| 517 |
It takes focus so it can be scrolled, but a query it will not let anyone |
| 518 |
change does not get a cursor. |
| 519 |
""" |
| 520 |
async with app.run_test() as pilot: |
| 521 |
while app.editor is None: |
| 522 |
await pilot.pause() |
| 523 |
screen = await focus_preview(pilot, app, wait_for_workers) |
| 524 |
|
| 525 |
assert not preview_area(screen)._draw_cursor |
| 526 |
assert not preview_area(screen)._has_cursor |
| 527 |
|
| 528 |
|
| 529 |
@pytest.mark.asyncio |
| 530 |
async def test_the_preview_scrolls_from_the_keyboard( |
| 531 |
app: Harlequin, |
| 532 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 533 |
) -> None: |
| 534 |
"""A query taller than the pane is readable without a mouse.""" |
| 535 |
async with app.run_test() as pilot: |
| 536 |
while app.editor is None: |
| 537 |
await pilot.pause() |
| 538 |
screen = await focus_preview(pilot, app, wait_for_workers) |
| 539 |
text_area = preview_area(screen) |
| 540 |
assert text_area.scroll_offset.y == 0 |
| 541 |
|
| 542 |
await pilot.press(*["down"] * 5) |
| 543 |
await pilot.pause() |
| 544 |
scrolled = text_area.scroll_offset.y |
| 545 |
assert scrolled > 0, "down did not scroll the preview" |
| 546 |
|
| 547 |
await pilot.press("pagedown") |
| 548 |
await pilot.pause() |
| 549 |
assert text_area.scroll_offset.y > scrolled, "pagedown did not scroll further" |
| 550 |
|
| 551 |
await pilot.press(*["up"] * 100) |
| 552 |
await pilot.pause() |
| 553 |
assert text_area.scroll_offset.y == 0 |
| 554 |
|
| 555 |
|
| 556 |
@pytest.mark.asyncio |
| 557 |
async def test_the_preview_scrolls_sideways_from_the_keyboard( |
| 558 |
app: Harlequin, |
| 559 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 560 |
) -> None: |
| 561 |
"""A query on one very long line is read across, not down.""" |
| 562 |
one_liner = "select '" + "lorem ipsum dolor sit amet " * 200 + "';" |
| 563 |
async with app.run_test() as pilot: |
| 564 |
while app.editor is None: |
| 565 |
await pilot.pause() |
| 566 |
screen = await focus_preview(pilot, app, wait_for_workers, query=one_liner) |
| 567 |
text_area = preview_area(screen) |
| 568 |
assert text_area.scroll_offset.x == 0 |
| 569 |
|
| 570 |
await pilot.press(*["right"] * 5) |
| 571 |
await pilot.pause() |
| 572 |
assert text_area.scroll_offset.x > 0, "right did not scroll the preview" |
| 573 |
|
| 574 |
await pilot.press(*["left"] * 100) |
| 575 |
await pilot.pause() |
| 576 |
assert text_area.scroll_offset.x == 0 |
| 577 |
|
| 578 |
|
| 579 |
@pytest.mark.asyncio |
| 580 |
async def test_the_preview_takes_no_input( |
| 581 |
app: Harlequin, |
| 582 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 583 |
) -> None: |
| 584 |
"""Focus makes it scrollable, not editable.""" |
| 585 |
async with app.run_test() as pilot: |
| 586 |
while app.editor is None: |
| 587 |
await pilot.pause() |
| 588 |
screen = await focus_preview(pilot, app, wait_for_workers) |
| 589 |
|
| 590 |
await pilot.press("x", "backspace", "delete", "ctrl+v", "shift+insert") |
| 591 |
await pilot.pause() |
| 592 |
assert screen.preview.text == LONG_QUERY |
| 593 |
|
| 594 |
|
| 595 |
@pytest.mark.asyncio |
| 596 |
async def test_the_preview_ignores_every_key_it_binds( |
| 597 |
app: Harlequin, |
| 598 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 599 |
monkeypatch: pytest.MonkeyPatch, |
| 600 |
) -> None: |
| 601 |
"""Every key the text area binds, rather than a list written here. |
| 602 |
|
| 603 |
Derived at run time, so a component-library bump that gives a read-only |
| 604 |
text area a way to edit itself fails here and names the key. |
| 605 |
""" |
| 606 |
async with app.run_test() as pilot: |
| 607 |
while app.editor is None: |
| 608 |
await pilot.pause() |
| 609 |
screen = await focus_preview(pilot, app, wait_for_workers) |
| 610 |
text_area = preview_area(screen) |
| 611 |
# a machine whose clipboard has something on it, as CI runners do |
| 612 |
monkeypatch.setattr(text_area, "system_paste", lambda: "PASTED") |
| 613 |
text_area.clipboard = "PASTED" |
| 614 |
|
| 615 |
edited_by = [] |
| 616 |
for key in sorted(set(text_area._bindings.key_to_bindings)): |
| 617 |
await pilot.press(key) |
| 618 |
await pilot.pause() |
| 619 |
await pilot.pause() |
| 620 |
if screen.preview.text != LONG_QUERY: |
| 621 |
edited_by.append(key) |
| 622 |
screen.preview.text = LONG_QUERY |
| 623 |
await pilot.pause() |
| 624 |
assert not edited_by, f"these keys edited a read-only preview: {edited_by}" |
| 625 |
|
| 626 |
|
| 627 |
@pytest.mark.asyncio |
| 628 |
async def test_the_previews_own_editor_keys_are_not_live( |
| 629 |
app: Harlequin, |
| 630 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 631 |
) -> None: |
| 632 |
"""Nothing in the preview is saved, opened or searched.""" |
| 633 |
async with app.run_test() as pilot: |
| 634 |
while app.editor is None: |
| 635 |
await pilot.pause() |
| 636 |
screen = await focus_preview(pilot, app, wait_for_workers) |
| 637 |
|
| 638 |
# ctrl+f is not here: it is the screen's own, for the filter controls |
| 639 |
editor_keys = {"ctrl+s", "ctrl+o", "f3", "ctrl+g"} |
| 640 |
assert not editor_keys & set(app.screen.active_bindings) |
| 641 |
|
| 642 |
for key in sorted(editor_keys): |
| 643 |
await pilot.press(key) |
| 644 |
await pilot.pause() |
| 645 |
assert app.is_running, f"{key} closed the app" |
| 646 |
assert app.screen is screen, f"{key} pushed a screen" |
| 647 |
|
| 648 |
|
| 649 |
@pytest.mark.asyncio |
| 650 |
async def test_quit_still_reaches_the_app_from_the_screen( |
| 651 |
app: Harlequin, |
| 652 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 653 |
) -> None: |
| 654 |
"""The modal stops the app's bindings, but quit is a priority binding.""" |
| 655 |
async with app.run_test() as pilot: |
| 656 |
while app.editor is None: |
| 657 |
await pilot.pause() |
| 658 |
await open_history(pilot, app, wait_for_workers) |
| 659 |
|
| 660 |
await pilot.press("ctrl+q") |
| 661 |
await pilot.pause() |
| 662 |
assert not app.is_running |
| 663 |
|
| 664 |
|
| 665 |
@pytest.mark.asyncio |
| 666 |
async def test_enter_selects_a_query_from_the_preview( |
| 667 |
app: Harlequin, |
| 668 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 669 |
) -> None: |
| 670 |
"""The screen's key wins wherever focus is.""" |
| 671 |
async with app.run_test() as pilot: |
| 672 |
while app.editor is None: |
| 673 |
await pilot.pause() |
| 674 |
screen = await focus_preview(pilot, app, wait_for_workers) |
| 675 |
|
| 676 |
await pilot.press("enter") |
| 677 |
await pilot.pause() |
| 678 |
assert app.screen is not screen |
| 679 |
assert app.editor is not None |
| 680 |
assert app.editor.text == LONG_QUERY |
| 681 |
|
| 682 |
|
| 683 |
@pytest.mark.asyncio |
| 684 |
async def test_the_filter_searches_the_whole_store( |
| 685 |
app: Harlequin, |
| 686 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 687 |
) -> None: |
| 688 |
"""https://github.com/tconbeer/harlequin/issues/429 |
| 689 |
|
| 690 |
The filter is a query over the store, so it reaches queries older than the |
| 691 |
ones the screen holds. |
| 692 |
""" |
| 693 |
log = QueryLog(program="hsql", connection="foo") |
| 694 |
log.write("select * from line_items") |
| 695 |
for i in range(DEFAULT_ROWS): |
| 696 |
log.write(f"select {i} from orders") |
| 697 |
log.close() |
| 698 |
|
| 699 |
async with app.run_test() as pilot: |
| 700 |
while app.editor is None: |
| 701 |
await pilot.pause() |
| 702 |
screen = await open_history(pilot, app, wait_for_workers) |
| 703 |
listed = [record.query_text for record in screen.history] |
| 704 |
assert len(listed) == DEFAULT_ROWS |
| 705 |
assert "select * from line_items" not in listed |
| 706 |
|
| 707 |
await type_filter(pilot, app, "line_items", wait_for_workers) |
| 708 |
assert [record.query_text for record in screen.history] == [ |
| 709 |
"select * from line_items" |
| 710 |
] |
| 711 |
|
| 712 |
|
| 713 |
@pytest.mark.asyncio |
| 714 |
async def test_an_edit_that_changes_nothing_keeps_your_place( |
| 715 |
app: Harlequin, |
| 716 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 717 |
) -> None: |
| 718 |
"""The debounce reads once per interval, whatever was typed inside it. |
| 719 |
|
| 720 |
A typo and its correction, or a term every match already contains, both |
| 721 |
arrive here as a read for the term already listed. |
| 722 |
""" |
| 723 |
log = QueryLog(program="hsql", connection="foo") |
| 724 |
for i in range(5): |
| 725 |
log.write(f"select {i} from orders") |
| 726 |
log.close() |
| 727 |
|
| 728 |
async with app.run_test() as pilot: |
| 729 |
while app.editor is None: |
| 730 |
await pilot.pause() |
| 731 |
screen = await open_history(pilot, app, wait_for_workers) |
| 732 |
await type_filter(pilot, app, "orders", wait_for_workers) |
| 733 |
|
| 734 |
# enter takes the term to the list, where the arrows move the highlight |
| 735 |
await pilot.press("enter", "down", "down") |
| 736 |
await pilot.pause() |
| 737 |
assert screen.list.highlighted == 2 |
| 738 |
chosen = screen.preview.text |
| 739 |
|
| 740 |
# the read the debounce will make for a term already listed -- watched, |
| 741 |
# because waiting on the term settling would be waiting on what is |
| 742 |
# already true |
| 743 |
reads: list[Filters] = [] |
| 744 |
read_history = screen.read_history |
| 745 |
|
| 746 |
def watched_read(filters: Filters) -> None: |
| 747 |
reads.append(filters) |
| 748 |
read_history(filters) |
| 749 |
|
| 750 |
screen.read_history = watched_read # type: ignore[assignment,method-assign] |
| 751 |
|
| 752 |
# typed and taken back inside one debounce window |
| 753 |
screen.filter_input.focus() |
| 754 |
await pilot.pause() |
| 755 |
await pilot.press("x", "backspace") |
| 756 |
for _ in range(int(FILTER_INTERVAL * 100)): |
| 757 |
await pilot.pause(FILTER_INTERVAL / 4) |
| 758 |
await wait_for_workers(app) |
| 759 |
await pilot.pause() |
| 760 |
if reads: |
| 761 |
break |
| 762 |
assert reads == [Filters(search="orders")], ( |
| 763 |
"the debounce never re-read the unchanged term" |
| 764 |
) |
| 765 |
|
| 766 |
assert screen.filter_input.value == "orders" |
| 767 |
assert screen.list.highlighted == 2, "the list jumped back to the top" |
| 768 |
assert screen.preview.text == chosen |
| 769 |
|
| 770 |
|
| 771 |
@pytest.mark.asyncio |
| 772 |
async def test_the_filter_matches_no_wildcards( |
| 773 |
app: Harlequin, |
| 774 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 775 |
) -> None: |
| 776 |
"""A typed `_` matches an underscore, not `like`'s any-character.""" |
| 777 |
log = QueryLog(program="hsql", connection="foo") |
| 778 |
log.write("select * from lineXitems") |
| 779 |
log.close() |
| 780 |
|
| 781 |
async with app.run_test() as pilot: |
| 782 |
while app.editor is None: |
| 783 |
await pilot.pause() |
| 784 |
screen = await open_history(pilot, app, wait_for_workers) |
| 785 |
assert len(screen.history) == 1, "the record the filter has to miss is absent" |
| 786 |
|
| 787 |
await type_filter(pilot, app, "line_items", wait_for_workers) |
| 788 |
assert len(screen.history) == 0 |
| 789 |
|
| 790 |
|
| 791 |
@pytest.mark.asyncio |
| 792 |
async def test_the_screen_opens_on_the_list( |
| 793 |
app: Harlequin, |
| 794 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 795 |
) -> None: |
| 796 |
"""Picking a query is what the screen is for, so the list has focus.""" |
| 797 |
async with app.run_test() as pilot: |
| 798 |
while app.editor is None: |
| 799 |
await pilot.pause() |
| 800 |
app.post_message(QuerySubmitted(queries=["select 1;"], limit=None)) |
| 801 |
await pilot.pause() |
| 802 |
await wait_for_workers(app) |
| 803 |
await pilot.pause() |
| 804 |
screen = await open_history(pilot, app, wait_for_workers) |
| 805 |
|
| 806 |
assert screen.list.has_focus |
| 807 |
|
| 808 |
|
| 809 |
@pytest.mark.asyncio |
| 810 |
async def test_tab_cycles_the_panes_that_are_shown( |
| 811 |
app: Harlequin, |
| 812 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 813 |
) -> None: |
| 814 |
async with app.run_test() as pilot: |
| 815 |
while app.editor is None: |
| 816 |
await pilot.pause() |
| 817 |
app.post_message(QuerySubmitted(queries=["select 1;"], limit=None)) |
| 818 |
await pilot.pause() |
| 819 |
await wait_for_workers(app) |
| 820 |
await pilot.pause() |
| 821 |
screen = await open_history(pilot, app, wait_for_workers) |
| 822 |
assert screen.list.has_focus |
| 823 |
|
| 824 |
# the controls are hidden, so they are not in the cycle |
| 825 |
await pilot.press("tab") |
| 826 |
await pilot.pause() |
| 827 |
assert preview_area(screen).has_focus |
| 828 |
await pilot.press("tab") |
| 829 |
await pilot.pause() |
| 830 |
assert screen.list.has_focus |
| 831 |
|
| 832 |
await show_filters(pilot, app) |
| 833 |
for expected in ( |
| 834 |
screen.program_select, |
| 835 |
screen.status_select, |
| 836 |
screen.list, |
| 837 |
preview_area(screen), |
| 838 |
screen.filter_input, |
| 839 |
): |
| 840 |
await pilot.press("tab") |
| 841 |
await pilot.pause() |
| 842 |
assert expected.has_focus |
| 843 |
|
| 844 |
|
| 845 |
@pytest.mark.asyncio |
| 846 |
async def test_typing_over_the_list_starts_a_search( |
| 847 |
app: Harlequin, |
| 848 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 849 |
) -> None: |
| 850 |
"""A key the list has no use for is the start of a filter term.""" |
| 851 |
async with app.run_test() as pilot: |
| 852 |
while app.editor is None: |
| 853 |
await pilot.pause() |
| 854 |
app.post_message( |
| 855 |
QuerySubmitted(queries=["select 1;", "select 2 as orders;"], limit=None) |
| 856 |
) |
| 857 |
await pilot.pause() |
| 858 |
await wait_for_workers(app) |
| 859 |
await pilot.pause() |
| 860 |
screen = await open_history(pilot, app, wait_for_workers) |
| 861 |
assert screen.list.has_focus |
| 862 |
assert not screen.filter_controls.display |
| 863 |
|
| 864 |
await pilot.press("o") |
| 865 |
await pilot.pause() |
| 866 |
assert screen.filter_controls.display, "typing did not reveal the controls" |
| 867 |
assert screen.filter_input.has_focus, "typing did not reach the filter" |
| 868 |
assert screen.filter_input.value == "o", "the first key typed was lost" |
| 869 |
|
| 870 |
await pilot.press("r", "d", "e", "r", "s") |
| 871 |
await settle_filter(pilot, app, wait_for_workers, search="orders") |
| 872 |
assert screen.filter_input.value == "orders" |
| 873 |
assert [record.query_text for record in screen.history] == [ |
| 874 |
"select 2 as orders;" |
| 875 |
] |
| 876 |
|
| 877 |
|
| 878 |
@pytest.mark.asyncio |
| 879 |
async def test_the_arrow_keys_do_not_reach_the_list_from_the_filter( |
| 880 |
app: Harlequin, |
| 881 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 882 |
) -> None: |
| 883 |
"""The filter is a text box, not a second set of keys for the list.""" |
| 884 |
async with app.run_test() as pilot: |
| 885 |
while app.editor is None: |
| 886 |
await pilot.pause() |
| 887 |
app.post_message( |
| 888 |
QuerySubmitted(queries=["select 1;", "select 2;", "select 3;"], limit=None) |
| 889 |
) |
| 890 |
await pilot.pause() |
| 891 |
await wait_for_workers(app) |
| 892 |
await pilot.pause() |
| 893 |
await open_history(pilot, app, wait_for_workers) |
| 894 |
screen = await show_filters(pilot, app) |
| 895 |
screen.filter_input.focus() |
| 896 |
await pilot.pause() |
| 897 |
highlighted = screen.list.highlighted |
| 898 |
|
| 899 |
await pilot.press("down", "down", "pagedown", "up", "pageup") |
| 900 |
await pilot.pause() |
| 901 |
assert screen.list.highlighted == highlighted |
| 902 |
|
| 903 |
|
| 904 |
@pytest.mark.asyncio |
| 905 |
async def test_escape_empties_the_filter_then_leaves_it( |
| 906 |
app: Harlequin, |
| 907 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 908 |
) -> None: |
| 909 |
async with app.run_test() as pilot: |
| 910 |
while app.editor is None: |
| 911 |
await pilot.pause() |
| 912 |
app.post_message(QuerySubmitted(queries=["select 1;"], limit=None)) |
| 913 |
await pilot.pause() |
| 914 |
await wait_for_workers(app) |
| 915 |
await pilot.pause() |
| 916 |
screen = await open_history(pilot, app, wait_for_workers) |
| 917 |
|
| 918 |
await type_filter(pilot, app, "nothing matches this", wait_for_workers) |
| 919 |
assert len(screen.history) == 0 |
| 920 |
|
| 921 |
await pilot.press("escape") |
| 922 |
await settle_filter(pilot, app, wait_for_workers) |
| 923 |
assert app.screen is screen |
| 924 |
assert screen.filter_input.value == "" |
| 925 |
assert screen.filter_input.has_focus, "an emptied filter also lost focus" |
| 926 |
assert [record.query_text for record in screen.history] == ["select 1;"] |
| 927 |
|
| 928 |
# empty, so the second one hands the screen back to the list |
| 929 |
await pilot.press("escape") |
| 930 |
await pilot.pause() |
| 931 |
assert app.screen is screen |
| 932 |
assert screen.list.has_focus |
| 933 |
|
| 934 |
await pilot.press("escape") |
| 935 |
await pilot.pause() |
| 936 |
assert app.screen is not screen |
| 937 |
|
| 938 |
|
| 939 |
@pytest.mark.asyncio |
| 940 |
async def test_escape_over_the_preview_leaves_the_screen( |
| 941 |
app: Harlequin, |
| 942 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 943 |
) -> None: |
| 944 |
"""Only the filter gives escape a second meaning.""" |
| 945 |
async with app.run_test() as pilot: |
| 946 |
while app.editor is None: |
| 947 |
await pilot.pause() |
| 948 |
screen = await focus_preview(pilot, app, wait_for_workers) |
| 949 |
|
| 950 |
await pilot.press("escape") |
| 951 |
await pilot.pause() |
| 952 |
assert app.screen is not screen |
| 953 |
|
| 954 |
|
| 955 |
@pytest.mark.asyncio |
| 956 |
async def test_enter_in_the_filter_goes_to_the_list( |
| 957 |
app: Harlequin, |
| 958 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 959 |
) -> None: |
| 960 |
"""Enter commits the search rather than the query, so the list can be driven.""" |
| 961 |
async with app.run_test() as pilot: |
| 962 |
while app.editor is None: |
| 963 |
await pilot.pause() |
| 964 |
app.post_message( |
| 965 |
QuerySubmitted(queries=["select 1;", "select 2 as orders;"], limit=None) |
| 966 |
) |
| 967 |
await pilot.pause() |
| 968 |
await wait_for_workers(app) |
| 969 |
await pilot.pause() |
| 970 |
screen = await open_history(pilot, app, wait_for_workers) |
| 971 |
|
| 972 |
await type_filter(pilot, app, "orders", wait_for_workers) |
| 973 |
await pilot.press("enter") |
| 974 |
await pilot.pause() |
| 975 |
assert app.screen is screen, "enter in the filter left the screen" |
| 976 |
assert screen.list.has_focus |
| 977 |
assert screen.filter_input.value == "orders", "the term was cleared" |
| 978 |
|
| 979 |
# and enter again, over the list, takes the query |
| 980 |
await pilot.press("enter") |
| 981 |
await pilot.pause() |
| 982 |
assert app.screen is not screen |
| 983 |
assert app.editor is not None |
| 984 |
assert app.editor.text == "select 2 as orders;" |
| 985 |
|
| 986 |
|
| 987 |
async def logged_by_both(app: Harlequin) -> None: |
| 988 |
"""One row from each program, one of them an error.""" |
| 989 |
agent = QueryLog(program="hsql", connection="foo") |
| 990 |
agent.write("select * from line_items", rows=3) |
| 991 |
agent.write("select bad from", status="error", error="Parser Error") |
| 992 |
agent.close() |
| 993 |
|
| 994 |
|
| 995 |
@pytest.mark.asyncio |
| 996 |
async def test_the_filter_controls_start_hidden( |
| 997 |
app: Harlequin, |
| 998 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 999 |
) -> None: |
| 1000 |
"""The screen is for picking a query; the controls are there when asked for.""" |
| 1001 |
async with app.run_test() as pilot: |
| 1002 |
while app.editor is None: |
| 1003 |
await pilot.pause() |
| 1004 |
screen = await open_history(pilot, app, wait_for_workers) |
| 1005 |
|
| 1006 |
assert not screen.filter_controls.display |
| 1007 |
assert screen.list.has_focus |
| 1008 |
|
| 1009 |
await pilot.press("ctrl+f") |
| 1010 |
await pilot.pause() |
| 1011 |
assert screen.filter_controls.display |
| 1012 |
assert screen.filter_input.has_focus, "the controls appeared without focus" |
| 1013 |
|
| 1014 |
await pilot.press("ctrl+f") |
| 1015 |
await pilot.pause() |
| 1016 |
assert not screen.filter_controls.display |
| 1017 |
assert screen.list.has_focus |
| 1018 |
|
| 1019 |
|
| 1020 |
@pytest.mark.asyncio |
| 1021 |
async def test_putting_the_controls_away_stops_filtering( |
| 1022 |
app: Harlequin, |
| 1023 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 1024 |
) -> None: |
| 1025 |
"""A filter still narrowing the list from behind a hidden control is a |
| 1026 |
list nobody can explain.""" |
| 1027 |
await logged_by_both(app) |
| 1028 |
async with app.run_test() as pilot: |
| 1029 |
while app.editor is None: |
| 1030 |
await pilot.pause() |
| 1031 |
screen = await open_history(pilot, app, wait_for_workers) |
| 1032 |
everything = len(screen.history) |
| 1033 |
|
| 1034 |
await type_filter(pilot, app, "line_items", wait_for_workers) |
| 1035 |
screen.status_select.value = "ok" |
| 1036 |
await settle_filter( |
| 1037 |
pilot, app, wait_for_workers, search="line_items", status="ok" |
| 1038 |
) |
| 1039 |
assert len(screen.history) < everything |
| 1040 |
|
| 1041 |
await pilot.press("ctrl+f") |
| 1042 |
await settle_filter(pilot, app, wait_for_workers) |
| 1043 |
assert not screen.filter_controls.display |
| 1044 |
assert screen.filter_input.value == "" |
| 1045 |
assert screen.program_select.is_blank() |
| 1046 |
assert screen.status_select.is_blank() |
| 1047 |
assert len(screen.history) == everything |
| 1048 |
|
| 1049 |
|
| 1050 |
@pytest.mark.asyncio |
| 1051 |
async def test_the_program_dropdown_narrows_to_one_command( |
| 1052 |
app: Harlequin, |
| 1053 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 1054 |
) -> None: |
| 1055 |
await logged_by_both(app) |
| 1056 |
async with app.run_test() as pilot: |
| 1057 |
while app.editor is None: |
| 1058 |
await pilot.pause() |
| 1059 |
app.post_message(QuerySubmitted(queries=["select 1;"], limit=None)) |
| 1060 |
await pilot.pause() |
| 1061 |
await wait_for_workers(app) |
| 1062 |
await pilot.pause() |
| 1063 |
screen = await open_history(pilot, app, wait_for_workers) |
| 1064 |
await show_filters(pilot, app) |
| 1065 |
|
| 1066 |
screen.program_select.value = "hsql" |
| 1067 |
await settle_filter(pilot, app, wait_for_workers, program="hsql") |
| 1068 |
assert [record.query_text for record in screen.history] == [ |
| 1069 |
"select bad from", |
| 1070 |
"select * from line_items", |
| 1071 |
] |
| 1072 |
|
| 1073 |
screen.program_select.value = "harlequin" |
| 1074 |
await settle_filter(pilot, app, wait_for_workers, program="harlequin") |
| 1075 |
assert [record.query_text for record in screen.history] == ["select 1;"] |
| 1076 |
|
| 1077 |
|
| 1078 |
@pytest.mark.asyncio |
| 1079 |
async def test_the_outcome_dropdown_narrows_to_one_status( |
| 1080 |
app: Harlequin, |
| 1081 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 1082 |
) -> None: |
| 1083 |
await logged_by_both(app) |
| 1084 |
async with app.run_test() as pilot: |
| 1085 |
while app.editor is None: |
| 1086 |
await pilot.pause() |
| 1087 |
screen = await open_history(pilot, app, wait_for_workers) |
| 1088 |
await show_filters(pilot, app) |
| 1089 |
|
| 1090 |
screen.status_select.value = "error" |
| 1091 |
await settle_filter(pilot, app, wait_for_workers, status="error") |
| 1092 |
assert [record.query_text for record in screen.history] == ["select bad from"] |
| 1093 |
|
| 1094 |
|
| 1095 |
@pytest.mark.asyncio |
| 1096 |
async def test_the_dropdowns_and_the_search_term_narrow_together( |
| 1097 |
app: Harlequin, |
| 1098 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 1099 |
) -> None: |
| 1100 |
await logged_by_both(app) |
| 1101 |
async with app.run_test() as pilot: |
| 1102 |
while app.editor is None: |
| 1103 |
await pilot.pause() |
| 1104 |
screen = await open_history(pilot, app, wait_for_workers) |
| 1105 |
|
| 1106 |
await type_filter(pilot, app, "select", wait_for_workers) |
| 1107 |
screen.program_select.value = "hsql" |
| 1108 |
screen.status_select.value = "ok" |
| 1109 |
await settle_filter( |
| 1110 |
pilot, app, wait_for_workers, search="select", program="hsql", status="ok" |
| 1111 |
) |
| 1112 |
assert [record.query_text for record in screen.history] == [ |
| 1113 |
"select * from line_items" |
| 1114 |
] |
| 1115 |
|
| 1116 |
|
| 1117 |
@pytest.mark.asyncio |
| 1118 |
async def test_an_open_dropdown_has_room_for_its_options( |
| 1119 |
app: Harlequin, |
| 1120 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 1121 |
) -> None: |
| 1122 |
"""A menu is an OptionList, so a rule written for the list can empty it. |
| 1123 |
|
| 1124 |
Asserted on the height it is given rather than on the options it holds: it |
| 1125 |
held all of them while rendering as an empty box. |
| 1126 |
""" |
| 1127 |
async with app.run_test() as pilot: |
| 1128 |
while app.editor is None: |
| 1129 |
await pilot.pause() |
| 1130 |
screen = await open_history(pilot, app, wait_for_workers) |
| 1131 |
await show_filters(pilot, app) |
| 1132 |
|
| 1133 |
for select in (screen.program_select, screen.status_select): |
| 1134 |
select.focus() |
| 1135 |
await pilot.pause() |
| 1136 |
await pilot.press("enter") |
| 1137 |
await pilot.pause() |
| 1138 |
await pilot.pause() |
| 1139 |
overlay = select.query_one(SelectOverlay) |
| 1140 |
assert overlay.option_count > 1 |
| 1141 |
assert overlay.size.height >= overlay.option_count, ( |
| 1142 |
f"{select.id} opened a menu with no room for its options" |
| 1143 |
) |
| 1144 |
await pilot.press("escape") |
| 1145 |
await pilot.pause() |
| 1146 |
|
| 1147 |
|
| 1148 |
@pytest.mark.asyncio |
| 1149 |
async def test_an_open_dropdown_keeps_enter_and_escape( |
| 1150 |
app: Harlequin, |
| 1151 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 1152 |
) -> None: |
| 1153 |
"""The screen owns both keys, so it has to hand them back to a menu.""" |
| 1154 |
await logged_by_both(app) |
| 1155 |
async with app.run_test() as pilot: |
| 1156 |
while app.editor is None: |
| 1157 |
await pilot.pause() |
| 1158 |
screen = await open_history(pilot, app, wait_for_workers) |
| 1159 |
await show_filters(pilot, app) |
| 1160 |
screen.program_select.focus() |
| 1161 |
await pilot.pause() |
| 1162 |
|
| 1163 |
await pilot.press("enter") |
| 1164 |
await pilot.pause() |
| 1165 |
assert screen.program_select.expanded, "enter did not open the menu" |
| 1166 |
|
| 1167 |
await pilot.press("escape") |
| 1168 |
await pilot.pause() |
| 1169 |
assert not screen.program_select.expanded, "escape did not close the menu" |
| 1170 |
assert app.screen is screen, "escape left the screen instead of the menu" |
| 1171 |
|
| 1172 |
await pilot.press("enter", "down", "enter") |
| 1173 |
await pilot.pause() |
| 1174 |
await settle_filter(pilot, app, wait_for_workers, program="harlequin") |
| 1175 |
assert screen.program_select.value == "harlequin" |
| 1176 |
assert app.screen is screen, "enter took a query instead of an option" |