-
Notifications
You must be signed in to change notification settings - Fork 14
Daily Reminder #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Daily Reminder #142
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
318d0f3
Added latest_thread and daily_reminder to Thread table
chrisdedman 1d1d210
added daily reminder option for recurrent threads
chrisdedman d79e102
Merge branch 'main' into feature/daily_reminder
chrisdedman 70cb9da
Merge branch 'main' into feature/daily_reminder
chrisdedman e700ec7
Added ignore reminder that was archived or locked + skip if there is …
chrisdedman 84d3d4c
Remove unused import from Thread model and add newline at EOF
chrisdedman b79d561
update grace-framework version for stagging version purposes [will be…
chrisdedman 2526221
Added initial threads cog unit test
chrisdedman d85664c
Merge branch 'main' into feature/daily_reminder
chrisdedman 8bf83b7
Add Alembic migration to add latest_thread (BigInteger) and daily_rem…
chrisdedman c263d19
fix ruff formatting for test
chrisdedman a58874e
Refactor Thread model to use Field annotations and simplify recurrenc…
chrisdedman 8ebd90f
Refactor threads cog: pass daily_reminder to Thread.create, use Threa…
chrisdedman e6806da
Ran ruff for formatting
chrisdedman 3b396de
Merge branch 'main' into feature/daily_reminder
chrisdedman 2469760
Rename latest_thread to latest_thread_id in Thread model and Alembic …
chrisdedman 2f14160
ran ruff format
chrisdedman 04a54c3
simplify threads fetch
chrisdedman 408ba59
refactor unit test to use the test db instead of patches + added unit…
chrisdedman 1b30f3e
add test db
chrisdedman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
db/alembic/versions/b7c695397ab2_add_latest_thread_and_daily_reminder_to_.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| """add_latest_thread_and_daily_reminder_to_threads | ||
|
|
||
| Revision ID: b7c695397ab2 | ||
| Revises: cc8da39749e7 | ||
| Create Date: 2025-10-15 15:58:03.467659 | ||
|
|
||
| """ | ||
|
|
||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "b7c695397ab2" | ||
| down_revision = "cc8da39749e7" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.add_column("threads", sa.Column("latest_thread", sa.BigInteger(), nullable=True)) | ||
| op.add_column("threads", sa.Column("daily_reminder", sa.Boolean())) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_column("threads", "latest_thread") | ||
| op.drop_column("threads", "daily_reminder") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import pytest | ||
|
|
||
| from bot.extensions.threads_cog import ThreadsCog | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_bot(): | ||
| """Create a mock Discord bot instance.""" | ||
| bot = MagicMock() | ||
| bot.default_color = 0xFFFFFF | ||
| bot.app.config.get = MagicMock(return_value=None) | ||
| bot.scheduler = MagicMock() | ||
| return bot | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def threads_cog(mock_bot): | ||
| """Instantiate the ThreadsCog with a mock bot.""" | ||
|
|
||
| return ThreadsCog(mock_bot) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def dummy_modal(monkeypatch): | ||
| """Fixture that patches ThreadModal and records constructor args.""" | ||
| called_args = {} | ||
|
|
||
| class DummyModal: | ||
| def __init__(self, recurrence, reminder=None, thread=None): | ||
| called_args["recurrence"] = recurrence | ||
| called_args["reminder"] = reminder | ||
| called_args["thread"] = thread | ||
|
|
||
| monkeypatch.setattr("bot.extensions.threads_cog.ThreadModal", DummyModal) | ||
|
|
||
| return called_args | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_create_thread_modal_called(threads_cog): | ||
| """Verify that thread modal is called.""" | ||
| ctx = MagicMock() | ||
| ctx.interaction = MagicMock() | ||
| ctx.interaction.response = MagicMock() | ||
| ctx.interaction.response.send_modal = AsyncMock() | ||
|
|
||
| recurrence = "DAILY" | ||
| reminder = True | ||
| await threads_cog.create.callback( | ||
| threads_cog, | ||
| ctx, | ||
| recurrence=recurrence, | ||
| reminder=reminder, | ||
| ) | ||
|
|
||
| ctx.interaction.response.send_modal.assert_awaited_once() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_create_modal_args(threads_cog, dummy_modal): | ||
| ctx = MagicMock() | ||
| ctx.interaction = MagicMock() | ||
| ctx.interaction.response = MagicMock() | ||
| ctx.interaction.response.send_modal = AsyncMock() | ||
|
|
||
| recurrence = "DAILY" | ||
| reminder = True | ||
|
|
||
| await threads_cog.create.callback( | ||
| threads_cog, | ||
| ctx, | ||
| recurrence=recurrence, | ||
| reminder=reminder, | ||
| ) | ||
|
|
||
| ctx.interaction.response.send_modal.assert_awaited_once() | ||
| assert dummy_modal["recurrence"] == recurrence | ||
| assert dummy_modal["reminder"] == reminder | ||
| assert dummy_modal["thread"] is None | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_daily_reminder_no_threads(threads_cog, mock_bot): | ||
| """Test daily_reminder when there are no threads.""" | ||
| with patch("bot.extensions.threads_cog.Thread.all", return_value=[]): | ||
| mock_channel = MagicMock() | ||
| mock_bot.get_channel.return_value = mock_channel | ||
|
|
||
| await threads_cog.daily_reminder() | ||
| mock_channel.send.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_daily_reminder_with_active_threads(threads_cog, mock_bot): | ||
| """Test daily_reminder sends reminders for active threads.""" | ||
| thread1 = MagicMock() | ||
| thread1.latest_thread = 123 | ||
| thread1.daily_reminder = True | ||
| thread1.title = "Thread 1" | ||
| thread1.content = "Content 1" | ||
|
|
||
| thread2 = MagicMock() | ||
| thread2.latest_thread = 456 | ||
| thread2.daily_reminder = False # Should not be included | ||
|
|
||
| # discord_thread is not archived or locked | ||
| discord_thread = MagicMock() | ||
| discord_thread.archived = False | ||
| discord_thread.locked = False | ||
|
|
||
| with patch( | ||
| "bot.extensions.threads_cog.Thread.all", return_value=[thread1, thread2] | ||
| ): | ||
| mock_bot.fetch_channel = AsyncMock(return_value=discord_thread) | ||
| mock_channel = MagicMock() | ||
| mock_channel.send = AsyncMock() | ||
| mock_bot.get_channel.return_value = mock_channel | ||
|
|
||
| await threads_cog.daily_reminder() | ||
|
|
||
| mock_channel.send.assert_awaited_once() | ||
| args, kwargs = mock_channel.send.await_args | ||
| embed = kwargs.get("embed") | ||
| assert embed is not None | ||
| assert "Daily Reminder" in embed.title | ||
|
|
||
| assert len(embed.fields) == 1 | ||
| assert embed.fields[0].value == f"- <#{thread1.latest_thread}>" | ||
| assert any(thread2.latest_thread != field.value for field in embed.fields) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_daily_reminder_skips_archived_and_locked(threads_cog, mock_bot): | ||
| """Test daily_reminder skips archived and locked threads.""" | ||
| thread = MagicMock() | ||
| thread.latest_thread = 789 | ||
| thread.daily_reminder = True | ||
|
|
||
| discord_thread = MagicMock() | ||
| discord_thread.archived = True | ||
| discord_thread.locked = True | ||
|
|
||
| with patch("bot.extensions.threads_cog.Thread.all", return_value=[thread]): | ||
| mock_bot.fetch_channel = AsyncMock(return_value=discord_thread) | ||
| mock_channel = MagicMock() | ||
| mock_bot.get_channel.return_value = mock_channel | ||
|
|
||
| await threads_cog.daily_reminder() | ||
| mock_channel.send.assert_not_called() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.