| 1 | """Tests for query.py — shared query utilities.""" |
| 2 | |
| 3 | import unittest |
| 4 | |
| 5 | from lib.query import ( |
| 6 | NOISE_WORDS, |
| 7 | PREFIXES, |
| 8 | SOCIAL_NOISE, |
| 9 | VIRAL_NOISE, |
| 10 | extract_compound_terms, |
| 11 | extract_core_subject, |
| 12 | infer_query_intent, |
| 13 | ) |
| 14 | |
| 15 | |
| 16 | class TestExtractCoreSubject(unittest.TestCase): |
| 17 | """Tests for extract_core_subject() with default noise set.""" |
| 18 | |
| 19 | def test_strips_what_are_prefix(self): |
| 20 | self.assertEqual(extract_core_subject("what are the best AI tools"), "ai") |
| 21 | |
| 22 | def test_strips_how_to_prefix(self): |
| 23 | self.assertEqual(extract_core_subject("how to use cursor IDE"), "cursor ide") |
| 24 | |
| 25 | def test_strips_what_do_people_think(self): |
| 26 | result = extract_core_subject("what do people think about React Server Components") |
| 27 | self.assertEqual(result, "react server components") |
| 28 | |
| 29 | def test_preserves_product_name(self): |
| 30 | self.assertEqual(extract_core_subject("cursor IDE"), "cursor ide") |
| 31 | |
| 32 | def test_strips_trailing_punctuation(self): |
| 33 | result = extract_core_subject("what is Claude?") |
| 34 | self.assertFalse(result.endswith("?")) |
| 35 | |
| 36 | def test_empty_string(self): |
| 37 | self.assertEqual(extract_core_subject(""), "") |
| 38 | |
| 39 | def test_second_person_use_prefixes_are_stripped(self): |
| 40 | self.assertEqual(extract_core_subject("how do you use Cursor"), "cursor") |
| 41 | self.assertEqual(extract_core_subject("how can I use Cursor"), "cursor") |
| 42 | |
| 43 | def test_bare_how_do_you_and_how_can_i_are_stripped(self): |
| 44 | self.assertEqual(extract_core_subject("how can I debug docker"), "debug docker") |
| 45 | self.assertEqual(extract_core_subject("how do you deploy Next.js"), "deploy next.js") |
| 46 | |
| 47 | def test_prefixes_are_ordered_longest_first(self): |
| 48 | """Prefix stripping stops after the first match, so a shorter prefix |
| 49 | listed before a longer one it leads would shadow it.""" |
| 50 | for i, shorter in enumerate(PREFIXES): |
| 51 | for longer in PREFIXES[i + 1:]: |
| 52 | self.assertFalse( |
| 53 | longer.startswith(shorter + " "), |
| 54 | f"{shorter!r} is listed before {longer!r} and would shadow it", |
| 55 | ) |
| 56 | |
| 57 | def test_all_noise_returns_original(self): |
| 58 | # When all words are noise, fall back to original text |
| 59 | result = extract_core_subject("best latest new") |
| 60 | self.assertTrue(len(result) > 0) |
| 61 | |
| 62 | def test_only_first_prefix_stripped(self): |
| 63 | result = extract_core_subject("how to use how to debug", noise=frozenset()) |
| 64 | self.assertEqual(result, "how to debug") |
| 65 | |
| 66 | |
| 67 | class TestMaxWords(unittest.TestCase): |
| 68 | """Tests for max_words parameter.""" |
| 69 | |
| 70 | def test_max_words_caps_output(self): |
| 71 | result = extract_core_subject( |
| 72 | "multi agent reinforcement learning framework distributed training", |
| 73 | max_words=5, |
| 74 | ) |
| 75 | self.assertEqual(result, "multi agent reinforcement learning framework") |
| 76 | |
| 77 | def test_max_words_none_no_cap(self): |
| 78 | result = extract_core_subject("cursor IDE react native components") |
| 79 | self.assertEqual(result, "cursor ide react native components") |
| 80 | |
| 81 | def test_max_words_strips_trailing_punctuation(self): |
| 82 | self.assertEqual(extract_core_subject("What is Claude Code?", max_words=5), "claude code") |
| 83 | |
| 84 | def test_trailing_punctuation_does_not_hide_last_noise_word(self): |
| 85 | result = extract_core_subject("Claude Code best practices?", max_words=5, strip_suffixes=True) |
| 86 | self.assertEqual(result, "claude code") |
| 87 | |
| 88 | def test_max_words_fallback_on_empty(self): |
| 89 | # All words filtered + max_words should fall back to original |
| 90 | result = extract_core_subject("best top latest", max_words=3) |
| 91 | self.assertTrue(len(result) > 0) |
| 92 | |
| 93 | |
| 94 | class TestStripSuffixes(unittest.TestCase): |
| 95 | """Tests for strip_suffixes parameter.""" |
| 96 | |
| 97 | def test_strips_best_practices(self): |
| 98 | result = extract_core_subject( |
| 99 | "claude code best practices", |
| 100 | strip_suffixes=True, |
| 101 | noise=frozenset(), |
| 102 | ) |
| 103 | self.assertEqual(result, "claude code") |
| 104 | |
| 105 | def test_strips_use_cases(self): |
| 106 | result = extract_core_subject( |
| 107 | "react hooks use cases", |
| 108 | strip_suffixes=True, |
| 109 | noise=frozenset(), |
| 110 | ) |
| 111 | self.assertEqual(result, "react hooks") |
| 112 | |
| 113 | def test_no_strip_without_flag(self): |
| 114 | result = extract_core_subject("claude code best practices", noise=frozenset()) |
| 115 | self.assertEqual(result, "claude code best practices") |
| 116 | |
| 117 | |
| 118 | class TestCustomNoise(unittest.TestCase): |
| 119 | """Tests for noise override parameter.""" |
| 120 | |
| 121 | def test_custom_noise_keeps_tips(self): |
| 122 | # YouTube keeps tips/tricks/tutorial — pass a noise set without them |
| 123 | youtube_noise = frozenset({ |
| 124 | 'best', 'top', 'good', 'great', 'awesome', 'killer', |
| 125 | 'latest', 'new', 'news', 'update', 'updates', |
| 126 | 'trending', 'hottest', 'popular', 'viral', |
| 127 | 'practices', 'features', |
| 128 | 'recommendations', 'advice', |
| 129 | 'prompt', 'prompts', 'prompting', |
| 130 | 'methods', 'strategies', 'approaches', |
| 131 | }) |
| 132 | result = extract_core_subject("best react tips", noise=youtube_noise) |
| 133 | self.assertIn("tips", result) |
| 134 | |
| 135 | def test_default_noise_removes_tips(self): |
| 136 | result = extract_core_subject("best react tips") |
| 137 | self.assertNotIn("tips", result) |
| 138 | |
| 139 | |
| 140 | class TestNoiseWordsCompleteness(unittest.TestCase): |
| 141 | """Verify NOISE_WORDS superset covers all platform sets.""" |
| 142 | |
| 143 | def test_question_words_present(self): |
| 144 | for w in ('who', 'why', 'when', 'where', 'does', 'should', 'could', 'would'): |
| 145 | self.assertIn(w, NOISE_WORDS, f"Missing question word: {w}") |
| 146 | |
| 147 | def test_core_filler_present(self): |
| 148 | for w in ('the', 'a', 'an', 'is', 'are', 'for', 'with', 'about'): |
| 149 | self.assertIn(w, NOISE_WORDS) |
| 150 | |
| 151 | def test_research_meta_present(self): |
| 152 | for w in ('best', 'top', 'latest', 'trending', 'popular'): |
| 153 | self.assertIn(w, NOISE_WORDS) |
| 154 | |
| 155 | |
| 156 | |
| 157 | class TestSharedAdapterNoiseSets(unittest.TestCase): |
| 158 | """Pin SOCIAL_NOISE / VIRAL_NOISE so adapters can rely on stable membership. |
| 159 | |
| 160 | Bluesky, Threads, Truth Social use SOCIAL_NOISE. |
| 161 | TikTok, Instagram, Pinterest use VIRAL_NOISE. |
| 162 | YouTube extends VIRAL_NOISE with temporal/meta tokens (asserted in its |
| 163 | own adapter test). |
| 164 | """ |
| 165 | |
| 166 | def test_social_noise_membership(self): |
| 167 | # Words shared with the historical _BSKY_NOISE / _TS_NOISE / _THREADS_NOISE. |
| 168 | expected = { |
| 169 | 'best', 'top', 'good', 'great', 'awesome', |
| 170 | 'latest', 'new', 'news', 'update', 'updates', |
| 171 | 'trending', 'hottest', 'popular', 'viral', |
| 172 | 'practices', 'features', 'recommendations', 'advice', |
| 173 | 'or', 'and', |
| 174 | } |
| 175 | self.assertEqual(set(SOCIAL_NOISE), expected) |
| 176 | |
| 177 | def test_viral_noise_is_social_superset(self): |
| 178 | self.assertTrue(SOCIAL_NOISE.issubset(VIRAL_NOISE)) |
| 179 | # The extra words VIRAL adds on top of SOCIAL: the historical |
| 180 | # tiktok / instagram / pinterest delta. |
| 181 | delta = VIRAL_NOISE - SOCIAL_NOISE |
| 182 | self.assertEqual( |
| 183 | delta, |
| 184 | {'killer', 'prompt', 'prompts', 'prompting', |
| 185 | 'methods', 'strategies', 'approaches'}, |
| 186 | ) |
| 187 | |
| 188 | def test_extract_core_subject_with_social_noise(self): |
| 189 | # Sanity: a Bluesky-style query strips through SOCIAL_NOISE. |
| 190 | result = extract_core_subject( |
| 191 | "best new Claude Code update", |
| 192 | noise=SOCIAL_NOISE, |
| 193 | ) |
| 194 | self.assertEqual(result, "claude code") |
| 195 | |
| 196 | def test_extract_core_subject_with_viral_noise(self): |
| 197 | # Viral set strips 'killer' and the prompt cluster. |
| 198 | result = extract_core_subject( |
| 199 | "killer prompting strategies for React", |
| 200 | noise=VIRAL_NOISE, |
| 201 | ) |
| 202 | # 'for' is in the default NOISE_WORDS path but VIRAL_NOISE alone |
| 203 | # doesn't include articles/prepositions; extract_core_subject |
| 204 | # falls back to original when nothing survives, so allow 'for'. |
| 205 | self.assertIn("react", result) |
| 206 | self.assertNotIn("killer", result) |
| 207 | self.assertNotIn("prompting", result) |
| 208 | |
| 209 | |
| 210 | class TestInferQueryIntent(unittest.TestCase): |
| 211 | """Tests for infer_query_intent() — the shared canonical classifier. |
| 212 | |
| 213 | Adapters previously kept five near-duplicate copies with subtle drift |
| 214 | (reddit added `prediction` and the longest `how_to` regex; youtube had |
| 215 | a partial extension; instagram and tiktok lagged). Canonical here is |
| 216 | reddit's superset. |
| 217 | """ |
| 218 | |
| 219 | def test_comparison(self): |
| 220 | self.assertEqual(infer_query_intent("Claude vs Gemini"), "comparison") |
| 221 | self.assertEqual(infer_query_intent("difference between X and Y"), "comparison") |
| 222 | |
| 223 | def test_how_to_base(self): |
| 224 | self.assertEqual(infer_query_intent("how to deploy Kubernetes"), "how_to") |
| 225 | self.assertEqual(infer_query_intent("install nginx"), "how_to") |
| 226 | self.assertEqual(infer_query_intent("setup OAuth tutorial"), "how_to") |
| 227 | |
| 228 | def test_how_to_extended_keywords(self): |
| 229 | # Bare imperatives covered by reddit's extended regex. |
| 230 | self.assertEqual(infer_query_intent("configure DNS"), "how_to") |
| 231 | self.assertEqual(infer_query_intent("troubleshoot router"), "how_to") |
| 232 | self.assertEqual(infer_query_intent("debug python"), "how_to") |
| 233 | self.assertEqual(infer_query_intent("fix kernel panic"), "how_to") |
| 234 | |
| 235 | def test_opinion(self): |
| 236 | self.assertEqual(infer_query_intent("thoughts on Claude Code"), "opinion") |
| 237 | self.assertEqual(infer_query_intent("should I buy a Pixel"), "opinion") |
| 238 | |
| 239 | def test_product(self): |
| 240 | self.assertEqual(infer_query_intent("best laptop for programming"), "product") |
| 241 | self.assertEqual(infer_query_intent("Surface pricing"), "product") |
| 242 | |
| 243 | def test_prediction(self): |
| 244 | self.assertEqual(infer_query_intent("predict the 2028 election"), "prediction") |
| 245 | self.assertEqual(infer_query_intent("odds Trump wins"), "prediction") |
| 246 | self.assertEqual(infer_query_intent("forecast Q4 earnings"), "prediction") |
| 247 | |
| 248 | def test_breaking_news_default(self): |
| 249 | self.assertEqual(infer_query_intent("Kanye West"), "breaking_news") |
| 250 | self.assertEqual(infer_query_intent("OpenAI"), "breaking_news") |
| 251 | |
| 252 | |
| 253 | class TestExtractCompoundTerms(unittest.TestCase): |
| 254 | """Tests for extract_compound_terms().""" |
| 255 | |
| 256 | def test_hyphenated(self): |
| 257 | terms = extract_compound_terms("multi-agent reinforcement learning") |
| 258 | self.assertIn("multi-agent", terms) |
| 259 | |
| 260 | def test_title_case(self): |
| 261 | terms = extract_compound_terms("Claude Code and React Native") |
| 262 | self.assertTrue(any("Claude Code" in t for t in terms)) |
| 263 | self.assertTrue(any("React Native" in t for t in terms)) |
| 264 | |
| 265 | def test_no_compounds(self): |
| 266 | terms = extract_compound_terms("python tutorial") |
| 267 | self.assertEqual(len(terms), 0) |
| 268 | |
| 269 | def test_multiple_hyphens(self): |
| 270 | terms = extract_compound_terms("vc-backed start-up") |
| 271 | self.assertIn("vc-backed", terms) |
| 272 | self.assertIn("start-up", terms) |
| 273 | |
| 274 | if __name__ == "__main__": |
| 275 | unittest.main() |
| 276 |