mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-30 01:08:33 +00:00
* test(proxy): move auth, hooks, policy_engine and client tests into tests/unit/proxy Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): stub HIBP through respx by disabling the aiohttp transport Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): share the httpx transport fixture across proxy unit tests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): restore proxy globals without a missing-value sentinel Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): package moved dirs and stub the login breach check at the HTTP boundary Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(proxy): isolate the mcp server manager per test Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: yuneng <yuneng@berri.ai> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
87 lines
2.8 KiB
Python
87 lines
2.8 KiB
Python
import os
|
|
import subprocess
|
|
import sys
|
|
from typing import Final
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
import pytest
|
|
|
|
from litellm.proxy.hooks.litellm_skills.main import SkillsInjectionHook
|
|
|
|
SKILL_TOOL_NAME = "litellm_skill_e2b8dca8_031a_4481_b034_b9ec7d4eb7bf"
|
|
|
|
|
|
def _request_data():
|
|
return {
|
|
"model": "claude-sonnet-4-5",
|
|
"messages": [{"role": "user", "content": "run the skill"}],
|
|
"litellm_metadata": {
|
|
"_litellm_code_execution_enabled": True,
|
|
"_skill_files": {SKILL_TOOL_NAME: {"main.py": b"print('hi')"}},
|
|
},
|
|
}
|
|
|
|
|
|
def _tool_use_response(tool_name):
|
|
return {
|
|
"stop_reason": "tool_use",
|
|
"content": [
|
|
{"type": "tool_use", "id": "toolu_1", "name": tool_name, "input": {}}
|
|
],
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_post_call_success_hook_executes_litellm_skill_tool():
|
|
"""DB skill tool names carry the litellm_skill_ prefix and must trigger the execution loop."""
|
|
hook = SkillsInjectionHook()
|
|
response = _tool_use_response(SKILL_TOOL_NAME)
|
|
|
|
with patch.object(
|
|
hook, "_execute_code_loop_messages_api", new=AsyncMock(return_value=response)
|
|
) as mock_loop:
|
|
result = await hook.async_post_call_success_deployment_hook(
|
|
request_data=_request_data(), response=response, call_type=None
|
|
)
|
|
|
|
mock_loop.assert_awaited_once()
|
|
assert result is response
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_execute_code_loop_dispatches_litellm_skill_tool():
|
|
"""The agentic loop must route litellm_skill_ tool calls to _execute_skill_tool."""
|
|
hook = SkillsInjectionHook()
|
|
final_response = {"stop_reason": "end_turn", "content": []}
|
|
|
|
with (
|
|
patch.object(
|
|
hook, "_execute_skill_tool", new=AsyncMock(return_value="skill ran")
|
|
) as mock_exec,
|
|
patch("litellm.anthropic.acreate", new=AsyncMock(return_value=final_response)),
|
|
):
|
|
result = await hook._execute_code_loop_messages_api(
|
|
data=_request_data(),
|
|
response=_tool_use_response(SKILL_TOOL_NAME),
|
|
skill_files={"main.py": b"print('hi')"},
|
|
)
|
|
|
|
mock_exec.assert_awaited_once()
|
|
assert mock_exec.await_args.args[0] == SKILL_TOOL_NAME
|
|
assert result is final_response
|
|
|
|
|
|
def test_importing_proxy_hooks_does_not_mutate_litellm_callbacks():
|
|
script: Final = (
|
|
"import litellm; "
|
|
"litellm.callbacks = []; "
|
|
"import litellm.proxy.hooks; "
|
|
"assert len(litellm.callbacks) == 0, f'mutated: {litellm.callbacks}'"
|
|
)
|
|
result: Final = subprocess.run(
|
|
[sys.executable, "-c", script],
|
|
capture_output=True,
|
|
text=True,
|
|
env={**os.environ, "LITELLM_LOCAL_MODEL_COST_MAP": "True"},
|
|
)
|
|
assert result.returncode == 0, f"stdout: {result.stdout}\nstderr: {result.stderr}"
|