Files
secure-agent/tests/conftest.py
T
2026-03-04 14:35:44 -07:00

83 lines
2.1 KiB
Python

"""
Shared test fixtures and config.
Fixtures defined here available to all tests
"""
from statistics import mode
from unittest.mock import AsyncMock, MagicMock
import pytest
from anthropic.types import ContentBlock, Message, TextBlock, Usage
from agent.config import Settings
@pytest.fixture
def settings():
"""provide test settings don't load from .env"""
return Settings(
anthropic_api_key="test-key-12345",
model="claude-test-model",
max_tokens=100,
safedir="./test-workspace",
)
@pytest.fixture
def mock_anthropic_client():
"""Mock anthropic client that returns a fake response."""
mock_client = AsyncMock()
# create a realistic fake response
fake_message = Message(
id="msg_test123",
type="message",
role="assistant",
content=[TextBlock(type="text", text="42")],
model="claude-test-model",
stop_reason="end_turn",
usage=Usage(input_tokens=10, output_tokens=5),
)
# make messages.create() return this fake message
mock_client.messages.create = AsyncMock(return_value=fake_message)
return mock_client
@pytest.fixture
def sample_history():
"""sample conversation history for testing"""
return [
{"role": "user", "content": "Hello"},
{"role": "assistance", "content": "Hi there!"},
{"role": "user", "content": "What's 2+2?"},
]
@pytest.fixture
def mock_sandbox():
"""Mock sandbox for unit tests."""
sandbox = MagicMock()
sandbox.run = AsyncMock(return_value="mock output\n")
return sandbox
@pytest.fixture
def crashed_sandbox():
"""Mock sandbox that simulates a crash."""
sandbox = MagicMock()
sandbox.run = AsyncMock(side_effect=RuntimeError("Container crashed unexpectedly"))
return sandbox
@pytest.fixture
def mock_podman_client():
"""Mock Podman client for unit tests."""
with patch("sandbox.session.podman.PodmanClient") as mock:
mock_container = MagicMock()
mock_container.exec_run.return_value = (0, b"mock output\n")
mock.return_value.containers.run.return_value = mock_container
yield mock, mock_container