litellm/tests/unit/test_mutation_report.py
yuneng-jiang f6882246d4
test: move tests/test_litellm root and small trees into tests/unit (#43186)
* 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: 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.

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-25 11:30:43 -07:00

139 lines
4.8 KiB
Python

"""Tests for scripts/mutation_report.py.
The report is the only thing anyone reads after a mutation run, so the one thing it
must never do is describe a run that produced nothing as a run that killed everything.
`render` decides that wording and `get_survivors` supplies the evidence for it, so both
are tested directly.
"""
import importlib.util
import sys
from pathlib import Path
_REPO_ROOT = Path(__file__).resolve().parents[2]
_MODULE_PATH = _REPO_ROOT / "scripts" / "mutation_report.py"
_spec = importlib.util.spec_from_file_location("mutation_report", _MODULE_PATH)
report = importlib.util.module_from_spec(_spec)
sys.modules[_spec.name] = report
_spec.loader.exec_module(report)
_CONFIG = {"paths_to_mutate": ["litellm/proxy/management_endpoints/"], "tests_dir": ["tests/"]}
def test_a_run_that_reported_nothing_is_not_a_clean_sweep():
rendered = report.render(_CONFIG, report.MutmutResults(survivors=(), reported=0), None)
assert "not a passing score" in rendered
assert "caught every mutation" not in rendered
def test_a_run_that_killed_every_mutant_says_so():
rendered = report.render(
_CONFIG, report.MutmutResults(survivors=(), reported=0), {"killed": 48, "survived": 0}
)
assert "caught every mutation" in rendered
assert "not a passing score" not in rendered
def test_stats_counting_survivors_results_never_listed_is_not_a_clean_sweep():
rendered = report.render(
_CONFIG, report.MutmutResults(survivors=(), reported=0), {"killed": 48, "survived": 3}
)
assert "not a passing score" in rendered
assert "caught every mutation" not in rendered
assert "3 surviving mutant(s)" in rendered
def test_mutants_that_never_reached_the_tests_are_not_a_clean_sweep():
rendered = report.render(
_CONFIG,
report.MutmutResults(survivors=(), reported=0),
{"killed": 48, "survived": 0, "no_tests": 4, "timeout": 1},
)
assert "not a passing score" in rendered
assert "caught every mutation" not in rendered
assert "4 no tests" in rendered
assert "1 timeout" in rendered
def test_a_status_the_reporter_has_never_met_still_blocks_a_clean_sweep():
rendered = report.render(
_CONFIG,
report.MutmutResults(survivors=(), reported=0),
{"killed": 48, "survived": 0, "check_was_interrupted_by_user": 2},
)
assert "not a passing score" in rendered
assert "caught every mutation" not in rendered
assert "2 check was interrupted by user" in rendered
def test_no_survivors_without_a_kill_is_not_a_clean_sweep():
rendered = report.render(
_CONFIG, report.MutmutResults(survivors=(), reported=48), {"killed": 0, "survived": 0}
)
assert "not a passing score" in rendered
assert "caught every mutation" not in rendered
def test_no_survivors_and_no_stats_cannot_claim_a_sweep():
"""`mutmut results` never lists killed mutants, so with the stats file missing an
empty survivor list is equally consistent with a perfect run and a dead one."""
rendered = report.render(_CONFIG, report.MutmutResults(survivors=(), reported=48), None)
assert "not a passing score" in rendered
assert "caught every mutation" not in rendered
def test_survivors_are_read_out_of_the_verdicts_they_came_with(monkeypatch):
class _Proc:
stdout = (
"litellm.proxy.management_endpoints.key_management_endpoints.x_1: killed\n"
"litellm.proxy.management_endpoints.key_management_endpoints.x_2: survived\n"
"litellm.proxy.management_endpoints.key_management_endpoints.x_3: no tests\n"
"not a verdict line at all\n"
)
monkeypatch.setattr(report.subprocess, "run", lambda *a, **k: _Proc())
results = report.get_survivors()
assert results.survivors == (
"litellm.proxy.management_endpoints.key_management_endpoints.x_2",
)
assert results.reported == 3
def test_every_multi_word_verdict_mutmut_can_emit_still_counts(monkeypatch):
class _Proc:
stdout = "".join(
f"litellm.proxy.management_endpoints.key_management_endpoints.x_{i}: {verdict}\n"
for i, verdict in enumerate(
(
"no tests",
"not checked",
"caught by type check",
"check was interrupted by user",
)
)
)
monkeypatch.setattr(report.subprocess, "run", lambda *a, **k: _Proc())
results = report.get_survivors()
assert results.survivors == ()
assert results.reported == 4
def test_an_empty_mutmut_results_reports_nothing_rather_than_zero_survivors(monkeypatch):
class _Proc:
stdout = ""
monkeypatch.setattr(report.subprocess, "run", lambda *a, **k: _Proc())
assert report.get_survivors() == report.MutmutResults(survivors=(), reported=0)