BlueGit

ict rusty-teams

Files

rusty-teams / .github / scripts / test_pr_gate.py

test_pr_gate.py

test_pr_gate.py 8.1 KiB Download Raw
1import unittest
2from pathlib import Path
3
4import pr_gate
5from pr_gate import LABEL_ACCEPTED, LABEL_NO_VISUAL, ChangedFile, PullRequest, check, linked_issue_numbers, parse_sections
6
7GOOD_BODY = """## Summary
8
9A sent image no longer blinks after sending.
10
11## Problem
12
13<!-- template hint -->
14After sending an image the bubble shows empty for 200 ms and then the image again.
15Expected: the image stays visible the whole time.
16
17## AI model
18
19Claude Code, Opus 5.5
20
21## Tests
22
23Ran: `cargo test -p app image_cache`. Not run: Windows.
24
25## Review
26
27- `/code-review medium`: 1 finding, fixed
28- fresh agent session: no findings
29"""
30
31BACKEND_FILE = ChangedFile("crates/chatsvc/src/window.rs", 20, 5)
32UI_FILE = ChangedFile("crates/app/src/views/conversation.rs", 10, 2)
33
34
35def make(title="fix(chat): a sent image no longer blinks", body=GOOD_BODY, files=None, labels=None, issue_labels=None):
36 return PullRequest(title, body, labels or [], files if files is not None else [BACKEND_FILE], issue_labels or {})
37
38
39def missing_keys(pull_request):
40 return [item.split("**")[1] for item in check(pull_request)]
41
42
43class SectionTests(unittest.TestCase):
44 def test_comments_do_not_count_as_content(self):
45 sections = parse_sections("## Summary\n<!-- what and why -->\n## AI model\nNone")
46 self.assertEqual(sections["summary"], "")
47 self.assertEqual(sections["ai model"], "None")
48
49 def test_test_plan_counts_as_tests(self):
50 self.assertEqual(check(make(body=GOOD_BODY.replace("## Tests", "## Test plan"))), [])
51
52 def test_issue_reference_only_with_closing_keyword(self):
53 self.assertEqual(linked_issue_numbers("Closes #12, fixes #3, see #99"), [3, 12])
54 self.assertEqual(linked_issue_numbers("<!-- Closes #123 -->"), [])
55 self.assertEqual(linked_issue_numbers("Fixes: https://github.com/danieldaeschle/rusty-teams/issues/42"), [42])
56
57
58class GateTests(unittest.TestCase):
59 def test_good_pr_passes(self):
60 self.assertEqual(check(make()), [])
61
62 def test_bad_title(self):
63 self.assertIn("Title", missing_keys(make(title="Bound audio capture lifetime")))
64
65 def test_untouched_template_fails_every_section(self):
66 template = "## Summary\n<!-- x -->\n## AI model\n<!-- x -->\n## Review\n<!-- x -->"
67 keys = missing_keys(make(body=template))
68 for name in ("Summary", "AI model", "Tests", "Review", "Problem"):
69 self.assertIn(name, keys)
70
71 def test_issue_link_replaces_problem(self):
72 body = GOOD_BODY.replace("## Problem", "Closes #5\n\n## Notes")
73 self.assertEqual(check(make(body=body)), [])
74
75 def test_feature_needs_accepted_issue(self):
76 title = "feat(chat): pin a message"
77 self.assertIn("Issue first", missing_keys(make(title=title)))
78 body = GOOD_BODY + "\nCloses #7\n"
79 self.assertIn("Issue first", missing_keys(make(title=title, body=body, issue_labels={7: ["enhancement"]})))
80 self.assertEqual(check(make(title=title, body=body, issue_labels={7: [LABEL_ACCEPTED]})), [])
81
82 def test_large_fix_needs_no_issue(self):
83 large = [ChangedFile("crates/chatsvc/src/window.rs", 3500, 1000)]
84 self.assertEqual(check(make(files=large)), [])
85
86 def test_ui_change_needs_screenshot(self):
87 self.assertIn("Screenshot", missing_keys(make(files=[UI_FILE])))
88 with_image = GOOD_BODY + "\n## Screenshot\n\n![chat](https://github.com/user-attachments/assets/abc)\n"
89 self.assertEqual(check(make(body=with_image, files=[UI_FILE])), [])
90
91 def test_ui_change_without_visible_change(self):
92 ticked = GOOD_BODY + "\n## Screenshot\n\n- [x] No visible change: only the cache key moved\n"
93 self.assertEqual(check(make(body=ticked, files=[UI_FILE])), [])
94 self.assertEqual(check(make(files=[UI_FILE], labels=[LABEL_NO_VISUAL])), [])
95
96 def test_windows_code_needs_windows_note(self):
97 windows_file = ChangedFile("crates/calling/src/device.rs", 5, 1, "@@\n+#[cfg(windows)]\n+fn open() {}")
98 body = GOOD_BODY.replace("Not run: Windows.", "")
99 self.assertIn("Tests", missing_keys(make(body=body, files=[windows_file])))
100 self.assertEqual(check(make(files=[windows_file])), [])
101
102 def test_gpui_window_file_is_not_windows_code(self):
103 body = GOOD_BODY.replace("Not run: Windows.", "")
104 self.assertEqual(check(make(body=body, files=[ChangedFile("crates/chatsvc/src/window.rs", 1, 1)])), [])
105
106 def test_single_review_line_is_not_enough(self):
107 body = GOOD_BODY.replace("- fresh agent session: no findings\n", "")
108 self.assertIn("Review", missing_keys(make(body=body)))
109
110 def test_planning_folder_and_vendor(self):
111 files = [BACKEND_FILE, ChangedFile("openspec/changes/x.md", 10, 0), ChangedFile("vendor/gpui/src/a.rs", 1, 1)]
112 self.assertEqual(missing_keys(make(files=files)), ["Scope", "Scope"])
113
114
115class DocumentationTests(unittest.TestCase):
116 """AGENTS.md and CONTRIBUTING.md repeat the gate's rules; they must name the same values."""
117
118 repository_root = Path(__file__).resolve().parents[2]
119
120 def assert_mentions(self, file_name, *expected):
121 text = (self.repository_root / file_name).read_text()
122 for value in expected:
123 self.assertIn(value, text, f"{file_name} does not mention {value!r}")
124
125 def shared_values(self):
126 return (
127 pr_gate.LABEL_ACCEPTED,
128 pr_gate.LABEL_NO_VISUAL,
129 pr_gate.LABEL_SKIP,
130 *(f"`{folder}/`" for folder in pr_gate.PLANNING_FOLDERS),
131 )
132
133 def test_agents_matches_gate(self):
134 self.assert_mentions(
135 "AGENTS.md",
136 *self.shared_values(),
137 f"at least {pr_gate.MIN_PROBLEM_CHARACTERS} characters",
138 *(f"{directory}/`" for directory in pr_gate.UI_DIRECTORIES),
139 *(f"`## {name}`" for name in pr_gate.REQUIRED_SECTIONS),
140 )
141
142 def test_contributing_matches_gate(self):
143 self.assert_mentions("CONTRIBUTING.md", *self.shared_values())
144
145
146
147class FakeGitHub:
148 def __init__(self, responses=None):
149 self.calls = []
150 self.responses = responses or {}
151
152 def request(self, method, path, payload=None):
153 self.calls.append((method, path, payload))
154 return self.responses.get(path)
155
156
157class LabelTests(unittest.TestCase):
158 def test_a_failing_gate_replaces_needs_review_with_needs_info(self):
159 github = FakeGitHub()
160 pr_gate.update_labels(github, 5, [pr_gate.LABEL_NEEDS_REVIEW], ready=False)
161 self.assertEqual(
162 github.calls,
163 [
164 ("DELETE", "/issues/5/labels/status%3A%20needs%20review", None),
165 ("POST", "/issues/5/labels", {"labels": [pr_gate.LABEL_NEEDS_INFO]}),
166 ],
167 )
168
169 def test_a_passing_gate_replaces_needs_info_with_needs_review(self):
170 github = FakeGitHub()
171 pr_gate.update_labels(github, 5, [pr_gate.LABEL_NEEDS_INFO], ready=True)
172 self.assertEqual(
173 github.calls,
174 [
175 ("DELETE", "/issues/5/labels/status%3A%20needs%20info", None),
176 ("POST", "/issues/5/labels", {"labels": [pr_gate.LABEL_NEEDS_REVIEW]}),
177 ],
178 )
179
180
181class RerunTests(unittest.TestCase):
182 RUNS_PATH = "/actions/workflows/pr-gate.yml/runs?head_sha=abc&event=pull_request_target&per_page=20"
183 PULL = {"number": 5, "head": {"sha": "abc"}}
184
185 def runs(self, *runs):
186 return FakeGitHub({self.RUNS_PATH: {"workflow_runs": list(runs)}})
187
188 def test_the_latest_failed_run_is_run_again(self):
189 github = self.runs(
190 {"id": 1, "created_at": "2026-11-25T10:04:38Z", "conclusion": "failure"},
191 {"id": 2, "created_at": "2026-11-25T10:05:01Z", "conclusion": "failure"},
192 )
193 pr_gate.rerun_failed_gate(github, self.PULL)
194 self.assertEqual(github.calls[-1], ("POST", "/actions/runs/2/rerun", None))
195
196 def test_a_passing_latest_run_is_left_alone(self):
197 github = self.runs(
198 {"id": 1, "created_at": "2026-11-25T10:04:38Z", "conclusion": "failure"},
199 {"id": 2, "created_at": "2026-11-25T10:05:01Z", "conclusion": "success"},
200 )
201 pr_gate.rerun_failed_gate(github, self.PULL)
202 self.assertEqual(len(github.calls), 1)
203
204
205if __name__ == "__main__":
206 unittest.main()