-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from pureooze/ushamim/notice-message
- Loading branch information
Showing
8 changed files
with
140 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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
99 changes: 99 additions & 0 deletions
99
TwitchEverywhere.UnitTests/TwitchConnectorTests/NoticeTests.cs
This file contains 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,99 @@ | ||
using System.Collections.Immutable; | ||
using Moq; | ||
using NUnit.Framework.Internal; | ||
using TwitchEverywhere.Implementation; | ||
using TwitchEverywhere.Types; | ||
|
||
namespace TwitchEverywhere.UnitTests.TwitchConnectorTests; | ||
|
||
[TestFixture] | ||
public class NoticeTests { | ||
private readonly TwitchConnectionOptions m_options = new( | ||
"channel", | ||
"access_token", | ||
"refresh_token", | ||
"client_id", | ||
"client_secret", | ||
"client_name" | ||
); | ||
|
||
private readonly DateTime m_startTime = DateTimeOffset.FromUnixTimeMilliseconds(1507246572675).UtcDateTime; | ||
|
||
private ITwitchConnector m_twitchConnector; | ||
|
||
[Test] | ||
[TestCaseSource(nameof(NoticeMessages))] | ||
public async Task Notice( IImmutableList<string> messages, Message? expectedMessage ) { | ||
Mock<IAuthorizer> authorizer = new( behavior: MockBehavior.Strict ); | ||
Mock<IDateTimeService> dateTimeService = new( MockBehavior.Strict ); | ||
dateTimeService.Setup( dts => dts.GetStartTime() ).Returns( m_startTime ); | ||
|
||
IWebSocketConnection webSocket = new TestWebSocketConnection( messages ); | ||
|
||
void MessageCallback( | ||
Message message | ||
) { | ||
Assert.That( message, Is.Not.Null ); | ||
|
||
switch( message.MessageType ) { | ||
case MessageType.Notice: { | ||
Notice msg = (Notice)message; | ||
Notice? expectedPrivMessage = (Notice)expectedMessage; | ||
NoticeMessageCallback( msg, expectedPrivMessage ); | ||
break; | ||
} | ||
case MessageType.PrivMsg: | ||
case MessageType.ClearMsg: | ||
case MessageType.ClearChat: | ||
case MessageType.GlobalUserState: | ||
case MessageType.RoomState: | ||
case MessageType.UserNotice: | ||
case MessageType.UserState: | ||
case MessageType.Whisper: | ||
default: | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
} | ||
|
||
authorizer.Setup( expression: a => a.GetToken() ).ReturnsAsync( value: "token" ); | ||
m_twitchConnector = new TwitchConnector( | ||
authorizer: authorizer.Object, | ||
webSocketConnection: webSocket, | ||
dateTimeService: dateTimeService.Object | ||
); | ||
|
||
bool result = await m_twitchConnector.TryConnect( m_options, MessageCallback ); | ||
Assert.That( result, Is.True ); | ||
} | ||
|
||
private void NoticeMessageCallback( | ||
Notice notice, | ||
Notice? expectedNoticeMessage | ||
) { | ||
Assert.That( notice.MsgId, Is.EqualTo( expectedNoticeMessage?.MsgId ), "MsgId was not equal to expected value"); | ||
Assert.That( notice.TargetUserId, Is.EqualTo( expectedNoticeMessage?.TargetUserId ), "TargetUserId was not equal to expected value"); | ||
Assert.That( notice.MessageType, Is.EqualTo( expectedNoticeMessage?.MessageType ), "MessageType was not equal to expected value"); | ||
} | ||
|
||
private static IEnumerable<TestCaseData> NoticeMessages() { | ||
yield return new TestCaseData( | ||
new List<string> { | ||
$"@msg-id=delete_message_success :tmi.twitch.tv NOTICE #channel :The message from foo is now deleted." | ||
}.ToImmutableList(), | ||
new Notice( | ||
MsgId: "delete_message_success", | ||
TargetUserId: "" | ||
) | ||
).SetName("Message Delete Success, No User ID"); | ||
|
||
yield return new TestCaseData( | ||
new List<string> { | ||
$"@msg-id=whisper_restricted;target-user-id=12345678 :tmi.twitch.tv NOTICE #channel :Your settings prevent you from sending this whisper." | ||
}.ToImmutableList(), | ||
new Notice( | ||
MsgId: "whisper_restricted", | ||
TargetUserId: "12345678" | ||
) | ||
).SetName("Whisper Restricted, With User ID"); | ||
} | ||
} |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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,6 @@ | ||
namespace TwitchEverywhere.Types; | ||
|
||
public record Notice( | ||
string MsgId, | ||
string TargetUserId | ||
) : Message ( MessageType.Notice ); |