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.

7.3 KB
Raw Blame
"""Unit tests for SpeechPipeline.request_voice_session (Chats manager, Slice 4).

The "Speak in this conversation" entry point: arm a wake-style session from
the /api/chats/.../speak route, optionally seeding the brain with prior turns.
Built via ``__new__`` + attribute injection (the established pipeline-unit-test
pattern) so we don't drag in stt/tts/audio.
"""
from __future__ import annotations

import asyncio
import threading
import time

import pytest

from dodaios.platform.permission_service import PermissionOutcome
from dodaios.platform.permissions import PermissionId
from dodaios.speech import start_refusal
from dodaios.speech.pipeline import PipelineState, SpeechPipeline, TurnTakingState
from tests.fakes.fake_permission_service import FakePermissionService

pytestmark = pytest.mark.usefixtures("granted_microphone")


class _FakeBrain:
    def __init__(self, raises: bool = False) -> None:
        self.seeded: list[tuple[str, str]] | None = None
        self._raises = raises

    def seed_history(self, turns) -> None:
        if self._raises:
            raise RuntimeError("boom")
        self.seeded = list(turns)


def _pipe(*, state=PipelineState.IDLE, gate=True, ptt=False, brain=None):
    p = SpeechPipeline.__new__(SpeechPipeline)
    p._ptt_mode = ptt
    p._state = state
    p._call_event = asyncio.Event()
    # A deliberate action is judged by the USER gate (not refused), never by the
    # background wake gate (a live grant).
    p._user_activation_gate = (lambda: gate)
    p._muted = False
    p._last_wake_keyword = ""
    p._brain = brain
    p._runtime_loop = None
    return p


def test_arms_when_idle() -> None:
    p = _pipe()
    assert p.request_voice_session() is True
    assert p._call_event.is_set()


def test_seeds_brain_on_arm() -> None:
    brain = _FakeBrain()
    p = _pipe(brain=brain)
    msgs = [("user", "hi"), ("assistant", "hello")]
    assert p.request_voice_session(seed_messages=msgs) is True
    assert brain.seeded == msgs


def test_noop_when_not_idle_and_does_not_seed() -> None:
    brain = _FakeBrain()
    p = _pipe(state=PipelineState.ACTIVE, brain=brain)
    assert p.request_voice_session(seed_messages=[("user", "x")]) is False
    assert not p._call_event.is_set()
    assert brain.seeded is None


def test_session_is_active_while_start_subscribers_are_still_running() -> None:
    """The close X must remain a hangup during the startup state gap.

    PipelineState becomes ACTIVE before VoiceSessionStarted is dispatched, but
    the turn-state stays IDLE until all start subscribers return. This is the
    exact interval in which the live bar previously routed X clicks back into
    request_voice_session(), which then rejected them as "pipeline not idle".
    """
    p = _pipe(state=PipelineState.ACTIVE)
    p._turn_state = TurnTakingState.IDLE

    assert p.is_session_active() is True


def test_visible_wake_candidate_routes_close_x_to_cancellation() -> None:
    """A pre-session candidate owns the visible close control."""
    p = _pipe()
    p._turn_state = TurnTakingState.IDLE
    p._wake_preroll_active = True

    assert p.is_session_active() is True


def test_noop_when_ptt_active() -> None:
    p = _pipe(ptt=True)
    assert p.request_voice_session() is False
    assert not p._call_event.is_set()


def test_noop_when_the_microphone_is_refused_and_does_not_seed() -> None:
    """The refusal comes from ``ensure`` (so the card is published), not from the
    silent user predicate alone: the request must reach the permission layer."""
    brain = _FakeBrain()
    p = _pipe(gate=False, brain=brain)
    gate = FakePermissionService()
    gate.script(PermissionId.MICROPHONE, PermissionOutcome.DENIED)
    p._permission_gate = gate
    assert p.request_voice_session(seed_messages=[("user", "x")]) is False
    assert not p._call_event.is_set()
    assert brain.seeded is None
    (call,) = gate.ensure_calls(PermissionId.MICROPHONE)
    assert (call.feature, call.interactive, call.wait_s) == ("voice", True, 0.0)


