"""RPA Engine 단위 테스트"""
import sys, os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
import pytest

from core.rpa_engine import RPAValidator, TASK_ENDPOINT_MAP


class TestRPAValidator:
    """RPAValidator 단위 테스트"""

    def test_empty_rules_passes_all(self):
        v = RPAValidator([])
        errors = v.validate({"any": "value"})
        assert errors == []

    def test_required_field_missing(self):
        rules = [{"field_name": "title", "is_required": True,
                   "field_type": "str", "allowed_values": [], "constraints": {}}]
        v = RPAValidator(rules)
        errors = v.validate({})
        assert any("title" in e for e in errors)

    def test_required_field_empty_string(self):
        rules = [{"field_name": "title", "is_required": True,
                   "field_type": "str", "allowed_values": [], "constraints": {}}]
        v = RPAValidator(rules)
        errors = v.validate({"title": ""})
        assert any("title" in e for e in errors)

    def test_required_field_present_passes(self):
        rules = [{"field_name": "title", "is_required": True,
                   "field_type": "str", "allowed_values": [], "constraints": {}}]
        v = RPAValidator(rules)
        errors = v.validate({"title": "SR 제목"})
        assert errors == []

    def test_enum_valid_value(self):
        rules = [{"field_name": "sr_type", "is_required": False,
                   "field_type": "enum", "allowed_values": ["DEPLOY","INQUIRY","OTHER"],
                   "constraints": {}}]
        v = RPAValidator(rules)
        errors = v.validate({"sr_type": "DEPLOY"})
        assert errors == []

    def test_enum_invalid_value(self):
        rules = [{"field_name": "sr_type", "is_required": False,
                   "field_type": "enum", "allowed_values": ["DEPLOY","INQUIRY","OTHER"],
                   "constraints": {}}]
        v = RPAValidator(rules)
        errors = v.validate({"sr_type": "INVALID"})
        assert any("sr_type" in e for e in errors)
        assert any("INVALID" in e for e in errors)

    def test_optional_missing_passes(self):
        rules = [{"field_name": "description", "is_required": False,
                   "field_type": "str", "allowed_values": [], "constraints": {}}]
        v = RPAValidator(rules)
        errors = v.validate({})
        assert errors == []

    def test_int_type_valid(self):
        rules = [{"field_name": "inst_id", "is_required": True,
                   "field_type": "int", "allowed_values": [], "constraints": {}}]
        v = RPAValidator(rules)
        errors = v.validate({"inst_id": 1})
        assert errors == []

    def test_int_type_invalid(self):
        rules = [{"field_name": "inst_id", "is_required": True,
                   "field_type": "int", "allowed_values": [], "constraints": {}}]
        v = RPAValidator(rules)
        errors = v.validate({"inst_id": "not-a-number"})
        assert any("inst_id" in e for e in errors)


class TestTaskEndpointMap:
    """TASK_ENDPOINT_MAP 단위 테스트"""

    def test_sr_create_mapped(self):
        assert "SR_CREATE" in TASK_ENDPOINT_MAP
        method, path = TASK_ENDPOINT_MAP["SR_CREATE"]
        assert method == "POST"
        assert "/api/tasks" in path

    def test_approval_mapped(self):
        assert "APPROVAL_PROCESS" in TASK_ENDPOINT_MAP

    def test_all_entries_have_method_and_path(self):
        for task_type, (method, path) in TASK_ENDPOINT_MAP.items():
            assert method in ("GET", "POST", "PUT", "PATCH", "DELETE"), \
                f"{task_type}: invalid method {method}"
            assert path.startswith("/api/"), \
                f"{task_type}: path should start with /api/"
