Repository: ErikBjare/gptme
File: tests/test_server_v2_hooks.py (Python)

Code before:
        assert (
            len(hook_messages) > 0
        ), f"MESSAGE_PRE_PROCESS hook message should be in log. Found {len(messages)} messages"

    finally:
        # Clean up
        unregister_hook("test_pre_process", HookType.MESSAGE_PRE_PROCESS)


# FIXME: This test is currently failing in CI
#
# The test registers a MESSAGE_POST_PROCESS hook in the test thread,
# but the hook message doesn't appear in the conversation log when checked.
# MESSAGE_POST_PROCESS hooks DO work in production (both CLI and server),
# so this is a testing infrastructure issue, not a production bug.
#
# Potential causes to investigate:
# - Hook registry visibility across Flask/worker threads
# - LogManager instance synchronization
# - Timing issues in the test
#
# TODO: Change hook registry to use threading.local() or contextvars
# for better thread isolation, similar to how tools are handled.
#
# See: https://github.com/gptme/gptme/pull/824
@pytest.mark.skip(reason="Hook testing infrastructure needs improvement")
def test_message_post_process_hook(client: FlaskClient):
    """Test that MESSAGE_POST_PROCESS hook is triggered after generation."""
    hook_triggered = []

    def post_process_hook(manager):
        hook_triggered.append("MESSAGE_POST_PROCESS")
        yield Message("system", "POST_PROCESS hook triggered")

    # Register the hook
    register_hook("test_post_process", HookType.MESSAGE_POST_PROCESS, post_process_hook)

    try:
        # Create a new conversation
        conv = create_conversation(client)

        # First, add a user message
        response = client.post(
            f"/api/v2/conversations/{conv['conversation_id']}",
            json={"role": "user", "content": "Say hello"},
        )
        assert response.status_code == 200

        # Then call step to generate response
        response = client.post(
            f"/api/v2/conversations/{conv['conversation_id']}/step",

Review comment (bug):
```suggestion
@pytest.mark.xfail(reason="Hook testing infrastructure needs improvement")
```

Code after:
        assert (
            len(hook_messages) > 0
        ), f"MESSAGE_PRE_PROCESS hook message should be in log. Found {len(messages)} messages"

    finally:
        # Clean up
        unregister_hook("test_pre_process", HookType.MESSAGE_PRE_PROCESS)


# FIXME: This test is currently failing in CI
#
# The test registers a MESSAGE_POST_PROCESS hook in the test thread,
# but the hook message doesn't appear in the conversation log when checked.
# MESSAGE_POST_PROCESS hooks DO work in production (both CLI and server),
# so this is a testing infrastructure issue, not a production bug.
#
# Potential causes to investigate:
# - Hook registry visibility across Flask/worker threads
# - LogManager instance synchronization
# - Timing issues in the test
#
# TODO: Change hook registry to use threading.local() or contextvars
# for better thread isolation, similar to how tools are handled.
#
# See: https://github.com/gptme/gptme/pull/824
@pytest.mark.xfail(reason="Hook testing infrastructure needs improvement")
def test_message_post_process_hook(client: FlaskClient):
    """Test that MESSAGE_POST_PROCESS hook is triggered after generation."""
    hook_triggered = []

    def post_process_hook(manager):
        hook_triggered.append("MESSAGE_POST_PROCESS")
        yield Message("system", "POST_PROCESS hook triggered")

    # Register the hook
    register_hook("test_post_process", HookType.MESSAGE_POST_PROCESS, post_process_hook)

    try:
        # Create a new conversation
        conv = create_conversation(client)

        # First, add a user message
        response = client.post(
            f"/api/v2/conversations/{conv['conversation_id']}",
            json={"role": "user", "content": "Say hello"},
        )
        assert response.status_code == 200

        # Then call step to generate response
        response = client.post(
            f"/api/v2/conversations/{conv['conversation_id']}/step",