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

Code before:
    assert wait_for_event(event_listener, "generation_started")

    # Should NOT get a config_changed event from auto-naming since name already exists
    # We'll wait a bit to make sure it doesn't happen
    assert not wait_for_event(event_listener, "config_changed", timeout=3)

    assert wait_for_event(event_listener, "generation_complete")

    # Verify that the config didn't change after generation
    assert not wait_for_event(event_listener, "config_changed", timeout=1)

    # Check that the predefined name wasn't changed
    logdir = get_logs_dir() / conversation_id
    chat_config = ChatConfig.from_logdir(logdir)

    assert chat_config.name == "Predefined Name"
    print(f"Display name preserved: '{chat_config.name}'")


@pytest.mark.timeout(30)
@pytest.mark.slow
@pytest.mark.requires_api
@pytest.mark.skipif(
    "claude-haiku" in os.getenv("MODEL", "").lower(),
    reason="Claude Haiku models output thinking tags in names",
)
def test_auto_naming_meaningful_content(event_listener, wait_for_event):
    """Test that auto-naming generates contextually relevant names."""
    import re

    port = event_listener["port"]
    conversation_id = event_listener["conversation_id"]
    session_id = event_listener["session_id"]

    # Use a simple but specific user message to test contextual naming
    requests.post(
        f"http://localhost:{port}/api/v2/conversations/{conversation_id}",
        json={
            "role": "user",
            "content": "How do I center a div in CSS? Short answer please.",
        },
    )

    assert wait_for_event(event_listener, "message_added")

    requests.post(
        f"http://localhost:{port}/api/v2/conversations/{conversation_id}/step",
        json={"session_id": session_id},
    )

    assert wait_for_event(event_listener, "generation_started")

Review comment (suggestion):
I think the real solution is to disable reasoning for the Haiku 4.5 request to generate meaningful name the server (leading to the `<think>...</think>` preamble), not to skip like this. If you can't easily override disabling reasoning, at least strip the content of the think tags in the output before returning it as a meaningful name.

Code after:
    # Should NOT get a config_changed event from auto-naming since name already exists
    # We'll wait a bit to make sure it doesn't happen
    assert not wait_for_event(event_listener, "config_changed", timeout=3)

    assert wait_for_event(event_listener, "generation_complete")

    # Verify that the config didn't change after generation
    assert not wait_for_event(event_listener, "config_changed", timeout=1)

    # Check that the predefined name wasn't changed
    logdir = get_logs_dir() / conversation_id
    chat_config = ChatConfig.from_logdir(logdir)

    assert chat_config.name == "Predefined Name"
    print(f"Display name preserved: '{chat_config.name}'")


@pytest.mark.timeout(30)
@pytest.mark.slow
@pytest.mark.requires_api
def test_auto_naming_meaningful_content(event_listener, wait_for_event):
    """Test that auto-naming generates contextually relevant names."""
    port = event_listener["port"]
    conversation_id = event_listener["conversation_id"]
    session_id = event_listener["session_id"]

    # Use a simple but specific user message to test contextual naming
    requests.post(
        f"http://localhost:{port}/api/v2/conversations/{conversation_id}",
        json={
            "role": "user",
            "content": "How do I center a div in CSS? Short answer please.",
        },
    )

    assert wait_for_event(event_listener, "message_added")

    requests.post(
        f"http://localhost:{port}/api/v2/conversations/{conversation_id}/step",
        json={"session_id": session_id},
    )

    assert wait_for_event(event_listener, "generation_started")

    # Wait for config_changed event
    assert wait_for_event(event_listener, "config_changed", timeout=20)

    # Check that the generated name is contextually relevant
    logdir = get_logs_dir() / conversation_id
    chat_config = ChatConfig.from_logdir(logdir)