"""task-2689 재현 시나리오: read-only watcher는 commit 0 / diff 0 / PR 0이 PASS evidence."""

from __future__ import annotations

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

_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 (  # type: ignore[reportMissingImports] # noqa: E402
    classify_task_mode,
    is_read_only_mode,
)


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_read_only_watcher_classified_correctly(tmp_path: Path) -> None:
    md = _write(
        tmp_path,
        "task-2689-watcher.md",
        """---
task_id: 2689
Level: Lv.0
---

# Watcher 작업

dispatch watcher 가 envelope 을 폴링한다. (read-only)
""",
    )
    assert classify_task_mode(md) == "read_only_watcher"


def test_read_only_watcher_means_commit_0_is_pass(tmp_path: Path) -> None:
    md = _write(
        tmp_path,
        "task-watcher.md",
        """---
task_id: 2689
Level: Lv.0
---

# watcher 폴링

read-only watcher.
""",
    )
    mode = classify_task_mode(md)
    assert mode == "read_only_watcher"
    # 호출자 계약: read-only mode이면 commit 0 / diff 0 / PR 0이 PASS evidence
    assert is_read_only_mode(mode) is True
    # code mode 는 False
    assert is_read_only_mode("code") is False


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

# 신규 기능 구현

새 모듈을 작성한다.
""",
    )
    mode = classify_task_mode(md)
    assert mode == "code"
    # code mode 에서 commit 0 은 FAIL 유지
    assert is_read_only_mode(mode) is False
