mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-04-24 06:54:44 +00:00
Basic automated tests through the CLI
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
<CollectCoverage>true</CollectCoverage>
|
||||
<CoverletOutputFormat>opencover</CoverletOutputFormat>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
|
||||
<None Include="*.secret" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AngleSharp" Version="0.16.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="5.10.3" />
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="1.2.0" />
|
||||
<PackageReference Include="JsonExtensions" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
|
||||
<PackageReference Include="coverlet.msbuild" Version="3.0.3" PrivateAssets="all" />
|
||||
<PackageReference Include="System.Reactive" Version="5.0.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DiscordChatExporter.Cli\DiscordChatExporter.Cli.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
25
DiscordChatExporter.Cli.Tests/Fixtures/TempOutputFixture.cs
Normal file
25
DiscordChatExporter.Cli.Tests/Fixtures/TempOutputFixture.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace DiscordChatExporter.Cli.Tests.Fixtures
|
||||
{
|
||||
public class TempOutputFixture : IDisposable
|
||||
{
|
||||
public string DirPath => Path.Combine(
|
||||
Path.GetDirectoryName(typeof(TempOutputFixture).Assembly.Location) ?? Directory.GetCurrentDirectory(),
|
||||
"Temp"
|
||||
);
|
||||
|
||||
public TempOutputFixture() => Directory.CreateDirectory(DirPath);
|
||||
|
||||
public string GetTempFilePath(string fileName) => Path.Combine(DirPath, fileName);
|
||||
|
||||
public string GetTempFilePath() => GetTempFilePath(Guid.NewGuid().ToString());
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Directory.Exists(DirPath))
|
||||
Directory.Delete(DirPath, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
39
DiscordChatExporter.Cli.Tests/Infra/Secrets.cs
Normal file
39
DiscordChatExporter.Cli.Tests/Infra/Secrets.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace DiscordChatExporter.Cli.Tests.Infra
|
||||
{
|
||||
public static class Secrets
|
||||
{
|
||||
private static readonly Lazy<string> DiscordTokenLazy = new(() =>
|
||||
{
|
||||
var fromEnvironment = Environment.GetEnvironmentVariable("DISCORD_TOKEN");
|
||||
if (!string.IsNullOrWhiteSpace(fromEnvironment))
|
||||
return fromEnvironment;
|
||||
|
||||
var secretFilePath = Path.Combine(
|
||||
Path.GetDirectoryName(typeof(Secrets).Assembly.Location) ?? Directory.GetCurrentDirectory(),
|
||||
"DiscordToken.secret"
|
||||
);
|
||||
|
||||
if (File.Exists(secretFilePath))
|
||||
return File.ReadAllText(secretFilePath);
|
||||
|
||||
throw new InvalidOperationException("Discord token not provided for tests.");
|
||||
});
|
||||
|
||||
private static readonly Lazy<bool> IsDiscordTokenBotLazy = new(() =>
|
||||
{
|
||||
// Default to true
|
||||
var fromEnvironment = Environment.GetEnvironmentVariable("DISCORD_TOKEN_BOT");
|
||||
if (string.IsNullOrWhiteSpace(fromEnvironment))
|
||||
return true;
|
||||
|
||||
return string.Equals(fromEnvironment, "true", StringComparison.OrdinalIgnoreCase);
|
||||
});
|
||||
|
||||
public static string DiscordToken => DiscordTokenLazy.Value;
|
||||
|
||||
public static bool IsDiscordTokenBot => IsDiscordTokenBotLazy.Value;
|
||||
}
|
||||
}
|
||||
304
DiscordChatExporter.Cli.Tests/MentionSpecs.cs
Normal file
304
DiscordChatExporter.Cli.Tests/MentionSpecs.cs
Normal file
@@ -0,0 +1,304 @@
|
||||
using System;
|
||||
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.Discord;
|
||||
using DiscordChatExporter.Core.Exporting;
|
||||
using FluentAssertions;
|
||||
using JsonExtensions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace DiscordChatExporter.Cli.Tests
|
||||
{
|
||||
public class MentionSpecs : IClassFixture<TempOutputFixture>
|
||||
{
|
||||
private readonly ITestOutputHelper _testOutput;
|
||||
private readonly TempOutputFixture _tempOutput;
|
||||
|
||||
public MentionSpecs(ITestOutputHelper testOutput, TempOutputFixture tempOutput)
|
||||
{
|
||||
_testOutput = testOutput;
|
||||
_tempOutput = tempOutput;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task User_mention_is_rendered_correctly_in_JSON()
|
||||
{
|
||||
// Arrange
|
||||
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "json");
|
||||
|
||||
// Act
|
||||
await new ExportChannelsCommand
|
||||
{
|
||||
TokenValue = Secrets.DiscordToken,
|
||||
IsBotToken = Secrets.IsDiscordTokenBot,
|
||||
ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)},
|
||||
ExportFormat = ExportFormat.Json,
|
||||
OutputPath = outputFilePath
|
||||
}.ExecuteAsync(new FakeConsole());
|
||||
|
||||
var jsonData = await File.ReadAllTextAsync(outputFilePath);
|
||||
_testOutput.WriteLine(jsonData);
|
||||
|
||||
var json = Json.Parse(jsonData);
|
||||
|
||||
var messageJson = json
|
||||
.GetProperty("messages")
|
||||
.EnumerateArray()
|
||||
.Single(j => string.Equals(
|
||||
j.GetProperty("id").GetString(),
|
||||
"866458840245076028",
|
||||
StringComparison.OrdinalIgnoreCase
|
||||
));
|
||||
|
||||
var content = messageJson
|
||||
.GetProperty("content")
|
||||
.GetString();
|
||||
|
||||
var mentionedUserIds = messageJson
|
||||
.GetProperty("mentions")
|
||||
.EnumerateArray()
|
||||
.Select(j => j.GetProperty("id").GetString())
|
||||
.ToArray();
|
||||
|
||||
// Assert
|
||||
content.Should().Be("User mention: @Tyrrrz");
|
||||
mentionedUserIds.Should().Contain("128178626683338752");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task User_mention_is_rendered_correctly_in_HTML()
|
||||
{
|
||||
// Arrange
|
||||
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html");
|
||||
|
||||
// Act
|
||||
await new ExportChannelsCommand
|
||||
{
|
||||
TokenValue = Secrets.DiscordToken,
|
||||
IsBotToken = Secrets.IsDiscordTokenBot,
|
||||
ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)},
|
||||
ExportFormat = ExportFormat.HtmlDark,
|
||||
OutputPath = outputFilePath
|
||||
}.ExecuteAsync(new FakeConsole());
|
||||
|
||||
var htmlData = await File.ReadAllTextAsync(outputFilePath);
|
||||
_testOutput.WriteLine(htmlData);
|
||||
|
||||
var html = Html.Parse(htmlData);
|
||||
|
||||
var messageHtml = html.GetElementById("message-866458840245076028");
|
||||
|
||||
// Assert
|
||||
messageHtml.Should().NotBeNull();
|
||||
messageHtml?.Text().Trim().Should().Be("User mention: @Tyrrrz");
|
||||
messageHtml?.InnerHtml.Should().Contain("Tyrrrz#5447");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Text_channel_mention_is_rendered_correctly_in_JSON()
|
||||
{
|
||||
// Arrange
|
||||
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "json");
|
||||
|
||||
// Act
|
||||
await new ExportChannelsCommand
|
||||
{
|
||||
TokenValue = Secrets.DiscordToken,
|
||||
IsBotToken = Secrets.IsDiscordTokenBot,
|
||||
ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)},
|
||||
ExportFormat = ExportFormat.Json,
|
||||
OutputPath = outputFilePath
|
||||
}.ExecuteAsync(new FakeConsole());
|
||||
|
||||
var jsonData = await File.ReadAllTextAsync(outputFilePath);
|
||||
_testOutput.WriteLine(jsonData);
|
||||
|
||||
var json = Json.Parse(jsonData);
|
||||
|
||||
var messageJson = json
|
||||
.GetProperty("messages")
|
||||
.EnumerateArray()
|
||||
.Single(j => string.Equals(
|
||||
j.GetProperty("id").GetString(),
|
||||
"866459040480624680",
|
||||
StringComparison.OrdinalIgnoreCase
|
||||
));
|
||||
|
||||
var content = messageJson
|
||||
.GetProperty("content")
|
||||
.GetString();
|
||||
|
||||
// Assert
|
||||
content.Should().Be("Text channel mention: #mention-tests");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Text_channel_mention_is_rendered_correctly_in_HTML()
|
||||
{
|
||||
// Arrange
|
||||
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html");
|
||||
|
||||
// Act
|
||||
await new ExportChannelsCommand
|
||||
{
|
||||
TokenValue = Secrets.DiscordToken,
|
||||
IsBotToken = Secrets.IsDiscordTokenBot,
|
||||
ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)},
|
||||
ExportFormat = ExportFormat.HtmlDark,
|
||||
OutputPath = outputFilePath
|
||||
}.ExecuteAsync(new FakeConsole());
|
||||
|
||||
var htmlData = await File.ReadAllTextAsync(outputFilePath);
|
||||
_testOutput.WriteLine(htmlData);
|
||||
|
||||
var html = Html.Parse(htmlData);
|
||||
|
||||
var messageHtml = html.GetElementById("message-866459040480624680");
|
||||
|
||||
// Assert
|
||||
messageHtml.Should().NotBeNull();
|
||||
messageHtml?.Text().Trim().Should().Be("Text channel mention: #mention-tests");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Voice_channel_mention_is_rendered_correctly_in_JSON()
|
||||
{
|
||||
// Arrange
|
||||
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "json");
|
||||
|
||||
// Act
|
||||
await new ExportChannelsCommand
|
||||
{
|
||||
TokenValue = Secrets.DiscordToken,
|
||||
IsBotToken = Secrets.IsDiscordTokenBot,
|
||||
ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)},
|
||||
ExportFormat = ExportFormat.Json,
|
||||
OutputPath = outputFilePath
|
||||
}.ExecuteAsync(new FakeConsole());
|
||||
|
||||
var jsonData = await File.ReadAllTextAsync(outputFilePath);
|
||||
_testOutput.WriteLine(jsonData);
|
||||
|
||||
var json = Json.Parse(jsonData);
|
||||
|
||||
var messageJson = json
|
||||
.GetProperty("messages")
|
||||
.EnumerateArray()
|
||||
.Single(j => string.Equals(
|
||||
j.GetProperty("id").GetString(),
|
||||
"866459175462633503",
|
||||
StringComparison.OrdinalIgnoreCase
|
||||
));
|
||||
|
||||
var content = messageJson
|
||||
.GetProperty("content")
|
||||
.GetString();
|
||||
|
||||
// Assert
|
||||
content.Should().Be("Voice channel mention: #chaos-vc [voice]");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Voice_channel_mention_is_rendered_correctly_in_HTML()
|
||||
{
|
||||
// Arrange
|
||||
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html");
|
||||
|
||||
// Act
|
||||
await new ExportChannelsCommand
|
||||
{
|
||||
TokenValue = Secrets.DiscordToken,
|
||||
IsBotToken = Secrets.IsDiscordTokenBot,
|
||||
ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)},
|
||||
ExportFormat = ExportFormat.HtmlDark,
|
||||
OutputPath = outputFilePath
|
||||
}.ExecuteAsync(new FakeConsole());
|
||||
|
||||
var htmlData = await File.ReadAllTextAsync(outputFilePath);
|
||||
_testOutput.WriteLine(htmlData);
|
||||
|
||||
var html = Html.Parse(htmlData);
|
||||
|
||||
var messageHtml = html.GetElementById("message-866459175462633503");
|
||||
|
||||
// Assert
|
||||
messageHtml.Should().NotBeNull();
|
||||
messageHtml?.Text().Trim().Should().Be("Voice channel mention: 🔊chaos-vc");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Role_mention_is_rendered_correctly_in_JSON()
|
||||
{
|
||||
// Arrange
|
||||
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "json");
|
||||
|
||||
// Act
|
||||
await new ExportChannelsCommand
|
||||
{
|
||||
TokenValue = Secrets.DiscordToken,
|
||||
IsBotToken = Secrets.IsDiscordTokenBot,
|
||||
ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)},
|
||||
ExportFormat = ExportFormat.Json,
|
||||
OutputPath = outputFilePath
|
||||
}.ExecuteAsync(new FakeConsole());
|
||||
|
||||
var jsonData = await File.ReadAllTextAsync(outputFilePath);
|
||||
_testOutput.WriteLine(jsonData);
|
||||
|
||||
var json = Json.Parse(jsonData);
|
||||
|
||||
var messageJson = json
|
||||
.GetProperty("messages")
|
||||
.EnumerateArray()
|
||||
.Single(j => string.Equals(
|
||||
j.GetProperty("id").GetString(),
|
||||
"866459254693429258",
|
||||
StringComparison.OrdinalIgnoreCase
|
||||
));
|
||||
|
||||
var content = messageJson
|
||||
.GetProperty("content")
|
||||
.GetString();
|
||||
|
||||
// Assert
|
||||
content.Should().Be("Role mention: @Role 1");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Role_mention_is_rendered_correctly_in_HTML()
|
||||
{
|
||||
// Arrange
|
||||
var outputFilePath = Path.ChangeExtension(_tempOutput.GetTempFilePath(), "html");
|
||||
|
||||
// Act
|
||||
await new ExportChannelsCommand
|
||||
{
|
||||
TokenValue = Secrets.DiscordToken,
|
||||
IsBotToken = Secrets.IsDiscordTokenBot,
|
||||
ChannelIds = new[] {Snowflake.Parse(ChannelIds.MentionTestCases)},
|
||||
ExportFormat = ExportFormat.HtmlDark,
|
||||
OutputPath = outputFilePath
|
||||
}.ExecuteAsync(new FakeConsole());
|
||||
|
||||
var htmlData = await File.ReadAllTextAsync(outputFilePath);
|
||||
_testOutput.WriteLine(htmlData);
|
||||
|
||||
var html = Html.Parse(htmlData);
|
||||
|
||||
var messageHtml = html.GetElementById("message-866459254693429258");
|
||||
|
||||
// Assert
|
||||
messageHtml.Should().NotBeNull();
|
||||
messageHtml?.Text().Trim().Should().Be("Role mention: @Role 1");
|
||||
}
|
||||
}
|
||||
}
|
||||
9
DiscordChatExporter.Cli.Tests/TestData/ChannelIds.cs
Normal file
9
DiscordChatExporter.Cli.Tests/TestData/ChannelIds.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace DiscordChatExporter.Cli.Tests.TestData
|
||||
{
|
||||
public static class ChannelIds
|
||||
{
|
||||
public static string MentionTestCases { get; } = "866458801389174794";
|
||||
|
||||
public static string ReplyTestCases { get; } = "866459871934677052";
|
||||
}
|
||||
}
|
||||
12
DiscordChatExporter.Cli.Tests/Utils/Html.cs
Normal file
12
DiscordChatExporter.Cli.Tests/Utils/Html.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using AngleSharp.Html.Dom;
|
||||
using AngleSharp.Html.Parser;
|
||||
|
||||
namespace DiscordChatExporter.Cli.Tests.Utils
|
||||
{
|
||||
internal static class Html
|
||||
{
|
||||
private static readonly IHtmlParser Parser = new HtmlParser();
|
||||
|
||||
public static IHtmlDocument Parse(string source) => Parser.ParseDocument(source);
|
||||
}
|
||||
}
|
||||
5
DiscordChatExporter.Cli.Tests/xunit.runner.json
Normal file
5
DiscordChatExporter.Cli.Tests/xunit.runner.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
|
||||
"methodDisplayOptions": "all",
|
||||
"methodDisplay": "method"
|
||||
}
|
||||
Reference in New Issue
Block a user