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.

11.4 KB
Raw Blame
"""REST tests for the Chats conversation manager router (Slice 3).

Isolated: a minimal FastAPI app with only ``chats_router`` and fakes injected
on ``app.state`` (the established pattern), driven via httpx ASGITransport.
"""
from __future__ import annotations

from types import SimpleNamespace

import httpx
from fastapi import FastAPI

from dodaios.core.bus import EventBus
from dodaios.state.chat_store import ChatStore
from dodaios.ui.web.chats_routes import router as chats_router


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

    def seed_history(self, turns) -> None:
        self.seeded = list(turns)


class _FakePipeline:
    def __init__(self, armed: bool = True, live: bool = False) -> None:
        self.armed = armed
        self.seed_messages = None
        # Whether a voice session is running — what the hangup contract reports.
        self.live = live
        self.hangups = 0

    def request_voice_session(self, *, seed_messages) -> bool:
        self.seed_messages = list(seed_messages)
        return self.armed

    def request_voice_hangup(self) -> bool:
        self.hangups += 1
        was_live, self.live = self.live, False
        return was_live


def _fake_session_store():
    """One voice session 'v1' with a single user+assistant turn."""
    sessions = [
        SimpleNamespace(
            id="v1",
            preview="how is the weather",
            started_ms=1_000,
            ended_ms=2_000,
            turn_count=1,
        )
    ]
    turns = [
        SimpleNamespace(
            user_text="how is the weather",
            jarvis_text="Sunny.",
            started_ms=1_000,
            ended_ms=1_500,
        )
    ]

    return SimpleNamespace(
        list_sessions=lambda *, limit=100, include_empty=True: list(sessions),
        get_session=lambda cid: sessions[0] if cid == "v1" else None,
        get_turns=lambda cid: list(turns) if cid == "v1" else [],
    )


async def _make_app(
    *, with_session=False, with_brain=False, with_pipeline=False, voice_live=False
):
    app = FastAPI()
    app.include_router(chats_router)
    store = ChatStore(bus=EventBus())  # in-memory
    app.state.chat_store = store
    app.state.session_store = _fake_session_store() if with_session else None
    app.state.brain = _FakeBrain() if with_brain else None
    app.state.speech_pipeline = (
        _FakePipeline(live=voice_live) if with_pipeline else None
    )
    return app, store


def _client(app: FastAPI) -> httpx.AsyncClient:
    return httpx.AsyncClient(
        transport=httpx.ASGITransport(app=app), base_url="http://t"
    )


async def test_list_merges_text_and_voice_newest_first() -> None:
    app, store = await _make_app(with_session=True)
    await store.add_message(thread_id="t1", role="user", text="hello text")
    async with _client(app) as c:
        r = await c.get("/api/chats")
    assert r.status_code == 200
    rows = r.json()
    kinds = {row["kind"] for row in rows}
    assert kinds == {"text", "voice"}
    # text thread was just updated → newer than the backdated voice session.
    assert rows[0]["kind"] == "text"
    assert rows[0]["preview"] == "hello text"


async def test_list_text_only_without_session_store() -> None:
    app, store = await _make_app(with_session=False)
    await store.add_message(thread_id="t1", role="user", text="solo")
    async with _client(app) as c:
        r = await c.get("/api/chats")
    rows = r.json()
    assert [row["kind"] for row in rows] == ["text"]


async def test_list_hides_unsent_and_assistant_only_text_threads() -> None:
    app, store = await _make_app(with_session=False)
    await store.create_thread(title="New Chat", thread_id="draft")
    await store.add_message(thread_id="orphan", role="assistant", text="internal reply")
    await store.add_message(thread_id="blank", role="user", text="   ")
    await store.add_message(thread_id="real", role="user", text="actual text chat")

    async with _client(app) as c:
        r = await c.get("/api/chats")

    assert r.status_code == 200
    assert [row["id"] for row in r.json()] == ["real"]


async def test_get_text_conversation_detail() -> None:
    app, store = await _make_app()
    await store.add_message(thread_id="t1", role="user", text="q")
    await store.add_message(thread_id="t1", role="assistant", text="a")
    async with _client(app) as c:
        r = await c.get("/api/chats/text/t1")
    assert r.status_code == 200
    body = r.json()
    assert [(m["role"], m["text"]) for m in body["messages"]] == [
        ("user", "q"),
        ("assistant", "a"),
    ]


async def test_get_voice_conversation_flattens_turns() -> None:
    app, _ = await _make_app(with_session=True)
    async with _client(app) as c:
        r = await c.get("/api/chats/voice/v1")
    assert r.status_code == 200
    body = r.json()
    assert [(m["role"], m["text"]) for m in body["messages"]] == [
        ("user", "how is the weather"),
        ("assistant", "Sunny."),
    ]


async def test_voice_history_keeps_preambles_when_hangup_precedes_reply() -> None:
    app, _ = await _make_app(with_session=True, with_brain=True)
    turn = app.state.session_store.get_turns("v1")[0]
    turn.id = "v1-auto-1"
    turn.jarvis_text = ""
    events = [
        SimpleNamespace(
            turn_id=turn.id,
            kind="SpeechSpoken",
            ts_ms=timestamp,
            seq=index,
            payload={"text": text, "spoken_kind": "preamble"},
        )
        for index, (timestamp, text) in enumerate(
            [(1_200, "I am checking the team."), (1_400, "Still working on it.")]
        )
    ]
    app.state.session_store.get_events = lambda cid: events

    async with _client(app) as client:
        detail = (await client.get("/api/chats/voice/v1")).json()
        resumed = await client.post("/api/chats/voice/v1/resume")

    assert [(m["role"], m["text"], m["ts_ms"]) for m in detail["messages"]] == [
        ("user", "how is the weather", 1_000),
        ("assistant", "I am checking the team.", 1_200),
        ("assistant", "Still working on it.", 1_400),
    ]
    assert all(m["trace"] is None for m in detail["messages"])
    assert resumed.status_code == 200
    assert app.state.brain.seeded[-1] == ("assistant", "Still working on it.")


