Files
smart-support/backend/tests/unit/test_graph.py
Yaojia Wang 33488fd634 feat: complete phase 1 -- core framework with chat loop, agents, and React UI
Backend:
- FastAPI WebSocket /ws endpoint with streaming via LangGraph astream
- LangGraph Supervisor connecting 3 mock agents (order_lookup, order_actions, fallback)
- YAML Agent Registry with Pydantic validation and immutable configs
- PostgresSaver checkpoint persistence via langgraph-checkpoint-postgres
- Session TTL with 30-min sliding window and interrupt extension
- LLM provider abstraction (Anthropic/OpenAI/Google)
- Token usage + cost tracking callback handler
- Input validation: message size cap, thread_id format, content length
- Security: no hardcoded defaults, startup API key validation, no input reflection

Frontend:
- React 19 + TypeScript + Vite chat UI
- WebSocket hook with reconnect + exponential backoff
- Streaming token display with agent attribution
- Interrupt approval/reject UI for write operations
- Collapsible tool call viewer

Testing:
- 87 unit tests, 87% coverage (exceeds 80% requirement)
- Ruff lint + format clean

Infrastructure:
- Docker Compose (PostgreSQL 16 + backend)
- pyproject.toml with full dependency management
2026-03-30 00:54:21 +02:00

45 lines
1.5 KiB
Python

"""Tests for app.graph module."""
from __future__ import annotations
from typing import TYPE_CHECKING
from unittest.mock import AsyncMock, MagicMock
import pytest
from app.graph import SUPERVISOR_PROMPT, build_agent_nodes, build_graph
if TYPE_CHECKING:
from app.registry import AgentRegistry
@pytest.mark.unit
class TestBuildAgentNodes:
def test_creates_correct_number_of_nodes(self, sample_registry: AgentRegistry) -> None:
mock_llm = MagicMock()
nodes = build_agent_nodes(sample_registry, mock_llm)
assert len(nodes) == 3
def test_nodes_are_runnable(self, sample_registry: AgentRegistry) -> None:
mock_llm = MagicMock()
nodes = build_agent_nodes(sample_registry, mock_llm)
for node in nodes:
assert hasattr(node, "invoke") or hasattr(node, "ainvoke")
@pytest.mark.unit
class TestBuildGraph:
def test_graph_compiles_with_mock_checkpointer(self, sample_registry: AgentRegistry) -> None:
mock_llm = MagicMock()
mock_llm.bind_tools = MagicMock(return_value=mock_llm)
mock_llm.with_structured_output = MagicMock(return_value=mock_llm)
mock_checkpointer = AsyncMock()
graph = build_graph(sample_registry, mock_llm, mock_checkpointer)
assert graph is not None
def test_supervisor_prompt_contains_routing_info(self) -> None:
assert "order_lookup" in SUPERVISOR_PROMPT
assert "order_actions" in SUPERVISOR_PROMPT
assert "fallback" in SUPERVISOR_PROMPT