forked from yusufkaraaslan/Skill_Seekers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_skip_llms_txt.py
More file actions
329 lines (276 loc) · 12.2 KB
/
test_skip_llms_txt.py
File metadata and controls
329 lines (276 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"""Tests for skip_llms_txt configuration option.
This config option allows users to explicitly skip llms.txt detection and fetching,
which is useful when:
- A site's llms.txt is incomplete or incorrect
- You need specific pages not in llms.txt
- You want to force HTML scraping
"""
import os
import tempfile
import unittest
from unittest.mock import patch
from skill_seekers.cli.doc_scraper import DocToSkillConverter
class TestSkipLlmsTxtConfig(unittest.TestCase):
"""Test skip_llms_txt configuration option."""
def test_default_skip_llms_txt_is_false(self):
"""Test that skip_llms_txt defaults to False when not specified."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
}
converter = DocToSkillConverter(config, dry_run=True)
self.assertFalse(converter.skip_llms_txt)
def test_skip_llms_txt_can_be_set_true(self):
"""Test that skip_llms_txt can be explicitly set to True."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"skip_llms_txt": True,
}
converter = DocToSkillConverter(config, dry_run=True)
self.assertTrue(converter.skip_llms_txt)
def test_skip_llms_txt_can_be_set_false(self):
"""Test that skip_llms_txt can be explicitly set to False."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"skip_llms_txt": False,
}
converter = DocToSkillConverter(config, dry_run=True)
self.assertFalse(converter.skip_llms_txt)
class TestSkipLlmsTxtSyncBehavior(unittest.TestCase):
"""Test skip_llms_txt behavior in sync scraping mode."""
def test_llms_txt_tried_when_not_skipped(self):
"""Test that _try_llms_txt is called when skip_llms_txt is False."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"skip_llms_txt": False,
}
original_cwd = os.getcwd()
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=False)
with (
patch.object(converter, "_try_llms_txt", return_value=False) as mock_try,
patch.object(converter, "scrape_page"),
patch.object(converter, "save_summary"),
):
converter.scrape_all()
mock_try.assert_called_once()
finally:
os.chdir(original_cwd)
def test_llms_txt_skipped_when_skip_true(self):
"""Test that _try_llms_txt is NOT called when skip_llms_txt is True."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"skip_llms_txt": True,
}
original_cwd = os.getcwd()
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=False)
with (
patch.object(converter, "_try_llms_txt") as mock_try,
patch.object(converter, "scrape_page"),
patch.object(converter, "save_summary"),
):
converter.scrape_all()
mock_try.assert_not_called()
finally:
os.chdir(original_cwd)
def test_llms_txt_skipped_in_dry_run_mode(self):
"""Test that _try_llms_txt is NOT called in dry-run mode regardless of skip setting."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"skip_llms_txt": False, # Even when False
}
original_cwd = os.getcwd()
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=True)
with (
patch.object(converter, "_try_llms_txt") as mock_try,
patch.object(converter, "save_summary"),
):
converter.scrape_all()
mock_try.assert_not_called()
finally:
os.chdir(original_cwd)
class TestSkipLlmsTxtAsyncBehavior(unittest.TestCase):
"""Test skip_llms_txt behavior in async scraping mode."""
def test_async_llms_txt_tried_when_not_skipped(self):
"""Test that _try_llms_txt is called in async mode when skip_llms_txt is False."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"async_mode": True,
"skip_llms_txt": False,
}
original_cwd = os.getcwd()
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=False)
with (
patch.object(converter, "_try_llms_txt", return_value=False) as mock_try,
patch.object(converter, "scrape_page_async", return_value=None),
patch.object(converter, "save_summary"),
):
converter.scrape_all()
mock_try.assert_called_once()
finally:
os.chdir(original_cwd)
def test_async_llms_txt_skipped_when_skip_true(self):
"""Test that _try_llms_txt is NOT called in async mode when skip_llms_txt is True."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"async_mode": True,
"skip_llms_txt": True,
}
original_cwd = os.getcwd()
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=False)
with (
patch.object(converter, "_try_llms_txt") as mock_try,
patch.object(converter, "scrape_page_async", return_value=None),
patch.object(converter, "save_summary"),
):
converter.scrape_all()
mock_try.assert_not_called()
finally:
os.chdir(original_cwd)
class TestSkipLlmsTxtWithRealConfig(unittest.TestCase):
"""Test skip_llms_txt with real-world config patterns."""
def test_telegram_bots_config_pattern(self):
"""Test the telegram-bots config pattern which uses skip_llms_txt."""
config = {
"name": "telegram-bots",
"description": "Telegram bot documentation",
"base_url": "https://core.telegram.org/bots",
"skip_llms_txt": True, # Telegram doesn't have useful llms.txt
"start_urls": [
"https://core.telegram.org/bots",
"https://core.telegram.org/bots/api",
],
"selectors": {
"main_content": "#dev_page_content, main, article",
"title": "h1, title",
"code_blocks": "pre code, pre",
},
}
converter = DocToSkillConverter(config, dry_run=True)
self.assertTrue(converter.skip_llms_txt)
self.assertEqual(converter.name, "telegram-bots")
def test_skip_llms_txt_with_multiple_start_urls(self):
"""Test skip_llms_txt works correctly with multiple start URLs."""
config = {
"name": "test-multi",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"skip_llms_txt": True,
"start_urls": [
"https://example.com/docs/",
"https://example.com/api/",
"https://example.com/guide/",
],
}
converter = DocToSkillConverter(config, dry_run=True)
self.assertTrue(converter.skip_llms_txt)
# start_urls are stored in pending_urls deque
self.assertEqual(len(converter.pending_urls), 3)
class TestSkipLlmsTxtEdgeCases(unittest.TestCase):
"""Test edge cases for skip_llms_txt."""
def test_skip_llms_txt_with_int_zero_logs_warning(self):
"""Test that integer 0 logs warning and defaults to False."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"skip_llms_txt": 0, # Invalid type
}
with self.assertLogs("skill_seekers.cli.doc_scraper", level="WARNING") as cm:
converter = DocToSkillConverter(config, dry_run=True)
self.assertFalse(converter.skip_llms_txt)
self.assertTrue(any("Invalid value" in log and "0" in log for log in cm.output))
def test_skip_llms_txt_with_int_one_logs_warning(self):
"""Test that integer 1 logs warning and defaults to False."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"skip_llms_txt": 1, # Invalid type
}
with self.assertLogs("skill_seekers.cli.doc_scraper", level="WARNING") as cm:
converter = DocToSkillConverter(config, dry_run=True)
self.assertFalse(converter.skip_llms_txt)
self.assertTrue(any("Invalid value" in log and "1" in log for log in cm.output))
def test_skip_llms_txt_with_string_logs_warning(self):
"""Test that string values log warning and default to False."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"skip_llms_txt": "true", # Invalid type
}
with self.assertLogs("skill_seekers.cli.doc_scraper", level="WARNING") as cm:
converter = DocToSkillConverter(config, dry_run=True)
self.assertFalse(converter.skip_llms_txt)
self.assertTrue(any("Invalid value" in log and "true" in log for log in cm.output))
def test_skip_llms_txt_with_none_logs_warning(self):
"""Test that None logs warning and defaults to False."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"skip_llms_txt": None, # Invalid type
}
with self.assertLogs("skill_seekers.cli.doc_scraper", level="WARNING") as cm:
converter = DocToSkillConverter(config, dry_run=True)
self.assertFalse(converter.skip_llms_txt)
self.assertTrue(any("Invalid value" in log and "None" in log for log in cm.output))
def test_scraping_proceeds_when_llms_txt_skipped(self):
"""Test that HTML scraping proceeds normally when llms.txt is skipped."""
config = {
"name": "test",
"base_url": "https://example.com/",
"selectors": {"main_content": "article"},
"skip_llms_txt": True,
}
original_cwd = os.getcwd()
with tempfile.TemporaryDirectory() as tmpdir:
try:
os.chdir(tmpdir)
converter = DocToSkillConverter(config, dry_run=False)
# Track if scrape_page was called
scrape_called = []
def mock_scrape(url):
scrape_called.append(url)
return None
with (
patch.object(converter, "scrape_page", side_effect=mock_scrape),
patch.object(converter, "save_summary"),
):
converter.scrape_all()
# Should have attempted to scrape the base URL
self.assertTrue(len(scrape_called) > 0)
finally:
os.chdir(original_cwd)
if __name__ == "__main__":
unittest.main()