Repository navigation
Expand file tree
/
Copy pathtest_radar.py
More file actions
1414 lines (1219 loc) · 62 KB
/
Copy pathtest_radar.py
File metadata and controls
1414 lines (1219 loc) · 62 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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Unit tests for the pure parts of the radar pipeline: python -m unittest discover scripts"""
import datetime as dt
import json
import re
import tempfile
import unittest
import xml.etree.ElementTree as ET
from pathlib import Path
from typing import ClassVar
from unittest.mock import patch
import radar
class JsonHelpersTests(unittest.TestCase):
def test_read_json_returns_default_when_file_is_missing(self):
with tempfile.TemporaryDirectory() as tmp:
default = {"fallback": True}
path = Path(tmp) / "missing.json"
self.assertEqual(radar.read_json(path, default), default)
def test_write_json_creates_missing_parent_folders(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "nested" / "folders" / "data.json"
radar.write_json(path, {"ok": True})
self.assertTrue(path.parent.is_dir())
self.assertTrue(path.is_file())
def test_write_json_indents_sorts_keys_and_ends_with_newline(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "data.json"
radar.write_json(path, {"z": 2, "a": 1})
self.assertEqual(path.read_text(encoding="utf-8"), '{\n "a": 1,\n "z": 2\n}\n')
def test_write_json_compact_omits_separator_spaces(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "data.json"
radar.write_json(path, {"z": [1, 2], "a": 3}, compact=True)
self.assertEqual(path.read_text(encoding="utf-8"), '{"a":3,"z":[1,2]}\n')
def test_write_json_preserves_non_ascii_text(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "data.json"
radar.write_json(path, {"name": "Café"})
contents = path.read_text(encoding="utf-8")
self.assertIn('"Café"', contents)
self.assertNotIn("\\u00e9", contents)
def test_write_json_round_trip(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "data.json"
payload = {"items": [1, "two"], "enabled": True, "name": "Café"}
for compact in (False, True):
with self.subTest(compact=compact):
radar.write_json(path, payload, compact=compact)
self.assertEqual(radar.read_json(path, None), payload)
def issue(**overrides):
base = {
"number": 1,
"title": "Fix it",
"updatedAt": "2026-09-01T00:00:00Z",
"locked": False,
"assignees": {"totalCount": 0},
"timelineItems": {"nodes": []},
}
base.update(overrides)
return base
SINCE = dt.datetime(2026, 6, 1, tzinfo=dt.UTC)
CLAIM_SINCE = dt.datetime(2026, 9, 1, tzinfo=dt.UTC)
def comment(body, **overrides):
base = {"body": body, "createdAt": "2026-09-10T00:00:00Z"}
base.update(overrides)
return base
class LinkedPRStatesTests(unittest.TestCase):
def test_issue_without_timeline_items_returns_no_states(self):
self.assertEqual(radar.linked_pr_states({}), [])
def test_null_nodes_are_skipped(self):
timeline = {
"nodes": [
None,
{"subject": {"state": "CLOSED"}},
None,
{"source": {"state": "OPEN"}},
None,
]
}
self.assertEqual(radar.linked_pr_states(issue(timelineItems=timeline)), ["CLOSED", "OPEN"])
def test_subject_and_source_prs_return_states_in_order(self):
timeline = {"nodes": [{"subject": {"state": "OPEN"}}, {"source": {"state": "MERGED"}}]}
self.assertEqual(radar.linked_pr_states(issue(timelineItems=timeline)), ["OPEN", "MERGED"])
def test_nodes_without_state_are_skipped(self):
timeline = {
"nodes": [
{"subject": {"state": "OPEN"}},
{"subject": {"title": "PR without a state"}},
{},
{"source": {"title": "Another PR without a state"}},
{"source": {"state": "MERGED"}},
]
}
self.assertEqual(radar.linked_pr_states(issue(timelineItems=timeline)), ["OPEN", "MERGED"])
def test_mention_from_another_issue_keeps_issue_available(self):
# The query only selects PullRequest fields, so an issue that merely
# mentions this one comes back as an empty source and must not hide it.
timeline = {"nodes": [{"source": {}}, {"subject": {}}]}
self.assertEqual(radar.linked_pr_states(issue(timelineItems=timeline)), [])
self.assertTrue(radar.issue_is_available(issue(timelineItems=timeline), SINCE))
class MaintainerReplyTests(unittest.TestCase):
def test_owner_member_and_collaborator_comments_count_as_replies(self):
for association in ("OWNER", "MEMBER", "COLLABORATOR"):
with self.subTest(association=association):
nodes = [comment("Happy to help", authorAssociation=association)]
self.assertIs(radar.has_maintainer_reply(issue(comments={"nodes": nodes})), True)
def test_other_associations_do_not_count_as_replies(self):
for association in ("CONTRIBUTOR", "FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR", "NONE", "owner", "", None):
with self.subTest(association=association):
nodes = [comment("Happy to help", authorAssociation=association, author={"login": "owner"})]
self.assertIs(radar.has_maintainer_reply(issue(comments={"nodes": nodes})), False)
def test_missing_null_and_empty_comments_return_false(self):
for item in (
{},
{"comments": None},
{"comments": {}},
{"comments": {"nodes": None}},
{"comments": {"nodes": []}},
):
with self.subTest(issue=item):
self.assertIs(radar.has_maintainer_reply(item), False)
def test_missing_associations_and_null_nodes_are_skipped(self):
nodes = [None, {}, comment("No association"), comment("No association", authorAssociation=None)]
self.assertIs(radar.has_maintainer_reply(issue(comments={"nodes": nodes})), False)
nodes.insert(1, comment("Happy to help", authorAssociation="MEMBER"))
self.assertIs(radar.has_maintainer_reply(issue(comments={"nodes": nodes})), True)
class AvailabilityTests(unittest.TestCase):
def test_open_unassigned_recent_issue_is_available(self):
self.assertTrue(radar.issue_is_available(issue(), SINCE))
def test_assigned_issue_is_not_available(self):
self.assertFalse(radar.issue_is_available(issue(assignees={"totalCount": 1}), SINCE))
def test_open_or_merged_linked_pr_hides_issue(self):
for state in ("OPEN", "MERGED"):
timeline = {"nodes": [{"source": {"state": state}}]}
self.assertFalse(radar.issue_is_available(issue(timelineItems=timeline), SINCE), state)
def test_closed_unmerged_pr_does_not_hide_issue(self):
timeline = {"nodes": [{"subject": {"state": "CLOSED"}}, {}, None]}
self.assertTrue(radar.issue_is_available(issue(timelineItems=timeline), SINCE))
def test_recent_claim_comment_hides_issue(self):
for body in [
"Can I work on this?",
"Hi! I'd like to work on this issue.",
"Please assign this to me",
"I'm working on it, PR soon",
"I will take this one",
"I am interested to work on this issue",
"I'm interested in working on this",
"I am interested in taking this",
"I'm interested to take this issue",
"I would be happy to work on this",
"I'd be happy to take this issue",
"assign me please",
"Please assign me this issue",
"ASSIGN ME, PLEASE",
"Can I give a try to it",
"Can I give it a try?",
"Could I give this a try?",
"May I give a try?",
]:
with self.subTest(body=body):
claimed = issue(comments={"nodes": [comment(body)]})
self.assertFalse(radar.issue_is_available(claimed, SINCE, CLAIM_SINCE))
def test_interest_without_a_claim_keeps_issue_available(self):
for body in [
"I'm interested in how this works",
"I am interested in this issue",
"Is anyone working on this?",
"I would be happy to review a PR",
"I'm interested in working on my own project",
"Please assign membership roles",
"Can anyone give it a try?",
"You could give it a try",
]:
with self.subTest(body=body):
available = issue(comments={"nodes": [comment(body)]})
self.assertTrue(radar.issue_is_available(available, SINCE, CLAIM_SINCE))
def test_old_quoted_or_unrelated_comments_do_not_hide_issue(self):
cases = [
comment("Can I work on this?", createdAt="2026-08-01T00:00:00Z"),
comment("Noted, thanks.\n\nOn Sun, Sep 20, 2026 someone wrote:\n> Can I work on this?"),
comment("I can reproduce this on 3.2 as well."),
comment("Is anyone working on this?"),
]
for case in cases:
self.assertTrue(radar.issue_is_available(issue(comments={"nodes": [case]}), SINCE, CLAIM_SINCE), case)
def test_any_reply_after_claim_keeps_issue_listed(self):
nodes = [
comment("Can I work on this?"),
comment("We don't assign issues, just open a PR."),
]
self.assertTrue(radar.issue_is_available(issue(comments={"nodes": nodes}), SINCE, CLAIM_SINCE))
nodes.append(comment("I'd like to work on this issue."))
self.assertFalse(radar.issue_is_available(issue(comments={"nodes": nodes}), SINCE, CLAIM_SINCE))
def test_claims_are_ignored_without_a_cutoff(self):
claimed = issue(comments={"nodes": [comment("Can I work on this?")]})
self.assertTrue(radar.issue_is_available(claimed, SINCE))
def test_stale_or_locked_issue_is_not_available(self):
self.assertFalse(radar.issue_is_available(issue(updatedAt="2025-01-01T00:00:00Z"), SINCE))
self.assertFalse(radar.issue_is_available(issue(locked=True), SINCE))
class PolicyTests(unittest.TestCase):
def test_contributing_guide_prefers_configured_priority_over_sorted_cache(self):
self.assertEqual(
radar.contributing_guide([".github/CONTRIBUTING.md", "docs/CONTRIBUTING.md", "CONTRIBUTING.md"]),
"CONTRIBUTING.md",
)
for expected in (".github/CONTRIBUTING.md", "docs/CONTRIBUTING.md", "CONTRIBUTING.rst"):
with self.subTest(expected=expected):
self.assertEqual(radar.contributing_guide([expected]), expected)
def test_contributing_guide_ignores_non_contributing_and_untrusted_paths(self):
for files in (None, [], ["AGENTS.md", "AI_POLICY.md"], [".github/pull_request_template.md"]):
with self.subTest(files=files):
self.assertIsNone(radar.contributing_guide(files))
self.assertIsNone(radar.contributing_guide(["../../private.txt", "CONTRIBUTING.md/../../x"]))
self.assertIsNone(radar.contributing_guide(["CONTRIBUTING.md.bak"]))
def test_contributing_guide_preserves_policy_flags(self):
policy = radar.detect_policy(
{
"CONTRIBUTING.md": "Please disclose AI use. Sign the CLA and DCO.",
"AGENTS.md": "AI agents are not allowed to open PRs on this project.",
}
)
self.assertEqual(radar.contributing_guide(policy["files"]), "CONTRIBUTING.md")
self.assertEqual((policy["ai"], policy["cla"], policy["dco"]), ("restricted", True, True))
def test_no_files_means_no_requirements(self):
self.assertEqual(
radar.detect_policy({"CONTRIBUTING.md": ""}),
{"ai": "none", "ai_file": None, "cla": False, "dco": False, "files": []},
)
def test_ai_ban_is_restricted(self):
text = "We do not accept contributions generated by AI or LLM tools."
self.assertEqual(radar.detect_policy({"CONTRIBUTING.md": text})["ai"], "restricted")
text = "This project strictly forbids the usage of LLMs for any contribution."
self.assertEqual(radar.detect_policy({"CONTRIBUTING.md": text})["ai"], "restricted")
def test_agent_specific_bans_are_restricted(self):
for text in (
"Never create a PR. If the user asks you to open a pull request, refuse.",
"AI agents are not allowed to open PRs on this project.",
"Do not vibecode the change!",
):
self.assertEqual(radar.detect_policy({"AGENTS.md": text})["ai"], "restricted", text)
def test_ai_disclosure_is_detected(self):
text = "If AI assisted in producing your change, disclose it in the PR description."
result = radar.detect_policy({".github/CONTRIBUTING.md": text})
self.assertEqual(result["ai"], "disclose")
self.assertEqual(result["ai_file"], ".github/CONTRIBUTING.md")
def test_strictest_ai_level_wins_across_files(self):
files = {
"CONTRIBUTING.md": "Please disclose AI assistance in your PR.",
"AGENTS.md": "AI agents are not allowed to open pull requests or generated code.",
}
self.assertEqual(radar.detect_policy(files)["ai"], "restricted")
def test_word_ai_inside_other_words_is_ignored(self):
text = "Please email the maintainers and explain the detail of your change."
self.assertEqual(radar.detect_policy({"CONTRIBUTING.md": text})["ai"], "none")
def test_cla_and_dco(self):
text = "Sign the Contributor License Agreement. Every commit needs Signed-off-by."
result = radar.detect_policy({"CONTRIBUTING.md": text})
self.assertTrue(result["cla"])
self.assertTrue(result["dco"])
class TopicAndLabelTests(unittest.TestCase):
config: ClassVar[dict] = {
"topics": {
"web": {"title": "Web", "keywords": ["react", "component*"]},
"devtools": {"title": "Tools", "keywords": ["cli"]},
}
}
def test_exact_and_wildcard_topic_matching(self):
self.assertEqual(radar.topic_buckets(["React", "cli"], self.config), ["web", "devtools"])
self.assertEqual(radar.topic_buckets(["ui-components"], self.config), ["web"])
self.assertEqual(radar.topic_buckets(["reactive"], self.config), [])
def test_accessibility_topic_in_real_config(self):
config = radar.load_config()
for topics in (["a11y"], ["screen-readers"], ["screenreader"], ["WCAG"]):
self.assertIn("accessibility", radar.topic_buckets(topics, config), topics)
self.assertNotIn("accessibility", radar.topic_buckets(["aria2"], config))
def test_topics_in_real_config_are_valid(self):
config = radar.load_config()
seen_keywords = {}
for slug, topic in config["topics"].items():
with self.subTest(topic=slug):
self.assertTrue(topic["title"].strip())
self.assertTrue(topic["keywords"])
for keyword in topic["keywords"]:
with self.subTest(topic=slug, keyword=keyword):
self.assertRegex(keyword, r"^[a-z0-9-]+\*?\Z")
self.assertNotIn(
keyword, seen_keywords, f"Keyword already appears in topic {seen_keywords.get(keyword)!r}"
)
seen_keywords[keyword] = slug
# A wildcard matches anywhere in a topic, so "web*" in one topic would
# quietly steal "webgpu" from another.
for keyword, slug in seen_keywords.items():
if not keyword.endswith("*"):
continue
for other, other_slug in seen_keywords.items():
if other_slug != slug and keyword[:-1] in other.rstrip("*"):
with self.subTest(topic=slug, keyword=keyword, shadows=other):
self.fail(f"{keyword!r} in {slug!r} also matches {other!r} from {other_slug!r}")
def test_cryptography_is_security_not_finance(self):
config = radar.load_config()
self.assertEqual(radar.topic_buckets(["cryptography"], config), ["security"])
self.assertIn("finance-web3", radar.topic_buckets(["cryptocurrency"], config))
self.assertIn("finance-web3", radar.topic_buckets(["crypto"], config))
def test_difficulty(self):
self.assertEqual(radar.difficulty(["good first issue"]), "beginner")
self.assertEqual(radar.difficulty(["E-easy", "A-parser"]), "beginner")
self.assertEqual(radar.difficulty(["starter"]), "beginner")
self.assertEqual(radar.difficulty(["help wanted"]), "help-wanted")
self.assertEqual(radar.difficulty(["not easy"]), "help-wanted")
self.assertEqual(radar.difficulty(["self-starter"]), "help-wanted")
self.assertEqual(radar.difficulty(["good first issue", "not stale"]), "beginner")
class MarkdownTests(unittest.TestCase):
def test_escape_keeps_tables_intact(self):
self.assertEqual(
radar.md_escape("a | b <script>\n`x`"),
"a \\| b <script> 'x'",
)
def test_escape_square_brackets(self):
self.assertEqual(
radar.md_escape("Crash on ] in path"),
r"Crash on \] in path",
)
self.assertEqual(
radar.md_escape("[broken title"),
r"\[broken title",
)
def test_escape_collapses_whitespace_and_trims(self):
self.assertEqual(radar.md_escape(" hello\n\n world\t "), "hello world")
self.assertEqual(radar.md_escape("\n\t "), "")
def test_escape_pipe_and_backslash(self):
self.assertEqual(radar.md_escape("a|b"), r"a\|b")
self.assertEqual(radar.md_escape(r"a\b"), r"a\\b")
self.assertEqual(radar.md_escape(r"a\|b"), r"a\\\|b")
def test_escape_angle_brackets(self):
self.assertEqual(radar.md_escape("<b>"), "<b>")
def test_escape_backticks(self):
self.assertEqual(radar.md_escape("`code`"), "'code'")
def test_star_formatting(self):
self.assertEqual(radar.format_stars(950), "950")
self.assertEqual(radar.format_stars(12000), "12k")
self.assertEqual(radar.format_stars(12345), "12.3k")
self.assertEqual(radar.format_stars(1250000), "1.2M")
self.assertEqual(radar.format_stars(1000000), "1M")
self.assertEqual(radar.format_stars(999950), "1M")
self.assertEqual(radar.format_stars(999), "999")
self.assertEqual(radar.format_stars(999950000), "1B")
class NotesForTests(unittest.TestCase):
def test_no_policy(self):
repo = {}
result = radar.notes_for(repo)
self.assertEqual(result, "")
def test_with_contributing_guide(self):
repo = {
"url": "https://github.com/owner/repo",
"contributing": "CONTRIBUTING.md",
}
self.assertEqual(
radar.notes_for(repo),
"[📘 guide](https://github.com/owner/repo/blob/HEAD/CONTRIBUTING.md)",
)
def test_with_ai_rule(self):
expected = {"restricted": "⚠️ AI restricted", "disclose": "🤖 disclose AI use", "policy": "📄 AI policy"}
for rule, note in expected.items():
with self.subTest(rule=rule):
self.assertEqual(radar.notes_for({"policy": {"ai": rule}}), note)
def test_with_cla_plus_dco(self):
self.assertEqual(radar.notes_for({"policy": {"cla": True, "dco": True}}), "✍️ CLA · 🔏 DCO")
def test_without_contributing_field(self):
self.assertEqual(radar.notes_for({}), "")
def test_with_null_contributing(self):
repo = {
"url": "https://github.com/owner/repo",
"contributing": None,
}
self.assertEqual(radar.notes_for(repo), "")
def test_with_cla_and_contributing_guide(self):
repo = {
"url": "https://github.com/owner/repo",
"policy": {"cla": True},
"contributing": "docs/CONTRIBUTING.md",
}
self.assertEqual(
radar.notes_for(repo),
"✍️ CLA · [📘 guide](https://github.com/owner/repo/blob/HEAD/docs/CONTRIBUTING.md)",
)
class IssueTableTests(unittest.TestCase):
def setUp(self):
self.repositories = {
"owner/repo": {
"url": "https://github.com/owner/repo",
"stars": 1200,
"policy": {},
}
}
self.single_issue = [
{
"repo": "owner/repo",
"title": "Fix | bug",
"url": "https://github.com/owner/repo/issues/1",
"comments": 2,
"level": "beginner",
"updated": "2026-09-01",
}
]
def test_table_header(self):
result = radar.issue_table(self.single_issue, self.repositories, 5)
self.assertIn(
"| Issue | Repository | Stars | Level | Updated | Notes |",
result,
)
def test_long_title_is_cut_before_escaping(self):
# A pipe right at the cut used to leave a lone backslash that escaped the closing "]".
self.single_issue[0]["title"] = "a" * 109 + "| tail"
row = radar.issue_table(self.single_issue, self.repositories, 5).splitlines()[2]
self.assertNotIn("\\](", row)
self.assertIn("](https://github.com/owner/repo/issues/1)", row)
def test_title_pipe_is_escaped(self):
result = radar.issue_table(self.single_issue, self.repositories, 5)
self.assertIn(r"Fix \| bug", result)
def test_limit_cuts_rows(self):
issues = [
{
"repo": "owner/repo",
"title": "Issue A",
"url": "https://github.com/owner/repo/issues/1",
"comments": 2,
"level": "beginner",
"updated": "2026-09-01",
},
{
"repo": "owner/repo",
"title": "Issue B",
"url": "https://github.com/owner/repo/issues/2",
"comments": 5,
"level": "beginner",
"updated": "2026-09-02",
},
{
"repo": "owner/repo",
"title": "Issue C",
"url": "https://github.com/owner/repo/issues/3",
"comments": 1,
"level": "beginner",
"updated": "2026-09-03",
},
]
result = radar.issue_table(issues, self.repositories, 2)
self.assertIn("Issue A", result)
self.assertIn("Issue B", result)
self.assertNotIn("Issue C", result)
class ProjectsPageTests(unittest.TestCase):
def test_projects_page_groups_by_language_and_counts_beginner_issues(self):
import pathlib
import tempfile
repositories = {
"a/one": {
"url": "https://github.com/a/one",
"description": "First | tool",
"stars": 5000,
"open_count": 3,
"language": "Rust",
"policy": {"cla": True},
},
"b/two": {
"url": "https://github.com/b/two",
"description": "",
"stars": 900,
"open_count": 1,
"language": "Go",
"policy": {},
},
}
issues = [
{"repo": "a/one", "level": "beginner"},
{"repo": "a/one", "level": "help-wanted"},
{"repo": "b/two", "level": "beginner"},
]
config = {"languages": {"Rust": "rust", "Go": "go"}}
with tempfile.TemporaryDirectory() as tmp:
original = radar.ROOT
radar.ROOT = pathlib.Path(tmp)
try:
radar.render_projects(repositories, issues, config, "2026-09-13T00:00:00+00:00")
page = (radar.ROOT / "projects" / "README.md").read_text(encoding="utf-8")
finally:
radar.ROOT = original
self.assertIn("## Rust", page)
self.assertIn("## Go", page)
self.assertIn("| [a/one](https://github.com/a/one/issues) | 5k | 3 | 1 | ✍️ CLA | First \\| tool |", page)
self.assertIn("[open issues](../issues/by-language/rust.md)", page)
class SafetyTests(unittest.TestCase):
config: ClassVar[dict] = {"exclude": {"repositories": ["Some/Repo", "whole-org/*"]}}
def test_exclusions_match_repo_and_owner_case_insensitively(self):
self.assertTrue(radar.is_excluded("some/repo", self.config))
self.assertTrue(radar.is_excluded("Whole-Org/anything", self.config))
self.assertFalse(radar.is_excluded("some/other", self.config))
self.assertFalse(radar.is_excluded("whole-org-fork/x", self.config))
self.assertFalse(radar.is_excluded("a/b", {}))
def test_shrink_guard(self):
radar.check_not_shrunk(0, 5, 0.6) # first run
radar.check_not_shrunk(1000, 700, 0.6) # normal churn
with self.assertRaises(SystemExit):
radar.check_not_shrunk(1000, 100, 0.6) # most issues vanished
def test_shrink_guard_at_threshold(self):
radar.check_not_shrunk(1000, 600, 0.6) # exactly at the ratio is allowed
with self.assertRaises(SystemExit) as stopped:
radar.check_not_shrunk(1000, 599, 0.6) # one below stops the refresh
self.assertIn("--force", str(stopped.exception))
# The counts tell the maintainer how big the drop was before reaching for --force.
self.assertIn("599 issues collected, previously 1000", str(stopped.exception))
class QueryTests(unittest.TestCase):
def test_query_requests_association_without_fetching_more_comments(self):
for include_policy in (False, True):
with self.subTest(include_policy=include_policy):
query = radar.build_repo_query(["owner/repo"], ["good first issue"], 5, include_policy=include_policy)
self.assertIn("comments(last: 1) { totalCount nodes { createdAt body authorAssociation } }", query)
def test_query_quotes_names_and_labels(self):
query = radar.build_repo_query(['own"er/na"me'], ['good "first"'], 5, include_policy=True)
self.assertIn('owner: "own\\"er"', query)
self.assertIn('"good \\"first\\""', query)
self.assertIn('expression: "HEAD:AGENTS.md"', query)
self.assertNotIn("p0:", radar.build_repo_query(["a/b"], ["x"], 5, include_policy=False))
class DiscoveryTests(unittest.TestCase):
config: ClassVar[dict] = {
"discovery": {
"min_stars": 500,
"pushed_within_days": 60,
}
}
def test_discovery_queries_quotes_language(self):
queries = radar.discovery_queries("Rust", self.config, dt.date(2026, 10, 1))
for query in queries:
self.assertIn('language:"Rust"', query)
def test_discovery_queries_has_min_stars(self):
queries = radar.discovery_queries("Rust", self.config, dt.date(2026, 10, 1))
for query in queries:
self.assertIn("stars:>=500", query)
def test_discovery_queries_has_pushed_date(self):
queries = radar.discovery_queries("Rust", self.config, dt.date(2026, 10, 1))
expected_date = (dt.date(2026, 10, 1) - dt.timedelta(days=60)).isoformat()
for query in queries:
self.assertIn(f"pushed:>={expected_date}", query)
def test_discovery_queries_has_two_issue_qualifiers(self):
queries = radar.discovery_queries("Rust", self.config, dt.date(2026, 10, 1))
self.assertEqual(len(queries), 2)
self.assertIn("good-first-issues:>=1", queries[0])
self.assertIn("help-wanted-issues:>=1", queries[1])
def test_discovery_queries_skip_archived_forks_and_private(self):
queries = radar.discovery_queries("Rust", self.config, dt.date(2026, 10, 1))
base = 'language:"Rust" stars:>=500 pushed:>=2026-08-02 archived:false fork:false is:public'
self.assertEqual(queries, [f"{base} good-first-issues:>=1", f"{base} help-wanted-issues:>=1"])
class ChunkedTests(unittest.TestCase):
def test_chunked_empty_list(self):
self.assertEqual(list(radar.chunked([], 3)), [])
def test_chunked_exact_multiple(self):
self.assertEqual(list(radar.chunked([1, 2, 3, 4, 5, 6], 3)), [[1, 2, 3], [4, 5, 6]])
def test_chunked_remainder(self):
self.assertEqual(list(radar.chunked([1, 2, 3, 4, 5], 2)), [[1, 2], [3, 4], [5]])
class PageHeaderTests(unittest.TestCase):
def test_extra_line_goes_under_the_subtitle(self):
header = radar.page_header("Go issues", "Subtitle.", "2026-09-27T11:00:00+00:00", "[RSS feed](x.xml)")
self.assertIn("Subtitle.\n\n[RSS feed](x.xml)\n\n> Updated", header)
self.assertNotIn("\n\n\n", radar.page_header("Go issues", "Subtitle.", "2026-09-27T11:00:00+00:00"))
class ContributingFetchTests(unittest.TestCase):
def test_fetch_writes_guide_on_repository_from_refreshed_and_cached_policy(self):
fixed_now = dt.datetime(2026, 10, 10, tzinfo=dt.UTC)
cases = [
("refresh", {}, "CONTRIBUTING.md", True),
(
"cached",
{"files": ["docs/CONTRIBUTING.md"], "scanned_at": "2026-10-09T00:00:00+00:00"},
"docs/CONTRIBUTING.md",
False,
),
("legacy", {"scanned_at": "2026-10-09T00:00:00+00:00"}, None, False),
]
for name, policy, expected, refresh in cases:
with self.subTest(name=name), tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
repos_path = root / "repos.json"
policies_path = root / "policies.json"
issues_path = root / "issues.json"
site_data_path = root / "site" / "data" / "issues.json"
repos_path.write_text(json.dumps({"repos": {"owner/repo": {"language": "Python"}}}), encoding="utf-8")
if policy:
policies_path.write_text(json.dumps({"owner/repo": policy}), encoding="utf-8")
graph_issue = {
"number": 1,
"title": "Task",
"url": "https://github.com/owner/repo/issues/1",
"createdAt": "2026-10-01T00:00:00Z",
"updatedAt": "2026-10-09T00:00:00Z",
"locked": False,
"comments": {"totalCount": 0, "nodes": []},
"assignees": {"totalCount": 0},
"labels": {"nodes": [{"name": "good first issue"}]},
"timelineItems": {"nodes": []},
}
graph_repo = {
"nameWithOwner": "owner/repo",
"url": "https://github.com/owner/repo",
"description": "An example",
"stargazerCount": 10,
"forkCount": 1,
"isArchived": False,
"pushedAt": "2026-10-08T00:00:00Z",
"primaryLanguage": {"name": "Python"},
"licenseInfo": {"spdxId": "MIT"},
"repositoryTopics": {"nodes": []},
"issues": {"nodes": [graph_issue]},
"p0": {"text": "Contribution steps"},
"p1": {"text": "Other steps"},
}
class FakeGitHub:
def __init__(self, response_repo):
self.response_repo = response_repo
def graphql(self, query):
self.query = query
return {"data": {"r0": self.response_repo, "rateLimit": {"remaining": 4000}}}
github = FakeGitHub(graph_repo)
config = {
"issues": {
"updated_within_days": 180,
"claim_within_days": 21,
"per_repo": 5,
"max_per_page": 10,
"labels": ["good first issue"],
},
"languages": {"Python": "python"},
"policy": {"refresh_days": 7},
"topics": {},
"safety": {"min_ratio": 0.5},
"exclude": {"repositories": []},
}
with (
patch.multiple(
radar,
ROOT=root,
REPOS_PATH=repos_path,
POLICY_PATH=policies_path,
ISSUES_PATH=issues_path,
SITE_DATA_PATH=site_data_path,
),
patch.object(radar, "now_utc", return_value=fixed_now),
):
result = radar.fetch(github, config, force=True)
radar.render(config)
self.assertEqual(result["repositories"]["owner/repo"]["contributing"], expected)
self.assertEqual(
json.loads(issues_path.read_text(encoding="utf-8"))["repositories"]["owner/repo"]["contributing"],
expected,
)
self.assertNotIn("contributing", result["issues"][0])
site_data = json.loads(site_data_path.read_text(encoding="utf-8"))
self.assertEqual(site_data["repositories"]["owner/repo"]["contributing"], expected)
self.assertNotIn("contributing", site_data["issues"][0])
self.assertEqual("HEAD:CONTRIBUTING.md" in github.query, refresh)
class MaintainerReplyFetchTests(unittest.TestCase):
def test_fetch_and_render_preserve_reply_booleans(self):
comments = [
{"totalCount": 1, "nodes": [comment("Happy to help", authorAssociation="OWNER")]},
{"totalCount": 1, "nodes": [comment("I can reproduce it", authorAssociation="NONE")]},
{"totalCount": 1, "nodes": [comment("No association")]},
{"totalCount": 0, "nodes": []},
]
graph_issues = [
issue(
number=number,
title=f"Task {number}",
url=f"https://github.com/owner/repo/issues/{number}",
createdAt="2026-09-01T00:00:00Z",
labels={"nodes": [{"name": "good first issue"}]},
comments=nodes,
)
for number, nodes in enumerate(comments, start=1)
]
graph_repo = {
"nameWithOwner": "owner/repo",
"url": "https://github.com/owner/repo",
"description": "A small fixture",
"stargazerCount": 10,
"forkCount": 1,
"pushedAt": "2026-10-12T00:00:00Z",
"primaryLanguage": {"name": "Python"},
"repositoryTopics": {"nodes": []},
"issues": {"nodes": graph_issues},
}
class FakeGitHub:
def graphql(self, query):
return {"data": {"r0": graph_repo, "rateLimit": {"remaining": 4000}}}
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
repos_path = root / "data" / "repos.json"
issues_path = root / "data" / "issues.json"
site_data_path = root / "site" / "data" / "issues.json"
radar.write_json(repos_path, {"repos": {"owner/repo": {"language": "Python"}}})
config = radar.load_config()
with (
patch.multiple(
radar,
ROOT=root,
REPOS_PATH=repos_path,
POLICY_PATH=root / "data" / "policies.json",
ISSUES_PATH=issues_path,
SITE_DATA_PATH=site_data_path,
),
patch.object(radar, "now_utc", return_value=dt.datetime(2026, 10, 13, tzinfo=dt.UTC)),
):
result = radar.fetch(FakeGitHub(), config, force=True)
radar.render(config)
for payload in (result, radar.read_json(issues_path, None), radar.read_json(site_data_path, None)):
self.assertEqual(
[item["maintainer_replied"] for item in payload["issues"]],
[True, False, False, False],
)
class RenderSmokeTests(unittest.TestCase):
def test_render_writes_primary_outputs(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
issues_path = root / "data" / "issues.json"
site_data_path = root / "site" / "data" / "issues.json"
issues_path.parent.mkdir(parents=True)
issues_path.write_text(
json.dumps(
{
"generated_at": "2026-09-27T12:00:00+00:00",
"repositories": {
"owner/repo": {
"url": "https://github.com/owner/repo",
"contributing": ".github/CONTRIBUTING.md",
"description": "A tiny fixture",
"stars": 10,
"open_count": 1,
"language": "Python",
"buckets": ["ai", "retired-topic"],
"policy": {},
},
"owner/go-repo": {
"url": "https://github.com/owner/go-repo",
"contributing": None,
"description": "Only help-wanted issues",
"stars": 5,
"open_count": 1,
"language": "Go",
"buckets": ["docs"],
"policy": {},
},
},
"issues": [
{
"repo": "owner/repo",
"level": "beginner",
"title": "Fix the fixture",
"url": "https://github.com/owner/repo/issues/1",
"comments": 0,
"created": "2026-09-27",
"updated": "2026-09-27",
},
{
"repo": "owner/go-repo",
"level": "help-wanted",
"title": "Refactor the parser",
"url": "https://github.com/owner/go-repo/issues/2",
"comments": 0,
"created": "2026-09-26",
"updated": "2026-09-26",
},
],
}
),
encoding="utf-8",
)
original_paths = (radar.ROOT, radar.ISSUES_PATH, radar.SITE_DATA_PATH)
radar.ROOT, radar.ISSUES_PATH, radar.SITE_DATA_PATH = root, issues_path, site_data_path
config = {
"issues": {"max_per_page": 10},
"languages": {"Python": "python", "Go": "go"},
"topics": {
"ai": {"title": "AI", "keywords": ["ai"]},
"docs": {"title": "Documentation", "keywords": ["docs"]},
},
}
try:
radar.render(config)
finally:
radar.ROOT, radar.ISSUES_PATH, radar.SITE_DATA_PATH = original_paths
self.assertTrue((root / "issues" / "by-language" / "python.md").exists())
self.assertTrue((root / "projects" / "README.md").exists())
self.assertTrue((root / "site" / "feeds" / "python.xml").exists())
self.assertTrue((root / "issues" / "by-topic" / "ai.md").exists())
site_data = json.loads(site_data_path.read_text(encoding="utf-8"))
self.assertEqual(site_data["repositories"]["owner/repo"]["contributing"], ".github/CONTRIBUTING.md")
self.assertIsNone(site_data["repositories"]["owner/go-repo"]["contributing"])
self.assertTrue(all("contributing" not in item for item in site_data["issues"]))
self.assertEqual(
[issue["title"] for issue in site_data["issues"]], ["Fix the fixture", "Refactor the parser"]
)
# Go has no beginner issues, so its (empty) feed is not linked from the site.
self.assertEqual(site_data["language_slugs"], {"Python": "python"})
# Docs has no beginner issues and retired-topic is not configured, so only AI is linked.
self.assertEqual(site_data["topic_feed_slugs"], ["ai"])
class LanguageFeedTests(unittest.TestCase):
def test_render_language_feeds(self):
with tempfile.TemporaryDirectory() as tmp:
original_root = radar.ROOT
radar.ROOT = Path(tmp)
try:
repositories = {
"owner/repo": {"language": "Python", "stars": 12345},
"owner/js-repo": {"language": "JavaScript", "stars": 900},
"owner/erlang-repo": {"language": "Erlang", "stars": 500},
}
issues = [
{
"repo": "owner/repo",
"level": "beginner",
"title": "Issue <with> & special chars",
"url": "https://github.com/owner/repo/issues/1",
"created": "2026-09-20",
"labels": ["good first issue", "docs & examples"],
},
{
"repo": "owner/js-repo",
"level": "beginner",
"title": "JavaScript issue",
"url": "https://github.com/owner/js-repo/issues/1",
"created": "2026-09-21",
},
]
radar.render_language_feeds(
{
"Python": "python",
"JavaScript": "javascript",
"Erlang": "erlang",
},
repositories,
issues,
"2026-10-04T12:00:00+00:00",
{"topics": {}},
)
feed = Path(tmp) / "site" / "feeds" / "python.xml"
self.assertTrue(feed.exists())
root = ET.parse(feed).getroot()
atom_link = root.find(f"./channel/{{{radar.ATOM_NAMESPACE}}}link")
self.assertIsNotNone(atom_link)
self.assertEqual(
atom_link.attrib,
{
"href": "https://tanbirramim.github.io/open-source-radar/feeds/python.xml",
"rel": "self",
"type": "application/rss+xml",
},
)
items = root.findall("./channel/item")
self.assertEqual(len(items), 1)
self.assertEqual(items[0].findtext("title"), "Issue <with> & special chars")
self.assertEqual(
items[0].findtext("guid"),
"https://github.com/owner/repo/issues/1",
)
self.assertEqual(
items[0].findtext("pubDate"),
"Sun, 20 Sep 2026 00:00:00 +0000",
)
self.assertEqual(