"""tests/regression/test_legacy_compat_2487.py — task-2487 회귀 (legacy dot-phase, task-2547 corrected).

PR #49/#50/#51 Gemini high 회귀 픽스:
- task-1234.5, task-9.1, task-648.1.dev1, task-648.1.dev1.done 같은 legacy
  dot-phase ID 들이 V2 strict 검증으로 인해 거부되지 않도록 한다.
- is_valid_task_id_with_legacy SSOT 함수가 V2 + legacy 양쪽 모두 PASS 한다.
"""
from __future__ import annotations

import importlib.util
import sys
from pathlib import Path

WORKSPACE = Path(__file__).resolve().parents[2]


def _load_module(mod_name: str, file_rel: str):
    file_path = WORKSPACE / file_rel
    spec = importlib.util.spec_from_file_location(mod_name, str(file_path))
    if spec is None or spec.loader is None:
        raise ImportError(f"cannot load spec for {file_path}")
    module = importlib.util.module_from_spec(spec)
    sys.modules[mod_name] = module
    spec.loader.exec_module(module)
    return module


tip = _load_module("tip_2487_legacy", "utils/task_id_parser.py")


# ---------------------------------------------------------------------------
# legacy dot-phase 패턴 PASS 검증
# ---------------------------------------------------------------------------


def test_legacy_simple_dot_phase():
    assert tip.is_valid_task_id_with_legacy("task-9.1") is True


def test_legacy_4digit_dot_phase():
    assert tip.is_valid_task_id_with_legacy("task-1234.5") is True


def test_legacy_with_dev_suffix():
    assert tip.is_valid_task_id_with_legacy("task-648.1.dev1") is True


def test_legacy_with_dev_done_suffix():
    assert tip.is_valid_task_id_with_legacy("task-648.1.dev1.done") is True


def test_legacy_multi_dot():
    assert tip.is_valid_task_id_with_legacy("task-1234.5.6") is True


# ---------------------------------------------------------------------------
# V2 패턴 PASS 검증 (legacy 함수가 V2 도 동시에 받는지)
# ---------------------------------------------------------------------------


def test_legacy_func_accepts_v2_base():
    assert tip.is_valid_task_id_with_legacy("task-2487") is True


def test_legacy_func_accepts_v2_retry():
    assert tip.is_valid_task_id_with_legacy("task-2487+1") is True


def test_legacy_func_accepts_v2_phase_parallel_retry():
    assert tip.is_valid_task_id_with_legacy("task-2469_1.2_a+3") is True


# ---------------------------------------------------------------------------
# 부적합 입력은 거부
# ---------------------------------------------------------------------------


def test_legacy_func_reject_non_string():
    assert tip.is_valid_task_id_with_legacy(None) is False
    assert tip.is_valid_task_id_with_legacy(1234) is False
    assert tip.is_valid_task_id_with_legacy(["task-1234"]) is False


def test_legacy_func_reject_empty():
    assert tip.is_valid_task_id_with_legacy("") is False


def test_legacy_func_reject_no_prefix():
    assert tip.is_valid_task_id_with_legacy("1234.5") is False


def test_legacy_func_reject_wrong_prefix():
    assert tip.is_valid_task_id_with_legacy("foo-task-1234") is False


def test_legacy_func_reject_alpha_only_id():
    assert tip.is_valid_task_id_with_legacy("task-abc") is False
