92 lines
4.0 KiB
Python
92 lines
4.0 KiB
Python
"""Красный тест T454 (12.10.2026): таймаут вызова по роли и честная квитанция (замечания 3 и 10 проверки)."""
|
||
import json
|
||
import os
|
||
import sys
|
||
import time
|
||
from pathlib import Path
|
||
|
||
import pytest
|
||
|
||
NEXT = Path(__file__).resolve().parents[1]
|
||
sys.path.insert(0, str(NEXT))
|
||
sys.path.insert(0, str(NEXT / "tests"))
|
||
|
||
import core # noqa: E402
|
||
import kvitanciya # noqa: E402
|
||
from next_helpers import make_case # noqa: E402
|
||
|
||
SCHEMA = {"type": "object", "properties": {"fakty": {"type": "array"}}, "required": ["fakty"]}
|
||
OK = {"structured_output": {"fakty": []}}
|
||
DOC = "иск"
|
||
HEAVY = ["doc-reviewer", "pdf-reader", "image-reader", "docx-reader", "doc-drafter", "koni-normy",
|
||
"position-council/taktik", "position-council/advokat-dyavola"]
|
||
LIGHT = ["koni-list", "doc-vychitka", "inbox-triage", "practice-leaf", "case-mapper"]
|
||
|
||
|
||
@pytest.mark.parametrize("role,expected", [(r, 780) for r in HEAVY] + [(r, 290) for r in LIGHT])
|
||
def test_role_timeout(role, expected):
|
||
assert core.role_timeout(role) == expected
|
||
|
||
|
||
def test_env_overrides_all(monkeypatch):
|
||
monkeypatch.setenv("THEMIZ_AGENT_TIMEOUT", "7")
|
||
assert core.role_timeout("doc-reviewer") == core.role_timeout("koni-list") == 7
|
||
|
||
|
||
def test_agent_passes_role_timeout(fake, tmp_path, monkeypatch):
|
||
seen = []
|
||
real = core._exec
|
||
monkeypatch.setattr(core, "_exec", lambda argv, cwd, timeout, *a, **k: seen.append(timeout) or real(argv, cwd, timeout, *a, **k))
|
||
fake.plan({"docx-reader": [OK], "inbox-triage": [OK]})
|
||
with core.Run(make_case(tmp_path / "cases" / "c" / "d"), "FAST", DOC) as run:
|
||
core.agent(run, "karta", "docx-reader", "РОЛЬ: docx-reader", "т", SCHEMA, "читает")
|
||
core.agent(run, "priem", "inbox-triage", "РОЛЬ: inbox-triage", "т", SCHEMA, "читает")
|
||
assert seen == [780, 290]
|
||
|
||
|
||
def test_hanging_light_role_two_attempts_within_600(fake, tmp_path, monkeypatch):
|
||
pauses = []
|
||
monkeypatch.setattr(core.time, "sleep", pauses.append)
|
||
monkeypatch.setattr(core, "role_timeout", lambda role: 0.5)
|
||
fake.plan({"inbox-triage": [{"sleep": 5, **OK}] * 3})
|
||
with core.Run(make_case(tmp_path / "cases" / "c" / "d"), "FAST", DOC) as run:
|
||
with pytest.raises(core.Stop, match="таймаут"):
|
||
core.agent(run, "priem", "inbox-triage", "РОЛЬ: inbox-triage", "т", SCHEMA, "читает")
|
||
assert len(fake.calls()) == 2
|
||
assert sum(pauses) + 2 * core.AGENT_TIMEOUT <= 600
|
||
|
||
|
||
def _case_with_docx(tmp_path, names_times, monkeypatch):
|
||
case = make_case(tmp_path / "cases" / "c" / "d")
|
||
ready = case / "GOTOVO"
|
||
ready.mkdir(exist_ok=True)
|
||
paths = core.contract(case, core.slug(DOC))
|
||
for name, when in names_times.items():
|
||
target = paths["ready_md"] if name == "md" else ready / name
|
||
target.parent.mkdir(parents=True, exist_ok=True)
|
||
target.write_bytes(b"x")
|
||
os.utime(target, (when, when))
|
||
monkeypatch.setattr(kvitanciya, "_code", lambda tool, args: 0)
|
||
return case
|
||
|
||
|
||
def test_newer_chernovik_beats_ready(tmp_path, monkeypatch, capsys):
|
||
slug, now = core.slug(DOC), time.time()
|
||
case = _case_with_docx(tmp_path, {f"{slug}_ГОТОВ_К_ПОДАЧЕ.docx": now - 100, f"{slug}_ЧЕРНОВИК.docx": now}, monkeypatch)
|
||
assert kvitanciya.main([str(case), "--doc", DOC]) == 1
|
||
assert "последняя выдача — черновик" in capsys.readouterr().out
|
||
|
||
|
||
def test_ready_docx_older_than_ready_md_is_stale(tmp_path, monkeypatch, capsys):
|
||
slug, now = core.slug(DOC), time.time()
|
||
case = _case_with_docx(tmp_path, {f"{slug}_ГОТОВ_К_ПОДАЧЕ.docx": now - 100, "md": now}, monkeypatch)
|
||
assert kvitanciya.main([str(case), "--doc", DOC]) == 1
|
||
assert "устаревший DOCX" in capsys.readouterr().out
|
||
|
||
|
||
def test_fresh_ready_docx_is_ok(tmp_path, monkeypatch, capsys):
|
||
slug, now = core.slug(DOC), time.time()
|
||
case = _case_with_docx(tmp_path, {"md": now - 100, f"{slug}_ГОТОВ_К_ПОДАЧЕ.docx": now}, monkeypatch)
|
||
assert kvitanciya.main([str(case), "--doc", DOC]) == 0
|
||
capsys.readouterr()
|