from unittest.mock import MagicMock

# Attempt to load the module
from GramAddict.core.qdrant_memory import UIMemoryDB


class DummyMemory(UIMemoryDB):
    def __init__(self):
        # Prevent actual QdrantClient initialization for offline tests
        self.client = None
        self.collection_name = "test_collection"


def test_decay_confidence_signature():
    """Ensure decay_confidence doesn't crash from legacy plugins passing xml_context."""
    mem = DummyMemory()

    # Mock _adjust_confidence to just capture arguments
    mem._adjust_confidence = MagicMock()

    # Legacy caller pattern A (positional intent and amount)
    mem.decay_confidence("my_intent", 0.40)
    # Wait, passing positional "my_intent", 0.40 makes `args[0] = "my_intent"`, `args[1] = 0.40`.
    # kwargs.get("amount") is 0.25 (default). But if passed positionally, args[1] was xml_context
    # in legacy! Let's ensure it doesn't crash.

    # Legacy caller pattern B (explicit kwargs)
    mem.decay_confidence(intent="my_intent", amount=0.40)
    mem._adjust_confidence.assert_called_with("my_intent", -0.40)

    # Legacy caller pattern C (positional with string where amount should be)
    mem.decay_confidence("my_intent", "xml_context_string")
    mem._adjust_confidence.assert_called_with("my_intent", -0.50)

    # Legacy caller pattern D (kwargs with xml_context)
    mem.decay_confidence(intent="my_intent", xml_context="<node/>", amount=0.30)
    mem._adjust_confidence.assert_called_with("my_intent", -0.30)


def test_boost_confidence_signature():
    mem = DummyMemory()
    mem._adjust_confidence = MagicMock()

    mem.boost_confidence("intent", "xml_string", 0.5)
    mem._adjust_confidence.assert_called_with(
        "intent", 0.15
    )  # Because "xml_string" fails float conversion, defaults to 0.15
