# Path: ipwb/util.py
# def check_daemon_is_alive(daemonMultiaddr=IPFSAPI_MUTLIADDRESS):
#     """Ensure that the IPFS daemon is running via HTTP before proceeding"""
#     client = ipfs_client()
# 
#     try:
#         # ConnectionError/AttributeError if IPFS daemon not running
#         client.id()
#         return True
# 
#     except ConnectionError as err:
#         raise IPFSDaemonNotAvailable(
#             f'Daemon is not running at: {daemonMultiaddr}',
#         ) from err
# 
#     except OSError as err:
#         raise IPFSDaemonNotAvailable(
#             'IPFS is likely not installed. See https://ipfs.io/docs/install/'
#         ) from err
# 
#     except Exception as err:
#         raise IPFSDaemonNotAvailable(
#             'Unknown error in retrieving IPFS daemon status.',
#         ) from err
# 
# def create_ipfs_client(daemonMultiaddr=IPFSAPI_MUTLIADDRESS):
#     """Create and return IPFS client."""
#     try:
#         return ipfshttpclient.Client(daemonMultiaddr)
#     except Exception as err:
#         raise Exception('Cannot create an IPFS client.') from err


from unittest.mock import MagicMock, patch
from ipwb.util import check_daemon_is_alive, create_ipfs_client
from ipfshttpclient.exceptions import ConnectionError
import pytest




def test_exception():
    mock_client = MagicMock()
    mock_client.side_effect = Exception('boo!')
    expected_error = 'Cannot create an IPFS client.'

    with patch('ipfshttpclient.Client', mock_client):
        with pytest.raises(Exception, match=expected_error):
            create_ipfs_client()


def test_is_alive():
    mock_client = MagicMock()

    with patch('ipwb.util.ipfs_client', mock_client):
        assert check_daemon_is_alive() is True


def test_connection_error():
    mock_client = MagicMock()
    mock_client.return_value.id.side_effect = ConnectionError('boo!')

    with patch('ipwb.util.ipfs_client', mock_client):
        with pytest.raises(Exception, match='Daemon is not running at'):

# Path: ipwb/util.py
# def check_daemon_is_alive(daemonMultiaddr=IPFSAPI_MUTLIADDRESS):
#     """Ensure that the IPFS daemon is running via HTTP before proceeding"""
#     client = ipfs_client()
# 
#     try:
#         # ConnectionError/AttributeError if IPFS daemon not running
#         client.id()
#         return True
# 
#     except ConnectionError as err:
#         raise IPFSDaemonNotAvailable(
#             f'Daemon is not running at: {daemonMultiaddr}',
#         ) from err
# 
#     except OSError as err:
#         raise IPFSDaemonNotAvailable(
#             'IPFS is likely not installed. See https://ipfs.io/docs/install/'
#         ) from err
# 
#     except Exception as err:
#         raise IPFSDaemonNotAvailable(
#             'Unknown error in retrieving IPFS daemon status.',
#         ) from err
# 
# def create_ipfs_client(daemonMultiaddr=IPFSAPI_MUTLIADDRESS):
#     """Create and return IPFS client."""
#     try:
#         return ipfshttpclient.Client(daemonMultiaddr)
#     except Exception as err:
#         raise Exception('Cannot create an IPFS client.') from err


# Path: tests/test_ipfs_client.py
from unittest.mock import MagicMock, patch
from ipwb.util import check_daemon_is_alive, create_ipfs_client
from ipfshttpclient.exceptions import ConnectionError
import pytest





def test_exception():
    mock_client = MagicMock()
    mock_client.side_effect = Exception('boo!')
    expected_error = 'Cannot create an IPFS client.'

    with patch('ipfshttpclient.Client', mock_client):
        with pytest.raises(Exception, match=expected_error):
            create_ipfs_client()


def test_is_alive():
    mock_client = MagicMock()

    with patch('ipwb.util.ipfs_client', mock_client):
        assert check_daemon_is_alive() is True


def test_connection_error():
    mock_client = MagicMock()
    mock_client.return_value.id.side_effect = ConnectionError('boo!')

    with patch('ipwb.util.ipfs_client', mock_client):
        with pytest.raises(Exception, match='Daemon is not running at'):

            check_daemon_is_alive()