# SPDX-License-Identifier: Apache-2.0 """Tests for the TTS adapter migration ratchet. The gate only earns its place if it actually catches the ways a per-model branch gets reintroduced. These pin what it detects — and, just as importantly, the one shape it is known to miss, so nobody mistakes it for an adversarial sandbox. """ from __future__ import annotations import ast import sys from pathlib import Path import pytest sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "tools" / "pre_commit")) from check_tts_adapter import ( # noqa: E402 MAX_MODEL_TYPE_BRANCHES, _model_type_branches, main, ) pytestmark = [pytest.mark.core_model, pytest.mark.cpu] def _count(src: str) -> int: return len(_model_type_branches(ast.parse(src))) @pytest.mark.parametrize( ("label", "src", "expected"), [ ("direct comparison", 'if self._tts_model_type == "foo": pass', 1), ("negated comparison", 'if self._tts_model_type != "foo": pass', 1), ("membership test", 'if self._tts_model_type in ("a", "b"): pass', 1), ("local alias", 'kind = self._tts_model_type\nif kind == "foo": pass', 1), ("getattr read", 'if getattr(self, "_tts_model_type") == "foo": pass', 1), ("getattr with default", 'if getattr(self, "_tts_model_type", None) == "foo": pass', 1), ("match statement counts each case", 'match self._tts_model_type:\n case "a": pass\n case _: pass', 2), ("alias then match", 'k = getattr(self, "_tts_model_type")\nmatch k:\n case "a": pass', 1), ("unrelated attribute is ignored", 'if self._other == "foo": pass', 0), ("unrelated local is ignored", 'kind = self._backend\nif kind == "foo": pass', 0), ], ) def test_branch_detection(label, src, expected): assert _count(src) == expected, label def test_dict_dispatch_is_a_known_gap(): """Documented limitation, asserted so it is a decision and not a surprise. Table dispatch keyed on the model type reads as zero branches. The ratchet guards against drift in the shape people actually write; catching every indirection is a review question, not a lint one. """ assert _count('TABLE = {"foo": 1}\nvalue = TABLE[self._tts_model_type]') == 0 def test_gate_passes_on_current_tree(): """The recorded budgets must match reality, or the ratchet is decorative.""" assert main([]) == 0 def test_budgets_are_not_exceeded(): """Budgets are a ceiling, not a target. This used to assert equality, which re-imposed through pytest exactly what #6008 removed from the checker: a PR that deleted branches failed CI unless it also hand-edited the constant. That is what broke `main` for 5h19m in #5746. The checker reports an under-budget count as a passing notice (check_tts_adapter.py:140); the test must agree. """ root = Path(__file__).resolve().parents[2] serving = root / "vllm_omni" / "entrypoints" / "openai" / "serving_speech.py" assert len(_model_type_branches(ast.parse(serving.read_text()))) <= MAX_MODEL_TYPE_BRANCHES