More test stuff

This commit is contained in:
Oleksii Holub 2022-06-30 19:07:11 +03:00
parent 565fb53d3b
commit 057902f919
9 changed files with 173 additions and 62 deletions

View file

@ -46,58 +46,46 @@ public class ExportWrapperFixture : IDisposable
return await File.ReadAllTextAsync(filePath); return await File.ReadAllTextAsync(filePath);
} }
public async ValueTask<IHtmlDocument> ExportAsHtmlAsync(Snowflake channelId) public async ValueTask<IHtmlDocument> ExportAsHtmlAsync(Snowflake channelId) => Html.Parse(
{ await ExportAsync(channelId, ExportFormat.HtmlDark)
var data = await ExportAsync(channelId, ExportFormat.HtmlDark); );
return Html.Parse(data);
}
public async ValueTask<JsonElement> ExportAsJsonAsync(Snowflake channelId) public async ValueTask<JsonElement> ExportAsJsonAsync(Snowflake channelId) => Json.Parse(
{ await ExportAsync(channelId, ExportFormat.Json)
var data = await ExportAsync(channelId, ExportFormat.Json); );
return Json.Parse(data);
}
public async ValueTask<string> ExportAsPlainTextAsync(Snowflake channelId) public async ValueTask<string> ExportAsPlainTextAsync(Snowflake channelId) =>
{ await ExportAsync(channelId, ExportFormat.PlainText);
var data = await ExportAsync(channelId, ExportFormat.PlainText);
return data;
}
public async ValueTask<string> ExportAsCsvAsync(Snowflake channelId) public async ValueTask<string> ExportAsCsvAsync(Snowflake channelId) =>
{ await ExportAsync(channelId, ExportFormat.Csv);
var data = await ExportAsync(channelId, ExportFormat.Csv);
return data;
}
public async ValueTask<IReadOnlyList<IElement>> GetMessagesAsHtmlAsync(Snowflake channelId) public async ValueTask<IReadOnlyList<IElement>> GetMessagesAsHtmlAsync(Snowflake channelId) =>
{ (await ExportAsHtmlAsync(channelId))
var document = await ExportAsHtmlAsync(channelId); .QuerySelectorAll("[data-message-id]")
return document.QuerySelectorAll("[data-message-id]").ToArray(); .ToArray();
}
public async ValueTask<IReadOnlyList<JsonElement>> GetMessagesAsJsonAsync(Snowflake channelId) public async ValueTask<IReadOnlyList<JsonElement>> GetMessagesAsJsonAsync(Snowflake channelId) =>
{ (await ExportAsJsonAsync(channelId))
var document = await ExportAsJsonAsync(channelId); .GetProperty("messages")
return document.GetProperty("messages").EnumerateArray().ToArray(); .EnumerateArray()
} .ToArray();
public async ValueTask<IElement> GetMessageAsHtmlAsync(Snowflake channelId, Snowflake messageId) public async ValueTask<IElement> GetMessageAsHtmlAsync(Snowflake channelId, Snowflake messageId)
{ {
var messages = await GetMessagesAsHtmlAsync(channelId); var message = (await GetMessagesAsHtmlAsync(channelId))
.SingleOrDefault(e =>
var message = messages.SingleOrDefault(e => string.Equals(
string.Equals( e.GetAttribute("data-message-id"),
e.GetAttribute("data-message-id"), messageId.ToString(),
messageId.ToString(), StringComparison.OrdinalIgnoreCase
StringComparison.OrdinalIgnoreCase )
) );
);
if (message is null) if (message is null)
{ {
throw new InvalidOperationException( throw new InvalidOperationException(
$"Message '{messageId}' does not exist in export of channel '{channelId}'." $"Message '{messageId}' does not exist in the export of channel '{channelId}'."
); );
} }
@ -106,20 +94,19 @@ public class ExportWrapperFixture : IDisposable
public async ValueTask<JsonElement> GetMessageAsJsonAsync(Snowflake channelId, Snowflake messageId) public async ValueTask<JsonElement> GetMessageAsJsonAsync(Snowflake channelId, Snowflake messageId)
{ {
var messages = await GetMessagesAsJsonAsync(channelId); var message = (await GetMessagesAsJsonAsync(channelId))
.SingleOrDefault(j =>
var message = messages.FirstOrDefault(j => string.Equals(
string.Equals( j.GetProperty("id").GetString(),
j.GetProperty("id").GetString(), messageId.ToString(),
messageId.ToString(), StringComparison.OrdinalIgnoreCase
StringComparison.OrdinalIgnoreCase )
) );
);
if (message.ValueKind == JsonValueKind.Undefined) if (message.ValueKind == JsonValueKind.Undefined)
{ {
throw new InvalidOperationException( throw new InvalidOperationException(
$"Message '{messageId}' does not exist in export of channel '{channelId}'." $"Message '{messageId}' does not exist in the export of channel '{channelId}'."
); );
} }

View file

@ -67,6 +67,8 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
[Fact] [Fact]
public async Task Message_with_a_video_attachment_is_rendered_correctly() public async Task Message_with_a_video_attachment_is_rendered_correctly()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/333
// Act // Act
var message = await _exportWrapper.GetMessageAsHtmlAsync( var message = await _exportWrapper.GetMessageAsHtmlAsync(
ChannelIds.AttachmentTestCases, ChannelIds.AttachmentTestCases,
@ -77,7 +79,7 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
message.Text().Should().Contain("Video attachment"); message.Text().Should().Contain("Video attachment");
var videoUrl = message.QuerySelector("video source")?.GetAttribute("src"); var videoUrl = message.QuerySelector("video source")?.GetAttribute("src");
videoUrl.Should().StartWith( videoUrl.Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4" "https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4"
); );
} }
@ -85,6 +87,8 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
[Fact] [Fact]
public async Task Message_with_an_audio_attachment_is_rendered_correctly() public async Task Message_with_an_audio_attachment_is_rendered_correctly()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/333
// Act // Act
var message = await _exportWrapper.GetMessageAsHtmlAsync( var message = await _exportWrapper.GetMessageAsHtmlAsync(
ChannelIds.AttachmentTestCases, ChannelIds.AttachmentTestCases,
@ -95,7 +99,7 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
message.Text().Should().Contain("Audio attachment"); message.Text().Should().Contain("Audio attachment");
var audioUrl = message.QuerySelector("audio source")?.GetAttribute("src"); var audioUrl = message.QuerySelector("audio source")?.GetAttribute("src");
audioUrl.Should().StartWith( audioUrl.Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3" "https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3"
); );
} }

View file

@ -42,6 +42,8 @@ public class EmbedSpecs : IClassFixture<ExportWrapperFixture>
[Fact] [Fact]
public async Task Message_containing_an_image_link_is_rendered_with_an_image_embed() public async Task Message_containing_an_image_link_is_rendered_with_an_image_embed()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/537
// Act // Act
var message = await _exportWrapper.GetMessageAsHtmlAsync( var message = await _exportWrapper.GetMessageAsHtmlAsync(
ChannelIds.EmbedTestCases, ChannelIds.EmbedTestCases,
@ -59,6 +61,8 @@ public class EmbedSpecs : IClassFixture<ExportWrapperFixture>
[Fact] [Fact]
public async Task Message_containing_an_image_link_and_nothing_else_is_rendered_without_text_content() public async Task Message_containing_an_image_link_and_nothing_else_is_rendered_without_text_content()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/682
// Act // Act
var message = await _exportWrapper.GetMessageAsHtmlAsync( var message = await _exportWrapper.GetMessageAsHtmlAsync(
ChannelIds.EmbedTestCases, ChannelIds.EmbedTestCases,
@ -73,6 +77,8 @@ public class EmbedSpecs : IClassFixture<ExportWrapperFixture>
[Fact] [Fact]
public async Task Message_containing_a_Spotify_track_link_is_rendered_with_a_track_embed() public async Task Message_containing_a_Spotify_track_link_is_rendered_with_a_track_embed()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/657
// Act // Act
var message = await _exportWrapper.GetMessageAsHtmlAsync( var message = await _exportWrapper.GetMessageAsHtmlAsync(
ChannelIds.EmbedTestCases, ChannelIds.EmbedTestCases,
@ -81,12 +87,14 @@ public class EmbedSpecs : IClassFixture<ExportWrapperFixture>
// Assert // Assert
var iframeUrl = message.QuerySelector("iframe")?.GetAttribute("src"); var iframeUrl = message.QuerySelector("iframe")?.GetAttribute("src");
iframeUrl.Should().StartWith("https://open.spotify.com/embed/track/1LHZMWefF9502NPfArRfvP"); iframeUrl.Should().Be("https://open.spotify.com/embed/track/1LHZMWefF9502NPfArRfvP");
} }
[Fact] [Fact]
public async Task Message_containing_a_YouTube_video_link_is_rendered_with_a_video_embed() public async Task Message_containing_a_YouTube_video_link_is_rendered_with_a_video_embed()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/570
// Act // Act
var message = await _exportWrapper.GetMessageAsHtmlAsync( var message = await _exportWrapper.GetMessageAsHtmlAsync(
ChannelIds.EmbedTestCases, ChannelIds.EmbedTestCases,
@ -95,6 +103,32 @@ public class EmbedSpecs : IClassFixture<ExportWrapperFixture>
// Assert // Assert
var iframeUrl = message.QuerySelector("iframe")?.GetAttribute("src"); var iframeUrl = message.QuerySelector("iframe")?.GetAttribute("src");
iframeUrl.Should().StartWith("https://www.youtube.com/embed/qOWW4OlgbvE"); iframeUrl.Should().Be("https://www.youtube.com/embed/qOWW4OlgbvE");
}
[Fact(Skip = "Unimplemented")]
public async Task Message_containing_a_Twitter_post_link_with_multiple_images_is_rendered_as_a_single_embed()
{
// https://github.com/Tyrrrz/DiscordChatExporter/issues/695
// Act
var message = await _exportWrapper.GetMessageAsHtmlAsync(
ChannelIds.EmbedTestCases,
Snowflake.Parse("991757444017557665")
);
// Assert
message
.QuerySelectorAll("img")
.Select(e => e.GetAttribute("src"))
.Should()
.ContainInOrder(
"https://images-ext-1.discordapp.net/external/-n--xW3EHH_3jlrheVkMXHCM7T86b5Ty4-MzXCT4m1Q/https/pbs.twimg.com/media/FVYIzYPWAAAMBqZ.png",
"https://images-ext-2.discordapp.net/external/z5nEmGeEldV-kswydGLhqUsFHbb5AWHtdvc9XT6N5rE/https/pbs.twimg.com/media/FVYJBWJWAAMNAx2.png",
"https://images-ext-2.discordapp.net/external/gnip03SawMB6uZLagN5sRDpA_1Ap1CcEhMbJfK1z6WQ/https/pbs.twimg.com/media/FVYJHiRX0AANZcz.png",
"https://images-ext-2.discordapp.net/external/jl1v6cCbLaGmiwmKU-ZkXnF4cFsJ39f9A3-oEdqPdZs/https/pbs.twimg.com/media/FVYJNZNXwAAPnVG.png"
);
message.QuerySelectorAll(".chatlog__embed").Should().ContainSingle();
} }
} }

View file

@ -0,0 +1,79 @@
using System.IO;
using System.Linq;
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.Exporting;
using FluentAssertions;
using Xunit;
namespace DiscordChatExporter.Cli.Tests.Specs.HtmlWriting;
public class GroupingSpecs : IClassFixture<TempOutputFixture>
{
private readonly TempOutputFixture _tempOutput;
public GroupingSpecs(TempOutputFixture tempOutput)
{
_tempOutput = tempOutput;
}
[Fact]
public async Task Messages_are_grouped_correctly()
{
// https://github.com/Tyrrrz/DiscordChatExporter/issues/152
// Arrange
var filePath = _tempOutput.GetTempFilePath();
// Act
await new ExportChannelsCommand
{
Token = Secrets.DiscordToken,
ChannelIds = new[] { ChannelIds.GroupingTestCases },
ExportFormat = ExportFormat.HtmlDark,
OutputPath = filePath
}.ExecuteAsync(new FakeConsole());
// Assert
var messageGroups = Html
.Parse(await File.ReadAllTextAsync(filePath))
.QuerySelectorAll(".chatlog__message-group");
messageGroups.Should().HaveCount(2);
messageGroups[0]
.QuerySelectorAll(".chatlog__content")
.Select(e => e.Text())
.Should()
.ContainInOrder(
"First",
"Second",
"Third",
"Fourth",
"Fifth",
"Sixth",
"Seventh",
"Eighth",
"Ninth",
"Tenth"
);
messageGroups[1]
.QuerySelectorAll(".chatlog__content")
.Select(e => e.Text())
.Should()
.ContainInOrder(
"Eleventh",
"Twelveth",
"Thirteenth",
"Fourteenth",
"Fifteenth"
);
}
}

View file

@ -34,6 +34,8 @@ public class ReplySpecs : IClassFixture<ExportWrapperFixture>
[Fact] [Fact]
public async Task Reply_to_a_deleted_message_is_rendered_correctly() public async Task Reply_to_a_deleted_message_is_rendered_correctly()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/645
// Act // Act
var message = await _exportWrapper.GetMessageAsHtmlAsync( var message = await _exportWrapper.GetMessageAsHtmlAsync(
ChannelIds.ReplyTestCases, ChannelIds.ReplyTestCases,
@ -50,6 +52,8 @@ public class ReplySpecs : IClassFixture<ExportWrapperFixture>
[Fact] [Fact]
public async Task Reply_to_an_empty_message_with_attachment_is_rendered_correctly() public async Task Reply_to_an_empty_message_with_attachment_is_rendered_correctly()
{ {
// https://github.com/Tyrrrz/DiscordChatExporter/issues/634
// Act // Act
var message = await _exportWrapper.GetMessageAsHtmlAsync( var message = await _exportWrapper.GetMessageAsHtmlAsync(
ChannelIds.ReplyTestCases, ChannelIds.ReplyTestCases,

View file

@ -31,7 +31,7 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray(); var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
attachments.Should().HaveCount(1); attachments.Should().HaveCount(1);
attachments.Single().GetProperty("url").GetString().Should().StartWith( attachments.Single().GetProperty("url").GetString().Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885587844964417596/Test.txt" "https://cdn.discordapp.com/attachments/885587741654536192/885587844964417596/Test.txt"
); );
attachments.Single().GetProperty("fileName").GetString().Should().Be("Test.txt"); attachments.Single().GetProperty("fileName").GetString().Should().Be("Test.txt");
@ -52,7 +52,7 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray(); var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
attachments.Should().HaveCount(1); attachments.Should().HaveCount(1);
attachments.Single().GetProperty("url").GetString().Should().StartWith( attachments.Single().GetProperty("url").GetString().Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885654862430359613/bird-thumbnail.png" "https://cdn.discordapp.com/attachments/885587741654536192/885654862430359613/bird-thumbnail.png"
); );
attachments.Single().GetProperty("fileName").GetString().Should().Be("bird-thumbnail.png"); attachments.Single().GetProperty("fileName").GetString().Should().Be("bird-thumbnail.png");
@ -73,7 +73,7 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray(); var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
attachments.Should().HaveCount(1); attachments.Should().HaveCount(1);
attachments.Single().GetProperty("url").GetString().Should().StartWith( attachments.Single().GetProperty("url").GetString().Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4" "https://cdn.discordapp.com/attachments/885587741654536192/885655761512968233/file_example_MP4_640_3MG.mp4"
); );
attachments.Single().GetProperty("fileName").GetString().Should().Be("file_example_MP4_640_3MG.mp4"); attachments.Single().GetProperty("fileName").GetString().Should().Be("file_example_MP4_640_3MG.mp4");
@ -94,7 +94,7 @@ public class AttachmentSpecs : IClassFixture<ExportWrapperFixture>
var attachments = message.GetProperty("attachments").EnumerateArray().ToArray(); var attachments = message.GetProperty("attachments").EnumerateArray().ToArray();
attachments.Should().HaveCount(1); attachments.Should().HaveCount(1);
attachments.Single().GetProperty("url").GetString().Should().StartWith( attachments.Single().GetProperty("url").GetString().Should().Be(
"https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3" "https://cdn.discordapp.com/attachments/885587741654536192/885656175348187146/file_example_MP3_1MG.mp3"
); );
attachments.Single().GetProperty("fileName").GetString().Should().Be("file_example_MP3_1MG.mp3"); attachments.Single().GetProperty("fileName").GetString().Should().Be("file_example_MP3_1MG.mp3");

View file

@ -33,7 +33,8 @@ public class MentionSpecs : IClassFixture<ExportWrapperFixture>
.GetProperty("mentions") .GetProperty("mentions")
.EnumerateArray() .EnumerateArray()
.Select(j => j.GetProperty("id").GetString()) .Select(j => j.GetProperty("id").GetString())
.Should().Contain("128178626683338752"); .Should()
.Contain("128178626683338752");
} }
[Fact] [Fact]

View file

@ -22,7 +22,7 @@ public class PartitioningSpecs : IClassFixture<TempOutputFixture>
} }
[Fact] [Fact]
public async Task Messages_partitioned_by_count_are_split_into_multiple_files_correctly() public async Task Messages_partitioned_by_count_are_split_into_a_corresponding_number_of_files()
{ {
// Arrange // Arrange
var filePath = _tempOutput.GetTempFilePath(); var filePath = _tempOutput.GetTempFilePath();
@ -46,7 +46,7 @@ public class PartitioningSpecs : IClassFixture<TempOutputFixture>
} }
[Fact] [Fact]
public async Task Messages_partitioned_by_file_size_are_split_into_multiple_files_correctly() public async Task Messages_partitioned_by_file_size_are_split_into_a_corresponding_number_of_files()
{ {
// Arrange // Arrange
var filePath = _tempOutput.GetTempFilePath(); var filePath = _tempOutput.GetTempFilePath();

View file

@ -10,7 +10,7 @@ public static class ChannelIds
public static Snowflake EmbedTestCases { get; } = Snowflake.Parse("866472452459462687"); public static Snowflake EmbedTestCases { get; } = Snowflake.Parse("866472452459462687");
public static Snowflake StickerTestCases { get; } = Snowflake.Parse("939668868253769729"); public static Snowflake GroupingTestCases { get; } = Snowflake.Parse("992092091545034842");
public static Snowflake FilterTestCases { get; } = Snowflake.Parse("866744075033641020"); public static Snowflake FilterTestCases { get; } = Snowflake.Parse("866744075033641020");
@ -19,4 +19,6 @@ public static class ChannelIds
public static Snowflake ReplyTestCases { get; } = Snowflake.Parse("866459871934677052"); public static Snowflake ReplyTestCases { get; } = Snowflake.Parse("866459871934677052");
public static Snowflake SelfContainedTestCases { get; } = Snowflake.Parse("887441432678379560"); public static Snowflake SelfContainedTestCases { get; } = Snowflake.Parse("887441432678379560");
public static Snowflake StickerTestCases { get; } = Snowflake.Parse("939668868253769729");
} }