# #region TestDeleteRunningTasks [C:2] [TYPE Module] [SEMANTICS test,maintenance,tasks,cleanup]
# @BRIEF Tests for delete_running_tasks.py script (mocked DB session).
# @RELATION BINDS_TO -> [DeleteRunningTasksUtil]
# @TEST_EDGE: delete_with_running_tasks -> Deletes found tasks
# @TEST_EDGE: delete_no_running_tasks -> Prints message, no deletion
# @TEST_EDGE: delete_db_error -> Rollback on failure
import sys
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest

sys.path.insert(0, str(Path(__file__).parent.parent.parent / "src"))


# =============================================================================
# delete_running_tasks
# =============================================================================

class TestDeleteRunningTasks:
    """Tests for delete_running_tasks.delete_running_tasks()."""

    @patch("src.scripts.delete_running_tasks.TasksSessionLocal")
    def test_deletes_found_tasks(self, MockSession, capsys):
        """Happy: running tasks found and deleted."""
        from src.scripts.delete_running_tasks import delete_running_tasks

        mock_session = MagicMock()
        MockSession.return_value = mock_session

        # Mock running tasks query results
        task1 = MagicMock()
        task1.id = "task-1"
        task1.type = "backup"
        task2 = MagicMock()
        task2.id = "task-2"
        task2.type = "migration"

        mock_query = MagicMock()
        mock_query.filter.return_value.all.return_value = [task1, task2]
        mock_session.query.return_value = mock_query

        delete_running_tasks()

        # Verify tasks were printed
        captured = capsys.readouterr()
        assert "Found 2 RUNNING tasks" in captured.out
        assert "task-1" in captured.out
        assert "task-2" in captured.out
        assert "Successfully deleted 2 RUNNING tasks" in captured.out

        # Verify delete was called
        mock_query.filter.assert_called()
        mock_session.commit.assert_called_once()
        mock_session.close.assert_called_once()

    @patch("src.scripts.delete_running_tasks.TasksSessionLocal")
    def test_no_running_tasks(self, MockSession, capsys):
        """Edge: no running tasks prints message, no delete."""
        from src.scripts.delete_running_tasks import delete_running_tasks

        mock_session = MagicMock()
        MockSession.return_value = mock_session

        mock_query = MagicMock()
        mock_query.filter.return_value.all.return_value = []
        mock_session.query.return_value = mock_query

        delete_running_tasks()

        captured = capsys.readouterr()
        assert "No RUNNING tasks found" in captured.out

        # Verify no delete or commit happened
        mock_session.commit.assert_not_called()
        mock_session.close.assert_called_once()

    @patch("src.scripts.delete_running_tasks.TasksSessionLocal")
    def test_db_error_rollback(self, MockSession, capsys):
        """Edge: DB error triggers rollback."""
        from src.scripts.delete_running_tasks import delete_running_tasks

        mock_session = MagicMock()
        MockSession.return_value = mock_session

        # Make all() raise exception
        mock_query = MagicMock()
        mock_query.filter.return_value.all.side_effect = Exception("DB connection lost")
        mock_session.query.return_value = mock_query

        delete_running_tasks()

        captured = capsys.readouterr()
        assert "Error deleting tasks" in captured.out
        assert "DB connection lost" in captured.out

        mock_session.rollback.assert_called_once()
        mock_session.close.assert_called_once()
# #endregion TestDeleteRunningTasks
