File size: 9,623 Bytes
a8fb296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
"""Topics API (#500): curated "Data centers" topic alongside the CivicSearch taxonomy.

The DB layer is mocked: ``get_db_pool`` returns a fake pool that records SQL and
params and returns canned rows.
"""

from __future__ import annotations

import asyncio
from datetime import date
from typing import Any

import httpx
import pytest
from fastapi import FastAPI

from api.routes import search, topics


class _Conn:
    def __init__(self, rows=None, total: int = 0, row=None):
        self.rows = rows or []
        self.total = total
        self.row = row
        self.calls: list[tuple[str, tuple[Any, ...]]] = []

    async def fetch(self, sql: str, *params: Any):
        self.calls.append((sql, params))
        return self.rows

    async def fetchval(self, sql: str, *params: Any):
        self.calls.append((sql, params))
        return self.total

    async def fetchrow(self, sql: str, *params: Any):
        self.calls.append((sql, params))
        return self.row


class _Acquire:
    def __init__(self, conn):
        self.conn = conn

    async def __aenter__(self):
        return self.conn

    async def __aexit__(self, *_):
        return None


class _Pool:
    def __init__(self, conn):
        self.conn = conn

    def acquire(self):
        return _Acquire(self.conn)


@pytest.fixture
def get(monkeypatch):
    def _get(conn: _Conn, path: str, params: dict[str, Any] | None = None) -> httpx.Response:
        async def pool():
            return _Pool(conn)

        monkeypatch.setattr(topics, "get_db_pool", pool)
        app = FastAPI()
        app.include_router(topics.router)

        async def go():
            async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://t") as c:
                return await c.get(path, params=params)

        return asyncio.run(go())

    return _get


def test_topic_list_carries_source_and_scoped_meeting_count(get):
    conn = _Conn(rows=[
        dict(topic_id=51, name="Education, schools, students", query_id="education",
             keyword_stats='["school board"]', transcript_occurrences=63046,
             source="civicsearch", meeting_count=14387),
        dict(topic_id=100001, name="Data centers", query_id="data-centers",
             keyword_stats='["data center", "hyperscale"]', transcript_occurrences=0,
             source="curated", meeting_count=81),
    ])
    r = get(conn, "/api/topics", {"state": "ga"})
    assert r.status_code == 200
    body = r.json()
    assert body[1]["source"] == "curated"
    assert body[1]["meeting_count"] == 81
    assert body[1]["keywords"] == ["data center", "hyperscale"]
    sql, params = conn.calls[0]
    # state is normalized and drives the per-state count + existence filter; no city
    assert params == (None, "GA", None, None)
    assert "browse_entity_state_transcript_count" in sql
    assert "to_jsonb(t) ->> 'source'" in sql


def test_every_topic_gets_the_scoped_count_not_only_curated_ones(get):
    """#504: under a state the CivicSearch topics carry that state's meeting count too."""
    conn = _Conn(rows=[
        dict(topic_id=51, name="Education, schools, students", query_id="education",
             keyword_stats="[]", transcript_occurrences=63046, source="civicsearch", meeting_count=193),
    ])
    body = get(conn, "/api/topics", {"state": "al"}).json()
    assert body[0]["source"] == "civicsearch" and body[0]["meeting_count"] == 193
    sql, params = conn.calls[0]
    assert params == (None, "AL", None, None)
    count_expr = sql[sql.index("CASE"):sql.index("END AS meeting_count")]
    assert "browse_entity_state_transcript_count" in count_expr
    assert "browse_transcript_count n" in count_expr
    assert "source" not in count_expr and "curated" not in count_expr  # no per-source branch


def test_topic_list_city_grain_uses_place_transcript_counts(get):
    """#590: with state+city, meeting_count comes from browse_entity_place_transcript_count."""
    conn = _Conn(rows=[
        dict(topic_id=46, name="Budget & finance", query_id="budget",
             keyword_stats="[]", transcript_occurrences=100, source="civicsearch", meeting_count=101),
    ])
    body = get(conn, "/api/topics", {"state": "ga", "city": "Atlanta"}).json()
    assert body[0]["meeting_count"] == 101
    sql, params = conn.calls[0]
    assert params == (None, "GA", "%Atlanta%", "Atlanta")
    assert "browse_entity_place_transcript_count" in sql
    assert "jurisdiction_name ILIKE $3" in sql


def test_topic_list_city_count_picks_one_place(get):
    """#1026: '%Tuscaloosa%' also matches 'Tuscaloosa City School District'. The scalar count subquery must return
    one row (else Postgres errors and the list 503s): it prefers the exact city name, then the most meetings."""
    conn = _Conn(rows=[])
    get(conn, "/api/topics", {"state": "AL", "city": "Tuscaloosa"})
    sql, params = conn.calls[0]
    assert params == (None, "AL", "%Tuscaloosa%", "Tuscaloosa")
    count_expr = sql.split("AS meeting_count")[0]
    assert "ORDER BY (lower(p.jurisdiction_name) = lower($4::text)) DESC" in count_expr
    assert "LIMIT 1" in count_expr


