"""This is a bigrep specific test file.
It tests that the incomming blade_materials changelog and the client ones are
merged successfully
"""
import pytest
from scripts import process_changelog as pc

def test_verify():
    """test what should be a valid input"""
    
    change_log = [
    "# 3.0.0",
    "        - Items1",
    "     - Items2",
    "- Items3",
    "## Internal",
    "- secret1",
    "> 1.1"
    ]
    assert pc.verify(change_log)
    
def test_token():
    change_log = [
    "# 3.0.0",
    "        - Items1",
    "     - Items2",
    "- Items3",
    "## internal",
    "- secret1",
    "> 1.1"
    ""
    ]
    tokenized = [pc.TokenizedLine(l) for l in change_log]
    assert tokenized[0].token==pc.LineTokens.HEADING1
    assert tokenized[1].token==pc.LineTokens.ITEM and tokenized[1].data=='Items1'
    assert tokenized[4].token==pc.LineTokens.HEADING2 and tokenized[4].data=='Internal'
    assert tokenized[6].token==pc.LineTokens.REFERENCE and tokenized[6].data.major == 1
    
def test_conversion():
    """Test that a basic conversion is possible, no references"""
    input = [
    "# 3.0.0",
    "        - Items1",
    "     - Items2",
    "- Items3",
    "## Internal",
    "- secret1",
    ]
    input = [pc.TokenizedLine(l) for l in input]
    expected = [
    "3.0.0",
    "- Items1",
    "- Items2",
    "- Items3",
    ]
    actual = pc.convert(input, external_only=True)
    assert expected == actual

def test_materials_gather():
    """Test that the materials change log is gathered and grouped correctly"""
    input_mat = [
    "# 1.1.0",
    "- 1.1.0",
    "## Internal",
    "- secret1",
    "# 1.1.3",
    "- 1.1.3",
    "# 2.1.3",
    "- 2.1.3",
    ] 
    input = [pc.TokenizedLine(l) for l in input_mat]
    expected = {
        "1.1":[pc.TokenizedLine('- 1.1.0'),pc.TokenizedLine('- 1.1.3')],
        "2.1":[pc.TokenizedLine('- 2.1.3')],
    }
    actual = pc.get_material_dicts(input)
    
    string_actual = pc.convert(actual["1.1"])
    string_expected = pc.convert(expected["1.1"])
    assert string_actual==string_expected

def test_reference_replace():
    """Check that reference substitution works"""
    input_mat = [
    "# 1.1.0",
    "- 1.1.0",
    "## Internal",
    "- secret1",
    "# 1.1.3",
    "- 1.1.3",
    "# 2.1.3",
    "- 2.1.3",
    ] 
    input_mat = [pc.TokenizedLine(l) for l in input_mat]
    mat_dict = pc.get_material_dicts(input_mat)
    
    input_ref = [
        "# 2.0",
        "> 2.1",
        "# 1.0",
        "> 1.1",
        "> 2.1",
    ]
    input_ref = [pc.TokenizedLine(l) for l in input_ref]
    string_actual = pc.convert(input_ref, mat_dict)
    
    string_expected = [
        "2.0",
        "Materials updates 2.1:",
        "- 2.1.3",
        "",
        "1.0",
        "Materials updates 1.1:",
        "- 1.1.0",
        "- 1.1.3",
        "Materials updates 2.1:",
        "- 2.1.3",
    ]
    assert string_actual==string_expected