|
| 1 | +import json |
| 2 | +from unittest import mock |
| 3 | +from fastapi.testclient import TestClient |
| 4 | +from vanilla_agent_custom_features.main import app |
| 5 | +import pytest |
| 6 | +from pathlib import Path |
| 7 | +from openbb_ai.testing import CopilotResponse |
| 8 | + |
| 9 | +test_client = TestClient(app) |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(autouse=True) |
| 13 | +def reset_sse_starlette_appstatus_event(): |
| 14 | + """ |
| 15 | + Fixture that resets the appstatus event in the sse_starlette app. |
| 16 | + Should be used on any test that uses sse_starlette to stream events. |
| 17 | + """ |
| 18 | + # See https://github.com/sysid/sse-starlette/issues/59 |
| 19 | + from sse_starlette.sse import AppStatus |
| 20 | + |
| 21 | + AppStatus.should_exit_event = None |
| 22 | + |
| 23 | + |
| 24 | +def test_agents_json(): |
| 25 | + """Test that the agents.json endpoint returns the correct configuration.""" |
| 26 | + response = test_client.get("/agents.json") |
| 27 | + assert response.status_code == 200 |
| 28 | + |
| 29 | + data = response.json() |
| 30 | + assert "vanilla_agent_custom_features" in data |
| 31 | + |
| 32 | + agent_config = data["vanilla_agent_custom_features"] |
| 33 | + assert agent_config["name"] == "Vanilla Agent Custom Features" |
| 34 | + assert "deep-research" in agent_config["features"] |
| 35 | + assert "web-search" in agent_config["features"] |
| 36 | + assert not agent_config["features"]["deep-research"]["default"] |
| 37 | + assert agent_config["features"]["web-search"]["default"] |
| 38 | + |
| 39 | + |
| 40 | +def test_query_simple_message(): |
| 41 | + """Test basic query with a simple message.""" |
| 42 | + test_payload_path = ( |
| 43 | + Path(__file__).parent.parent.parent |
| 44 | + / "testing" |
| 45 | + / "test_payloads" |
| 46 | + / "single_message.json" |
| 47 | + ) |
| 48 | + test_payload = json.load(open(test_payload_path)) |
| 49 | + |
| 50 | + response = test_client.post("/v1/query", json=test_payload) |
| 51 | + assert response.status_code == 200 |
| 52 | + copilot_response = CopilotResponse(response.text) |
| 53 | + assert copilot_response.has_any("copilotMessage", "2") |
| 54 | + |
| 55 | + |
| 56 | +def test_query_with_workspace_options(): |
| 57 | + """Test query with workspace options to verify feature detection.""" |
| 58 | + test_payload = { |
| 59 | + "messages": [{"role": "human", "content": "Hello, what features are enabled?"}], |
| 60 | + "workspace_options": {"deep-research": True, "web-search": False}, |
| 61 | + } |
| 62 | + |
| 63 | + with mock.patch("openai.AsyncOpenAI") as mock_openai: |
| 64 | + # Mock the OpenAI client |
| 65 | + mock_client = mock.MagicMock() |
| 66 | + mock_openai.return_value = mock_client |
| 67 | + |
| 68 | + # Verify that the system message contains the correct feature status |
| 69 | + async def mock_create(**kwargs): |
| 70 | + messages = kwargs["messages"] |
| 71 | + system_message = messages[0]["content"] |
| 72 | + |
| 73 | + # Check that the system message contains the feature status |
| 74 | + assert "Deep Research: ✅ Enabled" in system_message |
| 75 | + assert "Web Search: ❌ Disabled" in system_message |
| 76 | + |
| 77 | + # Return a mock stream |
| 78 | + class MockEvent: |
| 79 | + class Choice: |
| 80 | + class Delta: |
| 81 | + content = "Hello! Features are configured." |
| 82 | + |
| 83 | + delta = Delta() |
| 84 | + |
| 85 | + choices = [Choice()] |
| 86 | + |
| 87 | + yield MockEvent() |
| 88 | + |
| 89 | + mock_client.chat.completions.create = mock_create |
| 90 | + |
| 91 | + response = test_client.post("/v1/query", json=test_payload) |
| 92 | + assert response.status_code == 200 |
| 93 | + |
| 94 | + |
| 95 | +def test_query_default_workspace_options(): |
| 96 | + """Test query without workspace options uses defaults.""" |
| 97 | + test_payload = {"messages": [{"role": "human", "content": "Hi there!"}]} |
| 98 | + |
| 99 | + with mock.patch("openai.AsyncOpenAI") as mock_openai: |
| 100 | + # Mock the OpenAI client |
| 101 | + mock_client = mock.MagicMock() |
| 102 | + mock_openai.return_value = mock_client |
| 103 | + |
| 104 | + # Verify that the system message contains the default feature status |
| 105 | + async def mock_create(**kwargs): |
| 106 | + messages = kwargs["messages"] |
| 107 | + system_message = messages[0]["content"] |
| 108 | + |
| 109 | + # Check that defaults are used (deep-research: False, web-search: True) |
| 110 | + assert "Deep Research: ❌ Disabled" in system_message |
| 111 | + assert "Web Search: ✅ Enabled" in system_message |
| 112 | + |
| 113 | + # Return a mock stream |
| 114 | + class MockEvent: |
| 115 | + class Choice: |
| 116 | + class Delta: |
| 117 | + content = "Hello!" |
| 118 | + |
| 119 | + delta = Delta() |
| 120 | + |
| 121 | + choices = [Choice()] |
| 122 | + |
| 123 | + yield MockEvent() |
| 124 | + |
| 125 | + mock_client.chat.completions.create = mock_create |
| 126 | + |
| 127 | + response = test_client.post("/v1/query", json=test_payload) |
| 128 | + assert response.status_code == 200 |
| 129 | + |
| 130 | + |
| 131 | +def test_query_conversation(): |
| 132 | + """Test query with multiple messages in conversation.""" |
| 133 | + test_payload_path = ( |
| 134 | + Path(__file__).parent.parent.parent |
| 135 | + / "testing" |
| 136 | + / "test_payloads" |
| 137 | + / "multiple_messages.json" |
| 138 | + ) |
| 139 | + test_payload = json.load(open(test_payload_path)) |
| 140 | + |
| 141 | + response = test_client.post("/v1/query", json=test_payload) |
| 142 | + assert response.status_code == 200 |
| 143 | + copilot_response = CopilotResponse(response.text) |
| 144 | + assert copilot_response.has_any("copilotMessage", "4") |
| 145 | + |
| 146 | + |
| 147 | +def test_query_no_messages(): |
| 148 | + """Test query with empty messages list.""" |
| 149 | + test_payload = { |
| 150 | + "messages": [], |
| 151 | + } |
| 152 | + response = test_client.post("/v1/query", json=test_payload) |
| 153 | + assert "messages list cannot be empty" in response.text |
0 commit comments