Repository navigation
Expand file tree
/
Copy pathtest_functions.py
More file actions
551 lines (415 loc) · 17.6 KB
/
Copy pathtest_functions.py
File metadata and controls
551 lines (415 loc) · 17.6 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
"""Tests for pure functions extracted from the codebase.
This file tests functions that don't depend on NoneBot framework.
Functions are imported from production modules.
"""
import os
import time
import pytest
from collections import deque
from datetime import datetime, date
# Set test environment
os.environ["RULES_FILE"] = ""
os.environ["REMINDERS_FILE"] = ""
# Import production functions
from rules import normalize_rule, upsert_rule, find_rule
from reminders import normalize_reminder, next_reminder_id
from forward_utils import (
parse_int_set, parse_str_list, normalize_text, match_keywords,
is_duplicate, check_keyword_cooldown, track_keyword_hit,
parse_csv_items, parse_indices, resolve_group_id,
rule_by_index, resolve_rule_reference, rule_index, clean_display_text,
)
from remind_utils import (
render_reminder, parse_time, parse_id_list,
should_resume_period_interval, format_schedule_result, REMIND_TYPE_LABELS,
)
# ==================== Tests ====================
class TestParseIntSet:
"""Test parse_int_set function."""
def test_valid(self):
assert parse_int_set("1,2,3") == {1, 2, 3}
def test_with_spaces(self):
assert parse_int_set("1, 2 , 3") == {1, 2, 3}
def test_empty(self):
assert parse_int_set("") == set()
def test_single(self):
assert parse_int_set("42") == {42}
def test_duplicates(self):
assert parse_int_set("1,1,2,2") == {1, 2}
class TestParseStrList:
"""Test parse_str_list function."""
def test_valid(self):
assert parse_str_list("a,b,c") == ["a", "b", "c"]
def test_with_spaces(self):
assert parse_str_list(" a , b , c ") == ["a", "b", "c"]
def test_empty_items(self):
assert parse_str_list("a,,b,,,c") == ["a", "b", "c"]
def test_empty(self):
assert parse_str_list("") == []
class TestMatchKeywords:
"""Test match_keywords function."""
def test_basic_match(self):
keywords = [{"word": "test", "enabled": True}]
assert "test" in match_keywords("this is a test", keywords, False)
def test_disabled_keyword(self):
keywords = [{"word": "test", "enabled": False}]
assert "test" not in match_keywords("this is a test", keywords, False)
def test_regex_match(self):
keywords = [{"word": r"\d+", "enabled": True}]
assert r"\d+" in match_keywords("test 123", keywords, True)
def test_no_match(self):
keywords = [{"word": "xyz", "enabled": True}]
assert match_keywords("hello world", keywords, False) == []
def test_invalid_regex(self):
"""Invalid regex should not crash."""
keywords = [{"word": "[invalid", "enabled": True}]
result = match_keywords("test", keywords, True)
assert isinstance(result, list)
def test_case_insensitive(self):
keywords = [{"word": "TEST", "enabled": True}]
assert "TEST" in match_keywords("this is a test", keywords, False)
def test_case_sensitive(self):
keywords = [{"word": "TEST", "enabled": True}]
assert match_keywords("this is a test", keywords, False, case_sensitive=True) == []
class TestIsDuplicate:
"""Test is_duplicate function."""
def test_new_message(self):
recent_keys = deque(maxlen=1000)
recent_seen = set()
assert is_duplicate("group1:msg1", recent_keys, recent_seen) is False
def test_duplicate_message(self):
recent_keys = deque(maxlen=1000)
recent_seen = set()
is_duplicate("group1:msg1", recent_keys, recent_seen)
assert is_duplicate("group1:msg1", recent_keys, recent_seen) is True
def test_expired_cleanup(self):
recent_keys = deque(maxlen=1000)
recent_seen = set()
old_time = time.time() - 31
recent_keys.append((old_time, "old_key"))
recent_seen.add("old_key")
is_duplicate("new_key", recent_keys, recent_seen, dedupe_seconds=30)
assert "old_key" not in recent_seen
assert len(recent_keys) == 1
class TestCheckKeywordCooldown:
"""Test check_keyword_cooldown function."""
def test_no_cooldown(self):
cooldown_dict = {}
assert check_keyword_cooldown(123, "test", cooldown_dict) is False
def test_active_cooldown(self):
cooldown_dict = {}
check_keyword_cooldown(123, "test", cooldown_dict)
assert check_keyword_cooldown(123, "test", cooldown_dict) is True
def test_different_groups(self):
cooldown_dict = {}
check_keyword_cooldown(1, "test", cooldown_dict)
assert check_keyword_cooldown(2, "test", cooldown_dict) is False
def test_different_keywords(self):
cooldown_dict = {}
check_keyword_cooldown(123, "test1", cooldown_dict)
assert check_keyword_cooldown(123, "test2", cooldown_dict) is False
class TestTrackKeywordHit:
"""Test track_keyword_hit function."""
def test_track_hit(self):
stats = {}
track_keyword_hit(123, "test", stats)
today = time.strftime("%Y-%m-%d")
key = f"{today}:123:test"
assert stats[key] == 1
def test_multiple_hits(self):
stats = {}
track_keyword_hit(123, "test", stats)
track_keyword_hit(123, "test", stats)
today = time.strftime("%Y-%m-%d")
key = f"{today}:123:test"
assert stats[key] == 2
class TestResolveGroupId:
"""Test resolve_group_id function."""
def test_with_group(self):
group_id, keyword = resolve_group_id("add 123456 test")
assert group_id == 123456
assert keyword == "test"
def test_without_group(self):
group_id, keyword = resolve_group_id("add test")
assert group_id is None
assert keyword == "test"
def test_no_keyword(self):
group_id, keyword = resolve_group_id("add 123456")
assert group_id == 123456
assert keyword is None
def test_single_command(self):
group_id, keyword = resolve_group_id("status")
assert group_id is None
assert keyword is None
class TestRuleByIndex:
"""Test rule_by_index function."""
def test_valid(self):
rules = [{"group_id": 1}, {"group_id": 2}]
assert rule_by_index(rules, 1)["group_id"] == 1
def test_out_of_range(self):
rules = [{"group_id": 1}]
assert rule_by_index(rules, 5) is None
def test_zero(self):
rules = [{"group_id": 1}]
assert rule_by_index(rules, 0) is None
class TestResolveRuleReference:
"""Test resolve_rule_reference function."""
def test_by_group_id(self):
rules = [{"group_id": 123}, {"group_id": 456}]
assert resolve_rule_reference(rules, 123)["group_id"] == 123
def test_by_index(self):
rules = [{"group_id": 123}, {"group_id": 456}]
assert resolve_rule_reference(rules, 1)["group_id"] == 123
def test_not_found(self):
rules = [{"group_id": 123}]
assert resolve_rule_reference(rules, 999) is None
class TestRuleIndex:
"""Test rule_index function."""
def test_found(self):
rules = [{"group_id": 100}, {"group_id": 200}]
assert rule_index(rules, 200) == 2
def test_not_found(self):
rules = [{"group_id": 100}]
assert rule_index(rules, 999) is None
class TestCleanDisplayText:
"""Test clean_display_text function."""
def test_with_emoji(self):
assert clean_display_text("Hello 😀 World 🎉") == "Hello World"
def test_empty_after_clean(self):
assert clean_display_text("😀🎉") == "未知"
def test_normal(self):
assert clean_display_text("测试群名") == "测试群名"
def test_multiple_spaces(self):
assert clean_display_text("hello world") == "hello world"
class TestParseCsvItems:
"""Test parse_csv_items function."""
def test_chinese_comma(self):
assert parse_csv_items("a,b,c") == ["a", "b", "c"]
def test_english_comma(self):
assert parse_csv_items("a,b,c") == ["a", "b", "c"]
def test_mixed(self):
assert parse_csv_items("a,b,c") == ["a", "b", "c"]
class TestParseIndices:
"""Test parse_indices function."""
def test_valid(self):
indices, error = parse_indices("1,2,3")
assert indices == [1, 2, 3]
assert error is None
def test_invalid(self):
indices, error = parse_indices("a,b")
assert indices == []
assert error is not None
def test_empty(self):
indices, error = parse_indices("")
assert indices == []
assert error is not None
def test_dedup_and_sort(self):
indices, error = parse_indices("3,1,2,1,3")
assert indices == [1, 2, 3]
assert error is None
class TestParseTime:
"""Test parse_time function."""
def test_valid(self):
hour, minute, error = parse_time("09:30")
assert hour == 9
assert minute == 30
assert error is None
def test_single_digit_hour(self):
hour, minute, error = parse_time("9:30")
assert hour == 9
assert minute == 30
def test_invalid_hour(self):
hour, minute, error = parse_time("25:00")
assert error is not None
def test_invalid_minute(self):
hour, minute, error = parse_time("12:60")
assert error is not None
def test_no_colon(self):
hour, minute, error = parse_time("1230")
assert error is not None
class TestParseIdList:
"""Test parse_id_list function."""
def test_single(self):
ids, error = parse_id_list("1")
assert ids == [1]
assert error is None
def test_multiple(self):
ids, error = parse_id_list("1,2,3")
assert ids == [1, 2, 3]
assert error is None
def test_chinese_comma(self):
ids, error = parse_id_list("1,2,3")
assert ids == [1, 2, 3]
assert error is None
def test_invalid(self):
ids, error = parse_id_list("a,b")
assert ids == []
assert error is not None
def test_empty(self):
ids, error = parse_id_list("")
assert ids == []
assert error is not None
def test_dedup_and_sort(self):
ids, error = parse_id_list("3,1,2,1,3")
assert ids == [1, 2, 3]
assert error is None
class TestRenderReminder:
"""Test render_reminder function."""
def test_daily(self):
rem = {"id": 1, "type": "daily", "hour": 10, "minute": 30, "message": "背单词"}
result = render_reminder(rem)
assert "提醒 #1" in result
assert "每天" in result
assert "10:30" in result
assert "背单词" in result
def test_once(self):
rem = {"id": 2, "type": "once", "date": "2026-05-15", "hour": 9, "minute": 0, "message": "开会"}
result = render_reminder(rem)
assert "单次" in result
assert "2026-05-15" in result
def test_interval(self):
rem = {"id": 3, "type": "interval", "interval_minutes": 30, "message": "休息"}
result = render_reminder(rem)
assert "间隔" in result
assert "30 分钟" in result
def test_period(self):
rem = {"id": 4, "type": "period", "hour": 18, "minute": 0, "repeat_interval": 10, "message": "背单词"}
result = render_reminder(rem)
assert "周期催促" in result
assert "18:00" in result
assert "10 分钟" in result
def test_quote(self):
rem = {"id": 5, "type": "daily", "hour": 9, "minute": 0, "auto_generate": "quote"}
result = render_reminder(rem)
assert "每日一言" in result
def test_period_done_today(self):
rem = {
"id": 6, "type": "period", "hour": 18, "minute": 0,
"repeat_interval": 10, "message": "背单词",
"last_done_date": date.today().isoformat()
}
result = render_reminder(rem)
assert "已完成" in result
class TestShouldResumePeriodInterval:
"""Test should_resume_period_interval function."""
def test_not_period(self):
rem = {"type": "daily", "enabled": True}
assert should_resume_period_interval(rem) is False
def test_disabled(self):
rem = {"type": "period", "enabled": False, "hour": 9, "minute": 0}
assert should_resume_period_interval(rem) is False
def test_done_today(self):
rem = {"type": "period", "enabled": True, "hour": 9, "minute": 0, "last_done_date": date.today().isoformat()}
assert should_resume_period_interval(rem) is False
def test_before_start(self):
now = datetime.now()
future_hour = (now.hour + 2) % 24
rem = {"type": "period", "enabled": True, "hour": future_hour, "minute": 0}
assert should_resume_period_interval(rem) is False
class TestNormalizeRule:
"""Test normalize_rule from rules.py."""
def test_string_keywords(self):
raw = {"group_id": 1, "targets": [3, 1, 2], "keywords": ["word1", "word2"]}
result = normalize_rule(raw)
assert result["group_id"] == 1
assert result["targets"] == [1, 2, 3]
assert len(result["keywords"]) == 2
assert result["keywords"][0] == {"word": "word1", "enabled": True}
def test_dict_keywords(self):
raw = {"group_id": 1, "targets": [], "keywords": [{"word": "active", "enabled": True}, {"word": "disabled", "enabled": False}]}
result = normalize_rule(raw)
assert result["keywords"][0]["enabled"] is True
assert result["keywords"][1]["enabled"] is False
def test_empty_keywords(self):
raw = {"group_id": 1, "targets": [], "keywords": []}
result = normalize_rule(raw)
assert result["keywords"] == []
def test_blank_keyword_filtered(self):
raw = {"group_id": 1, "targets": [], "keywords": ["valid", "", " ", "also_valid"]}
result = normalize_rule(raw)
assert len(result["keywords"]) == 2
assert result["keywords"][0]["word"] == "valid"
assert result["keywords"][1]["word"] == "also_valid"
def test_deduplicate_targets(self):
raw = {"group_id": 1, "targets": [1, 1, 2, 2, 3], "keywords": []}
result = normalize_rule(raw)
assert result["targets"] == [1, 2, 3]
def test_per_keyword_targets(self):
"""Keywords with targets should preserve them."""
raw = {"group_id": 1, "targets": [100], "keywords": [
{"word": "a", "enabled": True, "targets": [200, 300]},
{"word": "b", "enabled": True}
]}
result = normalize_rule(raw)
assert result["keywords"][0]["targets"] == [200, 300]
assert "targets" not in result["keywords"][1]
class TestUpsertRule:
"""Test upsert_rule from rules.py."""
def test_add_new(self):
existing = [{"group_id": 1, "targets": [], "keywords": []}]
new_rule = {"group_id": 2, "targets": [111], "keywords": ["new"]}
result = upsert_rule(existing, new_rule)
assert len(result) == 2
assert result[1]["group_id"] == 2
def test_update_existing(self):
existing = [
{"group_id": 1, "targets": [], "keywords": ["old"]},
{"group_id": 2, "targets": [], "keywords": []}
]
updated = {"group_id": 1, "targets": [111], "keywords": ["new"]}
result = upsert_rule(existing, updated)
assert len(result) == 2
rule1 = next(r for r in result if r["group_id"] == 1)
assert rule1["keywords"][0]["word"] == "new"
class TestNormalizeReminder:
"""Test normalize_reminder from reminders.py."""
def test_daily(self):
raw = {"id": 1, "type": "daily", "hour": "10", "minute": "30", "message": "test", "targets": [3, 1, 2]}
result = normalize_reminder(raw)
assert result["id"] == 1
assert result["type"] == "daily"
assert result["hour"] == 10
assert result["minute"] == 30
assert result["targets"] == [1, 2, 3]
def test_once(self):
raw = {"id": 2, "type": "once", "date": "2026-05-15", "hour": 9, "minute": 0, "message": "once task"}
result = normalize_reminder(raw)
assert result["type"] == "once"
assert result["date"] == "2026-05-15"
assert result["fired"] is False
def test_interval(self):
raw = {"id": 3, "type": "interval", "interval_minutes": "15", "message": "interval task"}
result = normalize_reminder(raw)
assert result["type"] == "interval"
assert result["interval_minutes"] == 15
def test_period(self):
raw = {"id": 4, "type": "period", "hour": 18, "minute": 0, "repeat_interval": "10", "message": "period task", "last_done_date": "2026-05-14"}
result = normalize_reminder(raw)
assert result["type"] == "period"
assert result["repeat_interval"] == 10
assert result["last_done_date"] == "2026-05-14"
def test_invalid_type(self):
raw = {"id": 5, "type": "invalid", "message": "test"}
result = normalize_reminder(raw)
assert result["type"] == "daily"
def test_missing_id(self):
raw = {"type": "daily", "message": "test"}
with pytest.raises((KeyError, TypeError)):
normalize_reminder(raw)
def test_missing_creator_qq(self):
raw = {"id": 1, "type": "daily", "message": "test"}
result = normalize_reminder(raw)
assert result["creator_qq"] == 0
def test_auto_generate(self):
raw = {"id": 6, "type": "daily", "hour": 9, "minute": 0, "auto_generate": "quote"}
result = normalize_reminder(raw)
assert result["auto_generate"] == "quote"
class TestNextReminderId:
"""Test next_reminder_id from reminders.py."""
def test_empty(self):
assert next_reminder_id([]) == 1
def test_existing(self):
existing = [{"id": 1}, {"id": 5}, {"id": 3}]
assert next_reminder_id(existing) == 6
if __name__ == "__main__":
pytest.main([__file__, "-v"])