Files
themiz/themiz-next/tests/conftest.py
T

62 lines
2.5 KiB
Python

import json
import sys
from pathlib import Path
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import core # noqa: E402
from next_helpers import make_case # noqa: E402
FAKE_CLAUDE = Path(__file__).resolve().with_name("fake_claude.py")
class Fake:
"""Управление заглушкой claude: план исходов и журнал вызовов."""
def __init__(self, plan_path: Path, log_path: Path) -> None:
self.plan_path = plan_path
self.log_path = log_path
def plan(self, outcomes: dict) -> None:
"""Задает исходы по ролям: {роль: [исход, ...]}. Листы Кони и вычитка без плана молчат: чистый текст."""
outcomes = {"koni-list": [{"structured_output": {"zamechaniya": []}}] * 60,
"doc-vychitka": [{"structured_output": {"zamechaniya": []}}] * 10, **outcomes}
self.plan_path.write_text(json.dumps(outcomes, ensure_ascii=False), encoding="utf-8")
def calls(self, leaves: bool = False) -> list[dict]:
"""Вызовы заглушки по порядку: argv, stdin, cwd, role. Листы Кони и вычитка — только по leaves=True."""
if not self.log_path.is_file():
return []
found = [json.loads(line) for line in self.log_path.read_text(encoding="utf-8").splitlines()]
return [call for call in found if leaves or call["role"] not in ("koni-list", "doc-vychitka")]
@pytest.fixture
def fake(tmp_path, monkeypatch):
"""Подставляет заглушку вместо claude в core.CLAUDE."""
stub = Fake(tmp_path / "fake-plan.json", tmp_path / "fake-calls.jsonl")
stub.plan({})
asked = []
def router(role, strength):
asked.append((role, strength))
return {"executor": {"name": "claude", "invoke": [sys.executable, str(FAKE_CLAUDE), "-p"], "model": "",
"models": {s: f"m-{s}" for s in ("cheap", "work", "review", "chair")}}}
monkeypatch.setattr(core, "_ask_router", router)
monkeypatch.setattr(core, "ROUTER_ASKED", asked, raising=False)
core.route.cache_clear()
monkeypatch.setenv("FAKE_CLAUDE_PLAN", str(stub.plan_path))
monkeypatch.setenv("FAKE_CLAUDE_LOG", str(stub.log_path))
return stub
@pytest.fixture
def run_(tmp_path):
"""Начатый прогон на временном деле."""
case = make_case(tmp_path / "cases" / "c" / "d")
with core.Run(case, "FAST", "иск") as run:
yield run