Repository navigation
Expand file tree
/
Copy pathtest_pr_comment_building.py
More file actions
274 lines (241 loc) · 9.74 KB
/
Copy pathtest_pr_comment_building.py
File metadata and controls
274 lines (241 loc) · 9.74 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
"""Tests for comment content building and conflict grouping."""
import unittest
import comment_rendering
from conflict_detector import FileOverlap, PRInfo
from pr_comment import ConflictEntry, ResolvedConflictEntry
from test_helpers import _make_comment_conflict
class TestBuildConsolidatedComment(unittest.TestCase):
"""Tests for the _build_consolidated_comment function."""
def test_single_conflict(self):
"""Should build a properly formatted comment for one conflict."""
entries = [
ConflictEntry(
other_pr=PRInfo(
number=456,
url="https://github.com/org/repo/pull/456",
title="Refactor auth",
author="bob",
),
files=[
FileOverlap(
filename="src/auth.py",
pr_a_lines=[(10, 20)],
pr_b_lines=[(15, 25)],
overlapping_ranges=[(15, 20)],
)
],
)
]
comment = comment_rendering.build_consolidated_comment(entries)
self.assertIn(comment_rendering.COMMENT_SIGNATURE, comment)
self.assertIn("**1**", comment)
self.assertIn("#456", comment)
self.assertIn("Refactor auth", comment)
self.assertIn("@bob", comment)
self.assertIn("`src/auth.py`", comment)
self.assertIn("L15-L20", comment)
# Banner text
self.assertIn("🔄 **This comment updates automatically**", comment)
# 3-column table header
self.assertIn("| | Conflicting PR | Conflicting Files (Lines) |", comment)
def test_multiple_conflicts(self):
"""Should list all conflicting PRs in the table."""
entries = [
ConflictEntry(
other_pr=PRInfo(
number=200,
url="https://github.com/org/repo/pull/200",
title="PR 200",
author="alice",
),
files=[
FileOverlap(
filename="file_a.rb",
pr_a_lines=[(10, 20)],
pr_b_lines=[(10, 20)],
overlapping_ranges=[(10, 20)],
)
],
),
ConflictEntry(
other_pr=PRInfo(
number=300,
url="https://github.com/org/repo/pull/300",
title="PR 300",
author="charlie",
),
files=[
FileOverlap(
filename="file_a.rb",
pr_a_lines=[(5, 15)],
pr_b_lines=[(5, 15)],
overlapping_ranges=[(5, 15)],
),
FileOverlap(
filename="file_b.rb",
pr_a_lines=[(30, 40)],
pr_b_lines=[(30, 40)],
overlapping_ranges=[(30, 40)],
),
],
),
]
comment = comment_rendering.build_consolidated_comment(entries)
self.assertIn("**2**", comment)
self.assertIn("#200", comment)
self.assertIn("#300", comment)
self.assertIn("@alice", comment)
self.assertIn("@charlie", comment)
self.assertIn("`file_a.rb`", comment)
self.assertIn("`file_b.rb`", comment)
# Verify file-to-range attribution: each file's ranges appear next to the filename
self.assertIn("`file_a.rb` (L5-L15)", comment)
self.assertIn("`file_b.rb` (L30-L40)", comment)
# Banner text
self.assertIn("🔄 **This comment updates automatically**", comment)
# 3-column table header
self.assertIn("| | Conflicting PR | Conflicting Files (Lines) |", comment)
class TestGroupConflictsByPR(unittest.TestCase):
"""Tests for the _group_conflicts_by_pr function."""
def test_single_conflict(self):
"""Should create entries for both PRs in a conflict."""
conflict = _make_comment_conflict(pr_a_number=1, pr_b_number=2)
grouped = comment_rendering.group_conflicts_by_pr([conflict])
self.assertIn(1, grouped)
self.assertIn(2, grouped)
self.assertEqual(len(grouped[1]), 1)
self.assertEqual(grouped[1][0].other_pr.number, 2)
self.assertEqual(grouped[2][0].other_pr.number, 1)
def test_multiple_conflicts_same_pr(self):
"""PR appearing in multiple conflicts should have all entries grouped."""
conflict1 = _make_comment_conflict(pr_a_number=1, pr_b_number=2)
conflict2 = _make_comment_conflict(pr_a_number=1, pr_b_number=3)
grouped = comment_rendering.group_conflicts_by_pr([conflict1, conflict2])
self.assertEqual(len(grouped[1]), 2)
other_numbers = {e.other_pr.number for e in grouped[1]}
self.assertEqual(other_numbers, {2, 3})
class TestGroupResolvedByPR(unittest.TestCase):
"""Tests for _group_resolved_by_pr."""
def test_groups_by_pr_number(self):
"""Resolved entries should be grouped by both PR A and PR B."""
entries = [
{
"repo": "org/repo",
"pr_a": 1,
"pr_b": 2,
"pr_a_title": "PR 1",
"pr_b_title": "PR 2",
"pr_a_url": "http://pr1",
"pr_b_url": "http://pr2",
"resolved_at": "2026-03-15T10:00:00+00:00",
}
]
grouped = comment_rendering.group_resolved_by_pr(entries, "org/repo")
# PR 1 should see PR 2 as resolved
self.assertIn(1, grouped)
self.assertEqual(grouped[1][0].pr_number, 2)
self.assertEqual(grouped[1][0].pr_title, "PR 2")
# PR 2 should see PR 1 as resolved
self.assertIn(2, grouped)
self.assertEqual(grouped[2][0].pr_number, 1)
self.assertEqual(grouped[2][0].pr_title, "PR 1")
def test_filters_by_repo(self):
"""Should only include entries matching the given repo_name."""
entries = [
{
"repo": "org/repo",
"pr_a": 1,
"pr_b": 2,
"pr_a_title": "PR 1",
"pr_b_title": "PR 2",
"resolved_at": "2026-03-15T10:00:00+00:00",
},
{
"repo": "other/repo",
"pr_a": 3,
"pr_b": 4,
"pr_a_title": "PR 3",
"pr_b_title": "PR 4",
"resolved_at": "2026-03-15T10:00:00+00:00",
},
]
grouped = comment_rendering.group_resolved_by_pr(entries, "org/repo")
self.assertIn(1, grouped)
self.assertIn(2, grouped)
self.assertNotIn(3, grouped)
self.assertNotIn(4, grouped)
def test_empty_entries(self):
"""Empty entries should produce empty grouping."""
grouped = comment_rendering.group_resolved_by_pr([], "org/repo")
self.assertEqual(grouped, {})
class TestBuildResolvedSection(unittest.TestCase):
"""Tests for _build_resolved_section."""
def test_empty_resolved_returns_empty(self):
"""No resolved entries should produce empty string."""
result = comment_rendering.build_resolved_section([])
self.assertEqual(result, "")
def test_collapsed_html_structure(self):
"""Should produce details/summary HTML structure."""
entries = [
ResolvedConflictEntry(
pr_number=10,
pr_title="PR 10",
pr_url="http://pr10",
resolved_at="2026-03-15T10:00:00+00:00",
),
]
result = comment_rendering.build_resolved_section(entries)
self.assertIn("<details>", result)
self.assertIn("</details>", result)
self.assertIn("<summary>", result)
self.assertIn("1 previously resolved conflict", result)
self.assertIn("~", result) # strikethrough
def test_plural_summary(self):
"""Should use plural form for multiple resolved entries."""
entries = [
ResolvedConflictEntry(
pr_number=10,
pr_title="PR 10",
pr_url="",
resolved_at="2026-03-15T10:00:00+00:00",
),
ResolvedConflictEntry(
pr_number=20,
pr_title="PR 20",
pr_url="",
resolved_at="2026-03-14T10:00:00+00:00",
),
]
result = comment_rendering.build_resolved_section(entries)
self.assertIn("2 previously resolved conflicts", result)
def test_date_formatting_in_section(self):
"""Resolved date should be formatted as readable date."""
entries = [
ResolvedConflictEntry(
pr_number=10,
pr_title="PR 10",
pr_url="http://pr10",
resolved_at="2026-03-15T10:00:00+00:00",
),
]
result = comment_rendering.build_resolved_section(entries)
self.assertIn("Mar 15, 2026", result)
def test_max_display_cap(self):
"""Should cap the number of resolved entries displayed."""
entries = [
ResolvedConflictEntry(
pr_number=i,
pr_title=f"PR {i}",
pr_url=f"http://pr{i}",
resolved_at=f"2026-03-{15 - (i % 15):02d}T10:00:00+00:00",
)
for i in range(15)
]
result = comment_rendering.build_resolved_section(entries)
# Should cap at MAX_RESOLVED_DISPLAY (10)
self.assertIn(
f"{comment_rendering.MAX_RESOLVED_DISPLAY} previously resolved conflicts",
result,
)
if __name__ == "__main__":
unittest.main()