feat: add _lookup_remote_by_device_id helper for federation proxy

Implements _lookup_remote_by_device_id(device_id: str) -> dict | None
in muxplex/main.py, inserted before the federation WebSocket proxy section.

Logic:
- Load settings and get remote_instances list
- Primary: iterate remotes, return first where remote.get('device_id') == device_id
- Fallback: if device_id parses as integer, use index-based lookup for
  transition compatibility (0 <= idx < len(remotes))
- Return None if not found

Tests added:
- test_lookup_remote_by_device_id_found: two remotes with device_ids
  'aaa-111' and 'bbb-222'; lookup 'bbb-222' returns Desktop remote
- test_lookup_remote_by_device_id_not_found: one remote with device_id
  'aaa-111'; lookup 'zzz-999' returns None

Task: task-8
This commit is contained in:
Brian Krabach
2026-04-15 11:45:06 -07:00
parent 6984c71823
commit cc9bb81bc9
2 changed files with 118 additions and 0 deletions
+37
View File
@@ -1005,6 +1005,43 @@ async def terminal_ws_proxy(websocket: WebSocket) -> None:
pass
# ---------------------------------------------------------------------------
# Federation helper utilities
# ---------------------------------------------------------------------------
def _lookup_remote_by_device_id(device_id: str) -> dict | None:
"""Return the first remote instance whose ``device_id`` matches *device_id*.
Primary lookup: iterate ``remote_instances`` and return the first entry
where ``remote.get('device_id') == device_id``.
Fallback (transition compatibility): if *device_id* looks like an integer
(i.e. ``int(device_id)`` succeeds) treat it as a 0-based index into the
``remote_instances`` list and return the remote at that position, provided
the index is in range.
Returns ``None`` if no match is found.
"""
settings = load_settings()
remotes: list[dict] = settings.get("remote_instances", [])
# Primary: match by device_id field
for remote in remotes:
if remote.get("device_id") == device_id:
return remote
# Fallback: index-based lookup for transition compatibility
try:
idx = int(device_id)
if 0 <= idx < len(remotes):
return remotes[idx]
except (ValueError, TypeError):
pass
return None
# ---------------------------------------------------------------------------
# Federation WebSocket proxy — bridges browser to a remote instance's ttyd
# ---------------------------------------------------------------------------