Files
duempelkas/tests/Duempelkas.Tests/FileOperationTests.cs
T

42 lines
1.7 KiB
C#

using Duempelkas.App.Services;
using Duempelkas.App.Services.Models;
using Duempelkas.Infrastructure.Services;
using Xunit;
namespace Duempelkas.Tests;
public class FileOperationTests
{
[Fact]
public async Task Cancel_IsNeutral()
{
var service = new FileSaveService(new Dialog(null));
Assert.Equal(FileOperationStatus.Cancelled, (await service.SaveFileAsync([1], "test.pdf")).Status);
}
[Fact]
public async Task SaveAndFailure_AreDistinct_AndOpeningMissingFileIsError()
{
var folder = Path.Combine(Path.GetTempPath(), "duempelkas-files-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(folder);
try
{
var path = Path.Combine(folder, "Prüfung.pdf");
var service = new FileSaveService(new Dialog(path));
Assert.Equal(FileOperationStatus.Success, (await service.SaveFileAsync([1, 2, 3], "test.pdf")).Status);
Assert.Equal(new byte[] { 1, 2, 3 }, await File.ReadAllBytesAsync(path));
// A directory cannot be overwritten as a file; the existing file is preserved.
Assert.Equal(FileOperationStatus.Error, (await new FileSaveService(new Dialog(folder)).SaveFileAsync([4], "test.pdf")).Status);
Assert.Equal(FileOperationStatus.Error, (await service.OpenFileAsync(Path.Combine(folder, "missing.pdf"))).Status);
Assert.Equal(new byte[] { 1, 2, 3 }, await File.ReadAllBytesAsync(path));
}
finally { Directory.Delete(folder, true); }
}
private sealed class Dialog(string? path) : IFileDialogService
{
public Task<string?> SaveAsync(string suggestedFileName) => Task.FromResult(path);
public Task<string?> OpenBackupAsync() => Task.FromResult(path);
}
}