|
6 | 6 | from discord.app_commands import Choice |
7 | 7 |
|
8 | 8 | from src.cogs.admin.announcements import Announcements |
9 | | -from src.ui.modals.announcement import Announcement |
| 9 | +from src.ui.modals.announcement import Announcement, _format_announcement |
| 10 | + |
| 11 | + |
| 12 | +class TestAnnouncementFormatting(unittest.TestCase): |
| 13 | + def test_format_announcement_with_title_uses_single_newline(self): |
| 14 | + announcement = _format_announcement("my title", "test") |
| 15 | + |
| 16 | + self.assertEqual(announcement, "# my title\ntest") |
| 17 | + |
| 18 | + def test_format_announcement_with_title_strips_leading_line_breaks_from_content(self): |
| 19 | + announcement = _format_announcement("my title", "\n\ntest") |
| 20 | + |
| 21 | + self.assertEqual(announcement, "# my title\ntest") |
| 22 | + |
| 23 | + def test_format_announcement_without_title_has_no_leading_newline(self): |
| 24 | + announcement = _format_announcement("", "test") |
| 25 | + |
| 26 | + self.assertEqual(announcement, "test") |
| 27 | + |
| 28 | + def test_format_announcement_with_whitespace_title_treats_it_as_missing(self): |
| 29 | + announcement = _format_announcement(" ", "test") |
| 30 | + |
| 31 | + self.assertEqual(announcement, "test") |
10 | 32 |
|
11 | 33 |
|
12 | 34 | class TestAnnouncements(IsolatedAsyncioTestCase): |
@@ -49,5 +71,73 @@ async def test_announce_command(self, mock_wait): |
49 | 71 | self.assertIsInstance(sent_modal, Announcement) |
50 | 72 | mock_wait.assert_awaited_once() |
51 | 73 |
|
| 74 | + async def test_on_submit_regular_submission_uses_single_newline(self): |
| 75 | + mock_channel = MagicMock() |
| 76 | + mock_channel.send = AsyncMock() |
| 77 | + |
| 78 | + mock_interaction = MagicMock(spec=Interaction) |
| 79 | + mock_interaction.response = MagicMock() |
| 80 | + mock_interaction.response.is_done = MagicMock(return_value=False) |
| 81 | + mock_interaction.response.send_message = AsyncMock() |
| 82 | + mock_interaction.followup = MagicMock() |
| 83 | + mock_interaction.followup.send = AsyncMock() |
| 84 | + |
| 85 | + modal = Announcement(None, mock_channel, "regular") |
| 86 | + modal.announcement_title = MagicMock(value="my title") |
| 87 | + modal.announcement = MagicMock(value="test") |
| 88 | + |
| 89 | + await modal.on_submit(mock_interaction) |
| 90 | + |
| 91 | + mock_channel.send.assert_awaited_once_with( |
| 92 | + content="# my title\ntest", |
| 93 | + file=None |
| 94 | + ) |
| 95 | + |
| 96 | + async def test_on_submit_regular_submission_drops_leading_line_breaks_in_content(self): |
| 97 | + mock_channel = MagicMock() |
| 98 | + mock_channel.send = AsyncMock() |
| 99 | + |
| 100 | + mock_interaction = MagicMock(spec=Interaction) |
| 101 | + mock_interaction.response = MagicMock() |
| 102 | + mock_interaction.response.is_done = MagicMock(return_value=False) |
| 103 | + mock_interaction.response.send_message = AsyncMock() |
| 104 | + mock_interaction.followup = MagicMock() |
| 105 | + mock_interaction.followup.send = AsyncMock() |
| 106 | + |
| 107 | + modal = Announcement(None, mock_channel, "regular") |
| 108 | + modal.announcement_title = MagicMock(value="my title") |
| 109 | + modal.announcement = MagicMock(value="\n\ntest") |
| 110 | + |
| 111 | + await modal.on_submit(mock_interaction) |
| 112 | + |
| 113 | + mock_channel.send.assert_awaited_once_with( |
| 114 | + content="# my title\ntest", |
| 115 | + file=None |
| 116 | + ) |
| 117 | + |
| 118 | + async def test_on_submit_embed_submission_skips_leading_newline_without_title(self): |
| 119 | + mock_channel = MagicMock() |
| 120 | + mock_channel.send = AsyncMock() |
| 121 | + |
| 122 | + mock_interaction = MagicMock(spec=Interaction) |
| 123 | + mock_interaction.response = MagicMock() |
| 124 | + mock_interaction.response.is_done = MagicMock(return_value=False) |
| 125 | + mock_interaction.response.send_message = AsyncMock() |
| 126 | + mock_interaction.followup = MagicMock() |
| 127 | + mock_interaction.followup.send = AsyncMock() |
| 128 | + |
| 129 | + modal = Announcement(None, mock_channel, "embed") |
| 130 | + modal.announcement_title = MagicMock(value=" ") |
| 131 | + modal.announcement = MagicMock(value="test") |
| 132 | + |
| 133 | + await modal.on_submit(mock_interaction) |
| 134 | + |
| 135 | + mock_channel.send.assert_awaited_once() |
| 136 | + self.assertEqual( |
| 137 | + mock_channel.send.await_args.kwargs["embed"].description, |
| 138 | + "test" |
| 139 | + ) |
| 140 | + self.assertIsNone(mock_channel.send.await_args.kwargs["file"]) |
| 141 | + |
52 | 142 | if __name__ == "__main__": |
53 | 143 | unittest.main() |
0 commit comments