def test_topic_list_query_failure_is_503_not_an_empty_list(get):
    class _Broken(_Conn):
        async def fetch(self, sql: str, *params: Any):
            raise RuntimeError("relation does not exist")

    resp = get(_Broken(), "/api/topics")
    assert resp.status_code == 503
    assert resp.json()["detail"] == "Topics are unavailable right now."


def test_topic_list_source_defaults_when_column_missing(get):
    conn = _Conn(rows=[dict(topic_id=1, name="X", query_id=None, keyword_stats=None,
                            transcript_occurrences=3, source=None, meeting_count=None)])
    body = get(conn, "/api/topics").json()
    assert body[0]["source"] == "civicsearch"
    assert body[0]["meeting_count"] is None


def test_topic_meetings_returns_highlighted_passages_in_scope(get):
    conn = _Conn(total=2, rows=[dict(
        video_id="abc123", event_title="County Commission", event_date=date(2026, 9, 2),
        state_code="GA", state="Georgia", city=None, jurisdiction_name="Coweta County",
        video_url="https://www.youtube.com/watch?v=abc123",
        evidence_passage="the proposed <mark>data</mark> <mark>center</mark> on Highway 16",
        n_keyword_hits=2,
    )])
    r = get(conn, "/api/topics/100001/meetings", {"state": "ga", "city": "Coweta", "limit": 6})
    assert r.status_code == 200
    body = r.json()
    assert body["total"] == 2
    assert body["topic_id"] == 100001
    item = body["items"][0]
    assert item["date"] == "2026-09-02"
    assert "<mark>data</mark>" in item["excerpt"]
    count_sql, count_params = conn.calls[0]
    assert "topic_transcript_link" in count_sql
    assert count_params == (100001, "GA", "%Coweta%")
    _, list_params = conn.calls[1]
    assert list_params == (100001, "GA", "%Coweta%", 6, 0)


def test_topic_meetings_national_scope_passes_nulls(get):
    conn = _Conn(total=0, rows=[])
    body = get(conn, "/api/topics/51/meetings").json()
    assert body == {"topic_id": 51, "total": 0, "items": []}
    assert conn.calls[0][1] == (51, None, None)


def test_topic_meetings_query_failure_is_503_not_zero_meetings(get):
    class _Broken(_Conn):
        async def fetchval(self, sql: str, *params: Any):
            raise RuntimeError("relation does not exist")

    resp = get(_Broken(), "/api/topics/51/meetings")
    assert resp.status_code == 503
    assert resp.json()["detail"] == "Meetings for this topic are unavailable right now."


def test_curated_topic_tsquery_matches_phrases_not_loose_words():
    tq = search._curated_topic_tsquery(
        "Data centers",
        ["data center", "data centre", "data centers", "hyperscale", "server farm"],
    )
    assert tq == (
        "(data <-> center) | (data <-> centre) | (data <-> centers) | hyperscale | (server <-> farm)"
    )


def test_default_curated_topics_match_phrases_and_never_a_bare_flock():
    """The #808 seed rows: hyphens split into adjacent words, brand words stay phrases."""
    assert search._curated_topic_tsquery("Short-term rentals", ["short-term rental", "airbnb"]) == (
        "(short <-> term <-> rental) | airbnb"
    )
    tq = search._curated_topic_tsquery(
        "Flock and license plate readers", ["license plate reader", "flock safety", "flock camera", "alpr"]
    )
    assert tq == "(license <-> plate <-> reader) | (flock <-> safety) | (flock <-> camera) | alpr"
    assert "flock |" not in tq and not tq.endswith("flock")


def test_curated_topic_tsquery_is_injection_safe_and_handles_empty():
    assert search._curated_topic_tsquery("x", ["data' | !center & (farm"]) == (
        "(data <-> center <-> farm)"
    )
    assert search._curated_topic_tsquery("x", ["", "123"]) is None


def test_resolve_topic_tsquery_uses_phrases_for_curated(monkeypatch):
    conn = _Conn(row=dict(name="Data centers", keyword_stats='["data center", "hyperscale"]',
                          source="curated"))

    async def pool():
        return _Pool(conn)

    monkeypatch.setattr(search.search_postgres, "get_db_pool", pool)
    assert asyncio.run(search.resolve_topic_tsquery(100001)) == "(data <-> center) | hyperscale"


def test_resolve_topic_tsquery_keeps_word_or_for_civicsearch(monkeypatch):
    conn = _Conn(row=dict(name="Police matters", keyword_stats='["police chief"]',
                          source="civicsearch"))

    async def pool():
        return _Pool(conn)

    monkeypatch.setattr(search.search_postgres, "get_db_pool", pool)
    assert asyncio.run(search.resolve_topic_tsquery(66)) == "police | matters | chief"