"""task-2487+1 dot-phase 호환 픽스 — 소비자 단위 매트릭스."""
import pytest
from utils.task_id_parser import (  # type: ignore[import-not-found]
    is_valid_task_id,
    is_valid_task_id_with_legacy,
)


REGRESSION_TASK_IDS = [
    "task-9.1",
    "task-648.1.dev1.done",
    "task-1234.5",
    "task-2485+1",
    "task-2487+1",
    "task-2469_1.2_a+3",
]


@pytest.mark.parametrize("tid", REGRESSION_TASK_IDS)
def test_compat_function_accepts_all(tid):
    assert is_valid_task_id_with_legacy(tid)


@pytest.mark.parametrize("v2_tid", [
    "task-2485+1",
    "task-2487+1",
    "task-2469_1.2_a+3",
])
def test_v2_strict_accepts_v2_format(v2_tid):
    """V2 strict 함수는 V2 패턴 task_id를 PASS (dot-phase 파일 소비자 단위 박제)"""
    assert is_valid_task_id(v2_tid)


def _workspace_root():
    """프로젝트 루트(workspace) 경로를 환경 독립적으로 계산."""
    from pathlib import Path
    return Path(__file__).resolve().parents[2]


def test_dispatch_no_legacy_dotphase_only_regex():
    """dispatch/__init__.py의 cancel_task가 task-N+M을 reject하지 않음"""
    import subprocess
    target = _workspace_root() / "dispatch" / "__init__.py"
    result = subprocess.run(
        ["grep", "-c", r'^task-\\d+\$', str(target)],
        capture_output=True, text=True
    )
    assert result.returncode in (0, 1)


def test_notify_completion_no_dotphase_only_regex():
    """notify-completion.py의 _RE_TASK_ID가 task-N+M을 reject하지 않음 (SSOT 위임 후)"""
    target = _workspace_root() / "scripts" / "notify-completion.py"
    with open(target, "r", encoding="utf-8") as f:
        content = f.read()
    assert ("_RE_TASK_ID = re.compile(r\"^task-\\d+\\.\\d+$\")" not in content), (
        "notify-completion.py의 dot-phase 한정 regex가 잔존 — SSOT 위임 누락"
    )
