"""Gold verification: cells parsed out of chunk markup, and a gold value that
disagrees with its cell dropped rather than corrected."""

from quber.playground.benchmark.verify_gold import occurrences, tagged_cells, verify

MARKUP = (
    '<table><tr><td id="t0-1-1">Revenue</td>'
    '<td id="t0-1-2"><b>$ (10,550)</b></td></tr>'
    '<tr><td id="t0-2-2">6.7 %</td></tr></table>'
)


def test_tagged_cells_strip_markup_and_keep_ids():
    cells = dict(tagged_cells(MARKUP))
    assert cells["t0-1-2"] == "$ (10,550)"
    assert cells["t0-2-2"] == "6.7 %"
    assert tagged_cells("") == []


def index_for(_slug):
    return {
        ("", "t0-1-2"): "$ (10,550)",
        ("chunkA", "t0-1-2"): "$ (10,550)",
        ("", "t0-2-2"): "6.7 %",
    }


def question(cid, gold):
    return {"question": "q", "gold_cell_id": cid, "gold_value": gold}


def test_matching_gold_is_kept_with_occurrence_count():
    kept, dropped = verify([{"slug": "s", "value_questions": [question("t0-1-2", "$ (10,550)")]}], index_for)
    assert dropped == []
    assert kept[0]["gold_value"] == "$ (10,550)"
    assert kept[0]["occurrences_in_document"] == 1


def test_disagreeing_gold_is_dropped_not_corrected():
    kept, dropped = verify([{"slug": "s", "value_questions": [question("t0-1-2", "$ (10,551)")]}], index_for)
    assert kept == []
    assert "prints" in dropped[0]["why"]


def test_missing_cell_is_dropped():
    kept, dropped = verify([{"slug": "s", "value_questions": [question("t9-9-9", "1")]}], index_for)
    assert kept == []
    assert "not found" in dropped[0]["why"]


def test_occurrences_counts_cells_not_chunks():
    assert occurrences(index_for(""), "$ (10,550)") == 1
