#!/usr/bin/env python
"""
This module contains unit tests for the GPT4AllProvider class in the providers.gpt4all_provider module.

The tests include:
- test_init_success: Tests the successful initialization of the GPT4AllProvider class.
- test_init_failure: Tests the failure scenario during the initialization of the GPT4AllProvider class.
- test_improve_text: Tests the improve_text method of the GPT4AllProvider class.

These tests use patching and mocking to simulate interactions with the GPT4All model and handle exceptions.

Author: Unknown
"""

from unittest.mock import MagicMock, patch

import pytest
from exceptions.exceptions import GTP4AllError
from providers.gpt4all_provider import GPT4AllProvider


def test_init_success():
    """
    Initialize a GPT4AllProvider instance with a specified model name and assert its correctness.

    Arguments:
    - No arguments.

    Return:
    - No return value.

    Exceptions:
    - No exceptions are raised.

    Side Effects:
    - None.
    """

    with patch("providers.gpt4all_provider.GPT4All") as mock_gpt:
        instance = GPT4AllProvider("mock-model")
        assert instance.model_name == "mock-model"
        mock_gpt.assert_called_once_with("mock-model")


def test_init_failure():
    """
    Initialize GPT4AllProvider with a broken model and test initialization failure.

    Arguments:
    - No arguments.

    Exceptions:
    - GTP4AllError: Raised when the GPT4All provider encounters an error during initialization.
    """

    with patch("providers.gpt4all_provider.GPT4All", side_effect=GTP4AllError("💥")):
        with pytest.raises(GTP4AllError):
            GPT4AllProvider("broken-model")


def test_improve_text():
    """
    Summary:
    Function to test the improvement of text generated by a mock model.

    Arguments:
    - No arguments.

    Return:
    - None
    """

    mock_model = MagicMock()
    mock_model.generate.return_value = " improved text "

    with patch("providers.gpt4all_provider.GPT4All", return_value=mock_model):
        provider = GPT4AllProvider("mock-model")
        result = provider.improve_text("Prompt", "Original text")

        assert "Prompt" in mock_model.generate.call_args[0][0]
        assert "Original text" in mock_model.generate.call_args[0][0]
        assert result == "improved text"
