Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

6.5 KB
Raw Blame
"""GET /api/voice/state + POST /api/voice/call + /hangup — the voice orb's click.

The click must take the SAME path as the call hotkey (``start_voice_session``
/ ``request_voice_hangup``), report a pipeline refusal as an answer that names
its reason rather than an error, and degrade honestly on a headless install (503 / available=false)
instead of 500-ing.
"""
from __future__ import annotations

from types import SimpleNamespace

from fastapi import FastAPI
from fastapi.testclient import TestClient

from dodaios.ui.web import voice_call_routes
from dodaios.ui.web.voice_call_routes import router


class _Pipeline:
    """The two public arming methods, and the state the GET reads."""

    def __init__(
        self,
        refusal: str = "",
        stopped: bool = True,
        *,
        state: str = "IDLE",
        supervisor_state: str = "IDLE",
    ) -> None:
        self._refusal = refusal
        self._stopped = stopped
        self.calls: list[str] = []
        self._state = SimpleNamespace(name=state)
        self._supervisor = SimpleNamespace(state=supervisor_state)

    def start_voice_session(self) -> str:
        self.calls.append("call")
        return self._refusal

    def request_voice_hangup(self) -> bool:
        self.calls.append("hangup")
        return self._stopped


def _client(monkeypatch, pipeline, *, config=None, desktop_shell=False) -> TestClient:
    monkeypatch.setattr(voice_call_routes, "_pipeline", lambda: pipeline)
    app = FastAPI()
    app.include_router(router)
    if config is not None:
        app.state.config = config
    if desktop_shell:
        app.state.native_file_actions = True
    return TestClient(app)


def _cfg(mode: str = "pipeline", **browser_voice):
    from dodaios.core.config import JarvisConfig

    return JarvisConfig.model_validate(
        {"voice": {"mode": mode}, "browser_voice": browser_voice}
    )


def test_call_arms_a_wake_style_session(monkeypatch):
    pipeline = _Pipeline()
    client = _client(monkeypatch, pipeline)
    answer = client.post("/api/voice/call")
    assert answer.status_code == 200
    assert answer.json() == {"armed": True, "reason": ""}
    assert pipeline.calls == ["call"]


def test_call_reports_a_refusal_as_an_answer(monkeypatch):
    # A refusal is an answer, not an error, and it says which refusal it was:
    # the control must never guess "a conversation is already running".
    client = _client(monkeypatch, _Pipeline(refusal="muted"))
    answer = client.post("/api/voice/call")
    assert answer.status_code == 200
    assert answer.json() == {"armed": False, "reason": "muted"}


def test_hangup_takes_the_hangup_contract(monkeypatch):
    pipeline = _Pipeline()
    client = _client(monkeypatch, pipeline)
    answer = client.post("/api/voice/hangup")
    assert answer.status_code == 200
    assert answer.json() == {"stopped": True}
    assert pipeline.calls == ["hangup"]


def test_headless_answers_honestly_instead_of_500(monkeypatch):
    client = _client(monkeypatch, None)
    assert client.post("/api/voice/call").status_code == 503
    assert client.post("/api/voice/hangup").status_code == 503
    answer = client.get("/api/voice/state")
    assert answer.status_code == 200
    assert answer.json() == {
        "available": False,
        "state": "unavailable",
        "voice_state": "idle",
        # No config attached: the bridge cannot be read, so it is not offered.
        "browser_call": False,
    }


def test_headless_host_tells_the_browser_to_hold_the_call(monkeypatch):
    # Issue #399: `jarvis serve` has no speech pipeline, so the browser that
    # presses Start holds the call over /ws/audio.
    client = _client(monkeypatch, None, config=_cfg("pipeline"))
    body = client.get("/api/voice/state").json()
    assert body["available"] is False
    assert body["browser_call"] is True


def test_headless_realtime_mode_also_offers_the_browser_call(monkeypatch):
    client = _client(monkeypatch, None, config=_cfg("realtime", enabled=False))
    assert client.get("/api/voice/state").json()["browser_call"] is True


def test_switched_off_bridge_is_not_offered(monkeypatch):
    client = _client(monkeypatch, None, config=_cfg("pipeline", enabled=False))
    assert client.get("/api/voice/state").json()["browser_call"] is False


def test_desktop_shell_without_a_pipeline_never_offers_a_browser_call(monkeypatch):
    # A desktop app that is still booting has no pipeline yet; a browser-held
    # call there would be a second microphone owner.
    client = _client(monkeypatch, None, config=_cfg("pipeline"), desktop_shell=True)
    assert client.get("/api/voice/state").json()["browser_call"] is False


def test_a_running_pipeline_owns_the_call(monkeypatch):
    client = _client(monkeypatch, _Pipeline(), config=_cfg("pipeline"))
    assert client.get("/api/voice/state").json()["browser_call"] is False


def test_state_names_the_pipeline_state(monkeypatch):
    client = _client(monkeypatch, _Pipeline())
    assert client.get("/api/voice/state").json() == {
        "available": True,
        "state": "idle",
        "voice_state": "idle",
        "browser_call": False,
    }


def test_state_mirrors_the_supervisor_while_a_session_runs(monkeypatch):
    # The fine-grained state every voice surface renders: the REST mirror of
    # the one-shot SystemStateChanged event, so a window that missed one can
    # reconcile instead of showing the last thing it heard forever.
    client = _client(
        monkeypatch,
        _Pipeline(state="ACTIVE", supervisor_state="LISTENING"),
    )
    assert client.get("/api/voice/state").json() == {
        "available": True,
        "state": "active",
        "voice_state": "listening",
        "browser_call": False,
    }


def test_state_says_unknown_when_a_running_session_has_no_supervisor(monkeypatch):
    # Never a guess: the client leaves a live call alone on anything but idle.
    pipeline = _Pipeline(state="ACTIVE")
    del pipeline._supervisor
    client = _client(monkeypatch, pipeline)
    assert client.get("/api/voice/state").json()["voice_state"] == "unknown"


def test_state_clamps_a_left_over_supervisor_state_to_idle(monkeypatch):
    # No session runs, so LISTENING is unearned detail whatever the supervisor
    # still holds — and it is precisely the value that used to freeze the bar.
    client = _client(
        monkeypatch,
        _Pipeline(state="IDLE", supervisor_state="LISTENING"),
    )
    assert client.get("/api/voice/state").json() == {
        "available": True,
        "state": "idle",
        "voice_state": "idle",
        "browser_call": False,
    }