返回 last30days-skill
test_rerank_fun.py
根目录 / tests / test_rerank_fun.py
1 """Tests for the fun judge heuristic fallback in rerank.py."""
2
3 import pytest
4
5 from lib import schema
6 from lib.rerank import _apply_single_fun_fallback, _extract_comment_text
7
8
9 def _make_candidate(
10 title: str = "Some Title",
11 snippet: str = "",
12 engagement: float | None = 0.0,
13 top_comments: list[dict] | None = None,
14 ) -> schema.Candidate:
15 """Build a minimal Candidate with optional source_items carrying top_comments."""
16 source_items = []
17 if top_comments is not None:
18 source_items.append(
19 schema.SourceItem(
20 item_id="si-1",
21 source="reddit",
22 title=title,
23 body="",
24 url="https://reddit.com/r/test/1",
25 metadata={"top_comments": top_comments},
26 )
27 )
28 return schema.Candidate(
29 candidate_id="c-1",
30 item_id="i-1",
31 source="reddit",
32 title=title,
33 url="https://reddit.com/r/test/1",
34 snippet=snippet,
35 subquery_labels=["q1"],
36 native_ranks={"reddit": 1},
37 local_relevance=0.5,
38 freshness=50,
39 engagement=engagement,
40 source_quality=0.5,
41 rrf_score=0.01,
42 source_items=source_items,
43 )
44
45
46 class TestFunFallbackCommentText:
47 """Heuristic fallback reads comment text, not just title+snippet."""
48
49 def test_comment_with_lmao_gets_marker_bonus(self):
50 """A candidate with 'lmao' in a top_comment should get the marker bonus."""
51 candidate = _make_candidate(
52 title="Boring press conference recap",
53 snippet="Coach talked about the game plan.",
54 top_comments=[{"body": "lmao"}],
55 )
56 control = _make_candidate(
57 title="Boring press conference recap",
58 snippet="Coach talked about the game plan.",
59 top_comments=[{"body": "nope"}],
60 )
61 _apply_single_fun_fallback(candidate)
62 _apply_single_fun_fallback(control)
63 assert candidate.fun_score is not None
64 assert control.fun_score is not None
65 assert candidate.fun_score - control.fun_score == pytest.approx(10.0)
66 assert candidate.fun_explanation == "heuristic-fallback"
67
68 def test_short_punchy_comment_higher_shortness(self):
69 """A candidate with a short punchy comment should score higher shortness
70 bonus compared to one with a very long title and snippet."""
71 short_candidate = _make_candidate(
72 title="Hot dogs",
73 snippet="",
74 top_comments=[{"body": "bro what"}],
75 )
76 long_candidate = _make_candidate(
77 title="A very long and detailed analysis of the upcoming season with comprehensive breakdown of every roster move and coaching decision that happened over the past thirty days",
78 snippet="This extensive report covers all aspects of the team performance including advanced metrics and historical comparisons going back several decades.",
79 top_comments=[{"body": "bro what"}],
80 )
81 _apply_single_fun_fallback(short_candidate)
82 _apply_single_fun_fallback(long_candidate)
83 # Both get marker bonus from "bro", but short one gets higher shortness bonus
84 assert short_candidate.fun_score > long_candidate.fun_score
85
86 def test_no_comments_falls_back_to_title_snippet(self):
87 """A candidate with no source_items/comments still scores based on title+snippet."""
88 candidate = _make_candidate(
89 title="This is hilarious content",
90 snippet="Very funny stuff",
91 top_comments=None, # no source_items at all
92 )
93 _apply_single_fun_fallback(candidate)
94 assert candidate.fun_score is not None
95 assert candidate.fun_score >= 10.0 # marker bonus from "hilarious"
96 assert candidate.fun_explanation == "heuristic-fallback"
97
98 def test_empty_comment_bodies_no_crash(self):
99 """Candidates with empty comment bodies should not crash."""
100 candidate = _make_candidate(
101 title="Normal title",
102 snippet="Normal snippet",
103 top_comments=[
104 {"body": ""},
105 {"body": None},
106 {},
107 {"body": "actual comment"},
108 ],
109 )
110 _apply_single_fun_fallback(candidate)
111 assert candidate.fun_score is not None
112 assert candidate.fun_explanation == "heuristic-fallback"
113
114
115 class TestExtractCommentText:
116 """Verify _extract_comment_text handles edge cases."""
117
118 def test_extracts_from_top_comments(self):
119 candidate = _make_candidate(
120 top_comments=[{"body": "first comment"}, {"body": "second comment"}],
121 )
122 text = _extract_comment_text(candidate)
123 assert "first comment" in text
124 assert "second comment" in text
125
126 def test_empty_source_items(self):
127 candidate = _make_candidate(top_comments=None)
128 text = _extract_comment_text(candidate)
129 assert text == ""
130
130 lines PYTHON