using System.Linq; using System.Threading.Tasks; using AngleSharp.Dom; using DiscordChatExporter.Cli.Tests.Fixtures; using DiscordChatExporter.Cli.Tests.TestData; using DiscordChatExporter.Core.Discord; using FluentAssertions; using Xunit; namespace DiscordChatExporter.Cli.Tests { public record EmbedSpecs(ExportWrapperFixture ExportWrapper) : IClassFixture { [Fact] public async Task Message_with_an_embed_is_rendered_correctly_in_JSON() { // Act var message = await ExportWrapper.GetMessageAsJsonAsync( ChannelIds.EmbedTestCases, Snowflake.Parse("866769910729146400") ); var embed = message .GetProperty("embeds") .EnumerateArray() .Single(); var embedAuthor = embed.GetProperty("author"); var embedThumbnail = embed.GetProperty("thumbnail"); var embedFooter = embed.GetProperty("footer"); var embedFields = embed.GetProperty("fields").EnumerateArray().ToArray(); // Assert embed.GetProperty("title").GetString().Should().Be("Embed title"); embed.GetProperty("url").GetString().Should().Be("https://example.com"); embed.GetProperty("timestamp").GetString().Should().Be("2021-07-14T21:00:00+00:00"); embed.GetProperty("description").GetString().Should().Be("**Embed** _description_"); embed.GetProperty("color").GetString().Should().Be("#58B9FF"); embedAuthor.GetProperty("name").GetString().Should().Be("Embed author"); embedAuthor.GetProperty("url").GetString().Should().Be("https://example.com/author"); embedAuthor.GetProperty("iconUrl").GetString().Should().NotBeNullOrWhiteSpace(); embedThumbnail.GetProperty("url").GetString().Should().NotBeNullOrWhiteSpace(); embedThumbnail.GetProperty("width").GetInt32().Should().Be(120); embedThumbnail.GetProperty("height").GetInt32().Should().Be(120); embedFooter.GetProperty("text").GetString().Should().Be("Embed footer"); embedFooter.GetProperty("iconUrl").GetString().Should().NotBeNullOrWhiteSpace(); embedFields.Should().HaveCount(3); embedFields[0].GetProperty("name").GetString().Should().Be("Field 1"); embedFields[0].GetProperty("value").GetString().Should().Be("Value 1"); embedFields[0].GetProperty("isInline").GetBoolean().Should().BeTrue(); embedFields[1].GetProperty("name").GetString().Should().Be("Field 2"); embedFields[1].GetProperty("value").GetString().Should().Be("Value 2"); embedFields[1].GetProperty("isInline").GetBoolean().Should().BeTrue(); embedFields[2].GetProperty("name").GetString().Should().Be("Field 3"); embedFields[2].GetProperty("value").GetString().Should().Be("Value 3"); embedFields[2].GetProperty("isInline").GetBoolean().Should().BeTrue(); } [Fact] public async Task Message_with_an_embed_is_rendered_correctly_in_HTML() { // Act var message = await ExportWrapper.GetMessageAsHtmlAsync( ChannelIds.EmbedTestCases, Snowflake.Parse("866769910729146400") ); // Assert message.Text().Should().ContainAll( "Embed author", "Embed title", "Embed description", "Field 1", "Value 1", "Field 2", "Value 2", "Field 3", "Value 3", "Embed footer" ); } [Fact] public async Task Message_with_a_Spotify_track_is_rendered_using_an_iframe_in_HTML() { // Act var message = await ExportWrapper.GetMessageAsHtmlAsync( ChannelIds.EmbedTestCases, Snowflake.Parse("867886632203976775") ); var iframeHtml = message.QuerySelector("iframe"); // Assert iframeHtml.Should().NotBeNull(); iframeHtml?.GetAttribute("src").Should() .StartWithEquivalentOf("https://open.spotify.com/embed/track/1LHZMWefF9502NPfArRfvP"); } [Fact] public async Task Message_with_a_YouTube_video_is_rendered_using_an_iframe_in_HTML() { // Act var message = await ExportWrapper.GetMessageAsHtmlAsync( ChannelIds.EmbedTestCases, Snowflake.Parse("866472508588294165") ); var iframeHtml = message.QuerySelector("iframe"); // Assert iframeHtml.Should().NotBeNull(); iframeHtml?.GetAttribute("src").Should() .StartWithEquivalentOf("https://www.youtube.com/embed/qOWW4OlgbvE"); } } }