# Path: ipwb/indexer.py
# DEBUG = False
#     DEBUG = debug
# def s2b(s):  # Convert str to bytes, cross-py
# def push_to_ipfs(hstr, payload):
# def encrypt(hstr, payload, encryption_key):
# def create_ipfs_temp_path():
# def index_file_at(warc_paths, encryption_key=None,
#                   compression_level=None, encrypt_then_compress=True,
#                   quiet=False, outfile=None, debug=False):
# def sanitize_cdxj_line(cdxj_line):
# def cdx_cdxj_lines_from_file(warc_path, **enc_comp_opts):
# def generate_cdxj_metadata(cdxj_lines=None):
# def ask_user_for_encryption_key():
# def verify_daemon_is_alive(host_and_port):
# def verify_file_exists(warc_path):
# def show_progress(msg, i, n):
# def log_error(err_in, end='\n'):
# def pull_from_ipfs(hash_in):
# def push_bytes_to_ipfs(bytes_in):
# def write_file(filename, content):


import pytest
import os
from . import testUtil as ipwb_test
from ipwb import indexer
from pathlib import Path
# Number of entries in CDXJ == number of response records in WARC





def test_cdxj_warc_responserecord_count():
    new_warc_path = ipwb_test.createUniqueWARC()
    # use ipwb indexer to push
    cdxjList = indexer.index_file_at(new_warc_path, quiet=True)
    cdxj = '\n'.join(cdxjList)
    assert ipwb_test.count_cdxj_entries(cdxj) == 2


# A response record's content-length causes the payload to truncate
# WARC-Response record for html should still exist in output
def test_warc_ipwb_indexer_broken_warc_record():
    pathOfBrokenWARC = os.path.join(
        Path(os.path.dirname(__file__)).parent,
        'samples', 'warcs', 'broken.warc')

# Path: ipwb/indexer.py
# DEBUG = False
#     DEBUG = debug
# def s2b(s):  # Convert str to bytes, cross-py
# def push_to_ipfs(hstr, payload):
# def encrypt(hstr, payload, encryption_key):
# def create_ipfs_temp_path():
# def index_file_at(warc_paths, encryption_key=None,
#                   compression_level=None, encrypt_then_compress=True,
#                   quiet=False, outfile=None, debug=False):
# def sanitize_cdxj_line(cdxj_line):
# def cdx_cdxj_lines_from_file(warc_path, **enc_comp_opts):
# def generate_cdxj_metadata(cdxj_lines=None):
# def ask_user_for_encryption_key():
# def verify_daemon_is_alive(host_and_port):
# def verify_file_exists(warc_path):
# def show_progress(msg, i, n):
# def log_error(err_in, end='\n'):
# def pull_from_ipfs(hash_in):
# def push_bytes_to_ipfs(bytes_in):
# def write_file(filename, content):


# Path: tests/test_indexing.py
import pytest
import os
from . import testUtil as ipwb_test
from ipwb import indexer
from pathlib import Path

# Number of entries in CDXJ == number of response records in WARC





def test_cdxj_warc_responserecord_count():
    new_warc_path = ipwb_test.createUniqueWARC()
    # use ipwb indexer to push
    cdxjList = indexer.index_file_at(new_warc_path, quiet=True)
    cdxj = '\n'.join(cdxjList)
    assert ipwb_test.count_cdxj_entries(cdxj) == 2


# A response record's content-length causes the payload to truncate
# WARC-Response record for html should still exist in output
def test_warc_ipwb_indexer_broken_warc_record():
    pathOfBrokenWARC = os.path.join(
        Path(os.path.dirname(__file__)).parent,
        'samples', 'warcs', 'broken.warc')

    cdxjList = indexer.index_file_at(pathOfBrokenWARC, quiet=True)