| 1 |
from pathlib import Path |
| 2 |
from typing import Awaitable, Callable |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from harlequin import Harlequin |
| 7 |
from tests.functional_tests.helpers import wait_for_editor |
| 8 |
|
| 9 |
|
| 10 |
@pytest.mark.asyncio |
| 11 |
async def test_debug_info_screen( |
| 12 |
app: Harlequin, |
| 13 |
app_snapshot: Callable[..., Awaitable[bool]], |
| 14 |
wait_for_workers: Callable[[Harlequin], Awaitable[None]], |
| 15 |
monkeypatch: pytest.MonkeyPatch, |
| 16 |
) -> None: |
| 17 |
config_path = Path("tests/data/unit_tests/config/good_config.toml").resolve() |
| 18 |
monkeypatch.setattr( |
| 19 |
"harlequin.app.get_highest_priority_existing_config_file", lambda: config_path |
| 20 |
) |
| 21 |
|
| 22 |
async with app.run_test(size=(120, 36)) as pilot: |
| 23 |
await wait_for_workers(app) |
| 24 |
await wait_for_editor(pilot, app) |
| 25 |
assert len(app.screen_stack) == 1 |
| 26 |
|
| 27 |
app.profile_name = "my-duckdb-profile" |
| 28 |
app.action_show_debug_info() |
| 29 |
await pilot.pause() |
| 30 |
assert len(app.screen_stack) == 2 |
| 31 |
assert app.screen.id == "debug_info_screen" |
| 32 |
assert await app_snapshot(app, "Debug Info Screen") |
| 33 |
|
| 34 |
await pilot.press("tab") |
| 35 |
await pilot.press("tab") |
| 36 |
await pilot.press("shift+tab") |
| 37 |
await pilot.press("enter") |
| 38 |
await pilot.press("enter") |
| 39 |
await pilot.press("pageup") |
| 40 |
await pilot.press("pagedown") |
| 41 |
await pilot.press("escape") |
| 42 |
await pilot.pause() |
| 43 |
assert len(app.screen_stack) == 1 |
| 44 |
|
| 45 |
app.action_show_debug_info() |
| 46 |
await pilot.pause() |
| 47 |
assert len(app.screen_stack) == 2 |
| 48 |
await pilot.click() |
| 49 |
await pilot.pause() |
| 50 |
assert len(app.screen_stack) == 1 |
| 51 |
|
| 52 |
app.action_show_debug_info() |
| 53 |
await pilot.pause() |
| 54 |
assert app.screen.id == "debug_info_screen" |
| 55 |
assert await app_snapshot(app, "Debug Info Screen Focus") |
| 56 |
|
| 57 |
|
| 58 |
def test_the_debug_screen_prints_no_secret() -> None: |
| 59 |
"""The screen a user screenshots into an issue. |
| 60 |
|
| 61 |
Built directly rather than driven through the app: what is asserted is the |
| 62 |
content of the panels, and a snapshot of a screen would prove it for one |
| 63 |
theme and one terminal size. |
| 64 |
""" |
| 65 |
from harlequin.components.debug_info import HarlequinDebugInfo |
| 66 |
from harlequin.config import Config |
| 67 |
from harlequin.options import TextOption |
| 68 |
|
| 69 |
secret = "hunter2-and-then-some" |
| 70 |
profile = { |
| 71 |
"adapter": "duckdb", |
| 72 |
"conn_str": [f"md:my_db?motherduck_token={secret}"], |
| 73 |
"md_token": secret, |
| 74 |
"theme": "fruity", |
| 75 |
} |
| 76 |
options = [TextOption(name="md_token", description="A token.", secret=True)] |
| 77 |
widgets = HarlequinDebugInfo( |
| 78 |
all_keymaps=["vscode"], |
| 79 |
config=Config(default_profile="md", profiles={"md": profile}), |
| 80 |
config_path=Path(".harlequin.toml"), |
| 81 |
active_profile_name="md", |
| 82 |
active_profile_config=profile, |
| 83 |
adapter_options=options, |
| 84 |
).parse_info() |
| 85 |
|
| 86 |
printed = _every_string(widgets) |
| 87 |
assert secret not in printed |
| 88 |
assert "********" in printed |
| 89 |
# and still the screen it was: what a reader needs is all still there |
| 90 |
assert "fruity" in printed |
| 91 |
|
| 92 |
|
| 93 |
def test_the_debug_screen_names_the_tunnel_without_its_credential() -> None: |
| 94 |
"""`--ssh-host` takes an `ssh://user:pw@host`, and nothing else strips it.""" |
| 95 |
from harlequin.components.debug_info import HarlequinDebugInfo |
| 96 |
from harlequin.config import Config |
| 97 |
|
| 98 |
widgets = HarlequinDebugInfo( |
| 99 |
all_keymaps=[], |
| 100 |
config=Config(), |
| 101 |
config_path=None, |
| 102 |
ssh_tunnel="localhost:15439 -> db:5439 via ssh://tco:hunter2@bastion", |
| 103 |
).parse_info() |
| 104 |
|
| 105 |
printed = _every_string(widgets) |
| 106 |
assert "localhost:15439 -> db:5439" in printed |
| 107 |
assert "hunter2" not in printed |
| 108 |
|
| 109 |
|
| 110 |
def test_the_debug_screen_prints_no_secret_default() -> None: |
| 111 |
"""An adapter that ships a default for a secret has shipped the secret.""" |
| 112 |
from harlequin.components.debug_info import AdapterDebugInfo |
| 113 |
from harlequin.options import TextOption |
| 114 |
|
| 115 |
secret = "hunter2-and-then-some" |
| 116 |
widgets = AdapterDebugInfo( |
| 117 |
adapter_options=[ |
| 118 |
TextOption(name="md_token", description="x", default=secret, secret=True), |
| 119 |
TextOption(name="host", description="x", default="warehouse"), |
| 120 |
], |
| 121 |
adapter_type="DuckDbAdapter", |
| 122 |
adapter_details=None, |
| 123 |
adapter_driver_details=None, |
| 124 |
).parse_info() |
| 125 |
|
| 126 |
printed = _every_string(widgets) |
| 127 |
assert secret not in printed |
| 128 |
assert "********" in printed |
| 129 |
assert "warehouse" in printed |
| 130 |
|
| 131 |
|
| 132 |
def _every_string(widgets: object) -> str: |
| 133 |
"""Every piece of text a list of debug widgets would render, flattened.""" |
| 134 |
from harlequin.components.debug_info import DebugWidget |
| 135 |
|
| 136 |
if isinstance(widgets, DebugWidget): |
| 137 |
return f"{widgets.title}\n{_every_string(widgets.content)}" |
| 138 |
if isinstance(widgets, list): |
| 139 |
return "\n".join(_every_string(item) for item in widgets) |
| 140 |
return str(widgets) |