"""6개 모드 분류 회귀 테스트."""

from __future__ import annotations

import os as _os
import sys as _sys
from pathlib import Path

# 워크트리/메인 어느 환경이든 워크스페이스 루트를 sys.path[0]에 우선 등록.
_WROOT = _os.path.dirname(
    _os.path.dirname(_os.path.dirname(_os.path.dirname(_os.path.abspath(__file__))))
)
if _WROOT in _sys.path:
    _sys.path.remove(_WROOT)
_sys.path.insert(0, _WROOT)

from utils.task_mode_classifier import VALID_MODES, classify_task_mode  # type: ignore[reportMissingImports] # noqa: E402


def _write(tmp_path: Path, name: str, body: str) -> str:
    p = tmp_path / name
    p.write_text(body, encoding="utf-8")
    return str(p)


def test_classify_code_default(tmp_path: Path) -> None:
    md = _write(
        tmp_path,
        "task-code.md",
        """---
task_id: 9001
Level: Lv.3
---

# 일반 코드 작업

새 기능을 구현합니다.
""",
    )
    assert classify_task_mode(md) == "code"


def test_classify_docs_only_when_marker(tmp_path: Path) -> None:
    md = _write(
        tmp_path,
        "task-docs.md",
        """---
task_id: 9002
Level: Lv.2
docs_only: true
---

# 문서 업데이트

README만 수정.
""",
    )
    assert classify_task_mode(md) == "docs_only"


def test_classify_diagnosis_lv0(tmp_path: Path) -> None:
    md = _write(
        tmp_path,
        "task-diag.md",
        """---
task_id: 9003
Level: Lv.0
---

# 시스템 진단

현 상태를 분석한다.
""",
    )
    assert classify_task_mode(md) == "diagnosis"


def test_classify_callback_only_lv0(tmp_path: Path) -> None:
    md = _write(
        tmp_path,
        "task-cb.md",
        """---
task_id: 9004
Level: Lv.0
---

# callback envelope 송신

dispatch callback only.
""",
    )
    assert classify_task_mode(md) == "callback_only"


def test_classify_closeout_marker_lv0(tmp_path: Path) -> None:
    md = _write(
        tmp_path,
        "task-close.md",
        """---
task_id: 9005
Level: Lv.0
---

# closeout

.done marker 만 작성.
""",
    )
    assert classify_task_mode(md) == "closeout_marker_only"


def test_valid_modes_count_is_6() -> None:
    assert len(VALID_MODES) == 6
    assert set(VALID_MODES) == {
        "code",
        "docs_only",
        "read_only_watcher",
        "diagnosis",
        "callback_only",
        "closeout_marker_only",
    }
