import pytest
from src.log_pruner import LogPruner
from src.log_registry import LogRegistry
from pathlib import Path
from unittest.mock import patch
from datetime import datetime, timedelta

@pytest.fixture
def pruner_setup(tmp_path: Path) -> tuple[LogPruner, LogRegistry, Path]:
 logs_dir = tmp_path / "logs"
 logs_dir.mkdir()
 reg_file = tmp_path / "log_registry.toml"
 registry = LogRegistry(str(reg_file))
 pruner = LogPruner(registry, str(logs_dir))
 return pruner, registry, logs_dir

def test_prune_old_insignificant_logs(pruner_setup: tuple[LogPruner, LogRegistry, Path]) -> None:
 pruner, registry, logs_dir = pruner_setup
 
 # 1. Create a very old, small session
 old_session = "old_session"
 old_dir = logs_dir / old_session
 old_dir.mkdir()
 (old_dir / "comms.log").write_text("{}", encoding="utf-8")
 
 # Register it with a very old start time
 old_time = (datetime.now() - timedelta(days=40)).isoformat()
 registry.register_session(old_session, str(old_dir), old_time)
 
 # Ensure it is considered old by the registry
 old_sessions = registry.get_old_non_whitelisted_sessions(datetime.now() - timedelta(days=30))
 assert any(s['session_id'] == old_session for s in old_sessions)
 
 # 2. Run pruner
 with patch("shutil.rmtree") as mock_rm:
  pruner.prune(max_age_days=30)
  # Verify session removed from registry
  assert old_session not in registry.data
  # Verify directory deletion triggered
  assert mock_rm.called
