返回 last30days-skill
test_github_qualifier_spam.py
根目录 / tests / test_github_qualifier_spam.py
1 """Qualifier-only GitHub topics must not embed unbounded raw topics.
2
3 Stream fanout calls ``search_github`` once per subquery. Before #954 the
4 error envelope used ``{topic!r}`` and the strip log used the full core, so
5 log volume, error-report volume, and doctor-hint size scaled with both
6 fanout and topic length.
7 """
8
9 from unittest.mock import patch
10
11 from lib import github
12
13 _FROM = "2026-07-01"
14 _TO = "2026-07-31"
15 # Wrapper text plus a ~120-char topic slice. The pre-fix error embedded
16 # the entire raw topic (thousands of chars on a pathological planner query).
17 _ERROR_BOUND = 300
18 _STRIP_LOG_BOUND = 400
19
20
21 def _long_qualifier_only_topic() -> str:
22 return "created:>2025-03-20 " + ("stars:>1 " * 400)
23
24
25 def test_qualifier_only_topic_returns_clean_empty_envelope_without_network():
26 """#953 supersedes the #954 error bound: a qualifier-only topic is a clean
27 no-results envelope (no error key), so there is nothing to spam."""
28 topic = _long_qualifier_only_topic()
29 with patch.object(github, "_resolve_token", return_value="t"), patch.object(
30 github, "_fetch_json"
31 ) as fetch:
32 result = github.search_github(topic, _FROM, _TO)
33 fetch.assert_not_called()
34 assert result["items"] == []
35 assert "error" not in result
36
37
38 def test_short_qualifier_only_topic_is_also_clean_no_results():
39 topic = "created:>2025-03-20"
40 with patch.object(github, "_resolve_token", return_value="t"), patch.object(
41 github, "_fetch_json"
42 ) as fetch:
43 result = github.search_github(topic, _FROM, _TO)
44 fetch.assert_not_called()
45 assert result["items"] == []
46 assert "error" not in result
47
48
49 def test_qualifier_only_logs_are_bounded_across_fanout():
50 topic = _long_qualifier_only_topic()
51 logs: list[str] = []
52 with patch.object(github, "_resolve_token", return_value="t"), patch.object(
53 github, "_fetch_json"
54 ) as fetch, patch.object(github, "_log", side_effect=lambda m: logs.append(m)):
55 for _ in range(5):
56 github.search_github(topic, _FROM, _TO)
57 fetch.assert_not_called()
58 assert logs
59 for msg in logs:
60 assert len(msg) < _ERROR_BOUND
61 assert topic not in msg
62
63
64 def test_mixed_topic_strip_log_is_bounded():
65 subject = "open source ai " * 200
66 topic = subject + "stars:>1000 created:>2025-03-20"
67 logs: list[str] = []
68 with patch.object(github, "_resolve_token", return_value="t"), patch.object(
69 github, "_fetch_json", return_value={"items": []}
70 ), patch.object(github, "_log", side_effect=lambda m: logs.append(m)):
71 github.search_github(topic, _FROM, _TO)
72 strip_logs = [m for m in logs if m.startswith("Stripped search qualifiers:")]
73 assert strip_logs
74 for msg in strip_logs:
75 assert len(msg) < _STRIP_LOG_BOUND
76 assert subject not in msg
77
77 lines PYTHON