#!/usr/bin/python3
"""Tests for slpkg/views/process.py using pytest."""

from unittest.mock import MagicMock, patch

import pytest
from slpkg.views.process import ViewProcess


@pytest.fixture
def mock_config():
    """Mock config_load for ViewProcess."""
    with patch("slpkg.views.process.config_load") as mock:
        mock.red = "\x1b[91m"
        mock.endc = "\x1b[0m"
        yield mock


@pytest.fixture
def view_process_inst(mock_config):
    """Fixture for ViewProcess with dependencies mocked."""
    with patch("slpkg.views.process.ProgressBar"), \
         patch("slpkg.views.process.Imprint"):
        inst = ViewProcess()
        return inst


def test_view_process_message(view_process_inst):
    """Test starting a progress message."""
    with patch("slpkg.views.process.Process") as mock_process_cls:
        view_process_inst.message("Testing")
        
        # Should create a Process and start it
        mock_process_cls.assert_called_once()
        mock_process_cls.return_value.start.assert_called_once()
        assert view_process_inst.bar_process is not None


def test_view_process_done(view_process_inst, capsys):
    """Test done message."""
    mock_bar = MagicMock()
    mock_bar.is_alive.return_value = True
    view_process_inst.bar_process = mock_bar
    view_process_inst.imp.done = "[DONE]"
    
    view_process_inst.done()
    
    mock_bar.terminate.assert_called_once()
    mock_bar.join.assert_called_once()
    captured = capsys.readouterr()
    assert "[DONE]" in captured.out


def test_view_process_failed(view_process_inst, capsys):
    """Test failed message."""
    mock_bar = MagicMock()
    mock_bar.is_alive.return_value = False
    view_process_inst.bar_process = mock_bar
    view_process_inst.imp.failed = "[FAILED]"
    
    view_process_inst.failed()
    
    # terminate should not be called if not alive
    assert not mock_bar.terminate.called
    captured = capsys.readouterr()
    assert "[FAILED]" in captured.out
    assert "\x1b[91m" in captured.out # Red color
