"""
test_planned_check.py — planned_check verifier 테스트
작성자: 모리건 (개발3팀 테스터)
태스크: task-1917
"""

import sys

sys.path.insert(0, "/home/jay/workspace/teams/shared")
from verifiers import planned_check  # type: ignore[import-not-found]


class TestPlannedCheckVerifier:
    def _make_report(self, tmp_path, task_id: str, content: str):
        reports_dir = tmp_path / "reports"
        reports_dir.mkdir(parents=True, exist_ok=True)
        (reports_dir / f"{task_id}.md").write_text(content, encoding="utf-8")
        return str(reports_dir)

    def test_fail_with_planned_items(self, tmp_path):
        """planned 항목 포함 보고서 → FAIL"""
        content = """\
# 보고서

## 수정 파일별 검증 상태

| 파일 | 변경 내용 | grep 검증 | 상태 |
|------|-----------|-----------|------|
| a.py | 변경A | grep "X" OK | verified |
| b.py | 변경B | 없음 | planned |
"""
        rd = self._make_report(tmp_path, "task-test1", content)
        result = planned_check.verify("task-test1", reports_dir=rd)
        assert result["status"] == "FAIL"
        assert result["planned_count"] == 1
        assert len(result["planned_items"]) == 1
        assert "b.py" in result["planned_items"][0]

    def test_pass_with_all_verified(self, tmp_path):
        """모든 항목 verified → PASS"""
        content = """\
# 보고서

## 수정 파일별 검증 상태

| 파일 | 변경 내용 | grep 검증 | 상태 |
|------|-----------|-----------|------|
| a.py | 변경A | grep "X" OK | verified |
| b.py | 변경B | grep "Y" OK | verified |
"""
        rd = self._make_report(tmp_path, "task-test2", content)
        result = planned_check.verify("task-test2", reports_dir=rd)
        assert result["status"] == "PASS"
        assert result["planned_count"] == 0

    def test_skip_no_table(self, tmp_path):
        """테이블 없는 보고서 → SKIP"""
        content = "# 보고서\n\n간단한 내용.\n"
        rd = self._make_report(tmp_path, "task-test3", content)
        result = planned_check.verify("task-test3", reports_dir=rd)
        assert result["status"] == "SKIP"

    def test_skip_no_report(self, tmp_path):
        """보고서 없음 → SKIP"""
        result = planned_check.verify("task-nonexistent", reports_dir=str(tmp_path))
        assert result["status"] == "SKIP"

    def test_fail_multiple_planned(self, tmp_path):
        """planned 항목 여러 건 → FAIL + 정확한 카운트"""
        content = """\
# 보고서

## 수정 파일별 검증 상태

| 파일 | 변경 내용 | grep 검증 | 상태 |
|------|-----------|-----------|------|
| a.py | 변경A | 없음 | planned |
| b.py | 변경B | 없음 | planned |
| c.py | 변경C | grep "Z" OK | verified |
"""
        rd = self._make_report(tmp_path, "task-test4", content)
        result = planned_check.verify("task-test4", reports_dir=rd)
        assert result["status"] == "FAIL"
        assert result["planned_count"] == 2
        assert len(result["planned_items"]) == 2
