from unittest.mock import MagicMock, patch
import pytest

def test_bot_flow_goal_executor_init_order():
    # We want to prove that without explicit get_instance(device, username),
    # QNavGraph(device) initializes GoalExecutor with an empty username.
    
    # We have to reset GoalExecutor singleton first
    from GramAddict.core.goap import GoalExecutor
    from GramAddict.core.q_nav_graph import QNavGraph

    if hasattr(GoalExecutor, "_instance"):
        GoalExecutor._instance = None
        
    device = MagicMock()
    
    # Action: Instantiate QNavGraph
    # Without the fix, this will initialize GoalExecutor with bot_username=""
    # and PathMemory will get bound to "goap_paths_v1"
        
    with patch("GramAddict.core.qdrant_memory.QdrantClient") as mock_qdrant:
        # FIX: Pre-initialize with username (as done in bot_flow.py)
        executor_pre = GoalExecutor.get_instance(device, "marisaundmarc")
        nav_graph = QNavGraph(device)
        executor = GoalExecutor.get_instance(device, "marisaundmarc")

        # Now it should be bound correctly
        assert executor.path_memory._db.collection_name == "goap_paths_v1_marisaundmarc", "PathMemory collection name does not contain the username suffix! Initialization leak occurred."
