Initial public release. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
135 lines
5.4 KiB
Python
135 lines
5.4 KiB
Python
"""Контракты единого каталога моделей (bot/model_catalogue.py).
|
|
|
|
Каталог — единственный источник правды для бота и Mini App. Литеральные
|
|
проекции в bot/grok_bot.py и bot/webapp/server.py обязаны совпадать с модулем:
|
|
именно их (без импорта бота) читают commerce_access.py и abilities_guard.py.
|
|
"""
|
|
import ast
|
|
import pathlib
|
|
|
|
import pytest
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parent.parent
|
|
BOT = ROOT / "bot" / "grok_bot.py"
|
|
SERVER = ROOT / "bot" / "webapp" / "server.py"
|
|
|
|
import model_catalogue as cat
|
|
|
|
|
|
def _literal_assigns(path, names):
|
|
"""Вернуть {name: value} для литеральных top-level присваиваний файла."""
|
|
tree = ast.parse(path.read_text(encoding="utf-8"))
|
|
found = {}
|
|
for node in tree.body:
|
|
if not isinstance(node, ast.Assign) or len(node.targets) != 1:
|
|
continue
|
|
target = node.targets[0]
|
|
if isinstance(target, ast.Name) and target.id in names:
|
|
found[target.id] = ast.literal_eval(node.value)
|
|
return found
|
|
|
|
|
|
def test_catalogue_is_structurally_consistent():
|
|
assert cat.problems() == []
|
|
|
|
|
|
def test_authorized_model_set_is_unchanged():
|
|
assert cat.model_count() == 38
|
|
assert len({m for r in cat.TIER_MODELS for m in cat.TIER_MODELS[r]}) == 38
|
|
live = "deepseek-v4.1-flash-1017"
|
|
dead = "deepseek-v4.1-flash"
|
|
assert cat.TIER_MODELS[1].count(live) == 1
|
|
assert live not in cat.TIER_MODELS[2] + cat.TIER_MODELS[3] + cat.TIER_MODELS[4]
|
|
assert dead not in cat.CATALOGUE_MODELS
|
|
assert cat.MODEL_DESCS[live]
|
|
assert cat.min_rank(live) == 1
|
|
assert cat.min_rank("kimi/kimi-k3.1-pro") == 3
|
|
assert cat.min_rank("claude-opus-5.5") == 4
|
|
|
|
|
|
def test_removed_dots_model_is_not_offered_or_resolved():
|
|
removed = "dots-studio/dots-3-note-preview:free"
|
|
assert removed not in cat.CATALOGUE_MODELS
|
|
assert removed not in cat.MODEL_DESCS
|
|
assert removed not in cat.ALIASES
|
|
assert removed not in cat.ROUTING_ALIASES
|
|
assert removed not in cat.ALIASES.values()
|
|
assert removed not in cat.ROUTING_ALIASES.values()
|
|
assert cat.min_rank(removed) is None
|
|
for rank in cat.TIER_MODELS:
|
|
assert removed not in cat.tier_models(rank)
|
|
|
|
|
|
def test_every_catalogue_id_has_informative_metadata():
|
|
for model in cat.CATALOGUE_MODELS:
|
|
desc = cat.MODEL_DESCS[model]
|
|
assert desc and desc != model
|
|
assert len(cat.model_label(model)) >= 3
|
|
assert cat.describe(model) == desc
|
|
|
|
|
|
def test_min_rank_matches_first_tier_membership():
|
|
for rank in cat.TIER_MODELS:
|
|
for model in cat.TIER_MODELS[rank]:
|
|
if model in cat.MIN_RANKS:
|
|
assert cat.min_rank(model) <= rank
|
|
assert set(cat.MIN_RANKS) == set(cat.CATALOGUE_MODELS)
|
|
|
|
|
|
def test_aliases_are_metadata_only():
|
|
for alias, model in cat.ALIASES.items():
|
|
assert model in cat.CATALOGUE_MODELS
|
|
assert alias not in cat.CATALOGUE_MODELS
|
|
assert cat.canonical(alias) == model
|
|
assert cat.min_rank(alias) == cat.min_rank(model)
|
|
assert cat.MODEL_DESCS[alias] # подпись для кнопок/списков
|
|
for alias in ("claude-opus-5-5", "claude-fable-5-1"):
|
|
assert alias in cat.ALIASES
|
|
assert alias in cat.MODEL_DESCS
|
|
|
|
|
|
def test_routing_aliases_point_into_catalogue():
|
|
for legacy, model in cat.ROUTING_ALIASES.items():
|
|
assert model in cat.CATALOGUE_MODELS
|
|
assert legacy not in cat.CATALOGUE_MODELS
|
|
assert cat.ROUTING_ALIASES["deepseek-v4-flash"] == "deepseek-v4.1-flash-1017"
|
|
|
|
|
|
def test_bot_literal_projection_matches_module():
|
|
projection = _literal_assigns(BOT, {"TIER_MODELS", "MODEL_DESCS", "TIER_TITLES"})
|
|
assert projection["TIER_MODELS"] == cat.TIER_MODELS
|
|
assert projection["MODEL_DESCS"] == cat.MODEL_DESCS
|
|
assert projection["TIER_TITLES"] == cat.TIER_TITLES
|
|
|
|
|
|
def test_miniapp_literal_fallback_matches_module():
|
|
literals = _literal_assigns(SERVER, {"TIER_MODELS", "MODEL_DESCS"})
|
|
assert literals["TIER_MODELS"] == cat.TIER_MODELS
|
|
assert literals["MODEL_DESCS"] == cat.MODEL_DESCS
|
|
|
|
|
|
def test_catalogue_module_is_stdlib_only_and_carries_no_credentials():
|
|
source = (ROOT / "bot" / "model_catalogue.py").read_text(encoding="utf-8")
|
|
tree = ast.parse(source)
|
|
for node in ast.walk(tree):
|
|
assert not isinstance(node, (ast.Import, ast.ImportFrom)), "catalogue must not import anything"
|
|
lowered = source.lower()
|
|
for marker in ("http://", "https://", "api_key", "apikey", "bearer", "password", "secret"):
|
|
assert marker not in lowered
|
|
for model in cat.TIER_MODELS[1]:
|
|
assert isinstance(model, str) and model # только идентификаторы, без порогов доступности
|
|
|
|
|
|
def test_public_aliases_do_not_expand_the_offered_set():
|
|
"""Алиас не должен появляться в тир-списках: доступ выдаётся по рангу канонической модели."""
|
|
for alias in cat.ALIASES:
|
|
assert all(alias not in models for models in cat.TIER_MODELS.values())
|
|
|
|
|
|
def test_model_descriptions_are_unique_per_family_marker():
|
|
labels = [cat.model_label(model) for model in cat.CATALOGUE_MODELS]
|
|
assert len(set(labels)) == len(labels), "две модели делят одну подпись кнопки"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(pytest.main([__file__, "-q"])) |