Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Moq;
using Nullinside.Api.Common.Twitch;
using Nullinside.Api.Model.Ddl;
using Nullinside.Api.TwitchBot.ChatRules;

namespace Nullinside.Api.TwitchBot.Tests.ChatRules;

/// <summary>
/// A generic set of sets that all chat rules should be put through.
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class AChatRuleUnitTestBase<T> : UnitTestBase where T : AChatRule, new()
{
/// <summary>
/// Tests that the message filter is capable of passing at all.
/// </summary>
/// <param name="goodString">A friendly string with no issues.</param>
[Test]
[TestCase("Hello I love candy and sprinkles")]
public async Task TestItDoesntAlwaysFail(string goodString)
{
var rule = new T();
var botProxy = new Mock<ITwitchApiProxy>();

// Process the message and assert that we pass the message.
var chat = new TwitchChatMessage(true, goodString, "123", "456");
var result = await rule.Handle("123", botProxy.Object, chat, _db);
Assert.That(result, Is.True);

// Process the message and assert that we pass the message.
chat = new TwitchChatMessage(false, goodString, "123", "456");
result = await rule.Handle("123", botProxy.Object, chat, _db);
Assert.That(result, Is.True);
}

/// <summary>
/// Tests that the rules are only running when they should be.
/// </summary>
[Test]
public void TestShouldRun()
{
var rule = new T();

// Rule is turned on and so is scanning.
var shouldRun = rule.ShouldRun(new TwitchUserConfig { Enabled = true, BanKnownBots = true });
Assert.That(shouldRun, Is.True);

// Scanning is turned off
shouldRun = rule.ShouldRun(new TwitchUserConfig { Enabled = false, BanKnownBots = true });
Assert.That(shouldRun, Is.False);

// Rule is turned off.
shouldRun = rule.ShouldRun(new TwitchUserConfig { Enabled = true, BanKnownBots = false });
Assert.That(shouldRun, Is.False);

// Everything is off.
shouldRun = rule.ShouldRun(new TwitchUserConfig { Enabled = false, BanKnownBots = false });
Assert.That(shouldRun, Is.False);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
using Moq;
using Nullinside.Api.Common.Twitch;
using Nullinside.Api.TwitchBot.ChatRules;

namespace Nullinside.Api.TwitchBot.Tests.ChatRules;

public class BestCheapViewersTests
/// <summary>
/// Tests the <see cref="BestCheapViewers" /> class.
/// </summary>
public class BestCheapViewersTests : AChatRuleUnitTestBase<BestCheapViewers>
{
/// <summary>
/// Tests strings that have been typed in chats.
/// </summary>
/// <param name="badString">The string that should fail.</param>
[Test]
[TestCase("Best viewers on ***")]
[TestCase("Best viewers on ***")]
Expand All @@ -18,9 +29,14 @@ public class BestCheapViewersTests
[TestCase("C̀heap Viewers on ***")]
[TestCase("Cheaͧp v̫iewers on ***")]
[TestCase("Cheaͧp v̫iewers on *** ")]
public void TestKnownStrings(string badString)
public async Task TestKnownStrings(string badString)
{
// Need to put interfaces in front of the classes before we can do this.
Assert.Pass();
var rule = new BestCheapViewers();
var botProxy = new Mock<ITwitchApiProxy>();
var chat = new TwitchChatMessage(true, badString, "123", "456");

// Process the message and assert that we fail the message.
var result = await rule.Handle("123", botProxy.Object, chat, _db);
Assert.That(result, Is.False);
}
}
59 changes: 49 additions & 10 deletions src/Nullinside.Api.TwitchBot.Tests/ChatRules/DiscordTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
namespace Nullinside.Api.TwitchBot.Tests.ChatRules;
using Moq;
using Nullinside.Api.Common.Twitch;
using Nullinside.Api.TwitchBot.ChatRules;

public class DiscordTests
namespace Nullinside.Api.TwitchBot.Tests.ChatRules;

/// <summary>
/// Tests the <see cref="Discord" /> class.
/// </summary>
public class DiscordTests : AChatRuleUnitTestBase<Discord>
{
/// <summary>
/// Tests the strings typed in chats.
/// </summary>
/// <param name="badString">The string that should fail.</param>
[Test]
[TestCase("Hello mate you stream pretty cool that's why I follow I would love to be a fan of yours if you don't mind kindly add me on Discord: 👉👉lacaster5")]
[TestCase("Hello, I just recently found your channel and can already tell that your content is great, and I would love to stick with you long term. If you're open to it, I'd be willing to connect with you on Discord. My username is teecash1000")]
[TestCase("Hey there 👋 You stream pretty cool that’s why I followed you. I will like to make you a friend and be a fan, if you don’t mind Kindly chat me on Discord, my Discord username is tuckzay")]
[TestCase("Hi! Just wanted to say that I absolutely love your gameplay and content.l'd love to connect better with you on Discord if that's possible. Looking forward to more awesome streams from you! My username is 👉👉👉 edisonpires")]
[TestCase("What's up Friend, great stream! I'm having a blast watching you stream. Let's move the conversation to Discord, where we can discuss more about streaming in more detail and get to know each other better. See you there! My discord username is 👉john_6029")]
public void TestKnownStrings(string badString)
[TestCase(
"Hello mate you stream pretty cool that's why I follow I would love to be a fan of yours if you don't mind kindly add me on Discord: 👉👉lacaster5")]
[TestCase(
"Hello, I just recently found your channel and can already tell that your content is great, and I would love to stick with you long term. If you're open to it, I'd be willing to connect with you on Discord. My username is teecash1000")]
[TestCase(
"Hey there 👋 You stream pretty cool that’s why I followed you. I will like to make you a friend and be a fan, if you don’t mind Kindly chat me on Discord, my Discord username is tuckzay")]
[TestCase(
"Hi! Just wanted to say that I absolutely love your gameplay and content.l'd love to connect better with you on Discord if that's possible. Looking forward to more awesome streams from you! My username is 👉👉👉 edisonpires")]
[TestCase(
"What's up Friend, great stream! I'm having a blast watching you stream. Let's move the conversation to Discord, where we can discuss more about streaming in more detail and get to know each other better. See you there! My discord username is 👉john_6029")]
public async Task TestKnownStrings(string badString)
{
// Need to put interfaces in front of the classes before we can do this.
Assert.Pass();
var rule = new Discord();
var botProxy = new Mock<ITwitchApiProxy>();
var chat = new TwitchChatMessage(true, badString, "123", "456");

// Process the message and assert that we fail the message.
var result = await rule.Handle("123", botProxy.Object, chat, _db);
Assert.That(result, Is.False);
}

/// <summary>
/// Ensure that the rule doesn't fail just because it contains the word discord.
/// </summary>
/// <param name="message">The message.</param>
[Test]
[TestCase("I've heard of the application discord before and it sounds great")]
[TestCase("I was talking on my discord the other day")]
public async Task EnsureNoFalsePositives(string message)
{
var rule = new Discord();
var botProxy = new Mock<ITwitchApiProxy>();
var chat = new TwitchChatMessage(true, message, "123", "456");

// Process the message and assert that we do not fail the message.
var result = await rule.Handle("123", botProxy.Object, chat, _db);
Assert.That(result, Is.True);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
using Moq;
using Nullinside.Api.Common.Twitch;
using Nullinside.Api.TwitchBot.ChatRules;

namespace Nullinside.Api.TwitchBot.Tests.ChatRules;

public class StreamViewersTests
/// <summary>
/// Tests the <see cref="StreamViewers" /> class.
/// </summary>
public class StreamViewersTests : AChatRuleUnitTestBase<StreamViewers>
{
/// <summary>
/// Tests the strings typed in chats.
/// </summary>
/// <param name="badString">The string that should fail.</param>
[Test]
[TestCase("@jellynyeko dо уоu alrеady triеd strеamviewers оrg? Real viewеrs, fire works! Тhеy arе now giving оut а frее рackagе for streamers оО")]
[TestCase("@kygaming98 dо уоu аlready tried streаmviewers оrg? Real viewers, firе works! Thеy arе now giving оut а freе package fоr streamers oО")]
public void TestKnownStrings(string badString)
[TestCase(
"@jellynyeko dо уоu alrеady triеd strеamviewers оrg? Real viewеrs, fire works! Тhеy arе now giving оut а frее рackagе for streamers оО")]
[TestCase(
"@kygaming98 dо уоu аlready tried streаmviewers оrg? Real viewers, firе works! Thеy arе now giving оut а freе package fоr streamers oО")]
public async Task TestKnownStrings(string badString)
{
// Need to put interfaces in front of the classes before we can do this.
Assert.Pass();
var rule = new StreamViewers();
var botProxy = new Mock<ITwitchApiProxy>();
var chat = new TwitchChatMessage(true, badString, "123", "456");

// Process the message and assert that we fail the message.
var result = await rule.Handle("123", botProxy.Object, chat, _db);
Assert.That(result, Is.False);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/>
<PackageReference Include="Moq" Version="4.20.72"/>
<PackageReference Include="NUnit" Version="4.2.2"/>
<PackageReference Include="NUnit.Analyzers" Version="4.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0"/>
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Nullinside.Api.TwitchBot\Nullinside.Api.TwitchBot.csproj" />
<ProjectReference Include="..\Nullinside.Api.TwitchBot\Nullinside.Api.TwitchBot.csproj"/>
</ItemGroup>

</Project>
35 changes: 35 additions & 0 deletions src/Nullinside.Api.TwitchBot.Tests/UnitTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Nullinside.Api.Model;

namespace Nullinside.Api.TwitchBot.Tests;

/// <summary>
/// A base class for all unit tests.
/// </summary>
public abstract class UnitTestBase
{
/// <summary>
/// A fake database.
/// </summary>
protected INullinsideContext _db;

[SetUp]
public virtual void Setup()
{
// Create an in-memory database to fake the SQL queries. Note that we generate a random GUID for the name
// here. If you use the same name more than once you'll get collisions between tests.
var contextOptions = new DbContextOptionsBuilder<NullinsideContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.ConfigureWarnings(b => b.Ignore(InMemoryEventId.TransactionIgnoredWarning))
.Options;
_db = new NullinsideContext(contextOptions);
}

[TearDown]
public virtual async Task TearDown()
{
// Dispose since it has one.
await _db.DisposeAsync();
}
}
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/AChatRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class AChatRule : IChatRule {
public abstract bool ShouldRun(TwitchUserConfig config);

/// <inheritdoc />
public abstract Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message,
public abstract Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, TwitchChatMessage message,
INullinsideContext db, CancellationToken stoppingToken = new());

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override bool ShouldRun(TwitchUserConfig config) {
}

/// <inheritdoc />
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message,
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, TwitchChatMessage message,
INullinsideContext db, CancellationToken stoppingToken = new()) {
if (!message.IsFirstMessage) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/Discord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override bool ShouldRun(TwitchUserConfig config) {
}

/// <inheritdoc />
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message,
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, TwitchChatMessage message,
INullinsideContext db, CancellationToken stoppingToken = new()) {
if (!message.IsFirstMessage) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/Dogehype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override bool ShouldRun(TwitchUserConfig config) {
}

/// <inheritdoc />
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message,
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, TwitchChatMessage message,
INullinsideContext db, CancellationToken stoppingToken = new()) {
// The number of spaces per message may chance, so normalize that and lowercase it for comparison.
string normalized = string.Concat(message.Message.Split(" ").Where(s => !string.IsNullOrWhiteSpace(s)))
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/HostHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override bool ShouldRun(TwitchUserConfig config) {
}

/// <inheritdoc />
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message,
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, TwitchChatMessage message,
INullinsideContext db, CancellationToken stoppingToken = new()) {
// The number of spaces per message may change, so normalize that and lowercase it for comparison.
string normalized = string.Concat(message.Message.Split(" ").Where(s => !string.IsNullOrWhiteSpace(s)))
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/IChatRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public interface IChatRule {
/// <param name="db">The database.</param>
/// <param name="stoppingToken">The cancellation token.</param>
/// <returns>An asynchronous task.</returns>
public Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message, INullinsideContext db,
public Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, TwitchChatMessage message, INullinsideContext db,
CancellationToken stoppingToken = new());
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override bool ShouldRun(TwitchUserConfig config) {
}

/// <inheritdoc />
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message,
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, TwitchChatMessage message,
INullinsideContext db, CancellationToken stoppingToken = new()) {
if (!message.IsFirstMessage) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/Naked.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override bool ShouldRun(TwitchUserConfig config) {
}

/// <inheritdoc />
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message,
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, TwitchChatMessage message,
INullinsideContext db, CancellationToken stoppingToken = new()) {
if (message.IsFirstMessage &&
(message.Message.TrimStart().StartsWith(_spam, StringComparison.InvariantCultureIgnoreCase) ||
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/StreamRise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override bool ShouldRun(TwitchUserConfig config) {
}

/// <inheritdoc />
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message,
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, TwitchChatMessage message,
INullinsideContext db, CancellationToken stoppingToken = new()) {
if (message.IsFirstMessage && _spam.Equals(message.Message, StringComparison.InvariantCultureIgnoreCase)) {
await BanAndLog(channelId, botProxy, new[] { (message.UserId, message.Username) },
Expand Down
2 changes: 1 addition & 1 deletion src/Nullinside.Api.TwitchBot/ChatRules/StreamViewers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override bool ShouldRun(TwitchUserConfig config) {
}

/// <inheritdoc />
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, ChatMessage message,
public override async Task<bool> Handle(string channelId, ITwitchApiProxy botProxy, TwitchChatMessage message,
INullinsideContext db, CancellationToken stoppingToken = new()) {
if (!message.IsFirstMessage) {
return true;
Expand Down
Loading
Loading