141 lines
4.6 KiB
Python
141 lines
4.6 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
from collections.abc import Mapping, MutableMapping
|
|
|
|
import pytest
|
|
|
|
from agents.models import _openai_shared
|
|
from agents.models.openai_chatcompletions import OpenAIChatCompletionsModel
|
|
from agents.models.openai_responses import OpenAIResponsesModel
|
|
from agents.run import set_default_agent_runner
|
|
from agents.tracing.provider import DefaultTraceProvider
|
|
from agents.tracing.setup import set_trace_provider
|
|
|
|
from .testing_processor import SPAN_PROCESSOR_TESTING
|
|
|
|
_PROXY_ENVIRONMENT_VARIABLES = (
|
|
"ALL_PROXY",
|
|
"HTTP_PROXY",
|
|
"HTTPS_PROXY",
|
|
"all_proxy",
|
|
"http_proxy",
|
|
"https_proxy",
|
|
)
|
|
_PROXY_OPT_IN_ENVIRONMENT_VARIABLE = "OPENAI_AGENTS_TEST_USE_PROXY"
|
|
_CODEX_SANDBOX_ENVIRONMENT_VARIABLE = "OPENAI_AGENTS_TEST_IN_CODEX_SANDBOX"
|
|
_NATIVE_MACOS_SANDBOX_MARKER = "requires_native_macos_sandbox"
|
|
|
|
|
|
def _remove_ambient_proxy_environment(environment: MutableMapping[str, str]) -> None:
|
|
"""Keep unit tests independent from host proxy configuration."""
|
|
if environment.get(_PROXY_OPT_IN_ENVIRONMENT_VARIABLE, "").lower() in {
|
|
"1",
|
|
"true",
|
|
"yes",
|
|
}:
|
|
return
|
|
|
|
for variable in _PROXY_ENVIRONMENT_VARIABLES:
|
|
environment.pop(variable, None)
|
|
|
|
|
|
_remove_ambient_proxy_environment(os.environ)
|
|
|
|
|
|
def _running_in_nested_codex_macos_sandbox(
|
|
*, platform: str, environment: Mapping[str, str]
|
|
) -> bool:
|
|
return platform == "darwin" and environment.get(_CODEX_SANDBOX_ENVIRONMENT_VARIABLE) == "1"
|
|
|
|
|
|
def pytest_collection_modifyitems(items: list[pytest.Item]) -> None:
|
|
if not _running_in_nested_codex_macos_sandbox(platform=sys.platform, environment=os.environ):
|
|
return
|
|
|
|
skip_native_macos_sandbox = pytest.mark.skip(
|
|
reason="requires a native macOS sandbox and cannot run inside the Codex outer sandbox"
|
|
)
|
|
for item in items:
|
|
if item.get_closest_marker(_NATIVE_MACOS_SANDBOX_MARKER) is not None:
|
|
item.add_marker(skip_native_macos_sandbox)
|
|
|
|
|
|
collect_ignore: list[str] = []
|
|
|
|
if sys.platform == "win32":
|
|
collect_ignore.extend(
|
|
[
|
|
"test_example_workflows.py",
|
|
"test_run_state.py",
|
|
"sandbox/capabilities/test_filesystem_capability.py",
|
|
"sandbox/integration_tests/test_runner_pause_resume.py",
|
|
"sandbox/test_client_options.py",
|
|
"sandbox/test_exposed_ports.py",
|
|
"sandbox/test_extract.py",
|
|
"sandbox/test_memory.py",
|
|
"sandbox/test_runtime.py",
|
|
"sandbox/test_session_manager.py",
|
|
"sandbox/test_session_sinks.py",
|
|
"sandbox/test_snapshot.py",
|
|
"sandbox/test_unix_local.py",
|
|
]
|
|
)
|
|
|
|
|
|
# This fixture will run once before any tests are executed
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def setup_span_processor():
|
|
provider = DefaultTraceProvider()
|
|
provider.set_processors([SPAN_PROCESSOR_TESTING])
|
|
set_trace_provider(provider)
|
|
yield
|
|
provider.shutdown()
|
|
|
|
|
|
# Ensure a default OpenAI API key is present for tests that construct clients
|
|
# without explicitly configuring a key/client. Tests that need no key use
|
|
# monkeypatch.delenv("OPENAI_API_KEY", ...) to remove it locally.
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def ensure_openai_api_key():
|
|
if not os.environ.get("OPENAI_API_KEY"):
|
|
os.environ["OPENAI_API_KEY"] = "test_key"
|
|
|
|
|
|
# This fixture will run before each test
|
|
@pytest.fixture(autouse=True)
|
|
def clear_span_processor():
|
|
SPAN_PROCESSOR_TESTING.force_flush()
|
|
SPAN_PROCESSOR_TESTING.shutdown()
|
|
SPAN_PROCESSOR_TESTING.clear()
|
|
|
|
|
|
# This fixture will run before each test
|
|
@pytest.fixture(autouse=True)
|
|
def clear_openai_settings():
|
|
_openai_shared._default_openai_key = None
|
|
_openai_shared._default_openai_client = None
|
|
_openai_shared._use_responses_by_default = True
|
|
_openai_shared.set_default_openai_responses_transport("http")
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def clear_default_runner():
|
|
set_default_agent_runner(None)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def disable_real_model_clients(monkeypatch, request):
|
|
# If the test is marked to allow the method call, don't override it.
|
|
if request.node.get_closest_marker("allow_call_model_methods"):
|
|
return
|
|
|
|
def failing_version(*args, **kwargs):
|
|
pytest.fail("Real models should not be used in tests!")
|
|
|
|
monkeypatch.setattr(OpenAIResponsesModel, "get_response", failing_version)
|
|
monkeypatch.setattr(OpenAIResponsesModel, "stream_response", failing_version)
|
|
monkeypatch.setattr(OpenAIChatCompletionsModel, "get_response", failing_version)
|
|
monkeypatch.setattr(OpenAIChatCompletionsModel, "stream_response", failing_version)
|