Files

78 lines
3.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Выбор CLI и модели Next идет через scripts/cli_router.py, а не прошивкой в коде."""
import re
import sys
from pathlib import Path
import pytest
import core
from test_next_core import SCHEMA
NEXT = Path(__file__).resolve().parents[1]
def answer(invoke, models):
return {"role": "x", "executor": {"name": "claude", "invoke": invoke, "model": "", "models": models}}
def test_no_model_names_in_next_sources():
bad = re.compile(r"haiku|sonnet|opus|gpt-", re.I)
found = [p.name for p in NEXT.glob("*.py") if bad.search(p.read_text(encoding="utf-8"))]
assert found == []
def test_agent_builds_command_from_router_answer(fake, run_):
fake.plan({"docx-reader": [{"structured_output": {"fakty": []}}]})
core.agent(run_, "karta", "docx-reader", "РОЛЬ: docx-reader\nx", "T", SCHEMA, "читает")
argv = fake.calls()[0]["argv"]
assert argv[argv.index("--model") + 1] == "m-cheap"
assert core.ROUTER_ASKED == [("docx-reader", "cheap")]
def test_route_asked_once_per_role(fake, run_):
fake.plan({"docx-reader": [{"structured_output": {"fakty": []}}] * 2})
for _ in range(2):
core.agent(run_, "karta", "docx-reader", "РОЛЬ: docx-reader\nx", "T", SCHEMA, "читает")
assert core.ROUTER_ASKED == [("docx-reader", "cheap")]
def test_router_refusal_stops_without_fallback(run_, monkeypatch):
def refuse(role, strength):
raise core.Stop("cli_router: код 1")
monkeypatch.setattr(core, "_ask_router", refuse)
core.route.cache_clear()
with pytest.raises(core.Stop, match="cli_router"):
core.agent(run_, "karta", "docx-reader", "РОЛЬ: docx-reader\nx", "T", SCHEMA, "читает")
def test_router_without_confirmed_model_stops(run_, monkeypatch):
monkeypatch.setattr(core, "_ask_router", lambda role, strength: answer(["claude", "-p"], {}))
core.route.cache_clear()
with pytest.raises(core.Stop, match="модел"):
core.agent(run_, "karta", "docx-reader", "РОЛЬ: docx-reader\nx", "T", SCHEMA, "читает")
def test_writers_get_strength_not_cheap():
assert core.STRENGTH["doc-reviewer"] == "review"
assert core.STRENGTH["doc-drafter"] == core.STRENGTH["koni-normy"] == "work"
def test_router_refusal_names_reason_from_json(monkeypatch):
"""12.10.2026 01:03: прогон из Терминала падал «cli_router: код 1:» без слов — причина лежала в JSON роутера."""
import subprocess
reply = ('{"role": "inbox-triage", "data_class": "pd", "executor": null, "chain": [], '
'"skipped": [{"name": "active-session", "reason": "в цепочке живых родителей CLI не найден"}], '
'"session": {"outcome": "unknown", "cli": null}}')
monkeypatch.setattr(core, "_exec", lambda argv, cwd, timeout, stdin="": subprocess.CompletedProcess(argv, 1, reply, ""))
core.route.cache_clear()
with pytest.raises(core.Stop) as stop:
core._ask_router("inbox-triage", "cheap")
text = str(stop.value)
assert "в цепочке живых родителей CLI не найден" in text and "живой сессии Claude Code" in text
# 14.10.2026: «!» и nohup тоже теряют родителя-CLI; рабочий путь один - Bash сессии с run_in_background
assert "run_in_background" in text and "nohup" in text and "командой с «!»" not in text
monkeypatch.setattr(core, "_exec", lambda argv, cwd, timeout, stdin="": subprocess.CompletedProcess(argv, 1, "", "ОТКАЗ: реестр битый"))
with pytest.raises(core.Stop, match="реестр битый"):
core._ask_router("inbox-triage", "cheap")