Files
iishko-public/tests/test_bot_model_routing.py
T
reformandClaude Opus 5.5 0ee6fb843b
checks / pytest (push) Has been cancelled
checks / moderation (push) Has been cancelled
checks / node (push) Has been cancelled
IISHKO: Telegram bot, Mini App and gateway
Initial public release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 21:51:30 +03:00

162 lines
7.0 KiB
Python

"""Offline contracts for the production dead-model routing incident.
Only selected assignments/functions are executed: importing the bot would start
application resources and is deliberately avoided.
"""
import ast
import json
import logging
from pathlib import Path
import unittest
BOT = Path(__file__).resolve().parents[1] / "bot"
SOURCE = (BOT / "grok_bot.py").read_text(encoding="utf-8")
TREE = ast.parse(SOURCE)
DEAD = "deepseek-v4.1-flash"
LIVE = "deepseek-v4.1-flash-1017"
KIMI = "kimi/kimi-k3.1-pro"
CATALOGUE_ASSIGNMENTS = {
"TIER_MODELS", "MODEL_DESCS", "MODEL_ALIAS", "CHAT_MODELS",
"CHAT_FREE_MODELS", "CHAT_PICKER_MODELS", "SMART_UPGRADE_MODEL",
"AGENT_MODELS", "PREMIUM_WEEKS", "TIER_UNLOCK", "CHAT_FALLBACK",
}
def catalogue():
selected: list[ast.stmt] = [
node for node in TREE.body
if isinstance(node, ast.Assign)
and len(node.targets) == 1
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id in CATALOGUE_ASSIGNMENTS
]
namespace = {}
exec(compile(ast.Module(body=selected, type_ignores=[]), "<catalogue>", "exec"), namespace)
return namespace
class BotModelRouting(unittest.TestCase):
def test_catalogue_expression_executes_with_integer_rank_keys(self):
ns = catalogue()
tiers = ns["TIER_MODELS"]
self.assertEqual(set(tiers), {1, 2, 3, 4})
self.assertTrue(all(type(rank) is int for rank in tiers))
expected = list(dict.fromkeys(model for rank in (1, 2, 3, 4) for model in tiers[rank]))
self.assertEqual(ns["CHAT_MODELS"], expected)
self.assertNotIn(DEAD, expected)
def test_exact_live_default_is_rank_one_and_kimi_rank_is_preserved(self):
ns = catalogue()
example = json.loads((BOT / "config.example.json").read_text(encoding="utf-8"))
self.assertEqual(example["chat_default_model"], LIVE)
self.assertIn(LIVE, ns["CHAT_FREE_MODELS"])
self.assertEqual(min(rank for rank, models in ns["TIER_MODELS"].items() if KIMI in models), 3)
def test_repair_does_not_add_premium_requirements_or_silent_fallbacks(self):
ns = catalogue()
self.assertNotIn(LIVE, ns["PREMIUM_WEEKS"])
self.assertEqual(set(ns["TIER_UNLOCK"]), set(ns["TIER_MODELS"]))
self.assertEqual(ns["CHAT_FALLBACK"], [])
def test_miniapp_cold_start_catalogue_does_not_offer_dead_models(self):
source = (BOT / "webapp" / "server.py").read_text(encoding="utf-8")
tree = ast.parse(source)
fallback = {}
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 {"TIER_MODELS", "MODEL_DESCS", "AGENT_MODELS_WA"}:
fallback[target.id] = ast.literal_eval(node.value)
tiers = fallback["TIER_MODELS"]
models = [model for rank in (1, 2, 3, 4) for model in tiers[rank]]
for dead in (DEAD, "deepseek-v4-flash"):
self.assertNotIn(dead, models)
self.assertNotIn(dead, fallback["AGENT_MODELS_WA"])
self.assertNotIn(DEAD, fallback["MODEL_DESCS"])
self.assertIn(LIVE, tiers[1])
self.assertIn(KIMI, tiers[3])
def test_picker_and_agent_choices_have_no_duplicate_replacement(self):
ns = catalogue()
for name in ("CHAT_PICKER_MODELS", "AGENT_MODELS"):
with self.subTest(collection=name):
models = ns[name]
self.assertNotIn(DEAD, models)
self.assertEqual(models.count(LIVE), 1)
self.assertEqual(len(models), len(set(models)))
self.assertEqual(ns["MODEL_ALIAS"]["deepseek-v4-flash"], LIVE)
self.assertEqual(ns["SMART_UPGRADE_MODEL"], LIVE)
def test_both_real_vision_handlers_call_the_exact_live_model(self):
for name in ("handle_photo", "group_reply"):
with self.subTest(handler=name):
function = next(
node for node in TREE.body
if isinstance(node, ast.AsyncFunctionDef) and node.name == name
)
calls = [
node for node in ast.walk(function)
if isinstance(node, ast.Call)
and isinstance(node.func, ast.Name) and node.func.id == "gw_chat"
and len(node.args) >= 2 and isinstance(node.args[1], ast.Constant)
and node.args[1].value == LIVE
]
self.assertEqual(len(calls), 1)
def test_dead_id_is_not_used_as_a_route_target(self):
allowed_alias_keys = {
id(key)
for node in TREE.body
if isinstance(node, ast.Assign)
and any(isinstance(target, ast.Name) and target.id == "MODEL_ALIAS" for target in node.targets)
and isinstance(node.value, ast.Dict)
for key in node.value.keys
if isinstance(key, ast.Constant) and key.value == DEAD
}
offending_lines = [
node.lineno for node in ast.walk(TREE)
if isinstance(node, ast.Constant) and node.value == DEAD and id(node) not in allowed_alias_keys
]
self.assertEqual(offending_lines, [])
def test_stale_preferences_heal_to_explicit_allowed_default(self):
function = next(
node for node in TREE.body
if isinstance(node, ast.FunctionDef) and node.name == "_live_chat_model"
)
saved = []
ns = {
"CFG": {"chat_default_model": LIVE},
"rank_models": lambda uid: ["glm-5.3", LIVE],
"db_set_pref": lambda uid, **prefs: saved.append((uid, prefs)),
"log": logging.getLogger("test-model-routing"),
}
exec(compile(ast.Module(body=[function], type_ignores=[]), "<resolver>", "exec"), ns)
self.assertEqual(ns["_live_chat_model"](1, {"chat_model": DEAD}), LIVE)
self.assertEqual(saved, [(1, {"chat_model": LIVE})])
saved.clear()
self.assertEqual(ns["_live_chat_model"](1, {"chat_model": None}), LIVE)
self.assertEqual(saved, [])
def test_default_resolution_does_not_grant_a_model_outside_allowed_rank(self):
function = next(
node for node in TREE.body
if isinstance(node, ast.FunctionDef) and node.name == "_live_chat_model"
)
saved = []
ns = {
"CFG": {"chat_default_model": LIVE},
"rank_models": lambda uid: ["glm-5.3"],
"db_set_pref": lambda uid, **prefs: saved.append((uid, prefs)),
"log": logging.getLogger("test-model-routing"),
}
exec(compile(ast.Module(body=[function], type_ignores=[]), "<resolver>", "exec"), ns)
self.assertEqual(ns["_live_chat_model"](1, {"chat_model": DEAD}), "glm-5.3")
self.assertEqual(saved, [(1, {"chat_model": "glm-5.3"})])
if __name__ == "__main__":
unittest.main()