from __future__ import annotations

import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "scripts"))

import report_failed_turns as failed_turns_script  # noqa: E402


def write_jsonl(path: Path, records: list[dict]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w", encoding="utf-8") as handle:
        for record in records:
            handle.write(json.dumps(record, ensure_ascii=False) + "\n")


def test_report_failed_turns_filters_non_success(tmp_path: Path) -> None:
    root = tmp_path / "reports" / "observability" / "onec-agent" / "turn_audit" / "20260705.jsonl"
    write_jsonl(
        root,
        [
            {
                "timestamp": "2026-07-05T10:00:00+00:00",
                "turn_id": "ok-1",
                "plugin": "1c",
                "outcome": "success",
                "failure_type": "none",
                "user_text": "ok",
            },
            {
                "timestamp": "2026-07-05T11:00:00+00:00",
                "turn_id": "bad-1",
                "plugin": "1c",
                "project_id": "p1",
                "chat_id": "c1",
                "request_id": "r1",
                "outcome": "failure",
                "failure_type": "adapter_error",
                "error_code": "adapter_error",
                "error_message": "missing base_id",
                "user_text": "Проверь сохраненное состояние",
            },
        ],
    )

    records = failed_turns_script.iter_turn_audit(tmp_path / "reports" / "observability")
    failed = failed_turns_script.filter_failed(records, limit=10)

    assert len(failed) == 1
    assert failed[0]["turn_id"] == "bad-1"
    assert failed[0]["failure_type"] == "adapter_error"
    assert failed[0]["error_code"] == "adapter_error"
