|
| 1 | +from unittest.mock import MagicMock |
| 2 | + |
| 3 | +import pytest |
| 4 | +from _pytest.monkeypatch import MonkeyPatch |
| 5 | +from pytest_httpx import HTTPXMock |
| 6 | + |
| 7 | +from uipath.eval.mocks import mockable |
| 8 | +from uipath.eval.mocks._cache_manager import CacheManager |
| 9 | +from uipath.eval.mocks._mock_runtime import ( |
| 10 | + clear_execution_context, |
| 11 | + set_execution_context, |
| 12 | +) |
| 13 | +from uipath.eval.mocks._simulation_model import ( |
| 14 | + SIMULATION_MODEL_ENV, |
| 15 | + simulation_completion_kwargs, |
| 16 | +) |
| 17 | +from uipath.eval.mocks._types import ( |
| 18 | + LLMMockingStrategy, |
| 19 | + MockingContext, |
| 20 | + ModelSettings, |
| 21 | + ToolSimulation, |
| 22 | +) |
| 23 | + |
| 24 | +BYOM_MODEL = "Siemens-SDC-gpt-4o" |
| 25 | + |
| 26 | + |
| 27 | +def test_defaults_to_gateway_model_without_settings_or_env(monkeypatch: MonkeyPatch): |
| 28 | + monkeypatch.delenv(SIMULATION_MODEL_ENV, raising=False) |
| 29 | + |
| 30 | + assert simulation_completion_kwargs(None) == {"model": "gpt-4.1-mini-2025-04-14"} |
| 31 | + |
| 32 | + |
| 33 | +def test_env_model_applies_when_settings_have_no_model(monkeypatch: MonkeyPatch): |
| 34 | + monkeypatch.setenv(SIMULATION_MODEL_ENV, BYOM_MODEL) |
| 35 | + |
| 36 | + assert simulation_completion_kwargs(None) == {"model": BYOM_MODEL} |
| 37 | + |
| 38 | + |
| 39 | +def test_configured_model_wins_over_env(monkeypatch: MonkeyPatch): |
| 40 | + monkeypatch.setenv(SIMULATION_MODEL_ENV, BYOM_MODEL) |
| 41 | + settings = ModelSettings(model="gpt-4o-2024-11-20", temperature=0.2) |
| 42 | + |
| 43 | + assert simulation_completion_kwargs(settings) == { |
| 44 | + "model": "gpt-4o-2024-11-20", |
| 45 | + "temperature": 0.2, |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.httpx_mock(assert_all_responses_were_requested=False) |
| 50 | +def test_tool_simulation_sends_env_model( |
| 51 | + httpx_mock: HTTPXMock, monkeypatch: MonkeyPatch |
| 52 | +): |
| 53 | + monkeypatch.setenv("UIPATH_URL", "https://example.com") |
| 54 | + monkeypatch.setenv("UIPATH_ACCESS_TOKEN", "1234567890") |
| 55 | + monkeypatch.setenv(SIMULATION_MODEL_ENV, BYOM_MODEL) |
| 56 | + monkeypatch.setattr(CacheManager, "get", lambda *args, **kwargs: None) |
| 57 | + monkeypatch.setattr(CacheManager, "set", lambda *args, **kwargs: None) |
| 58 | + |
| 59 | + @mockable() |
| 60 | + def web_search(*args, **kwargs) -> str: |
| 61 | + raise NotImplementedError() |
| 62 | + |
| 63 | + for service in ("agenthub_", "orchestrator_"): |
| 64 | + httpx_mock.add_response( |
| 65 | + url=f"https://example.com/{service}/llm/api/capabilities", json={} |
| 66 | + ) |
| 67 | + httpx_mock.add_response( |
| 68 | + url="https://example.com/llm/api/chat/completions" |
| 69 | + "?api-version=2024-08-01-preview", |
| 70 | + json={ |
| 71 | + "id": "response-id", |
| 72 | + "object": "", |
| 73 | + "created": 0, |
| 74 | + "model": BYOM_MODEL, |
| 75 | + "choices": [ |
| 76 | + { |
| 77 | + "index": 0, |
| 78 | + "message": {"role": "ai", "content": '"ok"', "tool_calls": None}, |
| 79 | + "finish_reason": "EOS", |
| 80 | + } |
| 81 | + ], |
| 82 | + "usage": {"prompt_tokens": 1, "completion_tokens": 1, "total_tokens": 2}, |
| 83 | + }, |
| 84 | + ) |
| 85 | + # Mirrors what cli_run/cli_debug build for a coded agent: no model anywhere. |
| 86 | + set_execution_context( |
| 87 | + MockingContext( |
| 88 | + strategy=LLMMockingStrategy( |
| 89 | + prompt="", |
| 90 | + tools_to_simulate=[ToolSimulation(name="web_search")], |
| 91 | + ), |
| 92 | + name="debug-simulation", |
| 93 | + ), |
| 94 | + MagicMock(), |
| 95 | + "test-execution-id", |
| 96 | + ) |
| 97 | + try: |
| 98 | + assert web_search() == "ok" |
| 99 | + finally: |
| 100 | + clear_execution_context() |
| 101 | + |
| 102 | + request = httpx_mock.get_request(method="POST") |
| 103 | + assert request is not None |
| 104 | + assert request.headers["X-UiPath-LlmGateway-NormalizedApi-ModelName"] == BYOM_MODEL |
0 commit comments