From ec0494c0388f7a22799443cd9ad9a877ddc9af1d Mon Sep 17 00:00:00 2001 From: Tyrrrz Date: Mon, 19 Jul 2021 20:35:27 +0300 Subject: [PATCH] More tests --- DiscordChatExporter.Cli.Tests/EmbedSpecs.cs | 62 +++++++++ DiscordChatExporter.Cli.Tests/MentionSpecs.cs | 8 +- DiscordChatExporter.Cli.Tests/ReplySpecs.cs | 131 ++++++++++++++++++ .../TestData/ChannelIds.cs | 6 +- 4 files changed, 201 insertions(+), 6 deletions(-) create mode 100644 DiscordChatExporter.Cli.Tests/EmbedSpecs.cs create mode 100644 DiscordChatExporter.Cli.Tests/ReplySpecs.cs diff --git a/DiscordChatExporter.Cli.Tests/EmbedSpecs.cs b/DiscordChatExporter.Cli.Tests/EmbedSpecs.cs new file mode 100644 index 0000000..1e38509 --- /dev/null +++ b/DiscordChatExporter.Cli.Tests/EmbedSpecs.cs @@ -0,0 +1,62 @@ +using System.IO; +using System.Threading.Tasks; +using CliFx.Infrastructure; +using DiscordChatExporter.Cli.Commands; +using DiscordChatExporter.Cli.Tests.Fixtures; +using DiscordChatExporter.Cli.Tests.Infra; +using DiscordChatExporter.Cli.Tests.TestData; +using DiscordChatExporter.Cli.Tests.Utils; +using DiscordChatExporter.Core.Discord; +using DiscordChatExporter.Core.Exporting; +using FluentAssertions; +using Xunit; +using Xunit.Abstractions; + +namespace DiscordChatExporter.Cli.Tests +{ + public class EmbedSpecs : IClassFixture + { + private readonly ITestOutputHelper _testOutput; + private readonly TempOutputFixture _tempOutput; + + public EmbedSpecs(ITestOutputHelper testOutput, TempOutputFixture tempOutput) + { + _testOutput = testOutput; + _tempOutput = tempOutput; + } + + [Fact] + public async Task Message_with_YouTube_video_is_rendered_with_a_player_in_HTML() + { + // Arrange + var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html"); + + // Act + var htmlData = await GlobalCache.WrapAsync("embed-specs-output-html", async () => + { + await new ExportChannelsCommand + { + TokenValue = Secrets.DiscordToken, + IsBotToken = Secrets.IsDiscordTokenBot, + ChannelIds = new[] {Snowflake.Parse(ChannelIds.EmbedTestCases)}, + ExportFormat = ExportFormat.HtmlDark, + OutputPath = outputFilePath + }.ExecuteAsync(new FakeConsole()); + + return await File.ReadAllTextAsync(outputFilePath); + }); + + _testOutput.WriteLine(htmlData); + + var html = Html.Parse(htmlData); + + var messageHtml = html.QuerySelector("#message-866472508588294165"); + var iframeHtml = messageHtml?.QuerySelector("iframe"); + var iframeSrc = iframeHtml?.GetAttribute("src"); + + // Assert + iframeHtml.Should().NotBeNull(); + iframeSrc.Should().StartWithEquivalent("https://www.youtube.com/embed/qOWW4OlgbvE"); + } + } +} \ No newline at end of file diff --git a/DiscordChatExporter.Cli.Tests/MentionSpecs.cs b/DiscordChatExporter.Cli.Tests/MentionSpecs.cs index e7daa91..e02baac 100644 --- a/DiscordChatExporter.Cli.Tests/MentionSpecs.cs +++ b/DiscordChatExporter.Cli.Tests/MentionSpecs.cs @@ -103,7 +103,7 @@ namespace DiscordChatExporter.Cli.Tests var html = Html.Parse(htmlData); - var messageHtml = html.GetElementById("message-866458840245076028"); + var messageHtml = html.QuerySelector("#message-866458840245076028"); // Assert messageHtml.Should().NotBeNull(); @@ -178,7 +178,7 @@ namespace DiscordChatExporter.Cli.Tests var html = Html.Parse(htmlData); - var messageHtml = html.GetElementById("message-866459040480624680"); + var messageHtml = html.QuerySelector("#message-866459040480624680"); // Assert messageHtml.Should().NotBeNull(); @@ -252,7 +252,7 @@ namespace DiscordChatExporter.Cli.Tests var html = Html.Parse(htmlData); - var messageHtml = html.GetElementById("message-866459175462633503"); + var messageHtml = html.QuerySelector("#message-866459175462633503"); // Assert messageHtml.Should().NotBeNull(); @@ -326,7 +326,7 @@ namespace DiscordChatExporter.Cli.Tests var html = Html.Parse(htmlData); - var messageHtml = html.GetElementById("message-866459254693429258"); + var messageHtml = html.QuerySelector("#message-866459254693429258"); // Assert messageHtml.Should().NotBeNull(); diff --git a/DiscordChatExporter.Cli.Tests/ReplySpecs.cs b/DiscordChatExporter.Cli.Tests/ReplySpecs.cs new file mode 100644 index 0000000..5b1b0ea --- /dev/null +++ b/DiscordChatExporter.Cli.Tests/ReplySpecs.cs @@ -0,0 +1,131 @@ +using System.IO; +using System.Threading.Tasks; +using AngleSharp.Dom; +using CliFx.Infrastructure; +using DiscordChatExporter.Cli.Commands; +using DiscordChatExporter.Cli.Tests.Fixtures; +using DiscordChatExporter.Cli.Tests.Infra; +using DiscordChatExporter.Cli.Tests.TestData; +using DiscordChatExporter.Cli.Tests.Utils; +using DiscordChatExporter.Core.Discord; +using DiscordChatExporter.Core.Exporting; +using FluentAssertions; +using Xunit; +using Xunit.Abstractions; + +namespace DiscordChatExporter.Cli.Tests +{ + public class ReplySpecs : IClassFixture + { + private readonly ITestOutputHelper _testOutput; + private readonly TempOutputFixture _tempOutput; + + public ReplySpecs(ITestOutputHelper testOutput, TempOutputFixture tempOutput) + { + _testOutput = testOutput; + _tempOutput = tempOutput; + } + + [Fact] + public async Task Reply_to_a_normal_message_is_rendered_correctly_in_HTML() + { + // Arrange + var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html"); + + // Act + var htmlData = await GlobalCache.WrapAsync("reply-specs-output-html", async () => + { + await new ExportChannelsCommand + { + TokenValue = Secrets.DiscordToken, + IsBotToken = Secrets.IsDiscordTokenBot, + ChannelIds = new[] {Snowflake.Parse(ChannelIds.ReplyTestCases)}, + ExportFormat = ExportFormat.HtmlDark, + OutputPath = outputFilePath + }.ExecuteAsync(new FakeConsole()); + + return await File.ReadAllTextAsync(outputFilePath); + }); + + _testOutput.WriteLine(htmlData); + + var html = Html.Parse(htmlData); + + var messageHtml = html.QuerySelector("#message-866460738239725598"); + var referenceHtml = messageHtml?.QuerySelector(".chatlog__reference-link"); + + // Assert + messageHtml.Should().NotBeNull(); + messageHtml?.Text().Trim().Should().Be("reply to original"); + referenceHtml?.Text().Trim().Should().Be("original"); + } + + [Fact] + public async Task Reply_to_a_deleted_message_is_rendered_correctly_in_HTML() + { + // Arrange + var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html"); + + // Act + var htmlData = await GlobalCache.WrapAsync("reply-specs-output-html", async () => + { + await new ExportChannelsCommand + { + TokenValue = Secrets.DiscordToken, + IsBotToken = Secrets.IsDiscordTokenBot, + ChannelIds = new[] {Snowflake.Parse(ChannelIds.ReplyTestCases)}, + ExportFormat = ExportFormat.HtmlDark, + OutputPath = outputFilePath + }.ExecuteAsync(new FakeConsole()); + + return await File.ReadAllTextAsync(outputFilePath); + }); + + _testOutput.WriteLine(htmlData); + + var html = Html.Parse(htmlData); + + var messageHtml = html.QuerySelector("#message-866460975388819486"); + var referenceHtml = messageHtml?.QuerySelector(".chatlog__reference-link"); + + // Assert + messageHtml.Should().NotBeNull(); + messageHtml?.Text().Trim().Should().Be("reply to deleted"); + referenceHtml?.Text().Trim().Should().Be("Original message was deleted or could not be loaded."); + } + + [Fact] + public async Task Reply_to_a_empty_message_with_attachment_is_rendered_correctly_in_HTML() + { + // Arrange + var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html"); + + // Act + var htmlData = await GlobalCache.WrapAsync("reply-specs-output-html", async () => + { + await new ExportChannelsCommand + { + TokenValue = Secrets.DiscordToken, + IsBotToken = Secrets.IsDiscordTokenBot, + ChannelIds = new[] {Snowflake.Parse(ChannelIds.ReplyTestCases)}, + ExportFormat = ExportFormat.HtmlDark, + OutputPath = outputFilePath + }.ExecuteAsync(new FakeConsole()); + + return await File.ReadAllTextAsync(outputFilePath); + }); + + _testOutput.WriteLine(htmlData); + + var html = Html.Parse(htmlData); + + var messageHtml = html.QuerySelector("#message-866462470335627294"); + var referenceHtml = messageHtml?.QuerySelector(".chatlog__reference-link"); + + // Assert + messageHtml.Should().NotBeNull(); + messageHtml?.Text().Trim().Should().Be("reply to attachment"); + referenceHtml?.Text().Trim().Should().Be("Click to see attachment 🖼️"); + } + } +} \ No newline at end of file diff --git a/DiscordChatExporter.Cli.Tests/TestData/ChannelIds.cs b/DiscordChatExporter.Cli.Tests/TestData/ChannelIds.cs index c5f7ef4..e06fed1 100644 --- a/DiscordChatExporter.Cli.Tests/TestData/ChannelIds.cs +++ b/DiscordChatExporter.Cli.Tests/TestData/ChannelIds.cs @@ -2,8 +2,10 @@ { public static class ChannelIds { - public static string MentionTestCases { get; } = "866458801389174794"; + public static string EmbedTestCases => "866472452459462687"; - public static string ReplyTestCases { get; } = "866459871934677052"; + public static string MentionTestCases => "866458801389174794"; + + public static string ReplyTestCases => "866459871934677052"; } } \ No newline at end of file