File size: 9,288 Bytes
7302343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""Round-trip tests for the Postgres repository layer.

Auto-skips when SKIP_DB_TESTS=true (CI runs the unit suite without docker).
Locally, `make services-up` + `make test` exercises these against pg:16.
"""

from __future__ import annotations

import os
import uuid
from typing import TYPE_CHECKING

import pytest
import pytest_asyncio

if TYPE_CHECKING:
    from collections.abc import AsyncIterator

    from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker
else:
    from sqlalchemy.ext.asyncio import AsyncEngine  # noqa: TC002 — runtime fixture param

from api.config import get_settings
from store.connections import close_postgres, open_postgres
from store.postgres import (
    append_audit,
    append_evidence,
    ensure_subreddit_profile,
    finalize_investigation,
    get_investigation_by_correlation,
    get_subreddit_profile,
    get_user_memory,
    make_sessionmaker,
    record_feedback,
    start_investigation,
    upsert_user_memory,
    with_session,
)
from store.types import (
    EvidenceRowInput,
    FeedbackInput,
    FinalizeInvestigationInput,
    StartInvestigationInput,
)

pytestmark = pytest.mark.skipif(
    os.getenv("SKIP_DB_TESTS", "false").lower() in ("true", "1", "yes"),
    reason="SKIP_DB_TESTS=true (CI without docker services)",
)


@pytest_asyncio.fixture
async def engine() -> AsyncIterator[AsyncEngine]:
    get_settings.cache_clear()
    eng = await open_postgres(get_settings())
    try:
        yield eng
    finally:
        await close_postgres(eng)


@pytest.fixture
def sessions(engine: AsyncEngine) -> async_sessionmaker[AsyncSession]:
    return make_sessionmaker(engine)


def _sub_id() -> str:
    """Unique per-test subreddit_id so tests don't trample each other's data."""
    return f"t5_{uuid.uuid4().hex[:10]}"


@pytest.mark.asyncio
async def test_ensure_subreddit_profile_is_idempotent(
    sessions: async_sessionmaker[AsyncSession],
) -> None:
    sub = _sub_id()
    async with with_session(sessions) as s:
        a = await ensure_subreddit_profile(s, subreddit_id=sub, name="test-sub")
    async with with_session(sessions) as s:
        b = await ensure_subreddit_profile(s, subreddit_id=sub, name="test-sub-renamed")
    assert a.subreddit_id == b.subreddit_id == sub
    # on_conflict_do_nothing → name should not have updated
    assert b.name == "test-sub"


@pytest.mark.asyncio
async def test_user_memory_upsert_counters(
    sessions: async_sessionmaker[AsyncSession],
) -> None:
    sub, user = _sub_id(), "t2_testuser"
    async with with_session(sessions) as s:
        await ensure_subreddit_profile(s, subreddit_id=sub, name="x")
        first = await upsert_user_memory(s, subreddit_id=sub, user_id=user)
    assert first.prior_violations == 0
    assert first.risk_tier == "new"

    async with with_session(sessions) as s:
        bumped = await upsert_user_memory(
            s,
            subreddit_id=sub,
            user_id=user,
            risk_tier="watched",
            prior_violations_delta=2,
            prior_approvals_delta=1,
        )
    assert bumped.prior_violations == 2
    assert bumped.prior_approvals == 1
    assert bumped.risk_tier == "watched"

    async with with_session(sessions) as s:
        read = await get_user_memory(s, subreddit_id=sub, user_id=user)
    assert read is not None
    assert read.prior_violations == 2


@pytest.mark.asyncio
async def test_investigation_lifecycle(
    sessions: async_sessionmaker[AsyncSession],
) -> None:
    sub = _sub_id()
    corr = f"inv-{uuid.uuid4().hex[:8]}"

    async with with_session(sessions) as s:
        await ensure_subreddit_profile(s, subreddit_id=sub, name="lifecycle")
        inv = await start_investigation(
            s,
            input_=StartInvestigationInput(
                correlation_id=corr,
                subreddit_id=sub,
                target_kind="post",
                target_id="t3_x",
                target_body="hi all",
                target_author_id="t2_a",
                tier="DEEP",
            ),
        )
        for ev_id, tool, summary in [
            ("ev-1", "policy_match", "matched rule 2"),
            ("ev-2", "user_history", "3 prior removals"),
        ]:
            await append_evidence(
                s,
                investigation=inv,
                subreddit_id=sub,
                evidence=EvidenceRowInput(evidence_id=ev_id, tool=tool, summary=summary),
            )
        await finalize_investigation(
            s,
            correlation_id=corr,
            subreddit_id=sub,
            verdict=FinalizeInvestigationInput(
                risk_tier="HIGH",
                recommendation="REMOVE",
                calibrated_confidence=0.92,
                rationale="author has prior removals [ev-2]; matches [ev-1]",
                confidence_breakdown={
                    "llm_self_report": 0.95,
                    "evidence_convergence": 0.88,
                    "subreddit_accuracy": 0.87,
                    "rule_match_strength": 0.96,
                },
                model_reasoner="gemini-2.5-pro",
                model_summarizer="gemini-2.5-flash",
                cost_usd=0.018,
                latency_ms=1432,
            ),
        )

    async with with_session(sessions) as s:
        loaded = await get_investigation_by_correlation(
            s, correlation_id=corr, subreddit_id=sub
        )
    assert loaded is not None
    assert loaded.status == "completed"
    assert loaded.recommendation == "REMOVE"
    assert loaded.calibrated_confidence == 0.92
    assert loaded.completed_at is not None
    assert len(loaded.evidence) == 2


@pytest.mark.asyncio
async def test_finalize_rejects_subreddit_mismatch(
    sessions: async_sessionmaker[AsyncSession],
) -> None:
    sub_a, sub_b = _sub_id(), _sub_id()
    corr = f"inv-{uuid.uuid4().hex[:8]}"
    async with with_session(sessions) as s:
        await ensure_subreddit_profile(s, subreddit_id=sub_a, name="a")
        await ensure_subreddit_profile(s, subreddit_id=sub_b, name="b")
        await start_investigation(
            s,
            input_=StartInvestigationInput(
                correlation_id=corr,
                subreddit_id=sub_a,
                target_kind="comment",
                target_id="t1_x",
                tier="STANDARD",
            ),
        )

    with pytest.raises(LookupError):
        async with with_session(sessions) as s:
            await finalize_investigation(
                s,
                correlation_id=corr,
                subreddit_id=sub_b,  # WRONG subreddit_id — must not match
                verdict=FinalizeInvestigationInput(
                    risk_tier="LOW",
                    recommendation="NO_RECOMMENDATION",
                    calibrated_confidence=0.3,
                    rationale="",
                    confidence_breakdown={},
                    model_reasoner="",
                    model_summarizer="",
                    cost_usd=0.0,
                    latency_ms=0,
                ),
            )


@pytest.mark.asyncio
async def test_record_feedback_and_audit(
    sessions: async_sessionmaker[AsyncSession],
) -> None:
    sub = _sub_id()
    corr = f"inv-{uuid.uuid4().hex[:8]}"
    async with with_session(sessions) as s:
        await ensure_subreddit_profile(s, subreddit_id=sub, name="feedback")
        await record_feedback(
            s,
            feedback=FeedbackInput(
                correlation_id=corr,
                subreddit_id=sub,
                target_id="t1_x",
                mod_action="REMOVE",
                raw_action="removecomment",
                moderator_name="u/tester",
                source="reddit_native",
                aligned=True,
            ),
        )
        await append_audit(
            s,
            subreddit_id=sub,
            event_type="feedback.recorded",
            actor="u/tester",
            correlation_id=corr,
            detail={"aligned": True},
        )
    # No exception → both rows landed. We rely on the DB to validate enums.


@pytest.mark.asyncio
async def test_append_evidence_rejects_subreddit_mismatch(
    sessions: async_sessionmaker[AsyncSession],
) -> None:
    sub_a = _sub_id()
    async with with_session(sessions) as s:
        await ensure_subreddit_profile(s, subreddit_id=sub_a, name="a")
        inv = await start_investigation(
            s,
            input_=StartInvestigationInput(
                correlation_id=f"inv-{uuid.uuid4().hex[:8]}",
                subreddit_id=sub_a,
                target_kind="comment",
                target_id="t1_x",
                tier="STANDARD",
            ),
        )
        with pytest.raises(ValueError, match="subreddit_id mismatch"):
            await append_evidence(
                s,
                investigation=inv,
                subreddit_id="t5_wrong",
                evidence=EvidenceRowInput(evidence_id="ev-1", tool="policy_match"),
            )


@pytest.mark.asyncio
async def test_get_subreddit_profile_returns_none_for_unknown(
    sessions: async_sessionmaker[AsyncSession],
) -> None:
    async with with_session(sessions) as s:
        result = await get_subreddit_profile(s, subreddit_id=_sub_id())
    assert result is None