| 1 | """Reddit comment quotes must name the topic, or the section's entity in a comparison.""" |
| 2 | |
| 3 | from lib import render, schema |
| 4 | |
| 5 | |
| 6 | def _comment(text, *, depth=0, score=50, author="someone"): |
| 7 | return {"author": author, "score": score, "depth": depth, "excerpt": text} |
| 8 | |
| 9 | |
| 10 | def _candidate(title, comments, url="https://www.reddit.com/r/x/comments/abc/t/"): |
| 11 | item = schema.SourceItem( |
| 12 | item_id=f"i-{url}", source="reddit", title=title, body="", url=url, |
| 13 | author="op", container="x", engagement={"score": 500}, |
| 14 | metadata={"top_comments": comments}, |
| 15 | ) |
| 16 | cand = schema.Candidate( |
| 17 | candidate_id=f"c-{url}", item_id=item.item_id, source="reddit", title=title, |
| 18 | url=url, snippet="", subquery_labels=["q"], native_ranks={"reddit": 1}, |
| 19 | local_relevance=0.8, freshness=50, engagement=0, source_quality=0.5, |
| 20 | rrf_score=0.01, source_items=[item], |
| 21 | ) |
| 22 | cand.final_score = 50.0 |
| 23 | return cand |
| 24 | |
| 25 | |
| 26 | def _report(topic, *cands): |
| 27 | return schema.Report( |
| 28 | topic=topic, range_from="2026-10-11", range_to="2026-11-10", |
| 29 | generated_at="2026-11-10T00:00:00Z", |
| 30 | provider_runtime=schema.ProviderRuntime( |
| 31 | reasoning_provider="t", planner_model="t", rerank_model="t"), |
| 32 | query_plan=schema.QueryPlan( |
| 33 | intent="general", freshness_mode="balanced_recent", cluster_mode="debate", |
| 34 | raw_topic=topic, subqueries=[], source_weights={}), |
| 35 | ranked_candidates=list(cands), clusters=[], items_by_source={}, |
| 36 | errors_by_source={}, |
| 37 | ) |
| 38 | |
| 39 | |
| 40 | def _text(report): |
| 41 | return "\n".join(render._render_top_comments(report)) |
| 42 | |
| 43 | |
| 44 | def test_off_topic_top_comment_skipped_next_grounded_quoted(): |
| 45 | cand = _candidate("Ghostty release notes", [ |
| 46 | _comment("completely unrelated chatter about lunch plans", depth=2, score=900), |
| 47 | _comment("ghostty finally fixed the font rendering bug", depth=2, score=80), |
| 48 | ]) |
| 49 | other = _candidate("Ghostty tips", [ |
| 50 | _comment("ghostty config tips that saved me hours", depth=2)], |
| 51 | url="https://www.reddit.com/r/x/comments/def/t/") |
| 52 | out = _text(_report("ghostty", cand, other)) |
| 53 | assert "lunch plans" not in out |
| 54 | assert "font rendering bug" in out |
| 55 | |
| 56 | |
| 57 | def test_short_top_level_reply_under_on_topic_post_qualifies(): |
| 58 | cand = _candidate("Ghostty 2.0 is out", [_comment("this is huge", depth=0)]) |
| 59 | other = _candidate("Ghostty tips", [ |
| 60 | _comment("ghostty config tips that saved me hours", depth=2)], |
| 61 | url="https://www.reddit.com/r/x/comments/def/t/") |
| 62 | assert "this is huge" in _text(_report("ghostty", cand, other)) |
| 63 | |
| 64 | |
| 65 | def test_deep_reply_not_naming_topic_skipped(): |
| 66 | cand = _candidate("Ghostty 2.0 is out", [ |
| 67 | _comment("this is huge for everyone involved", depth=3), |
| 68 | _comment("ghostty startup time is great now", depth=3)]) |
| 69 | other = _candidate("Ghostty tips", [ |
| 70 | _comment("ghostty config tips that saved me hours", depth=2)], |
| 71 | url="https://www.reddit.com/r/x/comments/def/t/") |
| 72 | out = _text(_report("ghostty", cand, other)) |
| 73 | assert "huge for everyone" not in out |
| 74 | assert "startup time" in out |
| 75 | |
| 76 | |
| 77 | def test_top_level_reply_under_off_topic_post_needs_grounding(): |
| 78 | cand = _candidate("Best pizza places", [_comment("this is huge honestly", depth=0)]) |
| 79 | other = _candidate("Ghostty tips", [ |
| 80 | _comment("ghostty config tips that saved me hours", depth=2)], |
| 81 | url="https://www.reddit.com/r/x/comments/def/t/") |
| 82 | assert "this is huge honestly" not in _text(_report("ghostty", cand, other)) |
| 83 | |
| 84 | |
| 85 | def test_comparison_has_no_reply_exemption_and_tags_entity(): |
| 86 | cand = _candidate("Cursor vs Windsurf", [ |
| 87 | _comment("windsurf has better flow state handling", depth=0), |
| 88 | _comment("this is huge honestly", depth=0), |
| 89 | _comment("cursor tab completion wins for me", depth=1), |
| 90 | ]) |
| 91 | out = render._render_top_comments(_report("Cursor vs Windsurf", cand)) |
| 92 | joined = "\n".join(out) |
| 93 | assert "this is huge honestly" not in joined |
| 94 | wind = next(line for line in out if "windsurf has better" in line) |
| 95 | cur = next(line for line in out if "cursor tab completion" in line) |
| 96 | assert "windsurf" in wind.split(" (about ")[-1].lower() |
| 97 | assert "cursor" not in wind.split(" (about ")[-1].lower() |
| 98 | assert "cursor" in cur.split(" (about ")[-1].lower() |
| 99 | |
| 100 | |
| 101 | def test_single_topic_lines_carry_no_entity_tag(): |
| 102 | cand = _candidate("Ghostty 2.0", [_comment("ghostty is fast", depth=0)]) |
| 103 | other = _candidate("Ghostty tips", [_comment("ghostty tips", depth=0)], |
| 104 | url="https://www.reddit.com/r/x/comments/def/t/") |
| 105 | assert " (about " not in _text(_report("ghostty", cand, other)) |
| 106 | |
| 107 | |
| 108 | def test_thread_with_no_quotable_comment_keeps_its_citation(): |
| 109 | cand = _candidate("Ghostty 2.0", [_comment("unrelated lunch chatter here", depth=3)]) |
| 110 | other = _candidate("Ghostty tips", [ |
| 111 | _comment("ghostty config tips that saved me hours", depth=2)], |
| 112 | url="https://www.reddit.com/r/x/comments/def/t/") |
| 113 | report = _report("ghostty", cand, other) |
| 114 | assert "lunch chatter" not in _text(report) |
| 115 | assert cand in report.ranked_candidates |
| 116 | |
| 117 | |
| 118 | def test_cjk_topic_grounds_like_entity_grounded(): |
| 119 | cand = _candidate("東京の天気について", [ |
| 120 | _comment("東京の天気は今日とても暑いです", depth=2), |
| 121 | _comment("totally unrelated sentence here", depth=2), |
| 122 | _comment("東京の天気予報はよく外れるよね", depth=2)]) |
| 123 | out = _text(_report("東京の天気", cand)) |
| 124 | assert "東京の天気は今日" in out |
| 125 | assert "totally unrelated" not in out |
| 126 |