async def test_voice_history_prefers_heard_reply_and_keeps_spoken_fallback() -> None:
    app, _ = await _make_app(with_session=True)
    turn = app.state.session_store.get_turns("v1")[0]
    turn.id = "v1-turn-1"
    turn.jarvis_text = "Unspoken original reply."
    events = [
        SimpleNamespace(
            turn_id=turn_id,
            kind=kind,
            ts_ms=1_100 + index * 50,
            seq=index,
            payload={"text": text, "spoken_kind": spoken_kind},
        )
        for index, (turn_id, kind, spoken_kind, text) in enumerate([
            (turn.id, "ResponseGenerated", "", "Unspoken original reply."),
            (turn.id, "SpeechSpoken", "preamble", "Checking now."),
            (turn.id, "SpeechSpoken", "withheld", "I could not finish that answer."),
            (turn.id, "SpeechSpoken", "reply", "The reply actually heard."),
            ("another-turn", "SpeechSpoken", "reply", "Someone else's reply."),
        ])
    ]
    app.state.session_store.get_events = lambda cid: events

    async with _client(app) as client:
        detail = (await client.get("/api/chats/voice/v1")).json()

    assert [m["text"] for m in detail["messages"] if m["role"] == "assistant"] == [
        "Checking now.", "I could not finish that answer.", "The reply actually heard.",
    ]


async def test_get_unknown_conversation_404() -> None:
    app, _ = await _make_app()
    async with _client(app) as c:
        r = await c.get("/api/chats/text/nope")
    assert r.status_code == 404


async def test_create_chat() -> None:
    app, store = await _make_app()
    async with _client(app) as c:
        r = await c.post("/api/chats", json={"title": "Planning"})
    assert r.status_code == 200
    tid = r.json()["id"]
    assert store.get_thread(tid) is not None


async def test_resume_seeds_web_brain() -> None:
    app, store = await _make_app(with_brain=True)
    await store.add_message(thread_id="t1", role="user", text="remember this")
    await store.add_message(thread_id="t1", role="assistant", text="noted")
    async with _client(app) as c:
        r = await c.post("/api/chats/text/t1/resume")
    assert r.status_code == 200
    assert r.json()["seeded_turns"] == 2
    brain: _FakeBrain = app.state.brain
    assert brain.seeded == [("user", "remember this"), ("assistant", "noted")]


async def test_speak_without_pipeline_503() -> None:
    app, store = await _make_app(with_pipeline=False)
    await store.add_message(thread_id="t1", role="user", text="hi")
    async with _client(app) as c:
        r = await c.post("/api/chats/text/t1/speak")
    assert r.status_code == 503


async def test_speak_arms_pipeline_with_seed() -> None:
    app, store = await _make_app(with_pipeline=True)
    await store.add_message(thread_id="t1", role="user", text="continue please")
    await store.add_message(thread_id="t1", role="assistant", text="ok")
    async with _client(app) as c:
        r = await c.post("/api/chats/text/t1/speak")
    assert r.status_code == 200
    assert r.json() == {"armed": True, "seeded_turns": 2}
    pipe: _FakePipeline = app.state.speech_pipeline
    assert pipe.seed_messages == [("user", "continue please"), ("assistant", "ok")]


async def test_delete_text_conversation() -> None:
    app, store = await _make_app()
    await store.add_message(thread_id="t1", role="user", text="bye")
    async with _client(app) as c:
        r = await c.delete("/api/chats/text/t1")
        assert r.status_code == 200
        r2 = await c.get("/api/chats/text/t1")
    assert r2.status_code == 404


async def test_days_filter_excludes_old() -> None:
    app, store = await _make_app()
    await store.add_message(thread_id="old", role="user", text="ancient")
    store._backdate_for_test("old", days=99)
    await store.add_message(thread_id="fresh", role="user", text="recent")
    async with _client(app) as c:
        r = await c.get("/api/chats", params={"days": 10})
    ids = [row["id"] for row in r.json()]
    assert ids == ["fresh"]


async def test_new_voice_run_ends_a_live_session() -> None:
    """ "+ New voice chat": the brain drops the thread it was seeded with and a
    session still running is ended, so the next wake word opens a new one."""
    app, _ = await _make_app(with_brain=True, with_pipeline=True, voice_live=True)
    async with _client(app) as c:
        r = await c.post("/api/chats/voice/new")
    assert r.status_code == 200
    assert r.json() == {"cleared": True, "ended": True}
    assert app.state.brain.seeded == []
    assert app.state.speech_pipeline.hangups == 1


async def test_new_voice_run_reports_no_session_to_end() -> None:
    """Nothing running is the common case — an honest False, not an error."""
    app, _ = await _make_app(with_brain=True, with_pipeline=True, voice_live=False)
    async with _client(app) as c:
        r = await c.post("/api/chats/voice/new")
    assert r.status_code == 200
    assert r.json() == {"cleared": True, "ended": False}


async def test_new_voice_run_without_brain_or_pipeline() -> None:
    """Headless / voice-less installs answer instead of failing."""
    app, _ = await _make_app()
    async with _client(app) as c:
        r = await c.post("/api/chats/voice/new")
    assert r.status_code == 200
    assert r.json() == {"cleared": False, "ended": False}