"""Tests for contextual WS frame handlers. These tests only exercise the new handler functions in device_ws.py and do not depend on litellm or the full deep_agent import chain. They monkeypatch run_contextual_stream so no LLM call is made. """ import pytest from unittest.mock import AsyncMock, MagicMock, patch @pytest.mark.asyncio async def test_handle_contextual_scope_update_appends_system_message_no_llm(monkeypatch): """_handle_contextual_scope_update must: - call append_system_message on the session buffer - send a contextual_scope_ack back on the socket - make no LLM call """ from app.api.routes import device_ws ws = AsyncMock() buffer = MagicMock() buffer.append_system_message = MagicMock() payload = { "type": "contextual_scope_update", "session_id": "s1", "scope": { "page": "project", "entityType": "project", "entityId": "p1", "entityName": "Acme", "counts": {"tasks": 1, "notes": 0, "milestones": 0}, }, } monkeypatch.setattr(device_ws, "get_session_buffer", lambda *a, **kw: buffer) await device_ws._handle_contextual_scope_update(ws, "user1", payload) ws.send_text.assert_awaited_once() import json sent = json.loads(ws.send_text.await_args.args[0]) assert sent["type"] == "contextual_scope_ack" assert sent["session_id"] == "s1" buffer.append_system_message.assert_called_once()