import pytest
from src.core.logger import belief_scope, logger


# [DEF:test_belief_scope_logs_entry_action_exit:Function]
# @PURPOSE: Test that belief_scope generates [ID][Entry], [ID][Action], and [ID][Exit] logs.
# @PRE: belief_scope is available. caplog fixture is used.
# @POST: Logs are verified to contain Entry, Action, and Exit tags.
def test_belief_scope_logs_entry_action_exit(caplog):
    """Test that belief_scope generates [ID][Entry], [ID][Action], and [ID][Exit] logs."""
    caplog.set_level("INFO")

    with belief_scope("TestFunction"):
        logger.info("Doing something important")

    # Check that the logs contain the expected patterns
    log_messages = [record.message for record in caplog.records]

    assert any("[TestFunction][Entry]" in msg for msg in log_messages), "Entry log not found"
    assert any("[TestFunction][Action] Doing something important" in msg for msg in log_messages), "Action log not found"
    assert any("[TestFunction][Exit]" in msg for msg in log_messages), "Exit log not found"
# [/DEF:test_belief_scope_logs_entry_action_exit:Function]


# [DEF:test_belief_scope_error_handling:Function]
# @PURPOSE: Test that belief_scope logs Coherence:Failed on exception.
# @PRE: belief_scope is available. caplog fixture is used.
# @POST: Logs are verified to contain Coherence:Failed tag.
def test_belief_scope_error_handling(caplog):
    """Test that belief_scope logs Coherence:Failed on exception."""
    caplog.set_level("INFO")

    with pytest.raises(ValueError):
        with belief_scope("FailingFunction"):
            raise ValueError("Something went wrong")

    log_messages = [record.message for record in caplog.records]

    assert any("[FailingFunction][Entry]" in msg for msg in log_messages), "Entry log not found"
    assert any("[FailingFunction][Coherence:Failed]" in msg for msg in log_messages), "Failed coherence log not found"
    # Exit should not be logged on failure
# [/DEF:test_belief_scope_error_handling:Function]


# [DEF:test_belief_scope_success_coherence:Function]
# @PURPOSE: Test that belief_scope logs Coherence:OK on success.
# @PRE: belief_scope is available. caplog fixture is used.
# @POST: Logs are verified to contain Coherence:OK tag.
def test_belief_scope_success_coherence(caplog):
    """Test that belief_scope logs Coherence:OK on success."""
    caplog.set_level("INFO")

    with belief_scope("SuccessFunction"):
        pass

    log_messages = [record.message for record in caplog.records]

    assert any("[SuccessFunction][Coherence:OK]" in msg for msg in log_messages), "Success coherence log not found"
# [/DEF:test_belief_scope_success_coherence:Function]