import pytest
from unittest.mock import patch, MagicMock

# Mock dependencies before they are imported by the module we're testing
mock_embed = MagicMock(return_value=[[0.1] * 384, [0.9] * 384])
mock_query_log = MagicMock(return_value=[{"query": "a routine query"}])

@patch('pulse.brain.index.rule_embeddings.backends.embed_texts', mock_embed)
@patch('pulse.daemons.neural.prediction_daemon._get_recent_query_log', mock_query_log)
def test_surprise_gate_logic():
    from pulse.brain.brain_search.surprise_gate import get_surprising_search_depth
    import numpy as np

    # === Test 1: Routine Query (high similarity) ===
    # Mock the embedding to return a vector very similar to the "recent" one
    mock_embed.side_effect = [
        [[0.89] * 384], # Current query
        [[0.9] * 384]   # Recent query
    ]
    
    top_k, max_per_source = get_surprising_search_depth("a very routine query", 10, 10)
    
    # Assert that depth does NOT change for a routine query
    assert top_k == 10
    assert max_per_source == 10
    
    # === Test 2: Novel Query (low similarity) ===
    # Mock the embedding to return a vector very different from the "recent" one
    mock_embed.side_effect = [
        [[0.1] * 384], # Current query
        [[0.9] * 384]   # Recent query
    ]
    
    top_k_novel, max_per_source_novel = get_surprising_search_depth("a completely novel query", 10, 10)
    
    # Assert that depth INCREASES for a novel query
    assert top_k_novel == 15
    assert max_per_source_novel == 15

