#!/usr/bin/env python3
"""Tests for Story 1.01 (Track B): BaseInterceptor Abstract Class"""
import asyncio
import sys
sys.path.insert(0, '/mnt/e/genesis-system')


def test_base_interceptor():
    from core.interceptors.base_interceptor import BaseInterceptor, InterceptorMetadata

    # BB1: Cannot instantiate BaseInterceptor directly
    try:
        b = BaseInterceptor()
        assert False, "Should have raised TypeError"
    except TypeError:
        pass

    # BB2: Partial implementation (missing 1 method) raises TypeError
    class PartialInterceptor(BaseInterceptor):
        metadata = InterceptorMetadata(name="partial", priority=50)

        async def pre_execute(self, task_payload: dict) -> dict:
            return task_payload

        async def post_execute(self, result: dict, task_payload: dict) -> None:
            pass

        async def on_error(self, error: Exception, task_payload: dict) -> dict:
            return {}
        # Missing on_correction

    try:
        p = PartialInterceptor()
        assert False, "Should have raised TypeError for partial implementation"
    except TypeError:
        pass

    # BB3: Full implementation passes instantiation
    class FullInterceptor(BaseInterceptor):
        metadata = InterceptorMetadata(name="full", priority=50)

        async def pre_execute(self, task_payload: dict) -> dict:
            return task_payload

        async def post_execute(self, result: dict, task_payload: dict) -> None:
            pass

        async def on_error(self, error: Exception, task_payload: dict) -> dict:
            return {"error": str(error)}

        async def on_correction(self, correction_payload: dict) -> dict:
            return correction_payload

    f = FullInterceptor()
    assert f.metadata.name == "full"
    assert f.metadata.priority == 50
    assert f.metadata.enabled == True

    # WB1: All 4 methods are abstract
    abstract_methods = BaseInterceptor.__abstractmethods__
    assert "pre_execute" in abstract_methods
    assert "post_execute" in abstract_methods
    assert "on_error" in abstract_methods
    assert "on_correction" in abstract_methods
    assert len(abstract_methods) == 4

    # WB2: InterceptorMetadata dataclass validates field types
    meta = InterceptorMetadata(name="test", priority=10, enabled=False)
    assert meta.name == "test"
    assert meta.priority == 10
    assert meta.enabled == False

    # WB3: Full implementation methods are callable
    async def test_methods():
        result = await f.pre_execute({"test": True})
        assert result == {"test": True}

        await f.post_execute({"done": True}, {"test": True})

        error_result = await f.on_error(ValueError("test"), {"test": True})
        assert "error" in error_result

        correction_result = await f.on_correction({"fix": True})
        assert correction_result == {"fix": True}

    asyncio.run(test_methods())

    print("ALL TESTS PASSED — Story 1.01 (Track B)")


if __name__ == "__main__":
    test_base_interceptor()
