Skip to content

Commit

Permalink
Added tests for TwitchChat emote processor (#214)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
csharpfritz and github-actions[bot] authored Sep 20, 2023
1 parent 8519b8e commit 6469571
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/TagzApp.Providers.TwitchChat/ChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,7 @@ private void ProcessMessage(string msg)
var badges = ChatClient.reBadges.Match(msg).Groups[1].Value.Split(',');

// Handle Emotes
var emotesRaw = ChatClient.reEmotes.Match(msg).Groups[1].Value.Replace(';', ' ').Split('/', StringSplitOptions.TrimEntries);
var emotes = new List<Emote>(emotesRaw.Length);
if ((emotesRaw?.Any() ?? false) && !string.IsNullOrEmpty(emotesRaw.First()))
{
foreach (var emote in emotesRaw)
{
var parts = emote.Split(":");
var positions = parts[1].Split("-");
emotes.Add(new Emote(int.Parse(positions[0]), int.Parse(positions[1]) - int.Parse(positions[0]) + 1, $"https://static-cdn.jtvnw.net/emoticons/v2/{parts[0]}/static/light/2.0"));
}
}
List<Emote> emotes = IdentifyEmotes(msg);

message = ChatClient.reChatMessage.Match(msg).Groups[1].Value;
Logger.LogTrace($"Message received from '{userName}': {message}");
Expand Down Expand Up @@ -290,6 +280,23 @@ private void ProcessMessage(string msg)

}

internal static List<Emote> IdentifyEmotes(string msg)
{
var emotesRaw = ChatClient.reEmotes.Match(msg).Groups[1].Value.Replace(';', ' ').Split('/', StringSplitOptions.TrimEntries);
var emotes = new List<Emote>(emotesRaw.Length);
if ((emotesRaw?.Any() ?? false) && !string.IsNullOrEmpty(emotesRaw.First()))
{
foreach (var emote in emotesRaw)
{
var parts = emote.Split(":");
var positions = parts[1].Split("-");
emotes.Add(new Emote(int.Parse(positions[0]), int.Parse(positions[1]) - int.Parse(positions[0]) + 1, $"https://static-cdn.jtvnw.net/emoticons/v2/{parts[0]}/static/light/2.0"));
}
}

return emotes;
}

private string ReadMessage()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="TagzApp.UnitTest" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0-rc.1.23419.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0-rc.1.23419.4" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0-rc.1.23419.4" />
Expand Down
1 change: 1 addition & 0 deletions src/TagzApp.UnitTest/TagzApp.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<ProjectReference Include="..\TagzApp.Common\TagzApp.Common.csproj" />
<ProjectReference Include="..\TagzApp.Providers.Blazot\TagzApp.Providers.Blazot.csproj" />
<ProjectReference Include="..\TagzApp.Providers.Mastodon\TagzApp.Providers.Mastodon.csproj" />
<ProjectReference Include="..\TagzApp.Providers.TwitchChat\TagzApp.Providers.TwitchChat.csproj" />
<ProjectReference Include="..\TagzApp.Providers.Youtube\TagzApp.Providers.Youtube.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using SUT = TagzApp.Providers.TwitchChat.ChatClient;

namespace TagzApp.UnitTest.TwitchChat.ChatClient;


public class GivenMessageWithEmotes
{

const string RawMessageWithEmotes = """
@badge-info=subscriber/65;badges=vip/1,subscriber/48,glitchcon2020/1;client-nonce=1b97cd0780e2dc2b5f61d800a6f7d278;color=#1E90FF;display-name=csharpfritz;emotes=302930318:8-18/140102:97-107;first-msg=0;flags=;id=c986dadb-d9d5-4cb2-b8bb-953792fb6a41;mod=0;returning-chatter=0;room-id=63208102;subscriber=1;tmi-sent-ts=1695218877167;turbo=0;user-id=96909659;user-type=;vip=1 :csharpfritz!csharpfritz@csharpfritz.tmi.twitch.tv PRIVMSG #fiercekittenz :Lasers! csharpGuess and challenge coins? I've wanted to pull the trigger on those for a while! kittenzHypu
""";

[Fact]
public void ShouldReturnMultipleEmotes()
{

var outEmotes = SUT.IdentifyEmotes(RawMessageWithEmotes);

Assert.Equal(2, outEmotes.Count);

}

[Fact]
public void ShouldReturnCorrectEmotes()
{

var outEmotes = SUT.IdentifyEmotes(RawMessageWithEmotes);
var testEmote = outEmotes.First();

Assert.Contains("302930318", testEmote.ImageUrl);
Assert.Equal(8, testEmote.Pos);
Assert.Equal(11, testEmote.Length);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using SUT = TagzApp.Providers.TwitchChat.ChatClient;

namespace TagzApp.UnitTest.TwitchChat.ChatClient;

public class GivenMessageWithoutEmotes
{

const string RawMessage = """
@badge-info=subscriber/65;badges=vip/1,subscriber/48,glitchcon2020/1;client-nonce=1b97cd0780e2dc2b5f61d800a6f7d278;color=#1E90FF;display-name=csharpfritz;emotes=;first-msg=0;flags=;id=c986dadb-d9d5-4cb2-b8bb-953792fb6a41;mod=0;returning-chatter=0;room-id=63208102;subscriber=1;tmi-sent-ts=1695218877167;turbo=0;user-id=96909659;user-type=;vip=1 :csharpfritz!csharpfritz@csharpfritz.tmi.twitch.tv PRIVMSG #fiercekittenz :Lasers! and challenge coins? I've wanted to pull the trigger on those for a while!
""";

[Fact]
public void ShouldReturnMultipleEmotes()
{

var outEmotes = SUT.IdentifyEmotes(RawMessage);

Assert.Empty(outEmotes);

}

}

0 comments on commit 6469571

Please sign in to comment.