fix(files): report success cancellation and errors with safe next actions (U02)
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user