"""Presentation-only moderation badges must not change admission or routing.""" import ast import copy from contextlib import closing from pathlib import Path import sqlite3 import unittest import model_advisories as advisory import public_catalogue ROOT = Path(__file__).resolve().parents[1] EXPECTED = { "qwen3.8-max", "glm-5.3-prime", "kimi-k2.7-code", "qwen3-vl-plus", "qwen3.8-flash", "deepseek-v4.1-flash-1017", "kimi-k3.1-pro", "kimi-k3.1-pro-highspeed", "qwen-max", "qwen-plus", "deepseek-v4-pro", "glm-5.1", "kimi/kimi-k3.1-pro", "qwen3.8-max-0902", } class AdvisoryTests(unittest.TestCase): def test_exact_ids_and_explicit_scope(self): self.assertEqual(set(advisory.MODELS), EXPECTED) for name in EXPECTED: value = advisory.for_model(name) self.assertEqual(value["label"], "Строгая модерация провайдера") self.assertEqual(value["scope"], "some_provider_routes") self.assertIn("не означает недоступность", value["notice"]) self.assertIn("может не помочь", value["notice"]) self.assertNotIn("available", value) self.assertNotIn("configured", value) def test_no_heuristic_alias_or_non_string_matching(self): for name in [None, [], {}, 1, "qwen-plus-unknown", "qwen-plus ", "gpt-6.1-sol"]: self.assertIsNone(advisory.for_model(name)) def test_returned_objects_do_not_mutate_shared_state(self): value = advisory.for_model("qwen-plus") value["label"] = "changed" self.assertEqual(advisory.for_model("qwen-plus")["label"], advisory.LABEL) def test_actual_miniapp_enrichment_preserves_all_access_and_identity_fields(self): tree = ast.parse((ROOT / "bot/webapp/server.py").read_text(encoding="utf-8")) function = next(node for node in tree.body if isinstance(node, ast.AsyncFunctionDef) and node.name == "api_models") loops = [node for node in function.body if isinstance(node, ast.For) and 'row[\'provider_moderation\']' in ast.unparse(node)] self.assertEqual(len(loops), 1) models = [{"name": name, "available": False, "min_rank": 4, "configured": False, "status": "down", "ms": 123} for name in ["qwen-plus", "gpt-6.1-sol"]] before = copy.deepcopy(models) scope = {"models": models, "provider_moderation_for_model": advisory.for_model} statements: list[ast.stmt] = list(loops) exec(compile(ast.Module(body=statements, type_ignores=[]), "", "exec"), scope) for old, new in zip(before, models): self.assertEqual(old, {k: v for k, v in new.items() if k != "provider_moderation"}) self.assertIsNotNone(models[0]["provider_moderation"]) self.assertIsNone(models[1]["provider_moderation"]) def test_public_catalogue_preserves_configured_and_rank(self): import tempfile with tempfile.TemporaryDirectory() as directory: root = Path(directory) source = root / "bot.py" source.write_text("TIER_MODELS = {1: [], 2: [], 3: [], 4: ['qwen-plus', 'gpt-6.1-sol']}\n" "MODEL_DESCS = {'qwen-plus': 'Example'}\n", encoding="utf-8") database = root / "gateway.db" with closing(sqlite3.connect(database)) as db, db: db.executescript("CREATE TABLE channels(id INTEGER,status INTEGER);" "CREATE TABLE abilities(channel_id INTEGER,model TEXT,enabled INTEGER);" "INSERT INTO channels VALUES(1,1);" "INSERT INTO abilities VALUES(1,'gpt-6.1-sol',1);") snapshot = public_catalogue.catalogue_snapshot(source, database, now=100) rows = {row["id"]: row for row in snapshot["models"]} self.assertFalse(rows["qwen-plus"]["configured"]) self.assertEqual(rows["qwen-plus"]["min_rank"], 4) self.assertIsNotNone(rows["qwen-plus"]["provider_moderation"]) self.assertTrue(rows["gpt-6.1-sol"]["configured"]) self.assertIsNone(rows["gpt-6.1-sol"]["provider_moderation"]) html = public_catalogue.render_catalogue(snapshot) self.assertEqual(html.count(advisory.LABEL), 1) self.assertIn(advisory.NOTICE, html) def test_public_render_escapes_advisory(self): snapshot = {"models": [{"id": "test", "description": "", "min_rank": 1, "status": "unknown", "provider_moderation": { "label": '', "notice": ""}}]} html = public_catalogue.render_catalogue(snapshot) self.assertNotIn("", html) self.assertIn("<script>", html) if __name__ == "__main__": unittest.main()