import pytest
from runtime.learning.usage_tracker import UsageTracker
from runtime.learning.patterns import PatternDetector

@pytest.fixture
def detector(tmp_path):
    tracker = UsageTracker(tmp_path)
    # mock data
    for _ in range(4):
        tracker.log("claude", "debate", 10, 10, 100, "success", "s1")
    for _ in range(2):
        tracker.log("gemini", "single", 10, 10, 100, "success", "s2")
    return PatternDetector(tracker)

def test_frequent_chains(detector):
    chains = detector.frequent_chains(min_count=3)
    assert len(chains) == 1
    assert chains[0]["strategy"] == "debate"
    assert chains[0]["models"] == ["claude"]

def test_model_preferences(detector):
    prefs = detector.model_preferences()
    assert prefs["primary_model"] == "claude"
    assert prefs["fallback_model"] == "gemini"

def test_detect_degradation(detector):
    # Hard to test without mocking time in UsageTracker, we will just ensure it doesn't crash
    res = detector.detect_degradation("claude")
    assert res is None # No older data
