# Path: ipwb/backends.py
# def get_web_archive_index(path: str) -> str:
#     """
#     Based on path, choose appropriate backend and fetch the file contents.
#     """
# 
#     # TODO right now, every backend is just a function which returns contents
#     #   of a CDXJ file as string. In the future, however, backends will be
#     #   probably represented as classes with much more sophisticated methods
#     #   of manipulating the archive index records.
#     # TODO also, it will be possible to choose a backend and configure it;
#     #   whereas right now we choose a backend automatically based on the given
#     #   path itself.
# 
#     # Maybe it is an IPFS address?
#     response = fetch_ipfs_index(path)
#     if response is not None:
#         return response
# 
#     # Or a traditional Web address?
#     response = fetch_web_index(path)
#     if response is not None:
#         return response
# 
#     # Okay, this is probably a file on local disk
#     response = fetch_local_index(path)
#     if response is not None:
#         return response
# 
#     raise ValueError((
#         f'Unknown format of index file location: {path}. Please provide '
#         f'a valid local path, HTTP or FTP URL, or an IPFS QmHash.'
#     ))
# 
# class BackendError(Exception):
#     backend_name: str
# 
#     def __str__(self):
#         return 'Cannot load index file from {self.backend_name}.'.format(
#             self=self,
#         )


from unittest import mock
from ipfshttpclient.exceptions import StatusError
from ipwb.backends import get_web_archive_index, BackendError
from pathlib import Path
import pytest




SAMPLE_INDEX = str(
    Path(__file__).parent.parent / 'samples/indexes/salam-home.cdxj'
)


def test_local():
    assert get_web_archive_index(SAMPLE_INDEX).startswith(
        '!context ["https://tools.ietf.org/html/rfc7089"]'
    )


def test_https():
    assert get_web_archive_index(
        'https://raw.githubusercontent.com/oduwsdl/ipwb/master/samples/' +
        'indexes/salam-home.cdxj'
    ).startswith('!context ["https://tools.ietf.org/html/rfc7089"]')


def test_ipfs_success():
    with open(SAMPLE_INDEX, 'r') as f:
        expected_content = f.read()

    connect_to_ipfs = mock.MagicMock()
    connect_to_ipfs.return_value.cat.return_value = expected_content

    with mock.patch('ipfshttpclient.connect', connect_to_ipfs):

# Path: ipwb/backends.py
# def get_web_archive_index(path: str) -> str:
#     """
#     Based on path, choose appropriate backend and fetch the file contents.
#     """
# 
#     # TODO right now, every backend is just a function which returns contents
#     #   of a CDXJ file as string. In the future, however, backends will be
#     #   probably represented as classes with much more sophisticated methods
#     #   of manipulating the archive index records.
#     # TODO also, it will be possible to choose a backend and configure it;
#     #   whereas right now we choose a backend automatically based on the given
#     #   path itself.
# 
#     # Maybe it is an IPFS address?
#     response = fetch_ipfs_index(path)
#     if response is not None:
#         return response
# 
#     # Or a traditional Web address?
#     response = fetch_web_index(path)
#     if response is not None:
#         return response
# 
#     # Okay, this is probably a file on local disk
#     response = fetch_local_index(path)
#     if response is not None:
#         return response
# 
#     raise ValueError((
#         f'Unknown format of index file location: {path}. Please provide '
#         f'a valid local path, HTTP or FTP URL, or an IPFS QmHash.'
#     ))
# 
# class BackendError(Exception):
#     backend_name: str
# 
#     def __str__(self):
#         return 'Cannot load index file from {self.backend_name}.'.format(
#             self=self,
#         )


# Path: tests/test_backends.py
from unittest import mock
from ipfshttpclient.exceptions import StatusError
from ipwb.backends import get_web_archive_index, BackendError
from pathlib import Path
import pytest





SAMPLE_INDEX = str(
    Path(__file__).parent.parent / 'samples/indexes/salam-home.cdxj'
)


def test_local():
    assert get_web_archive_index(SAMPLE_INDEX).startswith(
        '!context ["https://tools.ietf.org/html/rfc7089"]'
    )


def test_https():
    assert get_web_archive_index(
        'https://raw.githubusercontent.com/oduwsdl/ipwb/master/samples/' +
        'indexes/salam-home.cdxj'
    ).startswith('!context ["https://tools.ietf.org/html/rfc7089"]')


def test_ipfs_success():
    with open(SAMPLE_INDEX, 'r') as f:
        expected_content = f.read()

    connect_to_ipfs = mock.MagicMock()
    connect_to_ipfs.return_value.cat.return_value = expected_content

    with mock.patch('ipfshttpclient.connect', connect_to_ipfs):

        assert get_web_archive_index(