"""auto_orch.py의 enabled 필드 처리 테스트 (TDD RED 단계 — task-1012.1)

테스트 대상:
  - load_pipeline()           : YAML 파이프라인 로드 시 enabled 필드 반영
  - is_pipeline_enabled()     : 파이프라인 dict에서 enabled 여부 반환하는 새 함수

모든 테스트는 RED 단계이므로 실행 시 실패가 예상됩니다.
(is_pipeline_enabled 함수가 아직 구현되지 않았습니다)
"""

import sys
from pathlib import Path

import pytest
import yaml

# workspace 루트를 sys.path에 추가
sys.path.insert(0, "/home/jay/workspace")


# ── load_pipeline() — enabled 필드 로드 검증 ─────────────────────────────


class TestLoadPipelineEnabledField:
    """load_pipeline()이 enabled 필드를 올바르게 로드하는지 검증."""

    def test_load_pipeline_with_enabled_true(self, tmp_path: Path) -> None:
        """enabled: true인 YAML 로드 시 enabled 값이 True인 dict 반환."""
        from orchestrator.auto_orch import load_pipeline

        yaml_content = (
            "name: test-pipeline\n"
            "enabled: true\n"
            "schedule: daily\n"
        )
        yaml_file = tmp_path / "test-pipeline.yaml"
        yaml_file.write_text(yaml_content, encoding="utf-8")

        pipeline = load_pipeline(str(yaml_file))

        assert "enabled" in pipeline
        assert pipeline["enabled"] is True

    def test_load_pipeline_with_enabled_false(self, tmp_path: Path) -> None:
        """enabled: false인 YAML 로드 시 enabled 값이 False인 dict 반환."""
        from orchestrator.auto_orch import load_pipeline

        yaml_content = (
            "name: disabled-pipeline\n"
            "enabled: false\n"
            "schedule: weekly\n"
        )
        yaml_file = tmp_path / "disabled-pipeline.yaml"
        yaml_file.write_text(yaml_content, encoding="utf-8")

        pipeline = load_pipeline(str(yaml_file))

        assert "enabled" in pipeline
        assert pipeline["enabled"] is False

    def test_load_pipeline_without_enabled_defaults_true(self, tmp_path: Path) -> None:
        """enabled 필드가 없는 YAML 로드 시 기본값 True를 반환해야 함.

        구현체는 enabled 키가 없을 경우 setdefault 또는 동등한 방법으로
        기본값 True를 pipeline dict에 삽입해야 한다.
        """
        from orchestrator.auto_orch import load_pipeline

        yaml_content = (
            "name: legacy-pipeline\n"
            "schedule: monthly\n"
            "token_budget: 50000\n"
        )
        yaml_file = tmp_path / "legacy-pipeline.yaml"
        yaml_file.write_text(yaml_content, encoding="utf-8")

        pipeline = load_pipeline(str(yaml_file))

        # enabled 키가 없으면 기본값 True가 설정되어 있어야 함
        assert pipeline.get("enabled", True) is True


# ── is_pipeline_enabled() — 새 함수 검증 ────────────────────────────────


class TestIsPipelineEnabled:
    """is_pipeline_enabled(pipeline: dict) -> bool 함수 검증.

    이 함수는 아직 구현되지 않아 ImportError가 발생합니다 (RED 단계).
    """

    def test_is_pipeline_enabled_function(self) -> None:
        """is_pipeline_enabled가 callable로 import 가능해야 함."""
        from orchestrator.auto_orch import is_pipeline_enabled

        assert callable(is_pipeline_enabled)

    def test_is_pipeline_enabled_returns_true_when_enabled_true(self) -> None:
        """enabled: True인 dict → True 반환."""
        from orchestrator.auto_orch import is_pipeline_enabled

        pipeline = {"name": "active", "enabled": True}
        assert is_pipeline_enabled(pipeline) is True

    def test_is_pipeline_enabled_returns_false_when_enabled_false(self) -> None:
        """enabled: False인 dict → False 반환."""
        from orchestrator.auto_orch import is_pipeline_enabled

        pipeline = {"name": "inactive", "enabled": False}
        assert is_pipeline_enabled(pipeline) is False

    def test_is_pipeline_enabled_defaults_true_when_key_missing(self) -> None:
        """enabled 키가 없는 dict → 기본값 True 반환."""
        from orchestrator.auto_orch import is_pipeline_enabled

        pipeline = {"name": "legacy-no-field", "schedule": "daily"}
        assert is_pipeline_enabled(pipeline) is True

    def test_is_pipeline_enabled_with_empty_dict(self) -> None:
        """빈 dict → 기본값 True 반환 (예외 발생하지 않아야 함)."""
        from orchestrator.auto_orch import is_pipeline_enabled

        pipeline: dict = {}
        assert is_pipeline_enabled(pipeline) is True

    def test_is_pipeline_enabled_returns_bool_type(self) -> None:
        """반환값이 반드시 bool 타입이어야 함 (truthy/falsy 값이 아닌 True/False)."""
        from orchestrator.auto_orch import is_pipeline_enabled

        pipeline_on = {"name": "a", "enabled": True}
        pipeline_off = {"name": "b", "enabled": False}

        result_on = is_pipeline_enabled(pipeline_on)
        result_off = is_pipeline_enabled(pipeline_off)

        assert type(result_on) is bool
        assert type(result_off) is bool
