-
Notifications
You must be signed in to change notification settings - Fork 1
feat: make DiscordChannelAdapter testable and add unit tests #387
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
dnyw4l3n13
merged 10 commits into
main
from
feature/374-discord-channel-adapter-testable
Jul 8, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
24b0e00
feat: make DiscordChannelAdapter testable and add unit tests
dnyw4l3n13 6740ca1
feat: make DiscordChannelAdapter testable and add unit tests
dnyw4l3n13 47b784f
refactor(tests): remove unnecessary IMessageChannel substitute in Sen…
dnyw4l3n13 c1a7332
refactor(tests): use ReturnsForAnyArgs for SendMessageAsync mock setup
dnyw4l3n13 f594b05
test: add Received() assertion to verify embed is forwarded in SendMe…
dnyw4l3n13 8f79eb4
docs(tests): clarify that CleanContent is always empty in production
dnyw4l3n13 fca6992
fix: add null-guard on ITextChannel constructor parameter in DiscordC…
dnyw4l3n13 52e6801
docs(tests): remove comment describing test intent from DiscordChanne…
dnyw4l3n13 e4e93d6
refactor: use this._channel.Name directly in SendMessageAsync instead…
dnyw4l3n13 964acef
refactor(tests): remove redundant Discord.Net PackageReference from t…
dnyw4l3n13 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
75 changes: 75 additions & 0 deletions
75
src/BuildBot.Discord.Tests/Services/DiscordChannelAdapterTests.cs
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,75 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using BuildBot.Discord.Services; | ||
| using Discord; | ||
| using FunFair.Test.Common; | ||
| using NSubstitute; | ||
| using Xunit; | ||
|
|
||
| namespace BuildBot.Discord.Tests.Services; | ||
|
|
||
| public sealed class DiscordChannelAdapterTests : TestBase | ||
| { | ||
| [Fact] | ||
| public void Name_ReturnsChannelName() | ||
| { | ||
| ITextChannel channel = GetSubstitute<ITextChannel>(); | ||
| channel.Name.Returns("test-channel"); | ||
|
|
||
| DiscordChannelAdapter adapter = new(channel); | ||
|
|
||
| Assert.Equal(expected: "test-channel", actual: adapter.Name); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EnterTypingState_ReturnsTypingStateFromChannel() | ||
| { | ||
| ITextChannel channel = GetSubstitute<ITextChannel>(); | ||
| IDisposable typingState = GetSubstitute<IDisposable>(); | ||
| channel.EnterTypingState(Arg.Any<RequestOptions>()).Returns(typingState); | ||
|
|
||
| DiscordChannelAdapter adapter = new(channel); | ||
|
|
||
| IDisposable result = adapter.EnterTypingState(); | ||
|
|
||
| Assert.Same(expected: typingState, actual: result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task SendMessageAsync_ReturnsChannelNameAndCleanContent() | ||
| { | ||
| ITextChannel channel = GetSubstitute<ITextChannel>(); | ||
| IUserMessage message = GetSubstitute<IUserMessage>(); | ||
|
|
||
| channel.Name.Returns("sent-channel"); | ||
|
|
||
| // In production CleanContent is always "" because the adapter sends text: string.Empty (embed-only). | ||
| message.CleanContent.Returns("clean content"); | ||
|
|
||
| channel.SendMessageAsync(text: string.Empty, embed: default).ReturnsForAnyArgs(Task.FromResult(message)); | ||
|
|
||
| DiscordChannelAdapter adapter = new(channel); | ||
|
|
||
| Embed embed = new EmbedBuilder().Build(); | ||
| (string sentToChannel, string messageContent) = await adapter.SendMessageAsync(embed); | ||
|
|
||
| Assert.Equal(expected: "sent-channel", actual: sentToChannel); | ||
| Assert.Equal(expected: "clean content", actual: messageContent); | ||
|
credfeto marked this conversation as resolved.
|
||
|
|
||
| await channel | ||
| .Received(1) | ||
| .SendMessageAsync( | ||
| text: string.Empty, | ||
| isTTS: Arg.Any<bool>(), | ||
| embed: embed, | ||
| options: Arg.Any<RequestOptions>(), | ||
| allowedMentions: Arg.Any<AllowedMentions>(), | ||
| messageReference: Arg.Any<MessageReference>(), | ||
| components: Arg.Any<MessageComponent>(), | ||
| stickers: Arg.Any<ISticker[]>(), | ||
| embeds: Arg.Any<Embed[]>(), | ||
| flags: Arg.Any<MessageFlags>(), | ||
| poll: Arg.Any<PollProperties>() | ||
| ); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.