Files
comet-ml--opik/sdks/python/tests/unit/simulation/test_simulator.py
T
wehub-resource-sync 5a558eb09e
TypeScript SDK Compatibility V1.x E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / TypeScript SDK Compatibility V1.x E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
TypeScript SDK E2E Tests / TypeScript SDK E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
Python SDK E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK E2E Tests / Python SDK E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK E2E Tests / build-opik (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Python SDK Compatibility V1.x E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK E2E Tests / build-opik (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer E2E Tests Python ${{matrix.python_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer Integration Smoke Tests (push) Has been cancelled
🐙 Code Quality / detect (push) Has been cancelled
🐙 Code Quality / lint (${{ matrix.leg.name }}) (push) Has been cancelled
🐙 Code Quality / summary (push) Has been cancelled
TypeScript SDK Library Integration Tests / Check Secrets (push) Has been cancelled
TypeScript SDK Library Integration Tests / opik-vercel (Vercel AI SDK / eve) (push) Has been cancelled
SDK Library Integration Tests Runner / Check Secrets (push) Has been cancelled
SDK Library Integration Tests Runner / Missed OpenAI API Key Warning (push) Has been cancelled
SDK Library Integration Tests Runner / Build (push) Has been cancelled
SDK Library Integration Tests Runner / openai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_legacy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / llama_index_tests (push) Has been cancelled
SDK Library Integration Tests Runner / anthropic_tests (push) Has been cancelled
SDK Library Integration Tests Runner / mistral_tests (push) Has been cancelled
SDK Library Integration Tests Runner / groq_tests (push) Has been cancelled
SDK Library Integration Tests Runner / aisuite_tests (push) Has been cancelled
SDK Library Integration Tests Runner / haystack_tests (push) Has been cancelled
SDK Library Integration Tests Runner / dspy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v1_tests (push) Has been cancelled
SDK Library Integration Tests Runner / genai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_legacy_1_3_0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / evaluation_metrics_tests (push) Has been cancelled
SDK Library Integration Tests Runner / bedrock_tests (push) Has been cancelled
SDK Library Integration Tests Runner / litellm_tests (push) Has been cancelled
SDK Library Integration Tests Runner / harbor_tests (push) Has been cancelled
SDK Library Integration Tests Runner / Slack Notification (push) Has been cancelled
Lint Opik Helm Chart / render-equality (push) Has been cancelled
Opik Optimizer - Unit Tests / Opik Optimizer Unit Tests Python ${{matrix.python_version}} (push) Has been cancelled
Python BE E2E Tests / Python BE E2E (push) Has been cancelled
Python Backend Tests / run-python-backend-tests (push) Has been cancelled
Python SDK Unit Tests / Python SDK Unit Tests ${{matrix.python_version}} (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
SDK E2E Libraries Integration Tests / Check Secrets (push) Has been cancelled
SDK E2E Libraries Integration Tests / Missed OpenAI API Key Warning (push) Has been cancelled
SDK E2E Libraries Integration Tests / build-opik (push) Has been cancelled
SDK E2E Libraries Integration Tests / E2E Lib Integration Python ${{matrix.python_version}} (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-gemini) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-langchain) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-openai) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-otel) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-vercel) (push) Has been cancelled
TypeScript SDK Build & Publish / build-and-publish (push) Has been cancelled
TypeScript SDK Unit Tests / Test on Node ${{ matrix.node-version }} (push) Has been cancelled
Backend Tests / discover-tests (push) Has been cancelled
Backend Tests / ${{ matrix.name }} (push) Has been cancelled
Build and Publish SDK / build-and-publish (push) Has been cancelled
Build Opik Docker Images / set-version (push) Has been cancelled
Build Opik Docker Images / build-backend (push) Has been cancelled
Build Opik Docker Images / build-sandbox-executor-python (push) Has been cancelled
Build Opik Docker Images / build-python-backend (push) Has been cancelled
Build Opik Docker Images / build-frontend (push) Has been cancelled
Build Opik Docker Images / create-git-tag (push) Has been cancelled
ClickHouse Migration Cluster Check / validate-clickhouse-migrations (push) Has been cancelled
Docs - Publish / run (push) Has been cancelled
E2E Tests - Post Merge (v2) / 🧪 E2E v2 Tests (${{ github.event.inputs.tier || 't1' }}) (push) Has been cancelled
E2E Tests - Post Merge (v2) / 📢 Slack Notification (push) Has been cancelled
Frontend Unit Tests / Test on Node 20 (push) Has been cancelled
Guardrails E2E Tests / Select Python version matrix (push) Has been cancelled
Guardrails E2E Tests / Guardrails E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Guardrails E2E Tests / 📢 Slack Notification (push) Has been cancelled
Guardrails Backend Unit Tests / Guardrails Backend Unit Tests (push) Has been cancelled
Guardrails Backend Unit Tests / 📢 Slack Notification (push) Has been cancelled
Lint Opik Helm Chart / lint-helm-chart (Helm v3.21.0) (push) Has been cancelled
Lint Opik Helm Chart / lint-helm-chart (Helm v4.2.0) (push) Has been cancelled
Lint Opik Helm Chart / unittest-helm-chart (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:25:44 +08:00

189 lines
6.7 KiB
Python

"""Tests for run_simulation function."""
from unittest.mock import patch
from opik.simulation.simulator import run_simulation
from opik.simulation.simulated_user import SimulatedUser
class TestRunSimulation:
"""Test cases for run_simulation function."""
def test_run_simulation_basic(self, fake_backend):
"""Test basic simulation functionality."""
# Mock app that returns simple responses
def mock_app(message, *, thread_id, **kwargs):
return {"role": "assistant", "content": f"Response to: {message}"}
# Mock user simulator with fixed responses
user_simulator = SimulatedUser(
persona="Test user", fixed_responses=["Hello", "How are you?", "Goodbye"]
)
result = run_simulation(
app=mock_app, user_simulator=user_simulator, max_turns=3
)
assert "thread_id" in result
assert "conversation_history" in result
assert len(result["conversation_history"]) == 6 # 3 turns * 2 messages each
# Check conversation structure
history = result["conversation_history"]
assert history[0]["role"] == "user"
assert history[0]["content"] == "Hello"
assert history[1]["role"] == "assistant"
assert "Response to: Hello" in history[1]["content"]
def test_run_simulation_with_initial_message(self, fake_backend):
"""Test simulation with provided initial message."""
def mock_app(message, *, thread_id, **kwargs):
return {"role": "assistant", "content": "Got it"}
user_simulator = SimulatedUser(
persona="Test user", fixed_responses=["Follow up message"]
)
result = run_simulation(
app=mock_app,
user_simulator=user_simulator,
initial_message="Custom initial message",
max_turns=2,
)
history = result["conversation_history"]
assert history[0]["content"] == "Custom initial message"
assert history[2]["content"] == "Follow up message"
def test_run_simulation_with_thread_id(self, fake_backend):
"""Test simulation with provided thread_id."""
def mock_app(message, *, thread_id, **kwargs):
return {"role": "assistant", "content": "Response"}
user_simulator = SimulatedUser(persona="Test user", fixed_responses=["Message"])
custom_thread_id = "custom-thread-123"
result = run_simulation(
app=mock_app,
user_simulator=user_simulator,
thread_id=custom_thread_id,
max_turns=1,
)
assert result["thread_id"] == custom_thread_id
def test_run_simulation_with_app_kwargs(self, fake_backend):
"""Test simulation with additional app kwargs."""
def mock_app(message, *, thread_id, custom_param=None, **kwargs):
return {"role": "assistant", "content": f"Custom: {custom_param}"}
user_simulator = SimulatedUser(persona="Test user", fixed_responses=["Message"])
result = run_simulation(
app=mock_app,
user_simulator=user_simulator,
custom_param="test_value",
max_turns=1,
)
history = result["conversation_history"]
assert "Custom: test_value" in history[1]["content"]
def test_run_simulation_app_error_handling(self, fake_backend):
"""Test simulation handles app errors gracefully."""
def failing_app(message, *, thread_id, **kwargs):
raise Exception("App error")
user_simulator = SimulatedUser(persona="Test user", fixed_responses=["Message"])
result = run_simulation(
app=failing_app, user_simulator=user_simulator, max_turns=1
)
history = result["conversation_history"]
assert "Error processing message: App error" in history[1]["content"]
def test_run_simulation_invalid_app_response(self, fake_backend):
"""Test simulation handles invalid app responses."""
def invalid_app(message, *, thread_id, **kwargs):
return "Not a dict" # Invalid response format
user_simulator = SimulatedUser(persona="Test user", fixed_responses=["Message"])
result = run_simulation(
app=invalid_app, user_simulator=user_simulator, max_turns=1
)
history = result["conversation_history"]
assert history[1]["role"] == "assistant"
assert history[1]["content"] == "Not a dict"
def test_run_simulation_app_returns_none(self, fake_backend):
"""Test simulation handles None app responses."""
def none_app(message, *, thread_id, **kwargs):
return None
user_simulator = SimulatedUser(persona="Test user", fixed_responses=["Message"])
result = run_simulation(
app=none_app, user_simulator=user_simulator, max_turns=1
)
history = result["conversation_history"]
assert history[1]["role"] == "assistant"
assert history[1]["content"] == "No response"
@patch("opik.simulation.simulator.id_helpers.generate_id")
def test_run_simulation_generates_thread_id(self, mock_generate_id, fake_backend):
"""Test that thread_id is generated when not provided."""
mock_generate_id.return_value = "generated-thread-456"
def mock_app(message, *, thread_id, **kwargs):
return {"role": "assistant", "content": "Response"}
user_simulator = SimulatedUser(persona="Test user", fixed_responses=["Message"])
result = run_simulation(
app=mock_app, user_simulator=user_simulator, max_turns=1
)
assert result["thread_id"] == "generated-thread-456"
mock_generate_id.assert_called_once()
def test_run_simulation_with_project_name(self, fake_backend):
"""Test simulation includes project_name in result."""
def mock_app(message, *, thread_id, **kwargs):
return {"role": "assistant", "content": "Response"}
user_simulator = SimulatedUser(persona="Test user", fixed_responses=["Message"])
result = run_simulation(
app=mock_app,
user_simulator=user_simulator,
project_name="test_project",
max_turns=1,
)
assert result["project_name"] == "test_project"
def test_run_simulation_max_turns_zero(self, fake_backend):
"""Test simulation with zero max_turns."""
def mock_app(message, *, thread_id, **kwargs):
return {"role": "assistant", "content": "Response"}
user_simulator = SimulatedUser(persona="Test user", fixed_responses=["Message"])
result = run_simulation(
app=mock_app, user_simulator=user_simulator, max_turns=0
)
assert len(result["conversation_history"]) == 0