# Path: tests/conftest.py
# class IaRequestsMock(RequestsMock):
#     def add_metadata_mock(self, identifier, body=None, method=responses.GET,
#                           protocol='https?', transform_body=None):
#         url = re.compile(f'{protocol}://archive.org/metadata/{identifier}')
#         if body is None:
#             body = load_test_data_file(f'metadata/{identifier}.json')
#         if transform_body:
#             body = transform_body(body)
#         self.add(method, url, body=body, content_type='application/json')
# 
#     def mock_all_downloads(self, num_calls=1, body='test content', protocol='https?'):
#         url = re.compile(f'{protocol}://archive.org/download/.*')
#         for _ in range(6):
#             self.add(responses.GET, url, body=body)
# 
# def ia_call(argv, expected_exit_code=0):
#     # Use a test config for all `ia` tests.
#     argv.insert(1, '--config-file')
#     argv.insert(2, TEST_CONFIG)
#     sys.argv = argv
#     try:
#         ia.main()
#     except SystemExit as exc:
#         exit_code = exc.code if exc.code else 0
#         assert exit_code == expected_exit_code


import sys
import responses
from time import time
from tests.conftest import IaRequestsMock, ia_call




def test_ia_metadata_exists(capsys):
    with IaRequestsMock() as rsps:
        rsps.add_metadata_mock('nasa')
        ia_call(['ia', 'metadata', '--exists', 'nasa'], expected_exit_code=0)
        out, err = capsys.readouterr()
        assert err == 'nasa exists\n'
        rsps.reset()
        rsps.add_metadata_mock('nasa', '{}')
        sys.argv = ['ia', 'metadata', '--exists', 'nasa']

# Path: tests/conftest.py
# class IaRequestsMock(RequestsMock):
#     def add_metadata_mock(self, identifier, body=None, method=responses.GET,
#                           protocol='https?', transform_body=None):
#         url = re.compile(f'{protocol}://archive.org/metadata/{identifier}')
#         if body is None:
#             body = load_test_data_file(f'metadata/{identifier}.json')
#         if transform_body:
#             body = transform_body(body)
#         self.add(method, url, body=body, content_type='application/json')
# 
#     def mock_all_downloads(self, num_calls=1, body='test content', protocol='https?'):
#         url = re.compile(f'{protocol}://archive.org/download/.*')
#         for _ in range(6):
#             self.add(responses.GET, url, body=body)
# 
# def ia_call(argv, expected_exit_code=0):
#     # Use a test config for all `ia` tests.
#     argv.insert(1, '--config-file')
#     argv.insert(2, TEST_CONFIG)
#     sys.argv = argv
#     try:
#         ia.main()
#     except SystemExit as exc:
#         exit_code = exc.code if exc.code else 0
#         assert exit_code == expected_exit_code


# Path: tests/cli/test_ia_metadata.py
import sys
import responses
from time import time
from tests.conftest import IaRequestsMock, ia_call





def test_ia_metadata_exists(capsys):
    with IaRequestsMock() as rsps:
        rsps.add_metadata_mock('nasa')
        ia_call(['ia', 'metadata', '--exists', 'nasa'], expected_exit_code=0)
        out, err = capsys.readouterr()
        assert err == 'nasa exists\n'
        rsps.reset()
        rsps.add_metadata_mock('nasa', '{}')
        sys.argv = ['ia', 'metadata', '--exists', 'nasa']

        ia_call(['ia', 'metadata', '--exists', 'nasa'], expected_exit_code=1)