DiscordChatExporter/DiscordChatExporter.Cli.Tests/Specs/FilterSpecs.cs

169 lines
5.4 KiB
C#
Raw Permalink Normal View History

2024-01-18 13:30:17 -05:00
using System;
using System.IO;
2021-09-14 17:33:53 -04:00
using System.Linq;
using System.Threading.Tasks;
using CliFx.Infrastructure;
using DiscordChatExporter.Cli.Commands;
using DiscordChatExporter.Cli.Tests.Infra;
2023-02-11 16:12:15 -05:00
using DiscordChatExporter.Cli.Tests.Utils;
2021-09-14 17:33:53 -04:00
using DiscordChatExporter.Core.Exporting;
using DiscordChatExporter.Core.Exporting.Filtering;
using FluentAssertions;
using JsonExtensions;
using Xunit;
2021-12-08 16:50:21 -05:00
namespace DiscordChatExporter.Cli.Tests.Specs;
2023-02-11 16:12:15 -05:00
public class FilterSpecs
2021-09-14 17:33:53 -04:00
{
2021-12-08 16:50:21 -05:00
[Fact]
2023-05-20 00:09:19 -04:00
public async Task I_can_filter_the_export_to_only_include_messages_that_contain_the_specified_text()
2021-09-14 17:33:53 -04:00
{
2021-12-08 16:50:21 -05:00
// Arrange
2023-02-11 16:12:15 -05:00
using var file = TempFile.Create();
2021-12-08 16:50:21 -05:00
// Act
await new ExportChannelsCommand
2021-09-14 17:33:53 -04:00
{
2022-01-03 17:52:16 -05:00
Token = Secrets.DiscordToken,
2024-01-11 15:21:53 -05:00
ChannelIds = [ChannelIds.FilterTestCases],
2021-12-08 16:50:21 -05:00
ExportFormat = ExportFormat.Json,
2023-02-11 16:12:15 -05:00
OutputPath = file.Path,
2021-12-08 16:50:21 -05:00
MessageFilter = MessageFilter.Parse("some text")
}.ExecuteAsync(new FakeConsole());
// Assert
2023-08-22 14:17:19 -04:00
Json.Parse(await File.ReadAllTextAsync(file.Path))
2021-12-08 16:50:21 -05:00
.GetProperty("messages")
.EnumerateArray()
.Select(j => j.GetProperty("content").GetString())
.Should()
2024-01-18 13:30:17 -05:00
.AllSatisfy(c => c.Contains("Some random text", StringComparison.Ordinal));
2021-12-08 16:50:21 -05:00
}
[Fact]
2023-05-20 00:09:19 -04:00
public async Task I_can_filter_the_export_to_only_include_messages_that_were_sent_by_the_specified_author()
2021-12-08 16:50:21 -05:00
{
// Arrange
2023-02-11 16:12:15 -05:00
using var file = TempFile.Create();
2021-12-08 16:50:21 -05:00
// Act
await new ExportChannelsCommand
2021-09-14 17:33:53 -04:00
{
2022-01-03 17:52:16 -05:00
Token = Secrets.DiscordToken,
2024-01-11 15:21:53 -05:00
ChannelIds = [ChannelIds.FilterTestCases],
2021-12-08 16:50:21 -05:00
ExportFormat = ExportFormat.Json,
2023-02-11 16:12:15 -05:00
OutputPath = file.Path,
2021-12-08 16:50:21 -05:00
MessageFilter = MessageFilter.Parse("from:Tyrrrz")
}.ExecuteAsync(new FakeConsole());
// Assert
2023-08-22 14:17:19 -04:00
Json.Parse(await File.ReadAllTextAsync(file.Path))
2021-12-08 16:50:21 -05:00
.GetProperty("messages")
.EnumerateArray()
.Select(j => j.GetProperty("author").GetProperty("name").GetString())
.Should()
2023-06-08 09:53:03 -04:00
.AllBe("tyrrrz");
2021-12-08 16:50:21 -05:00
}
[Fact]
public async Task I_can_filter_the_export_to_only_include_messages_that_contain_images()
2021-12-08 16:50:21 -05:00
{
// Arrange
2023-02-11 16:12:15 -05:00
using var file = TempFile.Create();
2021-12-08 16:50:21 -05:00
// Act
await new ExportChannelsCommand
2021-09-14 17:33:53 -04:00
{
2022-01-03 17:52:16 -05:00
Token = Secrets.DiscordToken,
2024-01-11 15:21:53 -05:00
ChannelIds = [ChannelIds.FilterTestCases],
2021-12-08 16:50:21 -05:00
ExportFormat = ExportFormat.Json,
2023-02-11 16:12:15 -05:00
OutputPath = file.Path,
2021-12-08 16:50:21 -05:00
MessageFilter = MessageFilter.Parse("has:image")
}.ExecuteAsync(new FakeConsole());
// Assert
2023-08-22 14:17:19 -04:00
Json.Parse(await File.ReadAllTextAsync(file.Path))
2021-12-08 16:50:21 -05:00
.GetProperty("messages")
.EnumerateArray()
.Select(j => j.GetProperty("content").GetString())
.Should()
2024-01-18 13:30:17 -05:00
.AllSatisfy(c => c.Contains("This has image", StringComparison.Ordinal));
}
[Fact]
2024-01-18 13:25:30 -05:00
public async Task I_can_filter_the_export_to_only_include_messages_that_have_been_pinned()
{
// Arrange
using var file = TempFile.Create();
// Act
await new ExportChannelsCommand
{
Token = Secrets.DiscordToken,
ChannelIds = [ChannelIds.FilterTestCases],
ExportFormat = ExportFormat.Json,
OutputPath = file.Path,
2024-01-18 13:25:30 -05:00
MessageFilter = MessageFilter.Parse("has:pin")
}.ExecuteAsync(new FakeConsole());
// Assert
Json.Parse(await File.ReadAllTextAsync(file.Path))
.GetProperty("messages")
.EnumerateArray()
.Select(j => j.GetProperty("content").GetString())
.Should()
2024-01-18 13:30:17 -05:00
.AllSatisfy(c => c.Contains("This is pinned", StringComparison.Ordinal));
2021-12-08 16:50:21 -05:00
}
2022-05-18 13:19:46 -04:00
[Fact]
2024-01-18 13:25:30 -05:00
public async Task I_can_filter_the_export_to_only_include_messages_that_contain_guild_invites()
2022-05-18 13:19:46 -04:00
{
// Arrange
2023-02-11 16:12:15 -05:00
using var file = TempFile.Create();
2022-05-18 13:19:46 -04:00
// Act
await new ExportChannelsCommand
{
Token = Secrets.DiscordToken,
2024-01-11 15:21:53 -05:00
ChannelIds = [ChannelIds.FilterTestCases],
2022-05-18 13:19:46 -04:00
ExportFormat = ExportFormat.Json,
2023-02-11 16:12:15 -05:00
OutputPath = file.Path,
2024-01-18 13:25:30 -05:00
MessageFilter = MessageFilter.Parse("has:invite")
2022-05-18 13:19:46 -04:00
}.ExecuteAsync(new FakeConsole());
// Assert
2023-08-22 14:17:19 -04:00
Json.Parse(await File.ReadAllTextAsync(file.Path))
2022-05-18 13:19:46 -04:00
.GetProperty("messages")
.EnumerateArray()
.Select(j => j.GetProperty("content").GetString())
.Should()
2024-01-18 13:30:17 -05:00
.AllSatisfy(c => c.Contains("This has invite", StringComparison.Ordinal));
2022-05-18 13:19:46 -04:00
}
2021-12-08 16:50:21 -05:00
[Fact]
2023-05-20 00:09:19 -04:00
public async Task I_can_filter_the_export_to_only_include_messages_that_contain_the_specified_mention()
2021-12-08 16:50:21 -05:00
{
// Arrange
2023-02-11 16:12:15 -05:00
using var file = TempFile.Create();
2021-12-08 16:50:21 -05:00
// Act
await new ExportChannelsCommand
2021-09-14 17:33:53 -04:00
{
2022-01-03 17:52:16 -05:00
Token = Secrets.DiscordToken,
2024-01-11 15:21:53 -05:00
ChannelIds = [ChannelIds.FilterTestCases],
2021-12-08 16:50:21 -05:00
ExportFormat = ExportFormat.Json,
2023-02-11 16:12:15 -05:00
OutputPath = file.Path,
2021-12-08 16:50:21 -05:00
MessageFilter = MessageFilter.Parse("mentions:Tyrrrz")
}.ExecuteAsync(new FakeConsole());
// Assert
2023-08-22 14:17:19 -04:00
Json.Parse(await File.ReadAllTextAsync(file.Path))
2021-12-08 16:50:21 -05:00
.GetProperty("messages")
.EnumerateArray()
.Select(j => j.GetProperty("content").GetString())
.Should()
2024-01-18 13:30:17 -05:00
.AllSatisfy(c => c.Contains("This has mention", StringComparison.Ordinal));
2021-09-14 17:33:53 -04:00
}
2023-08-22 14:17:19 -04:00
}