123 lines
5.5 KiB
Python
123 lines
5.5 KiB
Python
import json
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||
|
||
import board # noqa: E402
|
||
import core # noqa: E402
|
||
|
||
from next_helpers import make_case # noqa: E402
|
||
|
||
DEAD = "прогон умер без итога"
|
||
|
||
|
||
def test_board_refreshes_only_while_running():
|
||
assert 'content="3"' in board.render_html({"status": "running", "phases": {}}, [])
|
||
assert "refresh" not in board.render_html({"status": "ok", "phases": {}}, [])
|
||
|
||
|
||
def test_board_escapes_text():
|
||
events = [{"ts": "t", "phase": "karta", "who": "x", "name": "<b>", "action": "<script>", "state": "ok"}]
|
||
assert "<script>" not in board.render_html({"status": "running", "phases": {}}, events)
|
||
|
||
|
||
def test_read_events_skips_broken_tail(tmp_path):
|
||
path = tmp_path / "events.jsonl"
|
||
path.write_text('{"a": 1}\n{"b":', encoding="utf-8")
|
||
assert board.read_events(path) == [{"a": 1}]
|
||
|
||
|
||
def test_dead_run_detected(tmp_path):
|
||
case = make_case(tmp_path / "cases" / "c" / "d")
|
||
assert board.is_dead(case, {"status": "running"}) is True
|
||
|
||
|
||
def test_is_dead_only_without_lock_holder_and_creates_nothing(tmp_path):
|
||
case = make_case(tmp_path / "cases" / "c" / "d")
|
||
assert board.is_dead(case, {"status": "running"}) is True
|
||
assert not (case / ".agent").exists() # замка не было, табло его не создало
|
||
with core.Run(case, "FAST", "иск"):
|
||
assert board.is_dead(case, {"status": "running"}) is False
|
||
assert board.is_dead(case, {"status": "running"}) is True
|
||
assert board.is_dead(case, {"status": "ok"}) is False
|
||
|
||
|
||
def test_dead_status_shown_in_html_and_text():
|
||
state = {"status": "dead", "phases": {}}
|
||
html = board.render_html(state, [])
|
||
assert DEAD in html and "refresh" not in html
|
||
assert DEAD in board.render_text(state, [])
|
||
|
||
|
||
def test_phase_block_shows_every_role_and_phase_time():
|
||
state = {"status": "running", "phases": {"karta": {"status": "ok", "ms": 41230}}}
|
||
for text in (board.render_html(state, []), board.render_text(state, [])):
|
||
assert "Александр Гольмстен" in text and "Анатолий Кони · нормы" in text and "41,2 с" in text
|
||
|
||
|
||
def test_phase_block_adds_roles_met_in_events():
|
||
events = [{"ts": "t", "phase": "karta", "who": "markdown_extract", "name": "markdown_extract", "action": "a", "state": "ok"}]
|
||
block = board.render_text({"status": "running", "phases": {}}, events).split("Практика")[0]
|
||
assert "markdown_extract" in block
|
||
|
||
|
||
def test_render_survives_plain_string_phase_status():
|
||
assert "Карта" in board.render_html({"status": "running", "phases": {"karta": "ok"}}, [])
|
||
|
||
|
||
def _dead_run(tmp_path):
|
||
case = make_case(tmp_path / "cases" / "c" / "d")
|
||
with core.Run(case, "FAST", "иск") as run:
|
||
run.phase_start("priem")
|
||
return run.dir # выход без finish: замок свободен, а state.json остался running
|
||
|
||
|
||
def test_main_marks_dead_run_and_stops_the_browser_refresh(tmp_path, capsys):
|
||
run_dir = _dead_run(tmp_path)
|
||
assert json.loads((run_dir / "state.json").read_text(encoding="utf-8"))["status"] == "running"
|
||
assert board.main([str(run_dir), "--once"]) == 0
|
||
assert DEAD in capsys.readouterr().out
|
||
html = (run_dir / "board.html").read_text(encoding="utf-8")
|
||
assert DEAD in html and "refresh" not in html
|
||
|
||
|
||
def test_main_does_not_call_a_live_run_dead(tmp_path, capsys):
|
||
case = make_case(tmp_path / "cases" / "c" / "d")
|
||
with core.Run(case, "FAST", "иск") as run:
|
||
assert board.main([str(run.dir), "--once"]) == 0
|
||
assert 'content="3"' in (run.dir / "board.html").read_text(encoding="utf-8")
|
||
assert DEAD not in capsys.readouterr().out
|
||
|
||
|
||
def test_main_without_state_json_exits_2(tmp_path, capsys):
|
||
assert board.main([str(tmp_path), "--once"]) == 2
|
||
assert "state.json" in capsys.readouterr().err
|
||
|
||
|
||
def test_event_table_shows_phase_title_and_russian_state():
|
||
events = [{"ts": "t", "phase": "karta", "who": "x", "name": "Имя", "action": "читает", "state": "fail", "ms": 1500}]
|
||
text = board.render_text({"status": "running", "phases": {}}, events)
|
||
assert "t | Карта | Имя | читает | сбой | 1,5 с" in text
|
||
|
||
|
||
def test_missing_sources_shown_next_to_the_result():
|
||
state = {"status": "ok", "phases": {}, "nedostaet": ["стоимость объекта квартира", "Квитанция об уплате пошлины"]}
|
||
line = "Нет исходника: стоимость объекта квартира; Квитанция об уплате пошлины"
|
||
assert line in board.render_text(state, []) and f"<p>{line}</p>" in board.render_html(state, [])
|
||
stopped = {**state, "status": "stopped", "reason": "Кони: нет READY"}
|
||
assert board.render_text(stopped, []).splitlines()[1:3] == ["Кони: нет READY", line] # рядом с причиной остановки
|
||
|
||
|
||
def test_no_missing_sources_no_line():
|
||
for state in ({"status": "ok", "phases": {}}, {"status": "ok", "phases": {}, "nedostaet": []}):
|
||
assert "Нет исходника" not in board.render_text(state, []) + board.render_html(state, [])
|
||
|
||
|
||
def test_board_shows_case_progress():
|
||
state = {"status": "running", "phases": {"priem": {"status": "ok"}, "karta": {"status": "ok"}, "praktika": "start"}}
|
||
assert board.done(state) == (2, len(core.PHASES))
|
||
html = board.render_html(state, [])
|
||
assert '<progress max="8" value="2"' in html and "Готово 2 из 8 этапов · 25 %" in html
|
||
assert "Готово 2 из 8 этапов · 25 %" in board.render_text(state, [])
|