Skip to content

Commit

Permalink
Sum tests for pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
occluder committed Jan 5, 2024
1 parent a899ecd commit db0d363
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
132 changes: 132 additions & 0 deletions MiniTwitch.PubSub.Test/SpanSumTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System.Text;
using MiniTwitch.PubSub.Internal.Enums;
using Newtonsoft.Json.Linq;
using Xunit.Sdk;

namespace MiniTwitch.PubSub.Test;
public class SpanSumTests
{
private static readonly Dictionary<int, string> _topics = new()
{
{ (int)MessageTopic.None, "None" },
{ (int)MessageTopic.BitsEventsV1, "channel-bits-events-v1" },
{ (int)MessageTopic.BitsEventsV2, "channel-bits-events-v2" },
{ (int)MessageTopic.BitsBadgeUnlock, "channel-bits-badge-unlocks" },
{ (int)MessageTopic.ChannelPoints, "channel-points-channel-v1" },
{ (int)MessageTopic.SubscribeEvents, "channel-subscribe-events-v1" },
{ (int)MessageTopic.AutomodQueue, "automod-queue" },
{ (int)MessageTopic.LowTrustUsers, "low-trust-users" },
{ (int)MessageTopic.ModerationNotifications, "user-moderation-notifications" },
{ (int)MessageTopic.ChatroomsUser, "chatrooms-user-v1" },
{ (int)MessageTopic.ChannelPredictions, "predictions-channel-v1" },
{ (int)MessageTopic.PinnedChatUpdates, "pinned-chat-updates-v1" },
{ (int)MessageTopic.VideoPlayback, "video-playback-by-id" },
{ (int)MessageTopic.BroadcastSettingsUpdate, "broadcast-settings-update" },
{ (int)MessageTopic.ModeratorActions, "chat_moderator_actions" },
{ (int)MessageTopic.Polls, "polls" },
{ (int)MessageTopic.CommunityChannelPoints, "community-points-channel-v1" },
{ (int)MessageTopic.Following, "following" },
{ (int)MessageTopic.CommunityMoments, "community-moments-channel-v1" },
};

private static readonly Dictionary<int, string> _types = new()
{
{ (int)MessageType.None, "None" },
{ (int)MessageType.PONG, "PONG" },
{ (int)MessageType.MESSAGE, "MESSAGE" },
{ (int)MessageType.RESPONSE, "RESPONSE" },
{ (int)MessageType.RECONNECT, "RECONNECT" },
};

[Fact]
public void Test_Includes_All_Topics()
{
var topics = Enum.GetNames(typeof(MessageTopic));
if (topics.Length != _topics.Count)
{
var notIncluded = topics.Where(x => !_topics.ContainsKey((int)Enum.Parse<MessageTopic>(x)));
Assert.Fail($"Test does not account for all topics, expected {topics.Length}, got {_topics.Count}." +
$"\nMissing topics: \n{string.Join("\n", notIncluded)}");
}

foreach (var topic in topics)
{
var enumValue = Enum.Parse<MessageTopic>(topic);
try
{
Assert.True(_topics.ContainsKey((int)enumValue));
}
catch (TrueException)
{
Assert.Fail($"Enum value {enumValue} not present.");
}
}
}

[Fact]
public void Test_Includes_All_Types()
{
var types = Enum.GetNames(typeof(MessageType));
if (types.Length != _types.Count)
{
var notIncluded = types.Where(x => !_topics.ContainsKey((int)Enum.Parse<MessageType>(x)));
Assert.Fail($"Test does not account for all types, expected {types.Length}, got {_types.Count}." +
$"\nMissing types: \n{string.Join("\n", notIncluded)}");
}

foreach (var type in types)
{
var enumValue = Enum.Parse<MessageType>(type);
try
{
Assert.True(_types.ContainsKey((int)enumValue));
}
catch (TrueException)
{
Assert.Fail($"Enum value {enumValue} not present.");
}
}
}

[Fact]
public void All_Topic_Values_Match_Sum()
{
foreach (var kvp in _topics)
{
if (kvp.Key != MSum(kvp.Value))
{
Assert.Fail($"Topic \"{kvp.Value}\" does not match expected sum {kvp.Key}. Got {MSum(kvp.Value)} instead.");
}
}
}

[Fact]
public void All_Type_Values_Match_Sum()
{
foreach (var kvp in _types)
{
if (kvp.Key != MSum(kvp.Value))
{
Assert.Fail($"Type \"{kvp.Value}\" does not match expected sum {kvp.Key}. Got {MSum(kvp.Value)} instead.");
}
}
}

private static int MSum(string s)
{
var source = Encoding.UTF8.GetBytes(s);
if (source.Length == 0)
{
return 0;
}

int m = source[0];
int sum = 0;
foreach (byte b in source)
{
sum += b;
}

return m * sum;
}
}
2 changes: 1 addition & 1 deletion MiniTwitch.PubSub/Internal/Enums/MessageTopic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

internal enum MessageTopic
{
None,
None = 31200,
Polls = 62048,
Following = 99654,
AutomodQueue = 131435,
Expand Down
2 changes: 1 addition & 1 deletion MiniTwitch.PubSub/Internal/Enums/MessageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

internal enum MessageType
{
None = 0,
None = 31200,
PONG = 24640,
MESSAGE = 39809,
RESPONSE = 51086,
Expand Down

0 comments on commit db0d363

Please sign in to comment.