-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_scout.py
More file actions
868 lines (722 loc) · 31.9 KB
/
test_scout.py
File metadata and controls
868 lines (722 loc) · 31.9 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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
"""Tests for paperscout.scout."""
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from unittest.mock import MagicMock, patch
from paperscout.models import Paper, PerUserMatches, ProbeHit
from paperscout.monitor import DiffResult, DPTransition, PollResult
from paperscout.scout import (
_batch_lines,
_fmt_lm,
_format_uptime,
_handle_status,
_handle_uptime,
_handle_version,
_handle_watchlist,
_hit_label,
_paper_link,
_reply_opts,
_show_watchlist,
enqueue_startup_status,
format_status_message,
notify_channel,
notify_users,
register_handlers,
)
from paperscout.storage import ProbeState, UserWatchlist
# ── Helpers ───────────────────────────────────────────────────────────────────
def _empty_diff() -> DiffResult:
return DiffResult(new_papers=[], updated_papers=[])
def _make_result(
new_papers=None,
probe_hits=None,
dp_transitions=None,
per_user_matches=None,
) -> PollResult:
return PollResult(
diff=DiffResult(new_papers=new_papers or [], updated_papers=[]),
probe_hits=probe_hits or [],
dp_transitions=dp_transitions or [],
per_user_matches=per_user_matches or {},
)
def _make_settings(channel="C123456", **overrides):
defaults = dict(
notification_channel=channel,
notify_on_frontier_hit=True,
notify_on_any_draft=True,
notify_on_dp_transition=True,
poll_interval_minutes=30,
enable_iso_probe=True,
alert_modified_hours=24,
cold_cycle_divisor=48,
)
defaults.update(overrides)
mock = MagicMock()
for k, v in defaults.items():
setattr(mock, k, v)
return mock
def _recent_hit(tier="frontier", number=9999, **kwargs) -> ProbeHit:
defaults = dict(
url=f"https://isocpp.org/files/papers/D{number:04d}R0.pdf",
prefix="D",
number=number,
revision=0,
extension=".pdf",
tier=tier,
is_recent=True,
last_modified=datetime.now(timezone.utc) - timedelta(hours=2),
)
defaults.update(kwargs)
return ProbeHit(**defaults)
# ── _fmt_lm ───────────────────────────────────────────────────────────────────
class TestFmtLm:
def test_none(self):
assert "unknown" in _fmt_lm(None)
def test_minutes_ago(self):
lm = datetime.now(timezone.utc) - timedelta(minutes=30)
assert "30m ago" in _fmt_lm(lm)
def test_hours_ago(self):
lm = datetime.now(timezone.utc) - timedelta(hours=5)
assert "5h ago" in _fmt_lm(lm)
def test_days_ago_shows_date(self):
lm = datetime(2025, 1, 15, tzinfo=timezone.utc)
assert "2025-01-15" in _fmt_lm(lm)
# ── _paper_link / _hit_label ──────────────────────────────────────────────────
class TestHelpers:
def test_paper_link_uses_url(self):
paper = Paper(id="P2300R10", url="https://wg21.link/P2300R10")
assert _paper_link(paper) == "<https://wg21.link/P2300R10|P2300R10>"
def test_paper_link_falls_back_to_long_link(self):
paper = Paper(id="P2300R10", url="", long_link="https://wg21.link/P2300R10.pdf")
assert _paper_link(paper) == "<https://wg21.link/P2300R10.pdf|P2300R10>"
def test_paper_link_synthesises_wg21_url(self):
paper = Paper(id="P2300R10", url="", long_link="")
link = _paper_link(paper)
assert "wg21.link/P2300R10" in link
assert "|P2300R10>" in link
def test_hit_label(self):
label = _hit_label("https://isocpp.org/files/papers/D2300R11.pdf", "D", 2300, 11, ".pdf")
assert label == "<https://isocpp.org/files/papers/D2300R11.pdf|D2300R11.pdf>"
# ── notify_channel ────────────────────────────────────────────────────────────
class TestNotifyChannel:
def test_no_channel_returns_silently(self):
app = MagicMock()
mq = MagicMock()
with patch("paperscout.scout.settings", _make_settings(channel="")):
notify_channel(app, _make_result(), mq)
mq.enqueue.assert_not_called()
def test_empty_result_posts_nothing(self):
app = MagicMock()
mq = MagicMock()
with patch("paperscout.scout.settings", _make_settings()):
notify_channel(app, _make_result(), mq)
mq.enqueue.assert_not_called()
# ── Probe hits ────────────────────────────────────────────────────────────
def test_frontier_hits_batched_with_count_and_links(self):
app = MagicMock()
mq = MagicMock()
hits = [_recent_hit(tier="frontier", number=n) for n in (4033, 4034, 4035)]
result = _make_result(probe_hits=hits)
with patch("paperscout.scout.settings", _make_settings()):
notify_channel(app, result, mq)
mq.enqueue.assert_called_once()
text = mq.enqueue.call_args[0][1]
assert "3 new frontier draft(s)" in text
for h in hits:
assert h.url in text
def test_other_probe_hits_batched(self):
app = MagicMock()
mq = MagicMock()
hits = [_recent_hit(tier="recent", number=n) for n in (5000, 5001)]
result = _make_result(probe_hits=hits)
with patch("paperscout.scout.settings", _make_settings()):
notify_channel(app, result, mq)
text = mq.enqueue.call_args[0][1]
assert "2 new draft(s) discovered" in text
def test_cold_hits_batched_with_other(self):
app = MagicMock()
mq = MagicMock()
hit = _recent_hit(tier="cold")
result = _make_result(probe_hits=[hit])
with patch("paperscout.scout.settings", _make_settings()):
notify_channel(app, result, mq)
assert "1 new draft(s) discovered" in mq.enqueue.call_args[0][1]
def test_frontier_suppressed_when_disabled(self):
app = MagicMock()
mq = MagicMock()
result = _make_result(probe_hits=[_recent_hit(tier="frontier")])
with patch("paperscout.scout.settings", _make_settings(notify_on_frontier_hit=False)):
notify_channel(app, result, mq)
mq.enqueue.assert_not_called()
def test_any_draft_suppressed_when_disabled(self):
app = MagicMock()
mq = MagicMock()
for tier in ("recent", "cold"):
result = _make_result(probe_hits=[_recent_hit(tier=tier)])
with patch("paperscout.scout.settings", _make_settings(notify_on_any_draft=False)):
notify_channel(app, result, mq)
mq.enqueue.assert_not_called()
def test_last_modified_shown_in_batch(self):
app = MagicMock()
mq = MagicMock()
lm = datetime.now(timezone.utc) - timedelta(hours=3)
hit = _recent_hit(tier="frontier", last_modified=lm)
result = _make_result(probe_hits=[hit])
with patch("paperscout.scout.settings", _make_settings()):
notify_channel(app, result, mq)
assert "3h ago" in mq.enqueue.call_args[0][1]
# ── D→P transitions ───────────────────────────────────────────────────────
def test_dp_all_transitions_are_batched(self):
app = MagicMock()
mq = MagicMock()
paper = Paper(
id="P2300R11",
title="Senders",
author="Unknown Author",
url="https://wg21.link/P2300R11",
)
tr = DPTransition(
paper=paper,
draft_url="https://isocpp.org/files/papers/D2300R11.pdf",
last_modified=1_700_000_000.0,
discovered_at=1_699_900_000.0,
)
result = _make_result(dp_transitions=[tr])
with patch("paperscout.scout.settings", _make_settings()):
notify_channel(app, result, mq)
text = mq.enqueue.call_args[0][1]
assert "draft(s) now published" in text
assert "<https://wg21.link/P2300R11|P2300R11>" in text
assert "D2300R11" in text
def test_dp_suppressed_when_disabled(self):
app = MagicMock()
mq = MagicMock()
paper = Paper(id="P2300R11", title="X", author="Y")
tr = DPTransition(
paper=paper,
draft_url="https://isocpp.org/files/papers/D2300R11.pdf",
last_modified=None,
discovered_at=0.0,
)
result = _make_result(dp_transitions=[tr])
with patch("paperscout.scout.settings", _make_settings(notify_on_dp_transition=False)):
notify_channel(app, result, mq)
mq.enqueue.assert_not_called()
def test_dp_batch_contains_draft_link(self):
app = MagicMock()
mq = MagicMock()
paper = Paper(id="P9999R0", title="Foo", author="Bar", url="")
tr = DPTransition(
paper=paper,
draft_url="https://isocpp.org/files/papers/D9999R0.pdf",
last_modified=None,
discovered_at=0.0,
)
result = _make_result(dp_transitions=[tr])
with patch("paperscout.scout.settings", _make_settings()):
notify_channel(app, result, mq)
text = mq.enqueue.call_args[0][1]
assert "draft(s) now published" in text
assert "D9999R0.pdf" in text
# ── notify_users ──────────────────────────────────────────────────────────────
class TestNotifyUsers:
def test_no_matches_posts_nothing(self):
app = MagicMock()
mq = MagicMock()
result = _make_result()
notify_users(app, result, mq)
mq.enqueue.assert_not_called()
def test_author_match_sends_dm(self):
app = MagicMock()
mq = MagicMock()
paper = Paper(
id="P2300R11", title="Senders", author="Eric Niebler", url="https://wg21.link/P2300R11"
)
pum = PerUserMatches(papers=[(paper, "author")], probe_hits=[])
result = _make_result(per_user_matches={"U123": pum})
notify_users(app, result, mq)
mq.enqueue.assert_called_once()
channel, text = mq.enqueue.call_args[0]
assert channel == "U123"
assert "P2300R11" in text
assert "author match" in text
def test_paper_match_sends_dm(self):
app = MagicMock()
mq = MagicMock()
paper = Paper(id="P2300R11", title="X", author="Someone", url="https://wg21.link/P2300R11")
pum = PerUserMatches(papers=[(paper, "paper")], probe_hits=[])
result = _make_result(per_user_matches={"U456": pum})
notify_users(app, result, mq)
channel, text = mq.enqueue.call_args[0]
assert channel == "U456"
assert "paper match" in text
def test_probe_hit_match_sends_dm(self):
app = MagicMock()
mq = MagicMock()
hit = _recent_hit()
pum = PerUserMatches(papers=[], probe_hits=[(hit, "author")])
result = _make_result(per_user_matches={"U789": pum})
notify_users(app, result, mq)
mq.enqueue.assert_called_once()
_, text = mq.enqueue.call_args[0]
assert hit.url in text
def test_multiple_users_get_separate_dms(self):
app = MagicMock()
mq = MagicMock()
paper = Paper(id="P2300R11", title="X", author="Niebler")
pum = PerUserMatches(papers=[(paper, "author")], probe_hits=[])
result = _make_result(per_user_matches={"U1": pum, "U2": pum})
notify_users(app, result, mq)
assert mq.enqueue.call_count == 2
channels = {call[0][0] for call in mq.enqueue.call_args_list}
assert channels == {"U1", "U2"}
# ── _batch_lines ──────────────────────────────────────────────────────────────
class TestBatchLines:
def test_single_batch_when_small(self):
batches = _batch_lines(["line1", "line2", "line3"], max_len=1000)
assert len(batches) == 1
def test_splits_when_over_limit(self):
lines = ["x" * 100] * 10
batches = _batch_lines(lines, max_len=250)
assert len(batches) > 1
def test_empty_lines(self):
assert _batch_lines([], max_len=1000) == []
def test_single_line_exceeding_limit(self):
batches = _batch_lines(["x" * 500], max_len=100)
assert len(batches) == 1
# ── _reply_opts ───────────────────────────────────────────────────────────────
class TestReplyOpts:
def test_no_thread(self):
opts = _reply_opts({"ts": "123"})
assert "thread_ts" not in opts
assert opts["unfurl_links"] is False
def test_with_thread(self):
opts = _reply_opts({"ts": "123", "thread_ts": "456"})
assert opts["thread_ts"] == "456"
# ── _handle_watchlist ─────────────────────────────────────────────────────────
class TestHandleWatchlist:
def test_add_new_author(self, fake_pool):
say = MagicMock()
wl = UserWatchlist(fake_pool)
_handle_watchlist(["add", "Niebler"], "U1", wl, say, {})
text = say.call_args[1]["text"]
assert "Added" in text
assert "author" in text
def test_add_paper_number(self, fake_pool):
say = MagicMock()
wl = UserWatchlist(fake_pool)
_handle_watchlist(["add", "2300"], "U1", wl, say, {})
text = say.call_args[1]["text"]
assert "Added" in text
assert "paper number" in text
def test_add_existing_entry(self, fake_pool):
wl = UserWatchlist(fake_pool)
wl.add("U1", "Niebler")
say = MagicMock()
_handle_watchlist(["add", "Niebler"], "U1", wl, say, {})
assert "already" in say.call_args[1]["text"]
def test_add_multi_word_name(self, fake_pool):
say = MagicMock()
wl = UserWatchlist(fake_pool)
_handle_watchlist(["add", "Eric", "Niebler"], "U1", wl, say, {})
assert "Added" in say.call_args[1]["text"]
def test_remove_existing(self, fake_pool):
wl = UserWatchlist(fake_pool)
wl.add("U1", "Niebler")
say = MagicMock()
_handle_watchlist(["remove", "Niebler"], "U1", wl, say, {})
assert "Removed" in say.call_args[1]["text"]
def test_remove_nonexistent(self, fake_pool):
say = MagicMock()
wl = UserWatchlist(fake_pool)
_handle_watchlist(["remove", "Nobody"], "U1", wl, say, {})
assert "not on your watchlist" in say.call_args[1]["text"]
def test_list_shows_entries(self, fake_pool):
wl = UserWatchlist(fake_pool)
wl.add("U1", "Niebler")
say = MagicMock()
_handle_watchlist(["list"], "U1", wl, say, {})
assert "niebler" in say.call_args[1]["text"]
def test_no_args_shows_list(self, fake_pool):
wl = UserWatchlist(fake_pool)
wl.add("U1", "Stroustrup")
say = MagicMock()
_handle_watchlist([], "U1", wl, say, {})
assert "stroustrup" in say.call_args[1]["text"]
def test_add_without_name(self, fake_pool):
say = MagicMock()
wl = UserWatchlist(fake_pool)
_handle_watchlist(["add"], "U1", wl, say, {})
assert "Usage" in say.call_args[1]["text"]
def test_invalid_action(self, fake_pool):
say = MagicMock()
wl = UserWatchlist(fake_pool)
_handle_watchlist(["bogus", "name"], "U1", wl, say, {})
assert "Usage" in say.call_args[1]["text"]
def test_reply_opts_forwarded(self, fake_pool):
say = MagicMock()
wl = UserWatchlist(fake_pool)
_handle_watchlist(["list"], "U1", wl, say, {"thread_ts": "t1"})
assert say.call_args[1]["thread_ts"] == "t1"
# ── _show_watchlist ───────────────────────────────────────────────────────────
class TestShowWatchlist:
def test_empty_watchlist(self, fake_pool):
say = MagicMock()
_show_watchlist("U1", UserWatchlist(fake_pool), say, {})
assert "empty" in say.call_args[1]["text"].lower()
def test_non_empty_watchlist(self, fake_pool):
wl = UserWatchlist(fake_pool)
wl.add("U1", "Baker")
say = MagicMock()
_show_watchlist("U1", wl, say, {})
assert "baker" in say.call_args[1]["text"]
def test_shows_type_labels(self, fake_pool):
wl = UserWatchlist(fake_pool)
wl.add("U1", "niebler")
wl.add("U1", "2300")
say = MagicMock()
_show_watchlist("U1", wl, say, {})
text = say.call_args[1]["text"]
assert "author" in text
assert "paper" in text
# ── _handle_status ────────────────────────────────────────────────────────────
class TestHandleStatus:
def test_status_never_polled(self, fake_pool):
state = ProbeState(fake_pool)
say = MagicMock()
with patch("paperscout.scout.settings", _make_settings()):
_handle_status(state, lambda: 42, say, {})
text = say.call_args[1]["text"]
assert "42" in text and "never" in text
def test_status_after_poll(self, fake_pool):
state = ProbeState(fake_pool)
state.touch_poll()
say = MagicMock()
with patch("paperscout.scout.settings", _make_settings()):
_handle_status(state, lambda: 100, say, {})
text = say.call_args[1]["text"]
assert "100" in text and "never" not in text
class TestFormatStatusMessage:
def test_matches_handle_status_output(self, fake_pool):
state = ProbeState(fake_pool)
say = MagicMock()
with patch("paperscout.scout.settings", _make_settings()):
expected = format_status_message(state, lambda: 42)
_handle_status(state, lambda: 42, say, {})
assert say.call_args[1]["text"] == expected
class TestEnqueueStartupStatus:
def test_enqueues_when_channel_configured(self, fake_pool):
mq = MagicMock()
state = ProbeState(fake_pool)
with patch("paperscout.scout.settings", _make_settings(channel="C-alerts")):
enqueue_startup_status(mq, state, lambda: 7)
mq.enqueue.assert_called_once()
assert mq.enqueue.call_args[0][0] == "C-alerts"
assert "Paperscout Status" in mq.enqueue.call_args[0][1]
assert "7" in mq.enqueue.call_args[0][1]
def test_skips_when_no_channel(self, fake_pool):
mq = MagicMock()
state = ProbeState(fake_pool)
with patch("paperscout.scout.settings", _make_settings(channel="")):
enqueue_startup_status(mq, state, lambda: 1)
mq.enqueue.assert_not_called()
# ── register_handlers ─────────────────────────────────────────────────────────
class TestRegisterHandlers:
def _setup(self, fake_pool):
app = MagicMock()
registered: dict = {}
def capture_event(name):
def decorator(fn):
registered[name] = fn
return fn
return decorator
app.event.side_effect = capture_event
user_watchlist = UserWatchlist(fake_pool)
state = ProbeState(fake_pool)
register_handlers(app, user_watchlist, state, lambda: 99)
return registered, user_watchlist, state
def test_app_mention_status(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
with patch("paperscout.scout.settings", _make_settings()):
registered["app_mention"](
event={"text": "<@U1> status", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
assert "Status" in say.call_args[1]["text"]
def test_app_mention_empty_text(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["app_mention"](
event={"text": "", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_not_called()
def test_app_mention_only_mention_no_command(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["app_mention"](
event={"text": "<@U1>", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_not_called()
def test_message_dm_dispatches(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
with patch("paperscout.scout.settings", _make_settings()):
registered["message"](
event={"text": "status", "channel_type": "im", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_called_once()
def test_message_dm_strips_mention(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
with patch("paperscout.scout.settings", _make_settings()):
registered["message"](
event={"text": "<@U1> status", "channel_type": "im", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_called_once()
def test_message_dm_empty_after_strip(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["message"](
event={"text": "<@U1>", "channel_type": "im", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_not_called()
def test_message_channel_with_mention_ignored_by_message_handler(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["message"](
event={"text": "<@U1> status", "channel_type": "channel", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_not_called()
def test_app_mention_channel_watchlist_silently_ignored(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["app_mention"](
event={
"text": "<@U1> watchlist list",
"ts": "1",
"channel_type": "channel",
"user": "U1",
},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_not_called()
def test_message_mpim_watchlist_gets_error(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["message"](
event={
"text": "<@U1> watchlist add niebler",
"channel_type": "mpim",
"ts": "1",
"user": "U1",
},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_called_once()
assert "1:1 DM" in say.call_args[1]["text"]
def test_message_mpim_status_responds(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
with patch("paperscout.scout.settings", _make_settings()):
registered["message"](
event={"text": "<@U1> status", "channel_type": "mpim", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_called_once()
assert "Status" in say.call_args[1]["text"]
def test_message_subtype_ignored(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["message"](
event={
"text": "status",
"subtype": "message_changed",
"channel_type": "im",
"user": "U1",
},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_not_called()
def test_message_bot_id_ignored(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["message"](
event={"text": "status", "bot_id": "B123", "channel_type": "im", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_not_called()
def test_message_empty_text_ignored(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["message"](
event={"text": "", "channel_type": "im", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_not_called()
def test_dispatch_help(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["message"](
event={"text": "help", "channel_type": "im", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
assert "Commands" in say.call_args[1]["text"]
def test_dispatch_unknown_command(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["message"](
event={"text": "foobar", "channel_type": "im", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
assert "Unknown" in say.call_args[1]["text"]
def test_dispatch_watchlist_list_in_dm(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["message"](
event={"text": "watchlist list", "channel_type": "im", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_called_once()
assert "empty" in say.call_args[1]["text"].lower()
def test_dispatch_empty_text(self, fake_pool):
registered, _, _ = self._setup(fake_pool)
say = MagicMock()
registered["message"](
event={"text": " ", "channel_type": "im", "ts": "1", "user": "U1"},
context={"bot_user_id": ""},
say=say,
)
say.assert_not_called()
# ── _handle_version ───────────────────────────────────────────────────────────
class TestHandleVersion:
def test_version_contains_version_string(self):
say = MagicMock()
with patch("paperscout.__version__", "1.2.3"):
_handle_version(say, {})
text = say.call_args[1]["text"]
assert "1.2.3" in text
assert "Paperscout" in text
def test_version_forwards_reply_opts(self):
say = MagicMock()
_handle_version(say, {"thread_ts": "t1"})
assert say.call_args[1]["thread_ts"] == "t1"
# ── _format_uptime / _handle_uptime ──────────────────────────────────────────
class TestUptime:
def test_format_uptime_minutes_only(self):
delta = timedelta(minutes=5)
assert _format_uptime(delta) == "5m"
def test_format_uptime_hours_and_minutes(self):
delta = timedelta(hours=3, minutes=12)
assert _format_uptime(delta) == "3h 12m"
def test_format_uptime_days_hours_minutes(self):
delta = timedelta(days=2, hours=1, minutes=45)
assert _format_uptime(delta) == "2d 1h 45m"
def test_format_uptime_zero(self):
delta = timedelta(seconds=0)
assert _format_uptime(delta) == "0m"
def test_handle_uptime_with_launch_time(self):
say = MagicMock()
launch = datetime(2026, 3, 16, 10, 0, 0, tzinfo=timezone.utc)
now = datetime(2026, 3, 16, 13, 30, 0, tzinfo=timezone.utc)
with patch("paperscout.scout.datetime") as mock_dt:
mock_dt.now.return_value = now
mock_dt.side_effect = lambda *a, **kw: datetime(*a, **kw)
_handle_uptime(launch, say, {})
text = say.call_args[1]["text"]
assert "started" in text.lower()
assert "3h 30m" in text
assert "2026-03-16 10:00:00 UTC" in text
def test_handle_uptime_no_launch_time(self):
say = MagicMock()
_handle_uptime(None, say, {})
assert "not available" in say.call_args[1]["text"].lower()
def test_handle_uptime_forwards_reply_opts(self):
say = MagicMock()
launch = datetime.now(timezone.utc) - timedelta(hours=1)
_handle_uptime(launch, say, {"thread_ts": "t1"})
assert say.call_args[1]["thread_ts"] == "t1"
# ── dispatch: version / uptime ────────────────────────────────────────────────
class TestDispatchVersionUptime:
def _setup(self, fake_pool, launch_time=None):
app = MagicMock()
registered: dict = {}
def capture_event(name):
def decorator(fn):
registered[name] = fn
return fn
return decorator
app.event.side_effect = capture_event
user_watchlist = UserWatchlist(fake_pool)
state = ProbeState(fake_pool)
register_handlers(app, user_watchlist, state, lambda: 99, launch_time)
return registered
def test_dispatch_version(self, fake_pool):
registered = self._setup(fake_pool)
say = MagicMock()
registered["message"](
event={"text": "version", "channel_type": "im", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_called_once()
assert "Paperscout" in say.call_args[1]["text"]
def test_dispatch_uptime(self, fake_pool):
launch = datetime.now(timezone.utc) - timedelta(hours=2, minutes=15)
registered = self._setup(fake_pool, launch_time=launch)
say = MagicMock()
registered["message"](
event={"text": "uptime", "channel_type": "im", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
say.assert_called_once()
text = say.call_args[1]["text"]
assert "started" in text.lower()
assert "2h 15m" in text
def test_help_mentions_version_and_uptime(self, fake_pool):
registered = self._setup(fake_pool)
say = MagicMock()
registered["message"](
event={"text": "help", "channel_type": "im", "ts": "1", "user": "U1"},
context={"bot_user_id": "U1"},
say=say,
)
text = say.call_args[1]["text"]
assert "version" in text
assert "uptime" in text
# ── create_app ────────────────────────────────────────────────────────────────
class TestCreateApp:
def test_create_app_uses_settings(self):
from paperscout.scout import create_app
mock_settings = MagicMock()
mock_settings.slack_bot_token = "xoxb-test"
mock_settings.slack_signing_secret = "secret"
with patch("paperscout.scout.settings", mock_settings):
with patch("paperscout.scout.App") as mock_app_cls:
create_app()
mock_app_cls.assert_called_once_with(
token="xoxb-test",
signing_secret="secret",
)