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
368 changes: 368 additions & 0 deletions src/Nullinside.Api.TwitchBot.Tests/.editorconfig

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,60 +1,58 @@
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.
/// 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);
}
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");
bool 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.
bool 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,42 +1,41 @@
using Moq;

using Nullinside.Api.Common.Twitch;
using Nullinside.Api.TwitchBot.ChatRules;

namespace Nullinside.Api.TwitchBot.Tests.ChatRules;

/// <summary>
/// Tests the <see cref="BestCheapViewers" /> class.
/// 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 ***")]
[TestCase("Best vie̮wers on ***")]
[TestCase("Best́ viewers on ***")]
[TestCase("Be̩st Viewers on ***")]
[TestCase("Be̾st Viewers on ***")]
[TestCase("B͟est Viewers on ***")]
[TestCase("B̟est viewers on ***")]
[TestCase("Cheap viewers on ***")]
[TestCase("Che̢ap vie̮wers on ***")]
[TestCase("Ch̍eap Viewers on ***")]
[TestCase("Ch͟eap viewers on ***")]
[TestCase("C̀heap Viewers on ***")]
[TestCase("Cheaͧp v̫iewers on ***")]
[TestCase("Cheaͧp v̫iewers on *** ")]
public async Task TestKnownStrings(string badString)
{
var rule = new BestCheapViewers();
var botProxy = new Mock<ITwitchApiProxy>();
var chat = new TwitchChatMessage(true, badString, "123", "456");
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 ***")]
[TestCase("Best vie̮wers on ***")]
[TestCase("Best́ viewers on ***")]
[TestCase("Be̩st Viewers on ***")]
[TestCase("Be̾st Viewers on ***")]
[TestCase("B͟est Viewers on ***")]
[TestCase("B̟est viewers on ***")]
[TestCase("Cheap viewers on ***")]
[TestCase("Che̢ap vie̮wers on ***")]
[TestCase("Ch̍eap Viewers on ***")]
[TestCase("Ch͟eap viewers on ***")]
[TestCase("C̀heap Viewers on ***")]
[TestCase("Cheaͧp v̫iewers on ***")]
[TestCase("Cheaͧp v̫iewers on *** ")]
public async Task TestKnownStrings(string badString) {
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);
}
// Process the message and assert that we fail the message.
bool result = await rule.Handle("123", botProxy.Object, chat, _db);
Assert.That(result, Is.False);
}
}
79 changes: 36 additions & 43 deletions src/Nullinside.Api.TwitchBot.Tests/ChatRules/DiscordTests.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,48 @@
using Moq;

using Nullinside.Api.Common.Twitch;
using Nullinside.Api.TwitchBot.ChatRules;

namespace Nullinside.Api.TwitchBot.Tests.ChatRules;

/// <summary>
/// Tests the <see cref="Discord" /> class.
/// 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 async Task TestKnownStrings(string badString)
{
var rule = new Discord();
var botProxy = new Mock<ITwitchApiProxy>();
var chat = new TwitchChatMessage(true, badString, "123", "456");
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 async Task TestKnownStrings(string badString) {
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);
}
// Process the message and assert that we fail the message.
bool 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");
/// <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);
}
// Process the message and assert that we do not fail the message.
bool 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,31 +1,28 @@
using Moq;

using Nullinside.Api.Common.Twitch;
using Nullinside.Api.TwitchBot.ChatRules;

namespace Nullinside.Api.TwitchBot.Tests.ChatRules;

/// <summary>
/// Tests the <see cref="StreamViewers" /> class.
/// 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 async Task TestKnownStrings(string badString)
{
var rule = new StreamViewers();
var botProxy = new Mock<ITwitchApiProxy>();
var chat = new TwitchChatMessage(true, badString, "123", "456");
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 async Task TestKnownStrings(string badString) {
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);
}
// Process the message and assert that we fail the message.
bool 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 @@ -32,5 +32,4 @@
<ItemGroup>
<ProjectReference Include="..\Nullinside.Api.TwitchBot\Nullinside.Api.TwitchBot.csproj"/>
</ItemGroup>

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

using Nullinside.Api.Model;

namespace Nullinside.Api.TwitchBot.Tests;

/// <summary>
/// A base class for all unit tests.
/// A base class for all unit tests.
/// </summary>
public abstract class UnitTestBase
{
public abstract class UnitTestBase {
/// <summary>
/// A fake database.
/// 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);
}
[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.
DbContextOptions<NullinsideContext> 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();
}
[TearDown]
public virtual async Task TearDown() {
// Dispose since it has one.
await _db.DisposeAsync();
}
}
3 changes: 3 additions & 0 deletions src/Nullinside.Api.TwitchBot/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ root = true
[*]
indent_style = space

# ReSharper properties
resharper_csharp_wrap_lines = false

# Xml files
[*.xml]
indent_size = 2
Expand Down
Loading
Loading