|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from django_github_app.commands import check_event_for_mention |
| 4 | +from django_github_app.commands import parse_mentions |
| 5 | + |
| 6 | + |
| 7 | +class TestParseMentions: |
| 8 | + def test_simple_mention_with_command(self): |
| 9 | + text = "@mybot help" |
| 10 | + mentions = parse_mentions(text, "mybot") |
| 11 | + |
| 12 | + assert len(mentions) == 1 |
| 13 | + assert mentions[0].mention == "@mybot" |
| 14 | + assert mentions[0].command == "help" |
| 15 | + |
| 16 | + def test_mention_without_command(self): |
| 17 | + text = "@mybot" |
| 18 | + mentions = parse_mentions(text, "mybot") |
| 19 | + |
| 20 | + assert len(mentions) == 1 |
| 21 | + assert mentions[0].mention == "@mybot" |
| 22 | + assert mentions[0].command is None |
| 23 | + |
| 24 | + def test_case_insensitive_matching(self): |
| 25 | + text = "@MyBot help" |
| 26 | + mentions = parse_mentions(text, "mybot") |
| 27 | + |
| 28 | + assert len(mentions) == 1 |
| 29 | + assert mentions[0].mention == "@MyBot" |
| 30 | + assert mentions[0].command == "help" |
| 31 | + |
| 32 | + def test_command_case_normalization(self): |
| 33 | + text = "@mybot HELP" |
| 34 | + mentions = parse_mentions(text, "mybot") |
| 35 | + |
| 36 | + assert len(mentions) == 1 |
| 37 | + assert mentions[0].command == "help" |
| 38 | + |
| 39 | + def test_multiple_mentions(self): |
| 40 | + text = "@mybot help and then @mybot deploy" |
| 41 | + mentions = parse_mentions(text, "mybot") |
| 42 | + |
| 43 | + assert len(mentions) == 2 |
| 44 | + assert mentions[0].command == "help" |
| 45 | + assert mentions[1].command == "deploy" |
| 46 | + |
| 47 | + def test_ignore_other_mentions(self): |
| 48 | + text = "@otheruser help @mybot deploy @someone else" |
| 49 | + mentions = parse_mentions(text, "mybot") |
| 50 | + |
| 51 | + assert len(mentions) == 1 |
| 52 | + assert mentions[0].command == "deploy" |
| 53 | + |
| 54 | + def test_mention_in_code_block(self): |
| 55 | + text = """ |
| 56 | + Here's some text |
| 57 | + ``` |
| 58 | + @mybot help |
| 59 | + ``` |
| 60 | + @mybot deploy |
| 61 | + """ |
| 62 | + mentions = parse_mentions(text, "mybot") |
| 63 | + |
| 64 | + assert len(mentions) == 1 |
| 65 | + assert mentions[0].command == "deploy" |
| 66 | + |
| 67 | + def test_mention_in_inline_code(self): |
| 68 | + text = "Use `@mybot help` for help, or just @mybot deploy" |
| 69 | + mentions = parse_mentions(text, "mybot") |
| 70 | + |
| 71 | + assert len(mentions) == 1 |
| 72 | + assert mentions[0].command == "deploy" |
| 73 | + |
| 74 | + def test_mention_in_quote(self): |
| 75 | + text = """ |
| 76 | + > @mybot help |
| 77 | + @mybot deploy |
| 78 | + """ |
| 79 | + mentions = parse_mentions(text, "mybot") |
| 80 | + |
| 81 | + assert len(mentions) == 1 |
| 82 | + assert mentions[0].command == "deploy" |
| 83 | + |
| 84 | + def test_empty_text(self): |
| 85 | + mentions = parse_mentions("", "mybot") |
| 86 | + |
| 87 | + assert mentions == [] |
| 88 | + |
| 89 | + def test_none_text(self): |
| 90 | + mentions = parse_mentions(None, "mybot") |
| 91 | + |
| 92 | + assert mentions == [] |
| 93 | + |
| 94 | + def test_mention_at_start_of_line(self): |
| 95 | + text = "@mybot help" |
| 96 | + mentions = parse_mentions(text, "mybot") |
| 97 | + |
| 98 | + assert len(mentions) == 1 |
| 99 | + assert mentions[0].command == "help" |
| 100 | + |
| 101 | + def test_mention_in_middle_of_text(self): |
| 102 | + text = "Hey @mybot help me" |
| 103 | + mentions = parse_mentions(text, "mybot") |
| 104 | + |
| 105 | + assert len(mentions) == 1 |
| 106 | + assert mentions[0].command == "help" |
| 107 | + |
| 108 | + def test_mention_with_punctuation_after(self): |
| 109 | + text = "@mybot help!" |
| 110 | + mentions = parse_mentions(text, "mybot") |
| 111 | + |
| 112 | + assert len(mentions) == 1 |
| 113 | + assert mentions[0].command == "help" |
| 114 | + |
| 115 | + def test_hyphenated_username(self): |
| 116 | + text = "@my-bot help" |
| 117 | + mentions = parse_mentions(text, "my-bot") |
| 118 | + |
| 119 | + assert len(mentions) == 1 |
| 120 | + assert mentions[0].mention == "@my-bot" |
| 121 | + assert mentions[0].command == "help" |
| 122 | + |
| 123 | + def test_underscore_username(self): |
| 124 | + text = "@my_bot help" |
| 125 | + mentions = parse_mentions(text, "my_bot") |
| 126 | + |
| 127 | + assert len(mentions) == 1 |
| 128 | + assert mentions[0].mention == "@my_bot" |
| 129 | + assert mentions[0].command == "help" |
| 130 | + |
| 131 | + def test_no_space_after_mention(self): |
| 132 | + text = "@mybot, please help" |
| 133 | + mentions = parse_mentions(text, "mybot") |
| 134 | + |
| 135 | + assert len(mentions) == 1 |
| 136 | + assert mentions[0].command is None |
| 137 | + |
| 138 | + def test_multiple_spaces_before_command(self): |
| 139 | + text = "@mybot help" |
| 140 | + mentions = parse_mentions(text, "mybot") |
| 141 | + |
| 142 | + assert len(mentions) == 1 |
| 143 | + assert mentions[0].command == "help" |
| 144 | + |
| 145 | + def test_hyphenated_command(self): |
| 146 | + text = "@mybot async-test" |
| 147 | + mentions = parse_mentions(text, "mybot") |
| 148 | + |
| 149 | + assert len(mentions) == 1 |
| 150 | + assert mentions[0].command == "async-test" |
| 151 | + |
| 152 | + def test_special_character_command(self): |
| 153 | + text = "@mybot ?" |
| 154 | + mentions = parse_mentions(text, "mybot") |
| 155 | + |
| 156 | + assert len(mentions) == 1 |
| 157 | + assert mentions[0].command == "?" |
| 158 | + |
| 159 | + |
| 160 | +class TestCheckMentionMatches: |
| 161 | + def test_match_with_command(self): |
| 162 | + event = {"comment": {"body": "@bot help"}} |
| 163 | + |
| 164 | + assert check_event_for_mention(event, "help", "bot") is True |
| 165 | + assert check_event_for_mention(event, "deploy", "bot") is False |
| 166 | + |
| 167 | + def test_match_without_command(self): |
| 168 | + event = {"comment": {"body": "@bot help"}} |
| 169 | + |
| 170 | + assert check_event_for_mention(event, None, "bot") is True |
| 171 | + |
| 172 | + event = {"comment": {"body": "no mention here"}} |
| 173 | + |
| 174 | + assert check_event_for_mention(event, None, "bot") is False |
| 175 | + |
| 176 | + def test_no_comment_body(self): |
| 177 | + event = {} |
| 178 | + |
| 179 | + assert check_event_for_mention(event, "help", "bot") is False |
| 180 | + |
| 181 | + event = {"comment": {}} |
| 182 | + |
| 183 | + assert check_event_for_mention(event, "help", "bot") is False |
| 184 | + |
| 185 | + def test_case_insensitive_command_match(self): |
| 186 | + event = {"comment": {"body": "@bot HELP"}} |
| 187 | + |
| 188 | + assert check_event_for_mention(event, "help", "bot") is True |
| 189 | + assert check_event_for_mention(event, "HELP", "bot") is True |
| 190 | + |
| 191 | + def test_multiple_mentions(self): |
| 192 | + event = {"comment": {"body": "@bot help @bot deploy"}} |
| 193 | + |
| 194 | + assert check_event_for_mention(event, "help", "bot") is True |
| 195 | + assert check_event_for_mention(event, "deploy", "bot") is True |
| 196 | + assert check_event_for_mention(event, "test", "bot") is False |
0 commit comments