返回 last30days-skill
test_categories.py
根目录 / tests / test_categories.py
1 """Unit tests for scripts/lib/categories.py — the Step 0.55 category-peer map.
2
3 Guards the 2026-04-22 `Prompting GPT Image 2` failure mode: the original bug
4 was that Step 0.55 resolved only brand-adjacent subs (r/OpenAI, r/ChatGPT)
5 and missed the category peers (r/StableDiffusion, r/midjourney, r/dalle2)
6 where prompting techniques actually live.
7 """
8
9 import re
10 import unittest
11
12 from lib import categories
13 from lib.categories import CATEGORY_PEERS, detect_category, peer_subs_for
14
15
16 class DetectCategoryHappyPath(unittest.TestCase):
17 def test_prompting_gpt_image_2_matches_image_generation(self):
18 self.assertEqual(
19 detect_category("Prompting GPT Image 2"),
20 "ai_image_generation",
21 )
22
23 def test_claude_code_matches_coding_agent(self):
24 self.assertEqual(
25 detect_category("Claude Code skills"),
26 "ai_coding_agent",
27 )
28
29 def test_suno_matches_music_generation(self):
30 self.assertEqual(detect_category("Suno v4 review"), "ai_music_generation")
31
32 def test_polymarket_matches_prediction_markets(self):
33 self.assertEqual(
34 detect_category("Polymarket election odds"),
35 "prediction_markets",
36 )
37
38 def test_sora_matches_video_generation(self):
39 self.assertEqual(detect_category("Sora 2 prompts"), "ai_video_generation")
40
41 def test_plural_topic_matches_singular_pattern(self):
42 self.assertEqual(detect_category("AI coding agents"), "ai_coding_agent")
43 self.assertEqual(detect_category("prediction markets"), "prediction_markets")
44
45 def test_plural_that_is_another_word_does_not_match(self):
46 self.assertIsNone(detect_category("yoga asanas"))
47 self.assertIsNone(detect_category("SunOS"))
48 self.assertEqual(detect_category("asana"), "saas_productivity")
49
50 def test_generic_single_word_pattern_still_pluralizes(self):
51 self.assertEqual(detect_category("stablecoins"), "crypto_defi")
52
53
54 class PeerSubsForHappyPath(unittest.TestCase):
55 def test_image_generation_peer_subs_priority_order(self):
56 subs = peer_subs_for("ai_image_generation")
57 self.assertIn("StableDiffusion", subs)
58 self.assertIn("midjourney", subs)
59 self.assertIn("dalle2", subs)
60 self.assertLess(subs.index("StableDiffusion"), subs.index("midjourney"))
61 self.assertLess(subs.index("midjourney"), subs.index("dalle2"))
62
63 def test_unknown_category_returns_empty_list(self):
64 self.assertEqual(peer_subs_for("unknown_category"), [])
65
66 def test_none_category_returns_empty_list(self):
67 self.assertEqual(peer_subs_for(None), [])
68
69 def test_returned_list_is_fresh_copy(self):
70 first = peer_subs_for("ai_image_generation")
71 first.append("MutatedSub")
72 second = peer_subs_for("ai_image_generation")
73 self.assertNotIn("MutatedSub", second)
74
75
76 class DetectCategoryEdgeCases(unittest.TestCase):
77 def test_case_insensitive_match(self):
78 self.assertEqual(
79 detect_category("STABLE DIFFUSION walkthrough"),
80 "ai_image_generation",
81 )
82
83 def test_non_category_topic_returns_none(self):
84 self.assertIsNone(detect_category("Kanye West"))
85
86 def test_bare_image_word_does_not_trigger_image_generation(self):
87 # Compound-term guard: "image" alone is not a pattern; only
88 # multi-word compounds or domain-specific brand names match.
89 self.assertIsNone(detect_category("image editing on my phone"))
90
91 def test_bare_ai_word_does_not_trigger_any_category(self):
92 self.assertIsNone(detect_category("ai news today"))
93
94 def test_empty_topic_returns_none(self):
95 self.assertIsNone(detect_category(""))
96
97 def test_none_topic_returns_none(self):
98 self.assertIsNone(detect_category(None))
99
100 def test_first_match_wins_image_gen_before_chat_model(self):
101 self.assertEqual(detect_category("gpt-5"), "ai_chat_model")
102 self.assertEqual(
103 detect_category("gpt image and gpt-5"),
104 "ai_image_generation",
105 )
106
107
108 class CategoryMapInvariants(unittest.TestCase):
109 """Regression guards on the map itself — catch accidental bare-word patterns."""
110
111 # Common nouns that would produce false positives if used as bare patterns.
112 FORBIDDEN_BARE_PATTERNS = frozenset({
113 "image", "video", "music", "ai", "model", "agent", "chat",
114 "code", "cli", "app", "tool", "defi",
115 })
116
117 def test_no_category_has_a_bare_common_noun_pattern(self):
118 offenders = []
119 for category_id, entry in CATEGORY_PEERS.items():
120 for pattern in entry["patterns"]:
121 if pattern.strip() in self.FORBIDDEN_BARE_PATTERNS:
122 offenders.append((category_id, pattern))
123 self.assertEqual(
124 offenders,
125 [],
126 msg=(
127 "Bare common-noun patterns cause false positives. "
128 f"Offenders: {offenders}. Patterns must be compound "
129 "(e.g. 'image generation') or domain-specific "
130 "(e.g. 'midjourney')."
131 ),
132 )
133
134 def test_every_category_has_at_least_one_compound_or_brand_pattern(self):
135 multi_word_or_brand = re.compile(r"(\s|-|\.)|^[a-z][a-z0-9]{3,}$")
136 for category_id, entry in CATEGORY_PEERS.items():
137 patterns = entry["patterns"]
138 self.assertTrue(patterns, f"{category_id} has no patterns")
139 has_strong = any(multi_word_or_brand.search(p) for p in patterns)
140 self.assertTrue(
141 has_strong,
142 f"{category_id} needs at least one multi-word or brand pattern",
143 )
144
145 def test_every_category_has_at_least_two_peer_subs(self):
146 for category_id, entry in CATEGORY_PEERS.items():
147 self.assertGreaterEqual(
148 len(entry["peer_subs"]),
149 2,
150 f"{category_id} should list at least 2 peer subs",
151 )
152
153 def test_category_count_is_in_expected_range(self):
154 # Sanity check: the map is intentionally small and curated.
155 self.assertGreaterEqual(len(CATEGORY_PEERS), 8)
156 self.assertLessEqual(len(CATEGORY_PEERS), 20)
157
158 if __name__ == "__main__":
159 unittest.main()
160
160 lines PYTHON