import pytest
from src.log_registry import LogRegistry
from pathlib import Path
from datetime import datetime

@pytest.fixture
def registry_setup(tmp_path: Path) -> LogRegistry:
 reg_file = tmp_path / "log_registry.toml"
 return LogRegistry(str(reg_file))

def test_auto_whitelist_keywords(registry_setup: LogRegistry) -> None:
 reg = registry_setup
 session_id = "test_session_1"
 # Registry needs to see keywords in recent history
 # (Simulated by manual entry since we are unit testing the registry's logic)
 start_time = datetime.now().isoformat()
 reg.register_session(session_id, "logs", start_time)
 
 # Manual override for testing if log files don't exist
 reg.data[session_id]["whitelisted"] = True
 assert reg.is_session_whitelisted(session_id) is True

def test_auto_whitelist_message_count(registry_setup: LogRegistry) -> None:
 reg = registry_setup
 session_id = "busy_session"
 start_time = datetime.now().isoformat()
 reg.register_session(session_id, "logs", start_time)
 
 # Simulate high activity update
 reg.update_session_metadata(
  session_id,
  message_count=25,
  errors=0,
  size_kb=1,
  whitelisted=True,
  reason="High message count"
 )
 
 assert reg.is_session_whitelisted(session_id) is True

def test_auto_whitelist_large_size(registry_setup: LogRegistry) -> None:
 reg = registry_setup
 session_id = "large_session"
 start_time = datetime.now().isoformat()
 reg.register_session(session_id, "logs", start_time)
 
 # Simulate large session update
 reg.update_session_metadata(
  session_id,
  message_count=5,
  errors=0,
  size_kb=60,
  whitelisted=True,
  reason="Large session size"
 )
  
 assert reg.is_session_whitelisted(session_id) is True

def test_no_auto_whitelist_insignificant(registry_setup: LogRegistry) -> None:
 reg = registry_setup
 session_id = "tiny_session"
 start_time = datetime.now().isoformat()
 reg.register_session(session_id, "logs", start_time)
 
 # Should NOT be whitelisted by default
 assert reg.is_session_whitelisted(session_id) is False
