| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from datetime import datetime, timedelta |
| 4 |
from typing import Awaitable, Callable |
| 5 |
from unittest.mock import MagicMock |
| 6 |
|
| 7 |
import pytest |
| 8 |
from rich.console import COLOR_SYSTEMS |
| 9 |
|
| 10 |
from harlequin import Harlequin |
| 11 |
from harlequin.app import QuerySubmitted |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture |
| 15 |
def mock_time(monkeypatch: pytest.MonkeyPatch) -> None: |
| 16 |
base = datetime(2024, 1, 26, hour=10) |
| 17 |
mock_datetime = MagicMock() |
| 18 |
mock_datetime.now.side_effect = (base + timedelta(minutes=i) for i in range(1000)) |
| 19 |
monkeypatch.setattr("harlequin.history.datetime", mock_datetime) |
| 20 |
|
| 21 |
mock_time = MagicMock() |
| 22 |
mock_time.monotonic.side_effect = (float(i) for i in range(1000)) |
| 23 |
monkeypatch.setattr("harlequin.app.time", mock_time) |
| 24 |
|
| 25 |
|
| 26 |
@pytest.mark.asyncio |
| 27 |
async def test_history_screen( |
| 28 |
app: Harlequin, |
| 29 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 30 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 31 |
mock_time: None, |
| 32 |
) -> None: |
| 33 |
snap_results: list[bool] = [] |
| 34 |
async with app.run_test(size=(120, 36)) as pilot: |
| 35 |
# rich.Syntax calculates different colors for the line numbers, depending |
| 36 |
# on the color system of the rich.console, which is different across different |
| 37 |
# GitHub action runners. Here we force everything to truecolor. |
| 38 |
app.console._color_system = COLOR_SYSTEMS["truecolor"] |
| 39 |
q = [f"select {i};" for i in range(15)] |
| 40 |
while app.editor is None: |
| 41 |
await pilot.pause() |
| 42 |
app.post_message(QuerySubmitted(queries=q, limit=None)) |
| 43 |
await pilot.pause() |
| 44 |
await wait_for_workers(app) |
| 45 |
await pilot.pause() |
| 46 |
# run a bad query |
| 47 |
app.post_message(QuerySubmitted(queries=["sel;"], limit=None)) |
| 48 |
await pilot.pause() |
| 49 |
await wait_for_workers(app) |
| 50 |
await pilot.pause() |
| 51 |
await pilot.press("space") |
| 52 |
|
| 53 |
await pilot.press("f8") |
| 54 |
await pilot.press("down") |
| 55 |
snap_results.append(await app_snapshot(app, "History Viewer")) |
| 56 |
|
| 57 |
await pilot.press("enter") |
| 58 |
await pilot.pause() |
| 59 |
snap_results.append(await app_snapshot(app, "New buffer with select 14")) |
| 60 |
|
| 61 |
assert all(snap_results) |
| 62 |
|
| 63 |
|
| 64 |
@pytest.mark.asyncio |
| 65 |
async def test_history_screen_crash( |
| 66 |
app: Harlequin, |
| 67 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 68 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 69 |
mock_time: None, |
| 70 |
) -> None: |
| 71 |
async with app.run_test() as pilot: |
| 72 |
q = [f"select {i};" for i in range(15)] |
| 73 |
while app.editor is None: |
| 74 |
await pilot.pause() |
| 75 |
app.post_message(QuerySubmitted(queries=q, limit=None)) |
| 76 |
await pilot.pause() |
| 77 |
await wait_for_workers(app) |
| 78 |
await pilot.pause() |
| 79 |
|
| 80 |
# https://github.com/tconbeer/harlequin/issues/485 |
| 81 |
await pilot.press("f8") |
| 82 |
await pilot.press("f8") |