import pytest
from context import (
    SimpleEditTypes,
    StructuredEditTypes,
    curr_html,
    full_diff_to_simple,
    prev_html,
)


def test_basics():
    # https://en.wikipedia.org/w/index.php?title=Alabama&diff=1339266138&oldid=1338993392
    diff = SimpleEditTypes("html", prev_html, curr_html, lang='en').get_diff()
    expected_changes = {
        # Economy + References
        'Section': {'change': 2},
        # Added new sentence to paragraph
        'Paragraph': {'change': 1},
        'Sentence': {'insert': 1},
        # splits percentages into two words -- not great but okay
        'Word': {'insert': 18},
        'Punctuation': {'insert': 7},
        'Whitespace': {'insert': 16},
        # 1 new citation that has an externallink
        'Citation': {'insert': 1},
        'Reference': {'insert': 1},
        'List': {'change': 1},
        'ExternalLink': {'insert': 1},
        # [needs update] tag added two maintenance categories
        'InlineCleanup': {'insert': 1},
        'Category': {'insert': 2},
        # [needs update] + publisher in new reference
        'TF-i': {'insert': 2},
        # added one to sentence; one in [needs update] tag
        'Wikilink': {'insert': 2},
        }
    assert diff == expected_changes

    full_diff = StructuredEditTypes("html", prev_html, curr_html, lang='en').get_diff()
    assert full_diff_to_simple(full_diff) == expected_changes

@pytest.mark.xfail(reason="Can't currently detect changes in Comments as they aren't tags so aren't caught by _tag_to_element")
def test_comments():
    diff = SimpleEditTypes("html", "", prev_html, lang='en').get_diff()
    expected_changes = {"insert": 2}
    assert diff.get("Comment") == expected_changes

    diff = StructuredEditTypes("html", "", prev_html, lang="en").get_diff()
    assert full_diff_to_simple(diff).get("Comment") == expected_changes

def test_empty_revision():
    diff = SimpleEditTypes("html", "", prev_html, lang='en').get_diff()

    # Elements w/ 0 counts:
    # * Math
    # * Media-audio
    # * Media-video
    # * Various TF-* elements

    expected_changes = {
        "Section": {"insert": 58},
        "Heading": {"insert": 57},
        "Category": {"insert": 66},
        "Wikilink": {"insert": 1750},
        "ExternalLink": {"insert": 527},
        "Infobox": {"insert": 1},
        "Table": {"insert": 17},  # 27 but 1 infobox, 1 messagebox, 8 wikitables
        "Wikitable": {"insert": 8},
        "Media-img": {"insert": 79},
        "Navigation": {"insert": 9},
        "Reference": {"insert": 280},
        "Citation": {"insert": 340},
        "List": {"insert": 74},  # 75 but one is a navbox
        "TF-b": {"insert": 13},
        "TF-code": {"insert": 3},
        "TF-i": {"insert": 265},
        "TF-sup": {"insert": 16},
        "TF-blockquote": {"insert": 1},
        "InlineCleanup": {"insert": 1},
        "Messagebox": {"insert": 1},
        "Note": {"insert": 25},
        # "Comment": {"insert": 2},
        "Paragraph": {"insert": 168},
        "Sentence": {"insert": 606},
        "Word": {"insert": 12161},
        "Punctuation": {"insert": 2278},
        "Whitespace": {"insert": 12137}
    }
    assert diff == expected_changes

    diff = StructuredEditTypes("html", "", prev_html, lang="en").get_diff()
    # whitespace counting can vary between wikitext and HTML but
    # 12137 -> 12109 is within reason.
    expected_changes["Whitespace"]["insert"] = 12109
    assert full_diff_to_simple(diff) == expected_changes
