using System.Text.Json; using Microsoft.Extensions.Configuration; using RadicadeServer.Discord.Webhooks; using RadicadeServer.Radicade.Discord; using Xunit; namespace RadicadeServer.Tests; public sealed class DiscordWebhookTests { [Fact] public void WebhookConfig_LoadsLocalFileAndLetsConfigurationOverrideIt() { var root = Path.Combine(AppContext.BaseDirectory, $"webhook-config-test-{Guid.NewGuid():N}"); var webhookDirectory = Path.Combine(root, "Discord", "Webhooks"); Directory.CreateDirectory(webhookDirectory); try { File.WriteAllText(Path.Combine(webhookDirectory, "webhooks.json"), """ { "BanAppeals": "https://discord.com/api/webhooks/123/appeal", "Reports": "https://discord.com/api/webhooks/123/file-report" } """); var configuration = new ConfigurationBuilder() .AddInMemoryCollection(new Dictionary { ["Radicade:Discord:Webhooks:Reports"] = "https://discord.com/api/webhooks/456/config-report" }) .Build(); WebhookConfig.Load(configuration, root); Assert.Equal("https://discord.com/api/webhooks/123/appeal", WebhookConfig.BanAppealsUrl); Assert.Equal("https://discord.com/api/webhooks/456/config-report", WebhookConfig.ReportsUrl); } finally { WebhookConfig.Load(new ConfigurationBuilder().Build(), Path.Combine(root, "missing")); Directory.Delete(root, recursive: true); } } [Theory] [InlineData("Club", "https://cdn.radicade.app/ClubReport.png")] [InlineData("Player", "https://cdn.radicade.app/PlayerReport.png")] [InlineData("Room", "https://cdn.radicade.app/RoomReport.png")] [InlineData("Event", "https://cdn.radicade.app/EventReport.png")] [InlineData("Invention", "https://cdn.radicade.app/InventionReport.png")] public void ReportImage_MatchesReportCategory(string reportType, string expectedUrl) => Assert.Equal(expectedUrl, WebhookImages.ForReport(reportType)); [Fact] public void ReportImage_IsNotAssignedToUnspecifiedCategories() => Assert.Null(WebhookImages.ForReport("Board")); [Fact] public void CategoryArtwork_IsSerializedAsThumbnailRatherThanLargeImage() { var payload = new DiscordPayload { Title = "Bug report", ThumbnailUrl = WebhookImages.BugReport }; using var document = JsonDocument.Parse(DiscordPayloadJson.Build(payload)); var embed = document.RootElement.GetProperty("embeds")[0]; Assert.Equal(WebhookImages.BugReport, embed.GetProperty("thumbnail").GetProperty("url").GetString()); Assert.False(embed.TryGetProperty("image", out _)); } [Fact] public void UploadedBugScreenshot_RemainsTheLargeImageAlongsideCategoryThumbnail() { var payload = new DiscordPayload { Title = "Bug report", ThumbnailUrl = WebhookImages.BugReport }; using var document = JsonDocument.Parse(DiscordPayloadJson.Build(payload, embedImageUrl: "attachment://screenshot.png")); var embed = document.RootElement.GetProperty("embeds")[0]; Assert.Equal("attachment://screenshot.png", embed.GetProperty("image").GetProperty("url").GetString()); Assert.Equal(WebhookImages.BugReport, embed.GetProperty("thumbnail").GetProperty("url").GetString()); } }