Skip to content

Commit 390ad8e

Browse files
committed
fix: remove extra newline in announcements
1 parent c0ee087 commit 390ad8e

2 files changed

Lines changed: 76 additions & 6 deletions

File tree

src/cogs/admin/test_announcements.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,24 @@
66
from discord.app_commands import Choice
77

88
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_without_title_has_no_leading_newline(self):
19+
announcement = _format_announcement("", "test")
20+
21+
self.assertEqual(announcement, "test")
22+
23+
def test_format_announcement_with_whitespace_title_treats_it_as_missing(self):
24+
announcement = _format_announcement(" ", "test")
25+
26+
self.assertEqual(announcement, "test")
1027

1128

1229
class TestAnnouncements(IsolatedAsyncioTestCase):
@@ -49,5 +66,51 @@ async def test_announce_command(self, mock_wait):
4966
self.assertIsInstance(sent_modal, Announcement)
5067
mock_wait.assert_awaited_once()
5168

69+
async def test_on_submit_regular_submission_uses_single_newline(self):
70+
mock_channel = MagicMock()
71+
mock_channel.send = AsyncMock()
72+
73+
mock_interaction = MagicMock(spec=Interaction)
74+
mock_interaction.response = MagicMock()
75+
mock_interaction.response.is_done = MagicMock(return_value=False)
76+
mock_interaction.response.send_message = AsyncMock()
77+
mock_interaction.followup = MagicMock()
78+
mock_interaction.followup.send = AsyncMock()
79+
80+
modal = Announcement(None, mock_channel, "regular")
81+
modal.announcement_title = MagicMock(value="my title")
82+
modal.announcement = MagicMock(value="test")
83+
84+
await modal.on_submit(mock_interaction)
85+
86+
mock_channel.send.assert_awaited_once_with(
87+
content="**my title**\ntest",
88+
file=None
89+
)
90+
91+
async def test_on_submit_embed_submission_skips_leading_newline_without_title(self):
92+
mock_channel = MagicMock()
93+
mock_channel.send = AsyncMock()
94+
95+
mock_interaction = MagicMock(spec=Interaction)
96+
mock_interaction.response = MagicMock()
97+
mock_interaction.response.is_done = MagicMock(return_value=False)
98+
mock_interaction.response.send_message = AsyncMock()
99+
mock_interaction.followup = MagicMock()
100+
mock_interaction.followup.send = AsyncMock()
101+
102+
modal = Announcement(None, mock_channel, "embed")
103+
modal.announcement_title = MagicMock(value=" ")
104+
modal.announcement = MagicMock(value="test")
105+
106+
await modal.on_submit(mock_interaction)
107+
108+
mock_channel.send.assert_awaited_once()
109+
self.assertEqual(
110+
mock_channel.send.await_args.kwargs["embed"].description,
111+
"test"
112+
)
113+
self.assertIsNone(mock_channel.send.await_args.kwargs["file"])
114+
52115
if __name__ == "__main__":
53116
unittest.main()

src/ui/modals/announcement.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ def _unique(iterable: list) -> list:
2727
return r
2828

2929

30+
def _format_announcement(title: str, content: str) -> str:
31+
"""Build the outgoing announcement body."""
32+
if title and not title.isspace():
33+
return f"**{title}**\n{content}"
34+
35+
return content
36+
37+
3038
class Announcement(Modal, title='Announcement'):
3139
announcement_title = TextInput(
3240
label='Title',
@@ -57,15 +65,14 @@ def __init__(
5765

5866
async def on_submit(self, interaction: Interaction) -> None:
5967
photo = None
60-
announcement_title = ""
6168

6269
if self.attachment: # If the user has uploaded an attachment
6370
photo = await self.attachment.to_file()
6471

65-
if self.announcement_title.value:
66-
announcement_title = f"**{self.announcement_title.value}**"
67-
68-
announcement = announcement_title + f'\n\n{self.announcement.value}'
72+
announcement = _format_announcement(
73+
self.announcement_title.value,
74+
self.announcement.value
75+
)
6976

7077
if self.mention:
7178
selection_view = AnnouncementView()

0 commit comments

Comments
 (0)