返回 last30days-skill
test_bird_topic_operators.py
根目录 / tests / test_bird_topic_operators.py
1 """The Bird lane must not forward X advanced-search operators from a topic.
2
3 ``x_api.build_keyword_query`` drops operator-shaped tokens for the xurl lane
4 (#1179). The bird lane never did, so a topic like 'since:2020 docker tips'
5 emitted a duplicate ``since:`` and 'from:someone' silently restricted the
6 search to one account instead of searching the words.
7 """
8
9 from unittest import mock
10
11 from lib import bird_x, subproc
12
13
14 def test_operator_tokens_never_reach_the_query():
15 built = bird_x.build_topic_query("from:someone topic", "2026-11-01", "2026-11-20")
16 assert "from:" not in built, f"a from: operator reached X: {built!r}"
17 assert "topic" in built
18
19
20 def test_topic_since_does_not_duplicate_the_window_filter():
21 built = bird_x.build_topic_query("since:2020 docker tips", "2026-11-01", "2026-11-20")
22 assert built.count("since:") == 1, (
23 f"a topic-supplied since: collided with the window filter: {built!r}"
24 )
25 assert "since:2026-11-01" in built
26 assert "docker" in built
27
28
29 def test_lang_and_leading_negation_are_dropped():
30 lang = bird_x.build_topic_query("lang:en rust tips", "2026-11-01")
31 assert "lang:" not in lang
32 assert "rust" in lang
33
34 negated = bird_x.build_topic_query("-spam crypto news", "2026-11-01")
35 assert "-spam" not in negated, f"leading negation reached X: {negated!r}"
36 assert "crypto" in negated
37
38
39 def test_bare_boolean_words_are_dropped():
40 built = bird_x.build_topic_query("python and rust", "2026-11-01")
41 assert " and " not in built, f"a bare boolean reached X: {built!r}"
42 assert "python" in built and "rust" in built
43
44
45 def test_trailing_colon_subject_survives_as_a_word():
46 """'Docker:' is a subject with punctuation, not an operator."""
47 built = bird_x.build_topic_query("Docker:", "2026-11-01")
48 assert "docker" in built.lower()
49 assert "docker:" not in built.lower()
50
51
52 def test_query_is_bounded_before_it_reaches_x():
53 long_topic = " ".join(f"word{i}" for i in range(120))
54 built = bird_x.build_topic_query(long_topic, "2026-11-01", "2026-11-20")
55 assert len(built) <= bird_x.MAX_QUERY_CHARS, (
56 f"query exceeded X's limit ({len(built)} > {bird_x.MAX_QUERY_CHARS})"
57 )
58 # The window filter must survive the trim; it is not optional.
59 assert "since:2026-11-01" in built
60
61
62 def test_trim_never_leaves_an_unterminated_phrase():
63 """Trimming a long quoted topic must not cut the closing quote.
64
65 An orphan quote is read by X as an unterminated phrase and matches nothing,
66 so the length bound has to degrade to bare tokens instead.
67 """
68 phrase = " ".join(f"w{i}" for i in range(200))
69 built = bird_x.build_topic_query(f'"{phrase}" extra', "2026-11-01", "2026-11-20")
70 assert len(built) <= bird_x.MAX_QUERY_CHARS, f"over budget: {len(built)}"
71 assert built.count('"') % 2 == 0, f"unterminated phrase reached X: {built!r}"
72 assert "since:2026-11-01" in built
73
74
75 def _search_queries(topic):
76 with mock.patch.object(
77 bird_x.subproc, "run_with_timeout", autospec=True,
78 return_value=subproc.SubprocResult(returncode=0, stdout='{"items": []}', stderr=""),
79 ) as run:
80 bird_x.search_x(topic, "2026-11-01", "2026-11-20")
81 for call in run.call_args_list:
82 assert call.args[0][-2] == "--"
83 return [call.args[0][-1] for call in run.call_args_list]
84
85
86 def test_topic_operators_are_removed_before_selecting_retry_keywords():
87 queries = _search_queries("since:2012 from:noise lang:fr -spam multi-agent autonomous mission")
88 filters = "since:2026-11-01 until:2026-11-21"
89 assert queries == [
90 f"multi-agent autonomous mission {filters}",
91 f'("multi-agent") {filters}',
92 f"multi-agent autonomous {filters}",
93 f"multi-agent {filters}",
94 ]
95
96
97 def test_quoted_name_keeps_its_conjunction_in_primary_and_retries():
98 queries = _search_queries('"Research and Development" quantum topology simulations')
99 assert len(queries) >= 2
100 for query in queries:
101 assert query.lower().startswith(('"research and development" ', '("research and development") '))
102
103
104 def test_retries_keep_punctuated_title_even_with_an_unquoted_compound():
105 queries = _search_queries('"Mission: Impossible" multi-agent fallout')
106 filters = "since:2026-11-01 until:2026-11-21"
107 assert queries == [
108 f'"Mission: Impossible" multi-agent fallout {filters}',
109 f'("Mission: Impossible") {filters}',
110 f'"Mission: Impossible" multi-agent {filters}',
111 f'"Mission: Impossible" {filters}',
112 ]
113
114
115 def test_retries_prioritize_late_quoted_title_when_shortening():
116 queries = _search_queries('decentralization infrastructure economics "Dune" stunts')
117 filters = "since:2026-11-01 until:2026-11-21"
118 assert queries == [
119 f'decentralization infrastructure economics "Dune" stunts {filters}',
120 f'("Dune") {filters}',
121 f'"Dune" decentralization {filters}',
122 f'"Dune" decentralization {filters}',
123 ]
124
125
126 def test_quoted_phrase_keeps_literal_grouping_and_operator_shaped_words():
127 phrase = '"From: quote (and) -dash [literal]"'
128 query = bird_x.build_topic_query(f"{phrase} (from:ignored product)", "2026-11-01")
129 assert query == f"{phrase} product since:2026-11-01"
130
131
132 def test_or_retry_drops_oversized_compounds_without_breaking_group_or_window():
133 topic = " ".join(["Alpha"] * 112) + " multi-agent"
134 queries = _search_queries(topic)
135 assert len(queries) == 4
136 assert queries[1] == '("multi-agent") since:2026-11-01 until:2026-11-21'
137 assert all(len(query) <= bird_x.MAX_QUERY_CHARS for query in queries)
138
139
140 def test_operator_only_topic_never_starts_a_window_only_search():
141 assert _search_queries("since:2012 lang:en -spam OR") == []
142
143
144 def test_oversized_late_phrase_does_not_send_an_empty_retry():
145 phrase = "x" * bird_x.MAX_QUERY_CHARS
146 queries = _search_queries(f'decentralization economics "{phrase}"')
147 assert queries == ["decentralization economics since:2026-11-01 until:2026-11-21"]
148
148 lines PYTHON