chore: import upstream snapshot with attribution
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

This commit is contained in:
wehub-resource-sync
2026-07-13 13:25:44 +08:00
commit 5a558eb09e
11579 changed files with 1795921 additions and 0 deletions
@@ -0,0 +1,129 @@
"""Tests for SimulatedUser class."""
from unittest.mock import Mock, patch
from opik.simulation.simulated_user import SimulatedUser
class TestSimulatedUser:
"""Test cases for SimulatedUser class."""
def test_init_with_persona_and_model(self):
"""Test SimulatedUser initialization with persona and model."""
user = SimulatedUser(persona="You are a helpful assistant", model="gpt-4o-mini")
assert user.persona == "You are a helpful assistant"
assert user.model == "gpt-4o-mini"
assert user.fixed_responses == []
assert user._response_index == 0
assert user._llm is not None
def test_init_with_fixed_responses(self):
"""Test SimulatedUser initialization with fixed responses."""
fixed_responses = ["Hello!", "How are you?", "Goodbye!"]
user = SimulatedUser(persona="Test persona", fixed_responses=fixed_responses)
assert user.fixed_responses == fixed_responses
assert user._response_index == 0
def test_generate_response_with_fixed_responses(self):
"""Test response generation using fixed responses."""
fixed_responses = ["Response 1", "Response 2", "Response 3"]
user = SimulatedUser(persona="Test persona", fixed_responses=fixed_responses)
# First call
response1 = user.generate_response([])
assert response1 == "Response 1"
assert user._response_index == 1
# Second call
response2 = user.generate_response([])
assert response2 == "Response 2"
assert user._response_index == 2
# Third call
response3 = user.generate_response([])
assert response3 == "Response 3"
assert user._response_index == 3
# Fourth call (cycles back)
response4 = user.generate_response([])
assert response4 == "Response 1"
assert user._response_index == 4
@patch("opik.simulation.simulated_user.get_model")
def test_generate_response_with_llm(self, mock_get_model):
"""Test response generation using LLM when no fixed responses."""
# Mock the LLM response
mock_llm_instance = Mock()
mock_llm_instance.generate_string.return_value = "LLM generated response"
mock_get_model.return_value = mock_llm_instance
user = SimulatedUser(persona="You are a helpful assistant", model="gpt-4o-mini")
conversation_history = [
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi there!"},
]
response = user.generate_response(conversation_history)
assert response == "LLM generated response"
# Verify LLM was called with correct input
mock_llm_instance.generate_string.assert_called_once()
call_args = mock_llm_instance.generate_string.call_args[1]["input"]
assert (
"You are a simulated user with the following persona: You are a helpful assistant"
in call_args
)
assert "User: Hello" in call_args
assert "Assistant: Hi there!" in call_args
@patch("opik.simulation.simulated_user.get_model")
def test_generate_response_with_llm_error(self, mock_get_model):
"""Test response generation when LLM raises an exception."""
# Mock the LLM to raise an exception
mock_llm_instance = Mock()
mock_llm_instance.generate_string.side_effect = Exception("LLM error")
mock_get_model.return_value = mock_llm_instance
user = SimulatedUser(persona="Test persona", model="gpt-4o-mini")
response = user.generate_response([])
assert "I'm having trouble responding right now. (LLM error)" in response
@patch("opik.simulation.simulated_user.get_model")
def test_generate_response_with_long_history(self, mock_get_model):
"""Test that long conversation history is handled correctly."""
mock_llm_instance = Mock()
mock_llm_instance.generate_string.return_value = "Response"
mock_get_model.return_value = mock_llm_instance
user = SimulatedUser(persona="Test persona", model="gpt-4o-mini")
# Create a long conversation history (15 messages)
long_history = []
for i in range(15):
long_history.extend(
[
{"role": "user", "content": f"Message {i}"},
{"role": "assistant", "content": f"Response {i}"},
]
)
user.generate_response(long_history)
# Verify LLM was called
mock_llm_instance.generate_string.assert_called_once()
call_args = mock_llm_instance.generate_string.call_args[1]["input"]
# Should contain system message and all conversation messages
assert (
"You are a simulated user with the following persona: Test persona"
in call_args
)
assert "User: Message 0" in call_args
assert "Assistant: Response 0" in call_args
assert "User: Message 14" in call_args
assert "Assistant: Response 14" in call_args
@@ -0,0 +1,188 @@
"""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