| 1 | import unittest |
| 2 | |
| 3 | from pr_gate import LABEL_ACCEPTED, LABEL_NO_VISUAL, ChangedFile, PullRequest, check, linked_issue_numbers, parse_sections |
| 4 | |
| 5 | GOOD_BODY = """## Summary |
| 6 | |
| 7 | A sent image no longer blinks after sending. |
| 8 | |
| 9 | ## Problem |
| 10 | |
| 11 | <!-- template hint --> |
| 12 | After sending an image the bubble shows empty for 200 ms and then the image again. |
| 13 | Expected: the image stays visible the whole time. |
| 14 | |
| 15 | ## AI model |
| 16 | |
| 17 | Claude Code, Opus 5.5 |
| 18 | |
| 19 | ## Tests |
| 20 | |
| 21 | Ran: `cargo test -p app image_cache`. Not run: Windows. |
| 22 | |
| 23 | ## Review |
| 24 | |
| 25 | - `/code-review medium`: 1 finding, fixed |
| 26 | - fresh agent session: no findings |
| 27 | """ |
| 28 | |
| 29 | BACKEND_FILE = ChangedFile("crates/chatsvc/src/window.rs", 20, 5) |
| 30 | UI_FILE = ChangedFile("crates/app/src/views/conversation.rs", 10, 2) |
| 31 | |
| 32 | |
| 33 | def make(title="fix(chat): a sent image no longer blinks", body=GOOD_BODY, files=None, labels=None, issue_labels=None): |
| 34 | return PullRequest(title, body, labels or [], files if files is not None else [BACKEND_FILE], issue_labels or {}) |
| 35 | |
| 36 | |
| 37 | def missing_keys(pull_request): |
| 38 | return [item.split("**")[1] for item in check(pull_request)] |
| 39 | |
| 40 | |
| 41 | class SectionTests(unittest.TestCase): |
| 42 | def test_comments_do_not_count_as_content(self): |
| 43 | sections = parse_sections("## Summary\n<!-- what and why -->\n## AI model\nNone") |
| 44 | self.assertEqual(sections["summary"], "") |
| 45 | self.assertEqual(sections["ai model"], "None") |
| 46 | |
| 47 | def test_test_plan_counts_as_tests(self): |
| 48 | self.assertEqual(check(make(body=GOOD_BODY.replace("## Tests", "## Test plan"))), []) |
| 49 | |
| 50 | def test_issue_reference_only_with_closing_keyword(self): |
| 51 | self.assertEqual(linked_issue_numbers("Closes #12, fixes #3, see #99"), [3, 12]) |
| 52 | self.assertEqual(linked_issue_numbers("<!-- Closes #123 -->"), []) |
| 53 | self.assertEqual(linked_issue_numbers("Fixes: https://github.com/danieldaeschle/rusty-teams/issues/42"), [42]) |
| 54 | |
| 55 | |
| 56 | class GateTests(unittest.TestCase): |
| 57 | def test_good_pr_passes(self): |
| 58 | self.assertEqual(check(make()), []) |
| 59 | |
| 60 | def test_bad_title(self): |
| 61 | self.assertIn("Title", missing_keys(make(title="Bound audio capture lifetime"))) |
| 62 | |
| 63 | def test_untouched_template_fails_every_section(self): |
| 64 | template = "## Summary\n<!-- x -->\n## AI model\n<!-- x -->\n## Review\n<!-- x -->" |
| 65 | keys = missing_keys(make(body=template)) |
| 66 | for name in ("Summary", "AI model", "Tests", "Review", "Problem"): |
| 67 | self.assertIn(name, keys) |
| 68 | |
| 69 | def test_issue_link_replaces_problem(self): |
| 70 | body = GOOD_BODY.replace("## Problem", "Closes #5\n\n## Notes") |
| 71 | self.assertEqual(check(make(body=body)), []) |
| 72 | |
| 73 | def test_feature_needs_accepted_issue(self): |
| 74 | title = "feat(chat): pin a message" |
| 75 | self.assertIn("Issue first", missing_keys(make(title=title))) |
| 76 | body = GOOD_BODY + "\nCloses #7\n" |
| 77 | self.assertIn("Issue first", missing_keys(make(title=title, body=body, issue_labels={7: ["enhancement"]}))) |
| 78 | self.assertEqual(check(make(title=title, body=body, issue_labels={7: [LABEL_ACCEPTED]})), []) |
| 79 | |
| 80 | def test_large_fix_needs_accepted_issue_but_lockfile_does_not_count(self): |
| 81 | large = [ChangedFile("crates/chatsvc/src/window.rs", 350, 100)] |
| 82 | self.assertIn("Issue first", missing_keys(make(files=large))) |
| 83 | lock = [BACKEND_FILE, ChangedFile("Cargo.lock", 900, 900)] |
| 84 | self.assertEqual(check(make(files=lock)), []) |
| 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\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 | |
| 115 | if __name__ == "__main__": |
| 116 | unittest.main() |