BlueGit

ict rusty-teams

Files

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

test_pr_gate.py

test_pr_gate.py 8.3 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_accepted_issue_but_lockfile_does_not_count(self):
83 large = [ChangedFile("crates/chatsvc/src/window.rs", 350, 100)]
84 self.assertIn("Issue first", missing_keys(make(files=large)))
85 lock = [BACKEND_FILE, ChangedFile("Cargo.lock", 900, 900)]
86 self.assertEqual(check(make(files=lock)), [])
87
88 def test_ui_change_needs_screenshot(self):
89 self.assertIn("Screenshot", missing_keys(make(files=[UI_FILE])))
90 with_image = GOOD_BODY + "\n## Screenshot\n\n![chat](https://github.com/user-attachments/assets/abc)\n"
91 self.assertEqual(check(make(body=with_image, files=[UI_FILE])), [])
92
93 def test_ui_change_without_visible_change(self):
94 ticked = GOOD_BODY + "\n## Screenshot\n\n- [x] No visible change: only the cache key moved\n"
95 self.assertEqual(check(make(body=ticked, files=[UI_FILE])), [])
96 self.assertEqual(check(make(files=[UI_FILE], labels=[LABEL_NO_VISUAL])), [])
97
98 def test_windows_code_needs_windows_note(self):
99 windows_file = ChangedFile("crates/calling/src/device.rs", 5, 1, "@@\n+#[cfg(windows)]\n+fn open() {}")
100 body = GOOD_BODY.replace("Not run: Windows.", "")
101 self.assertIn("Tests", missing_keys(make(body=body, files=[windows_file])))
102 self.assertEqual(check(make(files=[windows_file])), [])
103
104 def test_gpui_window_file_is_not_windows_code(self):
105 body = GOOD_BODY.replace("Not run: Windows.", "")
106 self.assertEqual(check(make(body=body, files=[ChangedFile("crates/chatsvc/src/window.rs", 1, 1)])), [])
107
108 def test_single_review_line_is_not_enough(self):
109 body = GOOD_BODY.replace("- fresh agent session: no findings\n", "")
110 self.assertIn("Review", missing_keys(make(body=body)))
111
112 def test_planning_folder_and_vendor(self):
113 files = [BACKEND_FILE, ChangedFile("openspec/changes/x.md", 10, 0), ChangedFile("vendor/gpui/src/a.rs", 1, 1)]
114 self.assertEqual(missing_keys(make(files=files)), ["Scope", "Scope"])
115
116
117class DocumentationTests(unittest.TestCase):
118 """AGENTS.md and CONTRIBUTING.md repeat the gate's rules; they must name the same values."""
119
120 repository_root = Path(__file__).resolve().parents[2]
121
122 def assert_mentions(self, file_name, *expected):
123 text = (self.repository_root / file_name).read_text()
124 for value in expected:
125 self.assertIn(value, text, f"{file_name} does not mention {value!r}")
126
127 def shared_values(self):
128 return (
129 f"more than {pr_gate.MAX_CHANGED_LINES} changed lines",
130 pr_gate.LABEL_ACCEPTED,
131 pr_gate.LABEL_NO_VISUAL,
132 pr_gate.LABEL_SKIP,
133 *(f"`{folder}/`" for folder in pr_gate.PLANNING_FOLDERS),
134 )
135
136 def test_agents_matches_gate(self):
137 self.assert_mentions(
138 "AGENTS.md",
139 *self.shared_values(),
140 f"at least {pr_gate.MIN_PROBLEM_CHARACTERS} characters",
141 *(f"{directory}/`" for directory in pr_gate.UI_DIRECTORIES),
142 *(f"`## {name}`" for name in pr_gate.REQUIRED_SECTIONS),
143 )
144
145 def test_contributing_matches_gate(self):
146 self.assert_mentions("CONTRIBUTING.md", *self.shared_values())
147
148
149
150class FakeGitHub:
151 def __init__(self, responses=None):
152 self.calls = []
153 self.responses = responses or {}
154
155 def request(self, method, path, payload=None):
156 self.calls.append((method, path, payload))
157 return self.responses.get(path)
158
159
160class LabelTests(unittest.TestCase):
161 def test_a_failing_gate_replaces_needs_review_with_needs_info(self):
162 github = FakeGitHub()
163 pr_gate.update_labels(github, 5, [pr_gate.LABEL_NEEDS_REVIEW], ready=False)
164 self.assertEqual(
165 github.calls,
166 [
167 ("DELETE", "/issues/5/labels/status%3A%20needs%20review", None),
168 ("POST", "/issues/5/labels", {"labels": [pr_gate.LABEL_NEEDS_INFO]}),
169 ],
170 )
171
172 def test_a_passing_gate_replaces_needs_info_with_needs_review(self):
173 github = FakeGitHub()
174 pr_gate.update_labels(github, 5, [pr_gate.LABEL_NEEDS_INFO], ready=True)
175 self.assertEqual(
176 github.calls,
177 [
178 ("DELETE", "/issues/5/labels/status%3A%20needs%20info", None),
179 ("POST", "/issues/5/labels", {"labels": [pr_gate.LABEL_NEEDS_REVIEW]}),
180 ],
181 )
182
183
184class RerunTests(unittest.TestCase):
185 RUNS_PATH = "/actions/workflows/pr-gate.yml/runs?head_sha=abc&event=pull_request_target&per_page=20"
186 PULL = {"number": 5, "head": {"sha": "abc"}}
187
188 def runs(self, *runs):
189 return FakeGitHub({self.RUNS_PATH: {"workflow_runs": list(runs)}})
190
191 def test_the_latest_failed_run_is_run_again(self):
192 github = self.runs(
193 {"id": 1, "created_at": "2026-11-25T10:04:38Z", "conclusion": "failure"},
194 {"id": 2, "created_at": "2026-11-25T10:05:01Z", "conclusion": "failure"},
195 )
196 pr_gate.rerun_failed_gate(github, self.PULL)
197 self.assertEqual(github.calls[-1], ("POST", "/actions/runs/2/rerun", None))
198
199 def test_a_passing_latest_run_is_left_alone(self):
200 github = self.runs(
201 {"id": 1, "created_at": "2026-11-25T10:04:38Z", "conclusion": "failure"},
202 {"id": 2, "created_at": "2026-11-25T10:05:01Z", "conclusion": "success"},
203 )
204 pr_gate.rerun_failed_gate(github, self.PULL)
205 self.assertEqual(len(github.calls), 1)
206
207
208if __name__ == "__main__":
209 unittest.main()