Files
unshackle/tests/remote/unit/test_input_bridge.py
imSp4rky 8a9a1e1bdc refactor(api): remove dead code paths in serve/remote layer
Delete code paths that never executed in the serve/API/remote stack:

- BridgeCancelledError: raised but never caught; replace raises with RuntimeError and drop the class.
- InputBridge.error setter and _error field: written at the handler layer, never read (session.auth_error is the surfaced channel).
- cancel_event param and _check_cancel inner fn on _perform_download: the only caller always passed None (the live job.cancel_event is untouched).
- result_with_progress monkeypatch: its status pings were redundant with the file-polled progress_sink, which is now the sole path.
- session param on _resolve_manifest_data: unused in the body.

Reconcile SessionStore's hardcoded 600s auth-state TTL into a shared AUTH_INPUT_TIMEOUT constant referenced by both the InputBridge input timeout and the store, removing the magic number without changing the longer grace window.

Update input_bridge/session_store tests for the new channels and add an assertion that auth-state entries expire at the single rule.
2026-06-21 13:07:45 -06:00

97 lines
2.6 KiB
Python

"""Unit tests for unshackle.core.api.input_bridge.InputBridge."""
from __future__ import annotations
import threading
import time
import pytest
from unshackle.core.api.input_bridge import AuthStatus, InputBridge
pytestmark = pytest.mark.unit
def test_initial_status_is_authenticating() -> None:
bridge = InputBridge()
assert bridge.status is AuthStatus.AUTHENTICATING
assert bridge.get_pending_prompt() is None
def test_submit_response_returns_false_when_no_prompt_pending() -> None:
bridge = InputBridge()
assert bridge.submit_response("foo") is False
def test_request_input_blocks_until_submit() -> None:
bridge = InputBridge()
result: list[str] = []
def worker() -> None:
result.append(bridge.request_input("OTP?", timeout=5))
t = threading.Thread(target=worker)
t.start()
for _ in range(50):
if bridge.get_pending_prompt() == "OTP?":
break
time.sleep(0.02)
assert bridge.status is AuthStatus.PENDING_INPUT
assert bridge.get_pending_prompt() == "OTP?"
assert bridge.submit_response("123456") is True
t.join(timeout=2)
assert result == ["123456"]
assert bridge.status is AuthStatus.AUTHENTICATING
assert bridge.get_pending_prompt() is None
def test_request_input_times_out() -> None:
bridge = InputBridge()
with pytest.raises(TimeoutError, match="No client response"):
bridge.request_input("hello", timeout=0.6)
assert bridge.status is AuthStatus.FAILED
def test_cancel_before_request_raises_immediately() -> None:
bridge = InputBridge()
bridge.cancel()
with pytest.raises(RuntimeError, match="cancelled"):
bridge.request_input("hello", timeout=5)
assert bridge.status is AuthStatus.FAILED
def test_cancel_unblocks_pending_request() -> None:
bridge = InputBridge()
exc: list[Exception] = []
def worker() -> None:
try:
bridge.request_input("OTP?", timeout=5)
except RuntimeError as e:
exc.append(e)
t = threading.Thread(target=worker)
t.start()
for _ in range(50):
if bridge.status is AuthStatus.PENDING_INPUT:
break
time.sleep(0.02)
bridge.cancel()
t.join(timeout=2)
assert exc and isinstance(exc[0], RuntimeError)
assert bridge.status is AuthStatus.FAILED
def test_get_pending_prompt_returns_none_outside_pending_state() -> None:
bridge = InputBridge()
bridge.status = AuthStatus.AUTHENTICATED
assert bridge.get_pending_prompt() is None
def test_status_setter() -> None:
bridge = InputBridge()
bridge.status = AuthStatus.AUTHENTICATED
assert bridge.status is AuthStatus.AUTHENTICATED