"""Verifier content-token rule — acronym awareness (#000053).

`_content_tokens` drops <4-char tokens *except* all-caps 2-3-char
acronyms (CPU/GPU/DNA/FBI/USB…). Pre-#000053 it dropped every short
token, so a CPU/GPU claim cited to a "CPU foo" / "GPU bar" article
tripped TITLE_MISMATCH spuriously (the shared "CPU" token didn't
register on either side).
"""

from __future__ import annotations

from arborist.qa.evidence import _content_tokens
from arborist.qa.keys import _VERIFIER_POLICY_FIELDS, verifier_policy_hash
from arborist.qa.runner import DEFAULT_POLICY
from arborist.qa.query import DEFAULT_QUERY_POLICY
from arborist.qa.verify import _claim_title_overlap


def test_uppercase_acronym_survives_short_token_filter():
    toks = _content_tokens("A CPU, or central processing unit, is hardware.")
    assert "cpu" in toks
    assert "central" in toks and "processing" in toks


def test_lowercase_short_word_still_dropped():
    # only the *all-caps* form is rescued; a 3-char lowercase word stays out
    toks = _content_tokens("the cat sat and ran far")
    assert "cat" not in toks and "sat" not in toks and "ran" not in toks


def test_two_and_three_char_caps_only():
    toks = _content_tokens("GPU DNA FBI US AI ABCD running")
    assert {"gpu", "dna", "fbi", "us", "ai"} <= set(toks)
    assert "abcd" in toks  # 4 chars — passes the normal filter anyway
    assert "running" in toks


def test_punctuation_stripped_before_acronym_check():
    assert "cpu" in _content_tokens("(CPU). \"GPU,\"")
    assert "gpu" in _content_tokens("(CPU). \"GPU,\"")


def test_rule8_title_overlap_now_passes_on_shared_acronym():
    # the field case: a CPU claim cited to the "CPU design" article —
    # they share "CPU", which now counts as a content token.
    assert _claim_title_overlap(
        "A CPU is the central processing unit of a computer.", "CPU design"
    )


def test_rule8_still_rejects_when_no_overlap():
    assert not _claim_title_overlap(
        "A CPU is the central processing unit of a computer.",
        "Quantum chromodynamics",
    )


def test_content_token_rules_marker_is_in_policies_and_hash_field_set():
    assert DEFAULT_POLICY["content_token_rules"] == "v2-acronym-aware"
    assert DEFAULT_QUERY_POLICY["content_token_rules"] == "v2-acronym-aware"
    assert "content_token_rules" in _VERIFIER_POLICY_FIELDS


def test_verifier_policy_hash_tracks_content_token_rules():
    a = verifier_policy_hash(DEFAULT_POLICY)
    b = verifier_policy_hash(dict(DEFAULT_POLICY, content_token_rules="v1"))
    assert a != b
