|
| 1 | +"""Tests for the web chat UI module.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pydantic_ai import Agent |
| 8 | +from pydantic_ai.ui.web import AI_MODELS, BUILTIN_TOOLS, create_chat_app |
| 9 | + |
| 10 | +from .conftest import try_import |
| 11 | + |
| 12 | +with try_import() as fastapi_import_successful: |
| 13 | + from fastapi import FastAPI |
| 14 | + from fastapi.testclient import TestClient |
| 15 | + |
| 16 | +pytestmark = [ |
| 17 | + pytest.mark.skipif(not fastapi_import_successful, reason='fastapi not installed'), |
| 18 | +] |
| 19 | + |
| 20 | + |
| 21 | +def test_create_chat_app_basic(): |
| 22 | + """Test creating a basic chat app.""" |
| 23 | + agent = Agent('test') |
| 24 | + app = create_chat_app(agent) |
| 25 | + |
| 26 | + assert isinstance(app, FastAPI) |
| 27 | + assert app.state.agent is agent |
| 28 | + |
| 29 | + |
| 30 | +def test_agent_to_web(): |
| 31 | + """Test the Agent.to_web() method.""" |
| 32 | + agent = Agent('test') |
| 33 | + app = agent.to_web() |
| 34 | + |
| 35 | + assert isinstance(app, FastAPI) |
| 36 | + assert app.state.agent is agent |
| 37 | + |
| 38 | + |
| 39 | +def test_chat_app_health_endpoint(): |
| 40 | + """Test the /api/health endpoint.""" |
| 41 | + agent = Agent('test') |
| 42 | + app = create_chat_app(agent) |
| 43 | + |
| 44 | + with TestClient(app) as client: |
| 45 | + response = client.get('/api/health') |
| 46 | + assert response.status_code == 200 |
| 47 | + assert response.json() == {'ok': True} |
| 48 | + |
| 49 | + |
| 50 | +def test_chat_app_configure_endpoint(): |
| 51 | + """Test the /api/configure endpoint.""" |
| 52 | + agent = Agent('test') |
| 53 | + app = create_chat_app(agent) |
| 54 | + |
| 55 | + with TestClient(app) as client: |
| 56 | + response = client.get('/api/configure') |
| 57 | + assert response.status_code == 200 |
| 58 | + data = response.json() |
| 59 | + assert 'models' in data |
| 60 | + assert 'builtinTools' in data # camelCase due to alias generator |
| 61 | + |
| 62 | + # no snapshot bc we're checking against the actual model/tool definitions |
| 63 | + assert len(data['models']) == len(AI_MODELS) |
| 64 | + assert len(data['builtinTools']) == len(BUILTIN_TOOLS) |
| 65 | + |
| 66 | + |
| 67 | +def test_chat_app_index_endpoint(): |
| 68 | + """Test that the index endpoint serves the UI from CDN.""" |
| 69 | + agent = Agent('test') |
| 70 | + app = create_chat_app(agent) |
| 71 | + |
| 72 | + with TestClient(app) as client: |
| 73 | + response = client.get('/') |
| 74 | + assert response.status_code == 200 |
| 75 | + assert response.headers['content-type'] == 'text/html; charset=utf-8' |
| 76 | + assert len(response.content) > 0 |
| 77 | + |
| 78 | + |
| 79 | +def test_ai_models_configuration(): |
| 80 | + """Test that AI models are configured correctly.""" |
| 81 | + assert len(AI_MODELS) == 3 |
| 82 | + |
| 83 | + model_ids = {model.id for model in AI_MODELS} |
| 84 | + assert 'anthropic:claude-sonnet-4-5' in model_ids |
| 85 | + assert 'openai-responses:gpt-5' in model_ids |
| 86 | + assert 'google-gla:gemini-2.5-pro' in model_ids |
| 87 | + |
| 88 | + |
| 89 | +def test_builtin_tools_configuration(): |
| 90 | + """Test that builtin tools are configured correctly.""" |
| 91 | + assert len(BUILTIN_TOOLS) == 3 |
| 92 | + |
| 93 | + assert 'web_search' in BUILTIN_TOOLS |
| 94 | + assert 'code_execution' in BUILTIN_TOOLS |
| 95 | + assert 'image_generation' in BUILTIN_TOOLS |
| 96 | + |
| 97 | + from pydantic_ai.builtin_tools import CodeExecutionTool, ImageGenerationTool, WebSearchTool |
| 98 | + |
| 99 | + assert isinstance(BUILTIN_TOOLS['web_search'], WebSearchTool) |
| 100 | + assert isinstance(BUILTIN_TOOLS['code_execution'], CodeExecutionTool) |
| 101 | + assert isinstance(BUILTIN_TOOLS['image_generation'], ImageGenerationTool) |
0 commit comments