def test_seed_failure_still_arms() -> None:
    brain = _FakeBrain(raises=True)
    p = _pipe(brain=brain)
    assert p.request_voice_session(seed_messages=[("user", "x")]) is True
    assert p._call_event.is_set()


def test_arms_without_seed_messages() -> None:
    brain = _FakeBrain()
    p = _pipe(brain=brain)
    assert p.request_voice_session() is True
    assert p._call_event.is_set()
    assert brain.seeded is None  # seed_history never called


async def test_taskbar_thread_wakes_owner_loop_immediately() -> None:
    """A Tk-thread request must wake the pipeline loop without another timer.

    ``asyncio.Event.set()`` is not thread-safe. Calling it directly from the
    Jarvis Bar's Tk thread only flips the flag; the selector can remain asleep
    until unrelated I/O or a timer happens to wake it, which created the
    variable 100-300 ms click-to-listen delay. The public request method must
    marshal the edge through the pipeline loop's thread-safe scheduler.
    """
    p = _pipe()
    owner = asyncio.get_running_loop()
    p._runtime_loop = owner
    ready = threading.Event()
    release = threading.Event()

    def _click_from_taskbar() -> None:
        ready.set()
        release.wait(timeout=0.5)
        assert p.request_voice_session() is True

    thread = threading.Thread(target=_click_from_taskbar, daemon=True)
    thread.start()
    assert ready.wait(timeout=0.1)
    waiter = asyncio.create_task(p._call_event.wait())
    await asyncio.sleep(0)  # register the waiter before the Tk-thread edge

    began = time.perf_counter()
    release.set()
    await asyncio.wait_for(waiter, timeout=0.5)
    elapsed = time.perf_counter() - began
    thread.join(timeout=0.1)

    assert elapsed < 0.2, (
        "taskbar request did not wake the owner loop promptly; it likely set "
        "asyncio.Event directly from the Tk thread"
    )


# --- start_voice_session: every refusal names its own reason -----------------


def test_start_answers_empty_when_armed() -> None:
    p = _pipe()
    assert p.start_voice_session() == ""
    assert p._call_event.is_set()


def test_start_names_a_running_session() -> None:
    assert _pipe(state=PipelineState.ACTIVE).start_voice_session() == start_refusal.BUSY
    assert _pipe(ptt=True).start_voice_session() == start_refusal.BUSY


def test_start_names_mute_instead_of_a_running_session() -> None:
    p = _pipe()
    p._muted = True
    assert p.start_voice_session() == start_refusal.MUTED
    assert not p._call_event.is_set()


def test_start_names_a_dictation_that_holds_the_microphone() -> None:
    p = _pipe()
    p._dictation_blocks_activation = lambda: True
    assert p.start_voice_session() == start_refusal.DICTATION


def test_start_names_a_browser_call_that_owns_the_microphone(monkeypatch) -> None:
    from dodaios.live import runtime

    monkeypatch.setattr(runtime, "owns_microphone", lambda **_: True)
    assert _pipe().start_voice_session() == start_refusal.MICROPHONE_IN_USE


def test_start_names_a_refused_microphone() -> None:
    p = _pipe(gate=False)
    gate = FakePermissionService()
    gate.script(PermissionId.MICROPHONE, PermissionOutcome.DENIED)
    p._permission_gate = gate
    assert p.start_voice_session() == start_refusal.MICROPHONE


def test_every_refusal_is_part_of_the_shared_vocabulary() -> None:
    assert len(set(start_refusal.VOICE_START_REFUSALS)) == len(
        start_refusal.VOICE_START_REFUSALS
    )
    assert "" not in start_refusal.VOICE_START_REFUSALS