File size: 7,070 Bytes
d15f07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Who supported and opposed a decision, from the record's speaker slugs (#1320).

The slugs below are the shapes the meeting analysis writes into event_decision.competing_views (the Tuscaloosa ones are
quoted from the record behind "Prohibiting firearm possession while under the influence"); the official rows are test
stand-ins for person_government.
"""

from __future__ import annotations

import pytest

from api.routes.decision_people import OfficialIndex, decision_people, is_school_board, person_for, vote_counts

OFFICIALS = OfficialIndex(
    [
        {"full_name": "John Faile", "title": "City Councilor", "jurisdiction": "Tuscaloosa Government", "state_code": "AL"},
        {"full_name": "Joseph Eatmon, Sr.", "title": "City Councilor", "jurisdiction": "Tuscaloosa Government", "state_code": "AL"},
    ]
)


def who(slug: str, *, place: str | None = "Tuscaloosa", school: bool = False):
    p = person_for(slug, jurisdiction_name=place, state_code="AL", school_board=school, officials=OFFICIALS)
    # The identity fields; the attribution_* provenance (#1386) has its own tests below.
    return None if p is None else {k: p[k] for k in ("name", "role", "kind")}


def test_official_matched_to_the_officials_record_gets_canonical_name_and_title():
    assert who("john_faile_councilor_tuscaloosa") == {"name": "John Faile", "role": "City Councilor", "kind": "official"}
    assert who("joseph_eatmon_sr_councilor_tuscaloosa")["name"] == "Joseph Eatmon, Sr."


def test_unmatched_official_title_keeps_the_slug_title():
    assert who("lee_busby_councilor_tuscaloosa") == {"name": "Lee Busby", "role": "Councilor", "kind": "official"}


def test_a_title_heard_as_address_stays_in_the_name_with_no_role():
    # "chief_sky_unknown": the role word is "unknown", so no role is claimed; "Chief" is how they were addressed.
    assert who("chief_sky_unknown_tuscaloosa") == {"name": "Chief Sky", "role": None, "kind": "official"}


def test_a_non_official_is_first_l_with_no_role():
    assert who("scott_holmes_unknown_tuscaloosa") == {"name": "Scott H.", "role": None, "kind": "resident"}
    assert who("caroline_pantskey_resident_oshkosh", place=None)["name"] == "Caroline P."


@pytest.mark.parametrize(
    "slug",
    [
        "ian_miller_student_north_reading_2501748955",  # a student: never shown
        "unknown_unknown_unknown_pickens_13227",  # no name
        "mrs_gonzalez_north_reading",  # an honorific and a surname is not a first name
        "robey_oshkosh",  # one word may be a surname
        "david_zumwalt_former_homeless_drug_addict_federal_way_5323515",  # a description runs into the name
        "lawrence_biloxi_2806220",  # no role word and no place to tell the name from
        "artist_murphy_entrepreneur_federal_way",  # no role word and three words: where the name ends is a guess
    ],
)
def test_left_out(slug):
    assert who(slug, place=None if "biloxi" in slug else "Federal Way") is None


def test_no_resident_at_a_school_board_but_officials_still_shown():
    assert who("scott_holmes_unknown_tuscaloosa", school=True) is None
    assert who("lee_busby_councilor_tuscaloosa", school=True)["name"] == "Lee Busby"
    assert is_school_board("Tuscaloosa City Board of Education") and is_school_board(None, jurisdiction_type="school_district")
    assert not is_school_board("Tuscaloosa", "City Council")


def test_decision_people_sides_order_and_opposition():
    cv = {
        "dominant_view": {
            "view_label": "Prohibit firearm possession while impaired",
            "held_by": ["scott_holmes_unknown_tuscaloosa", "chief_sky_unknown_tuscaloosa", "john_faile_councilor_tuscaloosa"],
        },
        "counter_views": [],
    }
    out = decision_people(cv, speakers=["john_faile_councilor_tuscaloosa", "kip_tyner_councilor_tuscaloosa"],
                          jurisdiction_name="Tuscaloosa", state_code="AL", school_board=False, officials=OFFICIALS)
    # Officials with a role, then officials without, then everyone else.
    assert [p["name"] for p in out["supported_by"]] == ["John Faile", "Chief Sky", "Scott H."]
    assert out["opposed_by"] == [] and out["opposition_recorded"] is False
    # A speaker already on a side is not repeated.
    assert [p["name"] for p in out["spoke_on_it"]] == ["Kip Tyner"]
    assert out["supported_view"] == "Prohibit firearm possession while impaired"


def test_counter_view_without_names_is_still_recorded_opposition():
    out = decision_people({"dominant_view": {"held_by": []}, "counter_views": [{"view_label": "No"}]},
                          jurisdiction_name="X", state_code="AL", school_board=False)
    assert out["opposition_recorded"] is True and out["opposed_by"] == []


def test_no_competing_views_claims_nothing():
    out = decision_people(None, jurisdiction_name="X", state_code="AL", school_board=False)
    assert out["opposition_recorded"] is None and out["supported_by"] == []


def test_vote_counts():
    assert vote_counts({"yes": 11, "no": 0}) == (11, 0)
    assert vote_counts({"yes": None, "no": None}) == (None, None)
    assert vote_counts(None) == (None, None)


# --- attribution provenance (#1386) ---------------------------------------------------------------------------------


def test_held_by_rules_match_the_dbt_seed() -> None:
    import csv
    from pathlib import Path

    from api.routes.decision_people import ATTRIBUTION_VERSION, HELD_BY_RULES

    seed = Path(__file__).resolve().parents[1] / "dbt_project" / "seeds" / "speaker_attribution_rules.csv"
    rows = {r["rule_key"]: r for r in csv.DictReader(seed.open())}
    for rule, (method, conf) in HELD_BY_RULES.items():
        assert rows[rule]["attribution_method"] == method
        assert float(rows[rule]["attribution_confidence"]) == conf
    assert ATTRIBUTION_VERSION == "speaker_attribution/1"


def test_resident_slug_is_ai_extraction_medium_and_evidence_has_no_surname() -> None:
    from api.routes.decision_people import person_for

    p = person_for("jane_doe_resident_springfield", jurisdiction_name="Springfield", state_code="IL", school_board=False)
    assert p and p["name"] == "Jane D."
    assert (p["attribution_method"], p["attribution_tier"]) == ("ai_extraction_held_by", "medium")
    assert "doe" not in p["attribution_evidence"].lower()


def test_official_title_slug_is_medium_and_roster_match_is_high() -> None:
    from api.routes.decision_people import OfficialIndex, person_for

    p = person_for("john_faile_councilor_tuscaloosa", jurisdiction_name="Tuscaloosa", state_code="AL", school_board=False)
    assert p and (p["attribution_method"], p["attribution_tier"]) == ("ai_extraction_held_by", "medium")
    idx = OfficialIndex([{"full_name": "John Faile", "title": "Councilor", "jurisdiction": "Tuscaloosa Government", "state_code": "AL"}])
    q = person_for("john_faile_councilor_tuscaloosa", jurisdiction_name="Tuscaloosa", state_code="AL", school_board=False, officials=idx)
    assert q and (q["attribution_method"], q["attribution_tier"], q["attribution_confidence"]) == ("official_roster_match", "high", 0.9)