Files
voculinux/tests/test_remote_desktop_portal.py
T
d11bd1bf0d feat(post-processing): pipe transcriptions through an optional postprocessing script (#479)
* feat(post-processing): pipe transcriptions through user-defined script

Add a configurable post-processing step that passes each transcription
result to an external executable via stdin and uses its stdout as the
text to type. Enables custom transforms (formatting, LLM rewriting, etc.)
without modifying Vocalinux. Falls back to original text on script
failure or timeout.

* test(post-processor): add unit tests and extract apply_post_processing

Move config-reading logic from main.py closure into apply_post_processing()
in post_processor.py so it can be tested independently. Add 11 unit tests
covering PostProcessor.process() and apply_post_processing().

* docs(post-processing): document post-processing script feature

* fix formatting of one file

* fix(post-processing): run the user script with a stripped host environment

Rebase compat: the host_process AST guard requires every subprocess
spawned for a host binary to pass env=host_env() so an AppImage's
LD_LIBRARY_PATH/GI_TYPELIB_PATH/PYTHONHOME do not leak in. Also give
apply_post_processing() a typed ConfigManager parameter and keep
text_callback_wrapper's local variable typed as str for mypy.

* fix(post-processing): preserve paragraph breaks and tighten script handling

Take script stdout verbatim instead of rstripping every trailing
newline, so the "new paragraph" voice command's break survives a
passthrough script. A single trailing newline is still dropped, but
only when the transcription had none — it is almost always just the
line terminator echo/print-style tools append.

Also stop writing transformed output to the log (it can carry
sensitive dictation into retained logs), catch only the expected
process/OS/encoding failures instead of every Exception, and add the
missing signature annotations.

* fix(main): run the post-processing script off the recognition thread

A user script may take seconds per segment, and text callbacks run on
the recognition thread that drains the bounded audio-segment queue;
stalling it drops queued dictation once the queue fills. Submit
post-processing and injection to a dedicated single-worker executor so
the backlog waits instead of losing speech, while keeping injection
order. On timeout or failure the worker falls back to the original
text, and an unexpected error is logged with a traceback and still
injects the original rather than silently dropping the segment.

Also pin the callback tests' get_str mock to no-script so they
exercise the synchronous path deterministically, and add a callback
test proving transformed text (including preserved paragraph breaks)
reaches the injector.

* docs(post-processing): annotate new callbacks and clarify stdout contract

Add the missing type annotations on the new Settings page callbacks
per the repository guide, and document that script stdout is used
verbatim (a lone trailing newline added by line-oriented tools is
dropped only when the transcription had none) and that scripts run on
a dedicated worker.

* test(main): annotate new post-processing callback tests

* fix(main): bind queued dictation to its target app and drain worker on quit

Route every transcription segment through the single post-processing
worker (a pass-through when no script is configured) so clearing the
script path mid-queue can no longer let a later segment overtake an
earlier one still running.

A segment that can sit in the backlog — a script is configured or a job
is still running — is bound to the application focused when it was
dictated; the worker drops it if focus has verifiably moved to another
application, so delayed dictation cannot land in the wrong app.

Wire the worker into the tray quit path via an on_quit hook: it clears
the acceptance flag, cancels pending jobs, and waits — bounded by the
script's own timeout — so nothing injects after the injector stops.

* fix(main): keep voice actions ordered and probe focus off the recognition thread

- Queue voice-command actions on the post-processing worker so an action
  can no longer overtake dictated text still waiting to be injected.
- Move the submit-time focus probe onto a dedicated executor; the
  recognition thread only submits the probe, and the job joins the result
  at inject time, so compositor tools can no longer stall consumption of
  the bounded audio-segment queue.
- Re-check the quit flag after the inject-time focus re-probe, then shut
  both workers down without waiting on a running script so quitting does
  not freeze on the script timeout.

* fix(main): capture submit-time focus on dedicated threads and serialize quit

- Replace the shared focus-probe executor with a fresh daemon thread per
  submission so a slow earlier probe cannot delay a later one past the
  focus change it is meant to capture; jobs read the result off a
  one-slot queue at inject time.
- Give queued voice actions the same submit-time focus binding as text
  segments so a queued shortcut is dropped instead of fired into a newly
  focused application.
- Hold injection_lock across the accept-check plus inject/action call so
  quit waits only for an in-flight injection instead of racing it.
- Queue the IDLE reset of last_injected_text behind pending worker jobs
  so a queued 'delete that' still sees the text it targets.

* fix(main): keep actions instant on a free worker and bound quit wait

- An action now starts a submit-time focus probe only when it actually
  queues behind other jobs; with a free worker there is no delay window
  to bind against, so the probe's compositor calls are skipped and the
  action stays immediate even when a script is configured.
- Probe and executor submission now happen inside pending_jobs_lock so
  queue order matches callback order — a job that saw an empty queue can
  no longer slip behind one that arrived while it was submitting.
- Every inject-path subprocess call (xdotool window/key/type, wtype and
  ydotool type) is bounded by a timeout, and the quit hook's
  injection_lock wait is capped, so quitting can no longer freeze on a
  stalled injector subprocess.

* fix(main): bind every action to submit-time focus without slowing it down

- Every voice action now starts a submit-time focus probe: submission
  never guarantees immediate execution, so skipping the binding left a
  focus-change window in which a shortcut could fire into another app.
  The worker waits on the probe for at most one second — milliseconds
  on a healthy desktop — so immediate actions stay immediate while a
  genuinely stalled probe falls back to the injector's own targeting.
- Quit waits for an in-flight injection without a cap again: every
  inject-path subprocess is individually bounded, so the wait always
  ends with the injection and a long transcription is never cut off.
- The wtype/ydotool typing timeout is now derived from the configured
  per-key delay (a generous multiple), so a legitimately slow type can
  never be cut mid-text; a timed-out type no longer falls through to
  the clipboard fallback, which would duplicate the partial fragment.

* fix(main): drop unverified actions and make quit-time injection abortable

- An action whose submit-time focus probe cannot answer inside its
  one-second bound is now dropped instead of running unverified: a
  stalled probe can no longer leave a shortcut pointing at whatever
  application happens to be focused.
- Quit asks the injector to abort in-flight injections, and injection
  paths honour it: the xdotool chunk loop stops between chunks, the
  wtype/ydotool type call runs as a polling Popen that can be killed,
  and paste/shortcut/backspace entry points exit early. The quit hook
  still waits for an in-flight injection, but the wait is bounded by at
  most one already-running subprocess call — never the injection's full
  length — and no subprocess is ever stopped mid-call by a timeout cap
  on the wait itself.
- Annotate the remaining untyped test helpers.

* fix(main): wait out slow focus probes; restore run() in wayland typing

- An action no longer races its submit-time focus probe on a one-second
  bound: the job waits for the probe's own answer, whose compositor
  calls already carry per-call timeouts and short-circuit absent tools.
  A slow but living probe delays the command instead of discarding it,
  so a valid "undo" can no longer be lost to timing alone, and focus
  verification still decides every drop.
- Wayland typing goes back to subprocess.run so tests keep intercepting
  the same entry point. Abortability is preserved by typing in
  200-character chunks with the shutdown check between calls — a
  whole-string call could not be interrupted, but no single chunk holds
  the injector for more than its own scaled budget.
- The abort flag gains a class-level default so injectors built without
  __init__ in tests still answer the checks; each real instance gets its
  own event in __init__.
- Update call-shape assertions for the typed-call timeout and finish
  annotating the test methods touched by the callback refactor.

* fix(inject): never retype text a failed chunked call already sent

- _PartiallyTyped carries the confirmed-delivered prefix length out of
  the chunked type loops, so every downstream fallback continues from
  the untyped remainder: the Wayland-to-xdotool switch types only the
  tail, and the clipboard fallback copies only the tail, never the
  whole string a second time.
- The xdotool retry loop gains the same accounting — a retry now
  resumes at the first untyped chunk instead of restarting at the top
  and duplicating everything the failed attempt already delivered.
- Annotate the remaining test signature the repository guide requires.

* fix(inject): report partial typing as failure even when clipboard lands

A chunked type call that dies mid-text leaves only a prefix on screen,
and the remainder-fallback keeps it that way: it copies the tail for a
manual paste but now returns False, so the caller no longer records the
full transcription as injected and a following "delete that" cannot
erase text that was never typed.

* fix(inject): keep deletion state aligned with delivered text

inject_text now records how many characters a call confirmed on screen
as last_typed_count: the whole length on success, the typed prefix
after a partial failure, -1 when the delivered amount is unknowable.
The transcription caller uses it to keep last_injected_text honest —
a following "delete that" erases the typed prefix or nothing, never
the previous segment or text that never arrived.

* fix(inject): never replay input a timed-out portal job may have delivered

- RemoteDesktopPortalError.delivered=None marks an unreported job's count
  as unknowable; all three portal fallbacks demote and skip instead of
  replaying, and text injection surfaces as TimeoutExpired so no
  full-text clipboard copy is offered
- _PartiallyTyped counts raised after a portal demote are rebased onto
  the original text (typed_prefix) so the caller clips the real prefix
- evdev: a consumed pure-modifier press is withheld and replayed ahead
  of the first non-target press, so holding RightAlt for AltGr
  composition still reaches the application
- history: extend_latest gains an expected_latest CAS guard so a late
  segment from an older worker merges only into its own session's
  snippet, never into a newer session's entry
- quit path aborts in-flight injections before waiting on the lock so
  quitting cannot freeze behind a long transcription's typing budget
- tests: stop _ensure_ydotoold leaking a real Popen in Wayland tests

* fix(shortcut): replay withheld modifiers across devices and after SYN_DROPPED

- Replay withheld modifier presses from every device, not only the fd the
  character arrived on: split keyboards deliver AltGr and the character on
  separate devices, and a per-fd check forwarded a plain character.
- Let the post-SYN_DROPPED resync run when only withheld modifiers exist
  for the fd, so a press whose release the dropped burst ate is pruned
  instead of replaying a stale modifier on the next ordinary key.

* fix(history,pads): keep late segments pad-bound deliveries and pad deletions correct

- Track the worker behind the ended session's snippet so late segments
  from an older worker cannot merge into a newer session's entry
- Skip the focus-change drop for dictation-pad segments and pad-bound
  editing commands, which never target the focused application
- Refuse 'delete that' when a manual pad edit blurred the segment
  boundaries instead of deleting the stale recorded length

* style: drop unneeded nonlocal for mutated worker set

* fix(shortcut): share one evdev device layer across shortcut listeners

Each KeyboardShortcutManager built its own EvdevKeyboardBackend, and on
evdev every backend opened and grabbed the same physical keyboards: the
first grabber received all events and the rest received none, so
per-language shortcuts could never fire on Wayland.

All backends now bind to a process-wide EvdevDeviceHub that opens each
keyboard once, owns the grabs and the paired uinput clones, and fans
every event out to each registered engine. An event is re-emitted to
applications only when no engine consumed it, so suppression semantics
stay the same while per-engine matching state (combo progress, withheld
modifiers, swallowed presses) remains independent.

* fix(history): own late segments per worker and decide pad routing once

- ended_worker_snippets maps each ended session's worker to its own
  (snippet, commit epoch): late deliveries merge into their session's
  entry however many sessions have committed since, so one session's
  stragglers never split across Recent Snippets entries.
- The leftover-segment commit reads the live history epoch inside the
  session lock: a mid-session clear() no longer makes the stale ended
  epoch refuse valid post-clear dictation.
- The pad-bound routing decision is captured once per job and passed to
  the injector/action handler, so a toggle flip between the focus check
  and delivery cannot reroute text or shortcuts into the focused app.
- Dead recognition workers are pruned from the tracking set and map so
  long runs don't accumulate finished threads.
- The custom-terms switch now suppresses GTK's own state-set on save
  failure so it shows the value that actually persisted.

* fix(shortcut): serialize evdev device layer teardown against restart

The last listener's unregister released the engine lock before joining
the monitor thread and closing devices, so a new listener registering in
that gap opened keyboards the stale teardown then closed — it stayed
active but dead, and briefly grabbed keyboards stopped forwarding input.

A lifecycle lock now covers the whole close/open transition in register,
unregister, and the monitor's unexpected-exit teardown, so a cold start
can only open devices after the previous generation finished closing
(also making the new generation's grabs succeed). A generation stamp
tells a monitor thread that outlived its join() its incarnation is over
even when running has gone True again.

* fix(injection): log focus-probe failures and narrow clipboard fallback catch

A crashed focus probe turned into an unknown result with no logged
error, indistinguishable from unavailable focus information; the
partial-typing clipboard fallback caught every exception. Log the probe
failure with context and narrow the clipboard catch to the exceptions
the fallback path can actually raise.

* fix(shortcut): join the evdev monitor outside the lifecycle lock

If the monitor exited unexpectedly while the last listener unregistered,
its cleanup queued on the lifecycle lock that unregister() held for the
join — stalling shutdown for the join's full 2s timeout. The close now
finishes inside the lock and the join waits after it is released, so the
monitor's own cleanup can run and exit instead of queueing behind it.

Closing before the join also dies the monitor's read/select on the
closed fds right away. A _closed gate on _open_keyboard_device keeps a
rescan from opening a device between detach and close that nothing
would ever close.

* fix(injection): narrow focus-probe catch to the raisable exception set

get_focused_window() reports Optional and never raises; the catch now
covers only what can realistically escape the queue write and logs it
with context, keeping the waiting job released.

* fix(shortcut): tag evdev device fds with the generation that opened them

A monitor that outlived join() could still touch a newer generation's
devices: the loop re-checked the generation only at the top, so mid-iteration
it could dispatch from, forward to, or remove devices a restarted listener
had just opened.

Every fd is now tagged with the generation that opened it, and the tag is
verified at each point a monitor could mutate shared state — fd lookup for
dispatch, clone forwarding, forwarder release, device removal, and rescan
opens — while dispatch re-checks the generation between events and the
per-fd loop breaks on it. Cold starts bump the generation before opening
so fresh devices are tagged for the new generation. Devices an older
generation opened stay tagged stale, so its reader leaves them alone.

---------

Co-authored-by: Jatin K Malik <jatinkrmalik@gmail.com>
2026-09-30 03:55:17 -07:00

821 lines
34 KiB
Python

"""Tests for the RemoteDesktop portal injection backend and its selection."""
import subprocess
import threading
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple
from unittest.mock import MagicMock, patch
import pytest
from vocalinux.text_injection import remote_desktop_portal as rdp
from vocalinux.text_injection import text_injector as ti
from vocalinux.text_injection.remote_desktop_portal import (
KEYSYM_BACKSPACE,
RemoteDesktopPortal,
RemoteDesktopPortalError,
char_to_keysym,
portal_keysym_for_name,
)
from vocalinux.text_injection.text_injector import DesktopEnvironment, TextInjector
# ---------------------------------------------------------------------------
# Minimal GLib/Gio stand-ins. conftest replaces ``gi`` with a MagicMock, so the
# portal client runs against these fakes: a Variant carrying a real Python
# value tree, a connection that answers portal Requests synchronously, and a
# MainContext that delivers them on iteration().
# ---------------------------------------------------------------------------
class _FakeVariantType:
def __init__(self, signature: str) -> None:
self.signature = signature
class _FakeVariant:
"""Tiny GLib.Variant stand-in wrapping a Python value."""
def __init__(self, fmt: Optional[str], value: Any) -> None:
self.format = fmt
self.value = value
def get_child_value(self, index: int) -> "_FakeVariant":
child = self.value[index]
if isinstance(child, _FakeVariant):
return child
return _FakeVariant(None, child)
def get_variant(self) -> "_FakeVariant":
return (
self.value if isinstance(self.value, _FakeVariant) else _FakeVariant(None, self.value)
)
def get_string(self) -> str:
return str(self.value)
def get_uint32(self) -> int:
return int(self.value)
def get_int32(self) -> int:
return int(self.value)
def lookup_value(
self, key: str, variant_type: Optional[_FakeVariantType]
) -> Optional["_FakeVariant"]:
child = self.value.get(key) if isinstance(self.value, dict) else None
if child is None:
return None
sig = variant_type.signature if variant_type is not None else None
if isinstance(child, _FakeVariant):
if sig is not None and child.format != sig:
return None
return child
if sig in ("s", "o") and not isinstance(child, str):
return None
if sig in ("u", "i", "b", "d", "t", "x") and not isinstance(child, (int, float)):
return None
return _FakeVariant(sig, child)
class _FakeGLib:
"""Namespace covering the GLib surface the portal client touches."""
Variant = _FakeVariant
VariantType = _FakeVariantType
class _FakePortalConn:
"""Session-bus double: records calls and answers portal Requests.
Request methods (CreateSession/SelectDevices/Start) queue a Response that
``_FakeContext.iteration`` dispatches once the client has subscribed —
exactly how the real portal behaves.
"""
def __init__(
self,
version: Optional[int] = 2,
results: Optional[Dict[str, Any]] = None,
cancels: Optional[Dict[str, int]] = None,
notify_fail_at: Optional[int] = None,
) -> None:
self.version = version
self.results = dict(results or {})
self.cancels = dict(cancels or {}) # method -> remaining cancelled replies
# Raise inside NotifyKeyboardKeysym once this many notifies landed.
self.notify_fail_at = notify_fail_at
self.calls: List[Tuple[str, str, Any]] = []
self.notifies: List[Tuple[int, int]] = []
self.closed_sessions: List[Any] = []
self.subscriptions: Dict[str, Any] = {}
self.pending: List[Tuple[str, int, Dict[str, Any]]] = []
self._next_request = 0
def call_sync(
self,
bus_name: Any,
object_path: Any,
interface: str,
method: str,
params: Any,
reply_type: Any,
flags: Any,
timeout_ms: int,
cancellable: Any,
) -> Any:
self.calls.append((interface, method, object_path, params))
if interface == rdp._PROPERTIES_IFACE:
if self.version is None:
raise RuntimeError("RemoteDesktop interface not implemented")
return _FakeVariant("(v)", (_FakeVariant("u", self.version),))
if interface == rdp._SESSION_IFACE:
self.closed_sessions.append(object_path)
return None
if method == "NotifyKeyboardKeysym":
if self.notify_fail_at is not None and len(self.notifies) >= self.notify_fail_at:
raise RuntimeError("session-bus connection lost mid-injection")
self.notifies.append(
(
params.get_child_value(2).get_int32(),
params.get_child_value(3).get_uint32(),
)
)
return None
if method in ("CreateSession", "SelectDevices", "Start"):
self._next_request += 1
request_path = f"/org/freedesktop/portal/request/{self._next_request}"
remaining = self.cancels.get(method, 0)
code = rdp._RESPONSE_CANCELLED if remaining else rdp._RESPONSE_OK
if remaining:
self.cancels[method] = remaining - 1
self.pending.append((request_path, code, dict(self.results)))
return _FakeVariant("(o)", (request_path,))
raise AssertionError(f"Unexpected portal call: {interface}.{method}")
def signal_subscribe(
self,
sender: Any,
interface: Any,
member: Any,
object_path: Any,
arg0: Any,
flags: Any,
callback: Any,
) -> int:
self.subscriptions[object_path] = callback
return len(self.subscriptions)
def signal_unsubscribe(self, subscription_id: int) -> None:
pass
class _FakeContext:
"""MainContext double: delivers queued Responses on iteration."""
def __init__(self, conn: _FakePortalConn) -> None:
self._conn = conn
def iteration(self, may_block: bool) -> bool:
pending, self._conn.pending = self._conn.pending, []
for request_path, code, results in pending:
callback = self._conn.subscriptions.get(request_path)
if callback is not None:
callback(
self._conn,
"org.freedesktop.portal.Desktop",
request_path,
rdp._REQUEST_IFACE,
"Response",
_FakeVariant("(ua{sv})", (code, results)),
)
return True
@pytest.fixture()
def portal_client(
monkeypatch: pytest.MonkeyPatch,
) -> Callable[[_FakePortalConn], RemoteDesktopPortal]:
"""Build a RemoteDesktopPortal wired to a fake bus; returns (portal, conn)."""
def _run_submit(self: RemoteDesktopPortal, func: Callable[[], Any], timeout: float) -> Any:
try:
return func()
except RemoteDesktopPortalError:
raise
except Exception as e:
raise RemoteDesktopPortalError(str(e)) from e
def _make(conn: _FakePortalConn) -> RemoteDesktopPortal:
monkeypatch.setattr(rdp, "GLib", _FakeGLib)
monkeypatch.setattr(RemoteDesktopPortal, "_submit", _run_submit)
portal = RemoteDesktopPortal()
portal._conn = conn
portal._context = _FakeContext(conn)
return portal
return _make
_SESSION_RESULTS = {
"session_handle": "/org/freedesktop/portal/session/1",
"restore_token": "token-abc",
}
class TestKeysymMapping:
"""Character/shortcut-name to keysym conversion."""
def test_latin1_and_ascii(self) -> None:
assert char_to_keysym("a") == 0x61
assert char_to_keysym("A") == 0x41
assert char_to_keysym("é") == 0xE9
assert char_to_keysym("1") == 0x31
def test_unicode_above_latin1_uses_unicode_keysym(self) -> None:
assert char_to_keysym("€") == 0x01000000 | 0x20AC
assert char_to_keysym("\u4e2d") == 0x01000000 | 0x4E2D
def test_whitespace_maps_to_named_keysyms(self) -> None:
assert char_to_keysym("\n") == 0xFF0D
assert char_to_keysym("\t") == 0xFF09
assert char_to_keysym("\r") is None
def test_unrepresentable_chars_return_none(self) -> None:
assert char_to_keysym("\x00") is None
assert char_to_keysym("\x7f") is None
assert char_to_keysym("\ud800") is None
def test_portal_keysym_for_name(self) -> None:
assert portal_keysym_for_name("ctrl") == 0xFFE3
assert portal_keysym_for_name("SUPER") == 0xFFEB
assert portal_keysym_for_name("backspace") == KEYSYM_BACKSPACE
assert portal_keysym_for_name("z") == 0x7A
assert portal_keysym_for_name("not-a-key") is None
PortalFactory = Callable[[_FakePortalConn], RemoteDesktopPortal]
class TestPortalClient:
"""The synchronous client against the faked portal protocol."""
def test_supported_tracks_gi_availability(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(rdp, "PORTAL_AVAILABLE", False)
assert RemoteDesktopPortal.supported() is False
monkeypatch.setattr(rdp, "PORTAL_AVAILABLE", True)
assert RemoteDesktopPortal.supported() is True
def test_probe_reads_interface_version_and_caches(self, portal_client: PortalFactory) -> None:
conn = _FakePortalConn(version=2)
portal = portal_client(conn)
assert portal.probe() is True
assert portal.probe() is True
get_calls = [c for c in conn.calls if c[0] == rdp._PROPERTIES_IFACE]
assert len(get_calls) == 1
def test_probe_fails_when_interface_missing(self, portal_client: PortalFactory) -> None:
conn = _FakePortalConn(version=None)
portal = portal_client(conn)
assert portal.probe() is False
def test_inject_text_emits_keysyms_after_one_session(
self, portal_client: PortalFactory
) -> None:
conn = _FakePortalConn(version=2, results=_SESSION_RESULTS)
portal = portal_client(conn)
assert portal.inject_text("hé") is True
methods = [c[1] for c in conn.calls]
assert methods[:4] == [
"Get",
"CreateSession",
"SelectDevices",
"Start",
]
assert conn.notifies == [
(0x68, 1),
(0x68, 0),
(0xE9, 1),
(0xE9, 0),
]
# A second injection reuses the session rather than re-prompting.
conn.calls.clear()
assert portal.inject_text("a") is True
assert [c[1] for c in conn.calls] == ["NotifyKeyboardKeysym"] * 2
def test_inject_text_persists_restore_token(self, portal_client: PortalFactory) -> None:
conn = _FakePortalConn(version=2, results=_SESSION_RESULTS)
portal = portal_client(conn)
portal.inject_text("a")
assert rdp._load_restore_token() == "token-abc"
def test_version1_session_skips_persistence(
self, portal_client: PortalFactory, monkeypatch: pytest.MonkeyPatch
) -> None:
conn = _FakePortalConn(version=1, results=_SESSION_RESULTS)
saved: List[str] = []
monkeypatch.setattr(rdp, "_save_restore_token", saved.append)
portal = portal_client(conn)
portal.inject_text("a")
create = [c for c in conn.calls if c[1] == "CreateSession"][0]
assert "persist_mode" not in create[3].get_child_value(0).value
select = [c for c in conn.calls if c[1] == "SelectDevices"][0]
select_options = select[3].get_child_value(1).value
assert "persist_mode" not in select_options
assert "restore_token" not in select_options
assert saved == []
def test_restore_token_is_sent_to_select_devices(
self, portal_client: PortalFactory, monkeypatch: pytest.MonkeyPatch
) -> None:
# restore_token is a SelectDevices option on interface v2; sending it
# in CreateSession leaves it unread and the user is prompted again.
monkeypatch.setattr(rdp, "_load_restore_token", lambda: "old-token")
conn = _FakePortalConn(version=2, results=_SESSION_RESULTS)
portal = portal_client(conn)
portal.inject_text("a")
create = [c for c in conn.calls if c[1] == "CreateSession"][0]
create_options = create[3].get_child_value(0).value
assert "restore_token" not in create_options
select = [c for c in conn.calls if c[1] == "SelectDevices"][0]
select_options = select[3].get_child_value(1).value
assert select_options["restore_token"].value == "old-token"
assert select_options["persist_mode"].value == 2
def test_cancelled_start_is_not_retried(
self, portal_client: PortalFactory, monkeypatch: pytest.MonkeyPatch
) -> None:
# Dismissing the permission dialog must be respected: no second Start
# on the same session, and the saved restore token is kept.
monkeypatch.setattr(rdp, "_load_restore_token", lambda: "old-token")
saved: List[str] = []
monkeypatch.setattr(rdp, "_save_restore_token", saved.append)
conn = _FakePortalConn(version=2, results=_SESSION_RESULTS, cancels={"Start": 5})
portal = portal_client(conn)
with pytest.raises(RemoteDesktopPortalError):
portal.inject_text("a")
starts = [c for c in conn.calls if c[1] == "Start"]
assert len(starts) == 1
assert saved == []
def test_failed_session_is_closed(
self, portal_client: PortalFactory, monkeypatch: pytest.MonkeyPatch
) -> None:
# A session whose Start never succeeds is closed so the compositor
# does not keep a half-granted session alive.
monkeypatch.setattr(rdp, "_load_restore_token", lambda: "old-token")
conn = _FakePortalConn(version=2, results=_SESSION_RESULTS, cancels={"Start": 1})
portal = portal_client(conn)
with pytest.raises(RemoteDesktopPortalError):
portal.inject_text("a")
assert conn.closed_sessions == ["/org/freedesktop/portal/session/1"]
def test_inject_text_reports_delivered_count(self, portal_client: PortalFactory) -> None:
# 'b' press lands but its release fails: the character was typed, so
# the error reports it and the caller retries only the tail.
conn = _FakePortalConn(version=1, results=_SESSION_RESULTS, notify_fail_at=3)
portal = portal_client(conn)
with pytest.raises(RemoteDesktopPortalError) as exc_info:
portal.inject_text("abc")
assert exc_info.value.delivered == 2
def test_user_cancel_raises(self, portal_client: PortalFactory) -> None:
conn = _FakePortalConn(version=2, cancels={"Start": 5})
portal = portal_client(conn)
with pytest.raises(RemoteDesktopPortalError):
portal.inject_text("a")
def test_session_handle_as_object_path_is_accepted(self, portal_client: PortalFactory) -> None:
conn = _FakePortalConn(
version=1,
results={"session_handle": _FakeVariant("o", "/session/o")},
)
portal = portal_client(conn)
assert portal.inject_text("a") is True
def test_tap_keysym_press_releases_count_times(self, portal_client: PortalFactory) -> None:
conn = _FakePortalConn(version=1, results=_SESSION_RESULTS)
portal = portal_client(conn)
portal.tap_keysym(KEYSYM_BACKSPACE, 3)
assert (
conn.notifies
== [
(KEYSYM_BACKSPACE, 1),
(KEYSYM_BACKSPACE, 0),
]
* 3
)
def test_tap_keysym_reports_delivered_count(self, portal_client: PortalFactory) -> None:
conn = _FakePortalConn(version=1, results=_SESSION_RESULTS, notify_fail_at=2)
portal = portal_client(conn)
with pytest.raises(RemoteDesktopPortalError) as exc_info:
portal.tap_keysym(KEYSYM_BACKSPACE, 5)
# One full tap (press+release) landed; the second press failed.
assert exc_info.value.delivered == 1
def test_send_shortcut_wraps_key_with_modifiers(self, portal_client: PortalFactory) -> None:
conn = _FakePortalConn(version=1, results=_SESSION_RESULTS)
portal = portal_client(conn)
portal.send_shortcut([(["ctrl"], "a"), ([], "b")])
assert conn.notifies == [
(0xFFE3, 1), # ctrl down
(0x61, 1), # a down
(0x61, 0), # a up
(0xFFE3, 0), # ctrl up
(0x62, 1), # b down
(0x62, 0), # b up
]
def test_send_shortcut_unknown_key_raises(self, portal_client: PortalFactory) -> None:
conn = _FakePortalConn(version=1, results=_SESSION_RESULTS)
portal = portal_client(conn)
with pytest.raises(RemoteDesktopPortalError):
portal.send_shortcut([([], "flying-car")])
def test_close_closes_portal_session(self, portal_client: PortalFactory) -> None:
conn = _FakePortalConn(version=1, results=_SESSION_RESULTS)
portal = portal_client(conn)
portal.inject_text("a")
portal.close()
assert conn.closed_sessions == ["/org/freedesktop/portal/session/1"]
# ---------------------------------------------------------------------------
# Backend selection inside TextInjector.
# ---------------------------------------------------------------------------
_TI = "vocalinux.text_injection.text_injector"
def _stub_portal(available: bool = True) -> Tuple[MagicMock, MagicMock]:
"""A RemoteDesktopPortal stand-in: (class mock, shared instance mock)."""
instance = MagicMock()
instance.probe.return_value = available
cls = MagicMock()
cls.supported.return_value = True
cls.return_value = instance
return cls, instance
def _wayland_injector(
monkeypatch: pytest.MonkeyPatch,
*,
tools: Dict[str, Optional[str]],
portal: MagicMock,
forced: Optional[str] = None,
extra_env: Optional[Dict[str, Optional[str]]] = None,
) -> TextInjector:
"""Construct a TextInjector on a mocked Wayland session.
``extra_env`` values of None remove the variable instead of setting it.
"""
env: Dict[str, Optional[str]] = {
"XDG_SESSION_TYPE": "wayland",
"WAYLAND_DISPLAY": "wayland-0",
}
env.update(extra_env or {})
if forced:
env["VOCALINUX_FORCE_BACKEND"] = forced
for key, value in env.items():
if value is None:
monkeypatch.delenv(key, raising=False)
else:
monkeypatch.setenv(key, value)
monkeypatch.setattr("shutil.which", lambda cmd: tools.get(cmd))
monkeypatch.setattr(f"{_TI}.is_ibus_available", lambda: False)
monkeypatch.setattr(f"{_TI}.RemoteDesktopPortal", portal)
run = MagicMock()
run.return_value.returncode = 0
run.return_value.stderr = ""
monkeypatch.setattr("subprocess.run", run)
return TextInjector()
class TestPortalBackendSelection:
"""Where the portal lands in Wayland backend selection."""
def test_auto_prefers_portal_over_ydotool_wtype(self, monkeypatch: pytest.MonkeyPatch) -> None:
cls, _ = _stub_portal(available=True)
injector = _wayland_injector(
monkeypatch,
tools={"wtype": "/usr/bin/wtype", "ydotool": "/usr/bin/ydotool"},
portal=cls,
)
assert injector.wayland_tool == "portal"
assert injector.environment == DesktopEnvironment.WAYLAND
def test_pin_portal_selects_it(self, monkeypatch: pytest.MonkeyPatch) -> None:
cls, _ = _stub_portal(available=True)
injector = _wayland_injector(
monkeypatch,
tools={"wtype": "/usr/bin/wtype"},
portal=cls,
forced="portal",
)
assert injector.wayland_tool == "portal"
def test_pin_wtype_beats_portal(self, monkeypatch: pytest.MonkeyPatch) -> None:
cls, _ = _stub_portal(available=True)
injector = _wayland_injector(
monkeypatch,
tools={"wtype": "/usr/bin/wtype"},
portal=cls,
forced="wtype",
)
assert injector.wayland_tool == "wtype"
def test_pinned_tool_skips_portal_probe(self, monkeypatch: pytest.MonkeyPatch) -> None:
# With an installed backend explicitly pinned, the portal must not
# even be probed -- no worker thread, no session-bus round trip.
cls, _ = _stub_portal(available=True)
injector = _wayland_injector(
monkeypatch,
tools={"wtype": "/usr/bin/wtype"},
portal=cls,
forced="wtype",
)
assert injector.wayland_tool == "wtype"
cls.assert_not_called()
def test_uninstalled_pin_still_probes_portal(self, monkeypatch: pytest.MonkeyPatch) -> None:
# A pin the system cannot honour falls back to autodetection, which
# still includes the portal.
cls, _ = _stub_portal(available=True)
injector = _wayland_injector(
monkeypatch,
tools={"ydotool": "/usr/bin/ydotool"},
portal=cls,
forced="wtype",
)
assert injector.wayland_tool == "portal"
def test_portal_unavailable_falls_back_to_ydotool(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
cls, _ = _stub_portal(available=False)
with (
patch.object(TextInjector, "_is_ydotoold_running", return_value=True),
patch.object(TextInjector, "_uinput_usable", return_value=True),
):
injector = _wayland_injector(
monkeypatch, tools={"ydotool": "/usr/bin/ydotool"}, portal=cls
)
assert injector.wayland_tool == "ydotool"
def test_pin_portal_unavailable_falls_back(self, monkeypatch: pytest.MonkeyPatch) -> None:
cls, _ = _stub_portal(available=False)
injector = _wayland_injector(
monkeypatch,
tools={"wtype": "/usr/bin/wtype"},
portal=cls,
forced="portal",
)
assert injector.wayland_tool == "wtype"
def test_flatpak_xdotool_session_upgrades_to_portal(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Flatpak without a Wayland socket starts WAYLAND_XDOTOOL; the portal
still reaches native clients, so selection upgrades the environment."""
cls, _ = _stub_portal(available=True)
injector = _wayland_injector(
monkeypatch,
tools={},
portal=cls,
extra_env={
"WAYLAND_DISPLAY": None,
"FLATPAK_ID": "com.vocalinux.Vocalinux",
"DISPLAY": ":99",
},
)
assert injector.wayland_tool == "portal"
assert injector.environment == DesktopEnvironment.WAYLAND
def test_x11_never_probes_the_portal(self, monkeypatch: pytest.MonkeyPatch) -> None:
cls, instance = _stub_portal(available=True)
injector = _wayland_injector(
monkeypatch,
tools={"xdotool": "/usr/bin/xdotool"},
portal=cls,
extra_env={
"XDG_SESSION_TYPE": "x11",
"WAYLAND_DISPLAY": None,
"DISPLAY": ":0",
},
)
assert injector.environment == DesktopEnvironment.X11
cls.assert_not_called()
instance.probe.assert_not_called()
def _bare_injector(**attrs: Any) -> TextInjector:
"""A TextInjector without __init__, carrying only what the test needs."""
injector = TextInjector.__new__(TextInjector)
injector._state_lock = threading.Lock()
injector._ibus_injector = None
injector._ibus_ready = False
injector._ibus_init_failed = False
injector._ibus_init_thread = None
injector._portal = None
for key, value in attrs.items():
setattr(injector, key, value)
return injector
class TestPortalInjectionPaths:
"""Portal dispatch inside the Wayland injection paths."""
def test_inject_text_routes_to_portal(self) -> None:
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
injector._portal.inject_text.return_value = True
with (
patch.object(injector, "_wait_for_modifiers_released"),
patch("subprocess.run") as mock_run,
):
injector._inject_with_wayland_tool("hello")
injector._portal.inject_text.assert_called_once_with("hello")
mock_run.assert_not_called()
def test_inject_demotes_to_wtype_on_portal_failure(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
injector._portal.inject_text.side_effect = RemoteDesktopPortalError("boom")
monkeypatch.setattr("shutil.which", lambda c: "/usr/bin/wtype" if c == "wtype" else None)
with (
patch.object(injector, "_wait_for_modifiers_released"),
patch("subprocess.run") as mock_run,
):
injector._inject_with_wayland_tool("hello")
assert injector.wayland_tool == "wtype"
mock_run.assert_called_once()
assert mock_run.call_args[0][0][:1] == ["wtype"]
def test_inject_without_any_fallback_raises(self, monkeypatch: pytest.MonkeyPatch) -> None:
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
injector._portal.inject_text.side_effect = RemoteDesktopPortalError("boom")
monkeypatch.setattr("shutil.which", lambda c: None)
with patch.object(injector, "_wait_for_modifiers_released"):
with pytest.raises(RuntimeError):
injector._inject_with_wayland_tool("hello")
def test_press_backspace_uses_portal_tap(self) -> None:
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
with patch.object(injector, "_wait_for_modifiers_released"):
assert injector.press_backspace(2) is True
injector._portal.tap_keysym.assert_called_once_with(KEYSYM_BACKSPACE, 2)
def test_press_backspace_demotes_and_recurses(self, monkeypatch: pytest.MonkeyPatch) -> None:
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
injector._portal.tap_keysym.side_effect = RemoteDesktopPortalError("boom")
monkeypatch.setattr("shutil.which", lambda c: "/usr/bin/wtype" if c == "wtype" else None)
with (
patch.object(injector, "_wait_for_modifiers_released"),
patch("subprocess.run") as mock_run,
):
assert injector.press_backspace(2) is True
assert injector.wayland_tool == "wtype"
assert mock_run.call_args[0][0] == ["wtype", "-k", "BackSpace", "-k", "BackSpace"]
def test_inject_retries_only_undelivered_text(self, monkeypatch: pytest.MonkeyPatch) -> None:
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
injector._portal.inject_text.side_effect = RemoteDesktopPortalError("boom", delivered=2)
monkeypatch.setattr("shutil.which", lambda c: "/usr/bin/wtype" if c == "wtype" else None)
with (
patch.object(injector, "_wait_for_modifiers_released"),
patch("subprocess.run") as mock_run,
):
injector._inject_with_wayland_tool("hello")
# The portal typed "he" before failing; the fallback gets only "llo".
assert mock_run.call_args[0][0] == ["wtype", "llo"]
def test_backspace_retries_only_remaining_taps(self, monkeypatch: pytest.MonkeyPatch) -> None:
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
injector._portal.tap_keysym.side_effect = RemoteDesktopPortalError("boom", delivered=1)
monkeypatch.setattr("shutil.which", lambda c: "/usr/bin/wtype" if c == "wtype" else None)
with (
patch.object(injector, "_wait_for_modifiers_released"),
patch("subprocess.run") as mock_run,
):
assert injector.press_backspace(3) is True
# One press was already delivered through the portal.
assert mock_run.call_args[0][0] == ["wtype", "-k", "BackSpace", "-k", "BackSpace"]
def test_shortcut_retries_only_remaining_steps(self, monkeypatch: pytest.MonkeyPatch) -> None:
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
injector._portal.send_shortcut.side_effect = RemoteDesktopPortalError("boom", delivered=1)
monkeypatch.setattr("shutil.which", lambda c: "/usr/bin/wtype" if c == "wtype" else None)
with (
patch.object(injector, "_wait_for_modifiers_released"),
patch("subprocess.run") as mock_run,
):
# Two steps: [([], "Home"), (["shift"], "End")]; the first fired.
assert injector._inject_shortcut_with_wayland_tool("Home+shift+End") is True
assert mock_run.call_args[0][0] == ["wtype", "-M", "shift", "-k", "End", "-m", "shift"]
def test_partial_type_rebases_onto_portal_prefix(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""A fallback's typed count must include what the portal delivered."""
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
# The portal typed 2 chars before the error; the fallback gets "x"*248.
injector._portal.inject_text.side_effect = RemoteDesktopPortalError("boom", delivered=2)
monkeypatch.setattr("shutil.which", lambda c: "/usr/bin/wtype" if c == "wtype" else None)
calls = []
def wtype_side_effect(cmd: list, **kwargs: object) -> MagicMock:
calls.append(cmd)
if len(calls) == 2:
raise subprocess.CalledProcessError(1, cmd, stderr="wedged")
return MagicMock(returncode=0, stderr="")
with (
patch.object(injector, "_wait_for_modifiers_released"),
patch("subprocess.run", side_effect=wtype_side_effect),
):
with pytest.raises(ti._PartiallyTyped) as exc_info:
injector._inject_with_wayland_tool("x" * 250)
# The error count is against the original 250-char text, so the
# caller clips the typed prefix instead of replaying it.
assert exc_info.value.typed == 2 + 200
def test_inject_untracked_timeout_replays_nothing(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
"""A portal timeout with no delivered count must not retype anything."""
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
injector._portal.inject_text.side_effect = RemoteDesktopPortalError(
"Timed out waiting for the portal", delivered=None
)
monkeypatch.setattr("shutil.which", lambda c: "/usr/bin/wtype" if c == "wtype" else None)
with (
patch.object(injector, "_wait_for_modifiers_released"),
patch("subprocess.run") as mock_run,
):
with pytest.raises(subprocess.TimeoutExpired):
injector._inject_with_wayland_tool("hello")
# The broken portal is demoted, but no fallback retypes the text.
assert injector.wayland_tool == "wtype"
mock_run.assert_not_called()
def test_backspace_untracked_timeout_replays_nothing(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
injector._portal.tap_keysym.side_effect = RemoteDesktopPortalError(
"Timed out waiting for the portal", delivered=None
)
monkeypatch.setattr("shutil.which", lambda c: "/usr/bin/wtype" if c == "wtype" else None)
with (
patch.object(injector, "_wait_for_modifiers_released"),
patch("subprocess.run") as mock_run,
):
assert injector.press_backspace(3) is False
assert injector.wayland_tool == "wtype"
mock_run.assert_not_called()
def test_shortcut_untracked_timeout_replays_nothing(
self, monkeypatch: pytest.MonkeyPatch
) -> None:
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
injector._portal.send_shortcut.side_effect = RemoteDesktopPortalError(
"Timed out waiting for the portal", delivered=None
)
monkeypatch.setattr("shutil.which", lambda c: "/usr/bin/wtype" if c == "wtype" else None)
with (
patch.object(injector, "_wait_for_modifiers_released"),
patch("subprocess.run") as mock_run,
):
assert injector._inject_shortcut_with_wayland_tool("ctrl+a") is False
mock_run.assert_not_called()
def test_shortcut_routes_to_portal(self) -> None:
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND, wayland_tool="portal")
injector._portal = MagicMock()
with patch.object(injector, "_wait_for_modifiers_released"):
assert injector._inject_shortcut_with_wayland_tool("ctrl+a") is True
injector._portal.send_shortcut.assert_called_once()
steps = injector._portal.send_shortcut.call_args[0][0]
assert steps == [(["ctrl"], "a")]
def test_stop_closes_portal(self) -> None:
injector = _bare_injector(environment=DesktopEnvironment.WAYLAND)
portal = MagicMock()
injector._portal = portal
injector.stop()
portal.close.assert_called_once()
assert injector._portal is None