Clean up tests

This commit is contained in:
Tyrrrz
2021-09-08 01:18:35 +03:00
parent 301bca0633
commit 67313f2b22
10 changed files with 229 additions and 515 deletions

View File

@@ -0,0 +1,120 @@
using System;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using CliFx.Infrastructure;
using DiscordChatExporter.Cli.Commands;
using DiscordChatExporter.Cli.Tests.Infra;
using DiscordChatExporter.Cli.Tests.Utils;
using DiscordChatExporter.Core.Discord;
using DiscordChatExporter.Core.Exporting;
using JsonExtensions;
namespace DiscordChatExporter.Cli.Tests.Fixtures
{
public class ExportWrapperFixture : IDisposable
{
private string DirPath { get; } = Path.Combine(
Path.GetDirectoryName(typeof(ExportWrapperFixture).Assembly.Location) ?? Directory.GetCurrentDirectory(),
"ExportCache",
Guid.NewGuid().ToString()
);
public async ValueTask<IHtmlDocument> ExportAsHtmlAsync(Snowflake channelId)
{
var filePath = Path.Combine(DirPath, channelId + ".html");
// Perform export only if it hasn't been done before
if (!File.Exists(filePath))
{
await new ExportChannelsCommand
{
TokenValue = Secrets.DiscordToken,
IsBotToken = Secrets.IsDiscordTokenBot,
ChannelIds = new[] { channelId },
ExportFormat = ExportFormat.HtmlDark,
OutputPath = filePath
}.ExecuteAsync(new FakeConsole());
}
var data = await File.ReadAllTextAsync(filePath);
return Html.Parse(data);
}
public async ValueTask<IElement> GetMessageAsHtmlAsync(Snowflake channelId, Snowflake messageId)
{
var document = await ExportAsHtmlAsync(channelId);
var message = document.QuerySelector("#message-" + messageId);
if (message is null)
{
throw new InvalidOperationException(
$"Message '{messageId}' does not exist in export of channel '{channelId}'."
);
}
return message;
}
public async ValueTask<JsonElement> ExportAsJsonAsync(Snowflake channelId)
{
var filePath = Path.Combine(DirPath, channelId + ".json");
// Perform export only if it hasn't been done before
if (!File.Exists(filePath))
{
await new ExportChannelsCommand
{
TokenValue = Secrets.DiscordToken,
IsBotToken = Secrets.IsDiscordTokenBot,
ChannelIds = new[] { channelId },
ExportFormat = ExportFormat.Json,
OutputPath = filePath
}.ExecuteAsync(new FakeConsole());
}
var data = await File.ReadAllTextAsync(filePath);
return Json.Parse(data);
}
public async ValueTask<JsonElement> GetMessageAsJsonAsync(Snowflake channelId, Snowflake messageId)
{
var document = await ExportAsJsonAsync(channelId);
var message = document
.GetProperty("messages")
.EnumerateArray()
.SingleOrDefault(j => string.Equals(
j.GetProperty("id").GetString(),
messageId.ToString(),
StringComparison.OrdinalIgnoreCase
));
if (message.ValueKind == JsonValueKind.Undefined)
{
throw new InvalidOperationException(
$"Message '{messageId}' does not exist in export of channel '{channelId}'."
);
}
return message;
}
public void Dispose()
{
try
{
Directory.Delete(DirPath, true);
}
catch (DirectoryNotFoundException)
{
}
}
}
}

View File

@@ -1,31 +0,0 @@
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",
Guid.NewGuid().ToString()
);
public TempOutputFixture() => Directory.CreateDirectory(DirPath);
public string GetTempFilePath() => Path.Combine(DirPath, Guid.NewGuid().ToString());
public string GetTempFilePath(string extension) => Path.ChangeExtension(GetTempFilePath(), extension);
public void Dispose()
{
try
{
Directory.Delete(DirPath, true);
}
catch (DirectoryNotFoundException)
{
}
}
}
}