import pytest from backend.src.core.logger import belief_scope, logger 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_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_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"