"""Hotkeys must never repair the user's text by synthesizing deletion keys.""" from __future__ import annotations import sys from types import SimpleNamespace import pytest from voice_dictation.platform.macos import autopaste, hotkey_listener @pytest.mark.parametrize("hotkey", ["cmd+space", "cmd+tab", "cmd+enter", "cmd+alt+d"]) def test_start_and_stop_hotkeys_do_not_edit_text( hotkey: str, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.setattr(hotkey_listener, "_modifier_mask", lambda _: 0) def forbidden_thread(*args: object, **kwargs: object) -> None: pytest.fail("Hotkey spawned an unsolicited background input operation") monkeypatch.setattr(hotkey_listener.threading, "Thread", forbidden_thread) calls: list[str] = [] def callback() -> None: calls.append("toggle") listener = hotkey_listener.MacHotkeyListener() assert listener.register(hotkey, callback) for _ in range(2): listener._hotkeys[0][-1]() assert calls == ["toggle", "toggle"] def test_paste_posts_only_command_v(monkeypatch: pytest.MonkeyPatch) -> None: events: list[tuple[int, bool]] = [] quartz = SimpleNamespace( CGEventCreateKeyboardEvent=lambda source, key, down: (key, down), CGEventPost=lambda tap, event: events.append(event), CGEventSetFlags=lambda event, flags: None, kCGEventFlagMaskCommand=1, kCGHIDEventTap=0, ) monkeypatch.setitem(sys.modules, "Quartz", quartz) autopaste.MacAutoPaste().paste() assert events == [(0x09, True), (0x09, False)]