Spaces:
Paused
Paused
File size: 6,460 Bytes
a5784e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | """
Fixtures for integration tests.
These fixtures provide real instances of components (locks, queues, state)
rather than mocks, allowing tests to verify actual behavior and concurrency.
"""
import asyncio
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from api_utils.server_state import state
@pytest.fixture
def mock_expect():
"""Create a mock for playwright's expect function.
This fixture patches both:
1. browser_utils.initialization.core.expect_async (used directly in core.py)
2. playwright.async_api.expect (used by find_first_visible_locator in selector_utils.py)
This is necessary because find_first_visible_locator imports expect directly
from playwright.async_api, while core.py imports it with an alias.
"""
mock = MagicMock()
assertion_wrapper = MagicMock()
assertion_wrapper.to_be_visible = AsyncMock()
mock.return_value = assertion_wrapper
with (
patch("browser_utils.initialization.core.expect_async", mock),
patch("playwright.async_api.expect", mock),
):
yield mock
@pytest.fixture
async def real_server_state():
"""
Provide real server state with real asyncio primitives.
This fixture:
- Resets server state to clean slate
- Creates real asyncio.Lock, asyncio.Queue instances
- Mocks only external boundaries (browser, page)
- Cleans up properly after test
Use this for integration tests that verify:
- Lock behavior and concurrency
- Queue processing
- State management
- Async task coordination
"""
# Reset state to clean slate
state.reset()
# Create REAL asyncio primitives (not mocks)
state.processing_lock = asyncio.Lock()
state.model_switching_lock = asyncio.Lock()
state.params_cache_lock = asyncio.Lock()
state.request_queue = asyncio.Queue()
# Mock only external boundaries (browser/page - these are I/O)
mock_page = AsyncMock()
mock_page.goto = AsyncMock()
mock_page.wait_for_selector = AsyncMock()
mock_page.click = AsyncMock()
mock_page.fill = AsyncMock()
mock_page.evaluate = AsyncMock(return_value='{"mock": "preferences"}')
# Mock locator to return proper AsyncMock locator objects
mock_locator = AsyncMock()
mock_locator.fill = AsyncMock()
mock_locator.click = AsyncMock()
mock_locator.is_visible = AsyncMock(return_value=True)
mock_locator.wait_for = AsyncMock()
mock_page.locator = MagicMock(return_value=mock_locator)
mock_page.is_closed = MagicMock(return_value=False) # Page is open
mock_browser = AsyncMock()
mock_browser.new_context = AsyncMock(return_value=AsyncMock())
mock_browser.close = AsyncMock()
state.page_instance = mock_page
state.browser_instance = mock_browser
state.is_page_ready = True
state.is_browser_connected = True
yield state
# Cleanup: Cancel any tasks, release locks, clear queue
# This is CRITICAL for Windows to prevent hangs
# Clear queue
while not state.request_queue.empty():
try:
state.request_queue.get_nowait()
state.request_queue.task_done()
except asyncio.QueueEmpty:
break
# Cancel worker task if exists
if state.worker_task and not state.worker_task.done():
state.worker_task.cancel()
try:
await state.worker_task
except asyncio.CancelledError:
pass
# Reset state again
state.reset()
@pytest.fixture
def mock_http_request():
"""
Create a mock HTTP request for testing.
Provides:
- is_disconnected() method that can be controlled
- Common request attributes
"""
request = MagicMock()
request.is_disconnected = AsyncMock(return_value=False)
request.client = MagicMock()
request.client.host = "127.0.0.1"
return request
@pytest.fixture
def mock_chat_request():
"""
Create a mock ChatCompletionRequest for testing.
Provides a realistic request object without needing full Pydantic validation.
"""
from models import ChatCompletionRequest, Message
return ChatCompletionRequest(
model="gemini-1.5-pro",
messages=[Message(role="user", content="Test message")],
stream=False,
temperature=0.7,
max_output_tokens=1024,
)
@pytest.fixture
async def queue_with_items(real_server_state, mock_http_request):
"""
Provide a queue pre-populated with test items.
Returns:
tuple: (queue, items_list)
- queue: The real asyncio.Queue from state
- items_list: List of items added to queue for verification
"""
items = []
for i in range(3):
item = {
"req_id": f"test-req-{i}",
"request_data": MagicMock(),
"http_request": mock_http_request,
"result_future": asyncio.Future(),
"cancelled": False,
}
items.append(item)
await real_server_state.request_queue.put(item)
return real_server_state.request_queue, items
@pytest.fixture
def temp_auth_file(tmp_path):
"""
Create a temporary authentication file for browser initialization tests.
This fixture creates a realistic Playwright storage state JSON file
with minimal cookie data. Use this for integration tests that need
real file I/O instead of mocking os.path.exists.
Returns:
Path: Absolute path to the temporary auth.json file
"""
import json
auth_data = {
"cookies": [
{
"name": "test_sid",
"value": "test_session_id_12345",
"domain": ".google.com",
"path": "/",
"expires": 1798102822,
"httpOnly": False,
"secure": True,
"sameSite": "None",
}
],
"origins": [
{
"origin": "https://aistudio.google.com",
"localStorage": [{"name": "test_key", "value": "test_value"}],
}
],
}
auth_file = tmp_path / "test_auth.json"
auth_file.write_text(json.dumps(auth_data), encoding="utf-8")
return auth_file
@pytest.fixture
def temp_auth_file_missing(tmp_path):
"""
Create a path to a non-existent auth file for testing missing file scenarios.
Returns:
Path: Absolute path to a non-existent file
"""
return tmp_path / "missing_auth.json"
|