mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-26 01:12:21 +00:00
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ci: rename fork-flag to unit-flag now that it applies on every event * test: move tests/test_litellm root and small trees into tests/unit Pure renames, no content changes. Follow-up commits in this PR fix references, merge the three files that already existed in tests/unit, keep live-provider tests in tests/test_litellm and wire CI. * test: carry tests/test_litellm conftest isolation into tests/unit Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS, proxy-URL and keychain env, and session-end client cleanup now reset for unit tests too. The environment isolation owns its MonkeyPatch so a test's own monkeypatch is undone before the model-cost teardown runs. * test: merge, split and prune the moved root and small-tree tests Merge batches/test_batch_utils.py and the chat_completions and messages dispatch tests into the files that already existed in tests/unit. Keep the live Gemini interactions tests, the async image-fetch format test and the OpenAI embedding scorer test in tests/test_litellm since they need real network or keys. Put test_router.py under tests/unit/test_router so the existing package no longer shadows it. Delete eight tests the audit found superseded by stronger ones kept in this move. * ci: run the moved root and small-tree tests under their legacy flags Add the misc and responses-caching-types flags to unit_selection.sh and CircleCI, extend enterprise-routing and mcp-integration, and point the legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest and change classifier at the new paths. * test: make the new tests/unit directories packages tests/unit/test_package_layout.py requires every directory to carry an __init__.py, and without one the moved and retained test_litellm_responses_bridge.py modules collide on import. * test: scope the unit socket block to tests/unit in shared sessions The GHA shards collect the legacy test-path and the unit selection in one pytest session. The unit conftest's loopback-only block leaked into legacy modules that reach the network at import. The legacy conftest now lifts the restriction at collect and setup time, and the unit conftest re-applies it when collecting its own modules. * test: move tests/test_litellm/llms into tests/unit/llms Rename-only. Moves the provider tests and the fine-tuning fixtures they load, mirroring the old paths. Follow-up commits merge, split and wire them. * test: merge, split and prune the moved llms tests Merges the Databricks chat transformation tests into the existing unit file, keeps the tests that need real keys or the network in tests/test_litellm, deletes the audited tests a stronger unit test already covers, and points imports at tests.unit.llms. * ci: run the moved llms tests under their legacy flags The Vertex AI and All Other Providers shards keep their legacy test-path for the retained files and add the llm-vertex-ai and llm-other-providers unit selections. CircleCI gets matching unit jobs. * test: make the tests/unit/llms directories packages Adds __init__.py to the moved dirs and drops the legacy ones whose directories no longer hold tests. * test: drop script runners and path hacks the llms split left dangling The __main__ runners in the split openai_like files and the Databricks e2e runner called tests that now live in the other half of the split or were deleted. The retained legacy halves also no longer need sys.path edits. * test: give the shard-script tests their own GITHUB_OUTPUT They only passed where the runner set it. The CircleCI unit job's env allowlist drops it, so the script's redirect failed there. * test: point the router and module-deletion checks at tests/unit router_code_coverage and code_qa_check_tests only searched tests/test_litellm, so the moved router tests no longer counted. The two silent-experiment tests the audit deleted were the only direct callers of those methods; they are replaced with tests that assert the forwarded shadow request and the recursion guard. * test: move tests/test_litellm integrations and secret_managers into tests/unit Rename-only. Mirrors the old paths, including the directory conftests and the prompt and JSON fixtures. Follow-up commits prune and wire them. * test: prune and repoint the moved integrations tests Deletes the 7 audited tests a stronger test in the same tree already covers, imports the TLS sink helpers from their new conftest path, and restores os.environ after each integrations test. Some presets write OTEL_EXPORTER_OTLP_HEADERS straight into os.environ, and without the legacy tree's test ordering that header leaked into the AgentOps tests. * ci: run the moved integrations tests under their legacy flag The integrations GHA shard and a new CircleCI job run the integrations unit selection. secret_managers joins the misc selection. * docs: point integrations and secret_managers references at tests/unit * test: make the moved integrations directories packages * test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path The Databricks e2e file is a manual script whose main() calls the tests that were pruned, so pruning them broke the documented run. It is back to its main version. The SageMaker Nova docstring now points at the file's real location in tests/local_testing. * test: keep the job's UNIT_FLAG out of the shard-script tests --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
462 lines
16 KiB
Python
462 lines
16 KiB
Python
|
|
|
|
import pytest
|
|
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
from litellm.integrations.gitlab.gitlab_prompt_manager import GitLabPromptManager
|
|
|
|
|
|
# -----------------------------
|
|
# Basic init & template loading
|
|
# -----------------------------
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_prompt_manager_initialization_with_root_folder(mock_client_class):
|
|
"""Loads a prompt from the repo root when no prompts_path is specified."""
|
|
mock_client = MagicMock()
|
|
mock_client.get_file_content.return_value = """---
|
|
model: gpt-4
|
|
temperature: 0.7
|
|
max_tokens: 150
|
|
---
|
|
System: You are a helpful assistant.
|
|
|
|
User: {{user_message}}"""
|
|
mock_client_class.return_value = mock_client
|
|
|
|
config = {
|
|
"project": "group/sub/repo",
|
|
"access_token": "glpat_xxx",
|
|
# no prompts_path -> root
|
|
}
|
|
|
|
manager = GitLabPromptManager(config, prompt_id="test_prompt")
|
|
# Should have loaded the prompt
|
|
assert "test_prompt" in manager.prompt_manager.prompts
|
|
template = manager.prompt_manager.prompts["test_prompt"]
|
|
assert template.model == "gpt-4"
|
|
assert template.temperature == 0.7
|
|
assert template.max_tokens == 150
|
|
|
|
# Ensures correct file path was requested at repo root (test_prompt.prompt)
|
|
mock_client.get_file_content.assert_called_with("test_prompt.prompt", ref=None)
|
|
|
|
# Rendering
|
|
rendered = manager.prompt_manager.render_template(
|
|
"test_prompt", {"user_message": "What is AI?"}
|
|
)
|
|
assert "You are a helpful assistant." in rendered
|
|
assert "What is AI?" in rendered
|
|
|
|
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_prompt_manager_with_prompts_path(mock_client_class):
|
|
"""Loads a prompt from a configured prompts folder; ID maps to folder + .prompt."""
|
|
mock_client = MagicMock()
|
|
mock_client.get_file_content.return_value = "Hello {{name}}!"
|
|
mock_client_class.return_value = mock_client
|
|
|
|
config = {
|
|
"project": "group/repo",
|
|
"access_token": "token",
|
|
"prompts_path": "prompts/chat", # folder setting
|
|
}
|
|
|
|
manager = GitLabPromptManager(config, prompt_id="greet/hi")
|
|
# Expected path: prompts/chat/greet/hi.prompt
|
|
mock_client.get_file_content.assert_called_with(
|
|
"prompts/chat/greet/hi.prompt", ref=None
|
|
)
|
|
|
|
rendered = manager.prompt_manager.render_template("greet/hi", {"name": "World"})
|
|
assert rendered == "Hello World!"
|
|
|
|
|
|
# -----------------------------
|
|
# Error handling / validation
|
|
# -----------------------------
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_prompt_manager_error_handling_load(mock_client_class):
|
|
"""Errors from GitLabClient surface with helpful context."""
|
|
mock_client = MagicMock()
|
|
mock_client.get_file_content.side_effect = Exception("GitLab API error")
|
|
mock_client_class.return_value = mock_client
|
|
|
|
config = {"project": "g/s/r", "access_token": "tkn"}
|
|
|
|
with pytest.raises(
|
|
Exception, match="Failed to load prompt 'gitlab::oops' from GitLab"
|
|
):
|
|
_ = GitLabPromptManager(config, prompt_id="oops").prompt_manager
|
|
|
|
|
|
def test_gitlab_prompt_manager_config_validation_via_client_ctor():
|
|
"""
|
|
If GitLabClient validates config in __init__, simulate that with a side_effect.
|
|
Ensures manager surfaces the ValueError while building prompt_manager.
|
|
"""
|
|
with patch(
|
|
"litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient",
|
|
side_effect=ValueError("project and access_token are required"),
|
|
):
|
|
with pytest.raises(ValueError, match="project and access_token are required"):
|
|
_ = GitLabPromptManager({}).prompt_manager
|
|
|
|
|
|
# -----------------------------
|
|
# Message parsing
|
|
# -----------------------------
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_prompt_manager_message_parsing(mock_client_class):
|
|
mock_client = MagicMock()
|
|
mock_client.get_file_content.return_value = """---
|
|
model: gpt-4
|
|
---
|
|
System: You are a helpful assistant.
|
|
|
|
User: {{user_message}}
|
|
|
|
Assistant: I'll help you with that."""
|
|
mock_client_class.return_value = mock_client
|
|
|
|
config = {"project": "g/s/r", "access_token": "t"}
|
|
|
|
manager = GitLabPromptManager(config, prompt_id="conversation_prompt")
|
|
|
|
messages = manager._parse_prompt_to_messages(
|
|
"System: You are a helpful assistant.\n\nUser: Hello!\n\nAssistant: Hi there!"
|
|
)
|
|
assert len(messages) == 3
|
|
assert messages[0]["role"] == "system"
|
|
assert messages[0]["content"] == "You are a helpful assistant."
|
|
assert messages[1]["role"] == "user"
|
|
assert messages[1]["content"] == "Hello!"
|
|
assert messages[2]["role"] == "assistant"
|
|
assert messages[2]["content"] == "Hi there!"
|
|
|
|
|
|
# -----------------------------
|
|
# pre_call_hook behavior & ref precedence
|
|
# -----------------------------
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_prompt_manager_pre_call_hook_updates_params(mock_client_class):
|
|
mock_client = MagicMock()
|
|
mock_client.get_file_content.return_value = """---
|
|
model: gpt-4o
|
|
temperature: 0.8
|
|
max_tokens: 256
|
|
---
|
|
System: You are a helpful assistant.
|
|
|
|
User: {{user_message}}"""
|
|
mock_client_class.return_value = mock_client
|
|
|
|
config = {"project": "g/s/r", "access_token": "tkn"}
|
|
|
|
manager = GitLabPromptManager(config, prompt_id="test_prompt")
|
|
|
|
original_messages = [{"role": "user", "content": "This will be ignored"}]
|
|
litellm_params = {"api_key": "keep-me"}
|
|
|
|
result_messages, result_params = manager.pre_call_hook(
|
|
user_id="u",
|
|
messages=original_messages,
|
|
litellm_params=litellm_params,
|
|
prompt_id="test_prompt",
|
|
prompt_variables={"user_message": "What is AI?"},
|
|
)
|
|
|
|
# Prompt parsed into messages
|
|
assert len(result_messages) == 2
|
|
assert result_messages[0]["role"] == "system"
|
|
assert result_messages[1]["role"] == "user"
|
|
assert result_messages[1]["content"] == "What is AI?"
|
|
|
|
# Params merged + preserved
|
|
assert result_params["model"] == "gpt-4o"
|
|
assert result_params["temperature"] == 0.8
|
|
assert result_params["max_tokens"] == 256
|
|
assert result_params["api_key"] == "keep-me"
|
|
|
|
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_prompt_manager_pre_call_hook_ref_precedence(mock_client_class):
|
|
"""
|
|
Precedence for selecting git ref:
|
|
prompt_version (arg) > git_ref kwarg > manager's _ref_override > client's default
|
|
Validate that the chosen ref gets passed down to client.get_file_content.
|
|
"""
|
|
mock_client = MagicMock()
|
|
|
|
# Return any minimal valid prompt; we just need the call path to succeed.
|
|
mock_client.get_file_content.return_value = """---
|
|
model: gpt-4
|
|
---
|
|
User: {{q}}"""
|
|
mock_client_class.return_value = mock_client
|
|
|
|
config = {"project": "g/s/r", "access_token": "tkn"}
|
|
|
|
# Set a manager-level default ref override
|
|
manager = GitLabPromptManager(config, prompt_id=None, ref="manager-default")
|
|
|
|
# 1) No prior load; call with prompt_version -> should win
|
|
_msgs, _params = manager.pre_call_hook(
|
|
user_id="u",
|
|
messages=[],
|
|
litellm_params={},
|
|
prompt_id="p1",
|
|
prompt_variables={"q": "hello"},
|
|
prompt_version="explicit-sha",
|
|
)
|
|
# get_file_content called with ref="explicit-sha"
|
|
mock_client.get_file_content.assert_any_call("p1.prompt", ref="explicit-sha")
|
|
|
|
# 2) Use git_ref kwarg (when no prompt_version)
|
|
_msgs, _params = manager.pre_call_hook(
|
|
user_id="u",
|
|
messages=[],
|
|
litellm_params={},
|
|
prompt_id="p2",
|
|
prompt_variables={"q": "hello"},
|
|
git_ref="per-call-branch",
|
|
)
|
|
mock_client.get_file_content.assert_any_call("p2.prompt", ref="per-call-branch")
|
|
|
|
# 3) Neither prompt_version nor git_ref -> falls back to manager _ref_override
|
|
_msgs, _params = manager.pre_call_hook(
|
|
user_id="u",
|
|
messages=[],
|
|
litellm_params={},
|
|
prompt_id="p3",
|
|
prompt_variables={"q": "hello"},
|
|
)
|
|
mock_client.get_file_content.assert_any_call("p3.prompt", ref="manager-default")
|
|
|
|
|
|
# -----------------------------
|
|
# Listing & availability
|
|
# -----------------------------
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_prompt_manager_list_templates_with_prompts_path(mock_client_class):
|
|
mock_client = MagicMock()
|
|
mock_client.list_files.return_value = [
|
|
"prompts/chat/a.prompt",
|
|
"prompts/chat/sub/b.prompt",
|
|
"prompts/chat/ignore.txt",
|
|
]
|
|
mock_client.get_file_content.return_value = "Hello"
|
|
mock_client_class.return_value = mock_client
|
|
|
|
config = {
|
|
"project": "g/s/r",
|
|
"access_token": "tkn",
|
|
"prompts_path": "prompts/chat",
|
|
}
|
|
|
|
manager = GitLabPromptManager(config, prompt_id="a")
|
|
|
|
# list_templates strips folder prefix + extension
|
|
ids = manager.get_available_prompts()
|
|
assert "a" in ids
|
|
assert "gitlab::sub::b" in ids
|
|
assert all(not x.endswith(".prompt") for x in ids)
|
|
assert all("/prompts/chat/" not in x for x in ids)
|
|
|
|
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_template_manager_load_all_prompts(mock_client_class):
|
|
"""load_all_prompts should fetch all .prompt files and populate the internal cache."""
|
|
mock_client = MagicMock()
|
|
mock_client.list_files.return_value = [
|
|
"prompts/a.prompt",
|
|
"prompts/sub/b.prompt",
|
|
]
|
|
mock_client.get_file_content.side_effect = [
|
|
"Hello {{x}}", # for a.prompt
|
|
"---\nmodel: gpt-4\n---\nUser: {{y}}", # for b.prompt with frontmatter
|
|
]
|
|
mock_client_class.return_value = mock_client
|
|
|
|
config = {
|
|
"project": "g/s/r",
|
|
"access_token": "tkn",
|
|
"prompts_path": "prompts",
|
|
}
|
|
|
|
pm = GitLabPromptManager(config).prompt_manager
|
|
loaded = pm.load_all_prompts()
|
|
assert set(loaded) == {"gitlab::a", "gitlab::sub::b"}
|
|
assert "gitlab::a" in pm.prompts and "gitlab::sub::b" in pm.prompts
|
|
|
|
|
|
# -----------------------------
|
|
# post_call & integration name
|
|
# -----------------------------
|
|
def test_gitlab_prompt_manager_integration_name():
|
|
config = {"project": "g/s/r", "access_token": "tkn"}
|
|
manager = GitLabPromptManager(config)
|
|
assert manager.integration_name == "gitlab"
|
|
|
|
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_prompt_manager_post_call_hook_passthrough(mock_client_class):
|
|
mock_client = MagicMock()
|
|
mock_client.get_file_content.return_value = "User: {{m}}"
|
|
mock_client_class.return_value = mock_client
|
|
|
|
config = {"project": "g/s/r", "access_token": "tkn"}
|
|
|
|
manager = GitLabPromptManager(config, prompt_id="p")
|
|
|
|
dummy_response = MagicMock()
|
|
out = manager.post_call_hook(
|
|
user_id="u",
|
|
response=dummy_response,
|
|
input_messages=[{"role": "user", "content": "x"}],
|
|
litellm_params={},
|
|
prompt_id="p",
|
|
)
|
|
assert out is dummy_response
|
|
|
|
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_prompt_version_precedence_prompt_version_wins(mock_client_class):
|
|
"""
|
|
prompt_version > git_ref kwarg > manager _ref_override.
|
|
Ensure prompt_version wins and is passed down to GitLabClient.get_file_content.
|
|
"""
|
|
mock_client = MagicMock()
|
|
mock_client.get_file_content.return_value = """---
|
|
model: gpt-4
|
|
---
|
|
User: {{q}}"""
|
|
mock_client_class.return_value = mock_client
|
|
|
|
cfg = {"project": "g/s/r", "access_token": "tkn"}
|
|
|
|
# Manager with a default override ref
|
|
mgr = GitLabPromptManager(cfg, ref="manager-default")
|
|
|
|
# Provide both git_ref kwarg and prompt_version, the latter should win
|
|
msgs, params = mgr.pre_call_hook(
|
|
user_id="u",
|
|
messages=[],
|
|
litellm_params={},
|
|
prompt_id="promptA",
|
|
prompt_variables={"q": "hello"},
|
|
prompt_version="sha-111", # highest precedence
|
|
git_ref="feature/branch-xyz", # should be ignored because prompt_version provided
|
|
)
|
|
|
|
mock_client.get_file_content.assert_any_call("promptA.prompt", ref="sha-111")
|
|
# sanity — prompt parsed and params returned
|
|
assert any(m["role"] == "user" for m in msgs)
|
|
assert params.get("model") == "gpt-4"
|
|
|
|
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_prompt_version_ref_kwarg_used_when_no_prompt_version(mock_client_class):
|
|
"""
|
|
If prompt_version is omitted, git_ref kwarg should be used.
|
|
"""
|
|
mock_client = MagicMock()
|
|
mock_client.get_file_content.return_value = "User: {{q}}"
|
|
mock_client_class.return_value = mock_client
|
|
|
|
cfg = {"project": "g/s/r", "access_token": "tkn"}
|
|
mgr = GitLabPromptManager(cfg, ref="fallback-manager-ref")
|
|
|
|
_msgs, _params = mgr.pre_call_hook(
|
|
user_id="u",
|
|
messages=[],
|
|
litellm_params={},
|
|
prompt_id="promptB",
|
|
prompt_variables={"q": "hi"},
|
|
git_ref="hotfix/ref-2", # used since prompt_version not provided
|
|
)
|
|
|
|
mock_client.get_file_content.assert_any_call("promptB.prompt", ref="hotfix/ref-2")
|
|
|
|
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_prompt_version_manager_override_used_when_no_prompt_version_or_kwarg(
|
|
mock_client_class,
|
|
):
|
|
"""
|
|
If neither prompt_version nor git_ref is supplied, fall back to manager-level ref override.
|
|
"""
|
|
mock_client = MagicMock()
|
|
mock_client.get_file_content.return_value = "User: {{q}}"
|
|
mock_client_class.return_value = mock_client
|
|
|
|
cfg = {"project": "g/s/r", "access_token": "tkn"}
|
|
mgr = GitLabPromptManager(cfg, ref="manager-override-ref")
|
|
|
|
_msgs, _params = mgr.pre_call_hook(
|
|
user_id="u",
|
|
messages=[],
|
|
litellm_params={},
|
|
prompt_id="promptC",
|
|
prompt_variables={"q": "hey"},
|
|
)
|
|
|
|
mock_client.get_file_content.assert_any_call(
|
|
"promptC.prompt", ref="manager-override-ref"
|
|
)
|
|
|
|
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_get_prompt_template_explicit_ref_param(mock_client_class):
|
|
"""
|
|
Directly calling get_prompt_template(ref=...) should pass that ref to GitLabClient.
|
|
"""
|
|
mock_client = MagicMock()
|
|
mock_client.get_file_content.return_value = """---
|
|
model: gpt-4o
|
|
---
|
|
User: {{x}}"""
|
|
mock_client_class.return_value = mock_client
|
|
|
|
cfg = {"project": "g/s/r", "access_token": "tkn"}
|
|
mgr = GitLabPromptManager(cfg)
|
|
|
|
rendered, metadata = mgr.get_prompt_template(
|
|
prompt_id="promptD",
|
|
prompt_variables={"x": "value"},
|
|
ref="v1.2.3", # explicit tag
|
|
)
|
|
mock_client.get_file_content.assert_any_call("promptD.prompt", ref="v1.2.3")
|
|
assert "value" in rendered
|
|
assert metadata.get("model") == "gpt-4o"
|
|
|
|
|
|
@patch("litellm.integrations.gitlab.gitlab_prompt_manager.GitLabClient")
|
|
def test_gitlab_prompt_version_with_prompts_path(mock_client_class):
|
|
"""
|
|
Ensure prompts_path + prompt_version work together (path resolution + ref).
|
|
"""
|
|
mock_client = MagicMock()
|
|
mock_client.get_file_content.return_value = "User: {{q}}"
|
|
mock_client_class.return_value = mock_client
|
|
|
|
cfg = {
|
|
"project": "g/s/r",
|
|
"access_token": "tkn",
|
|
"prompts_path": "prompts/chat",
|
|
}
|
|
mgr = GitLabPromptManager(cfg)
|
|
|
|
_msgs, _params = mgr.pre_call_hook(
|
|
user_id="u",
|
|
messages=[],
|
|
litellm_params={},
|
|
prompt_id="folder/sub/my_prompt",
|
|
prompt_variables={"q": "ok"},
|
|
prompt_version="commit-sha-999",
|
|
)
|
|
|
|
# Path should include prompts_path and end with .prompt
|
|
mock_client.get_file_content.assert_any_call(
|
|
"prompts/chat/folder/sub/my_prompt.prompt", ref="commit-sha-999"
|
|
)
|