91 lines
3.5 KiB
C#
91 lines
3.5 KiB
C#
using ItWorx.KnowledgeHub.Api.Operations;
|
|
using ItWorx.KnowledgeHub.Application.Operations;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace ItWorx.KnowledgeHub.Api.Tests;
|
|
|
|
/// <summary>
|
|
/// <see cref="RestoreModeRunner"/> is deliberately pure/testable without booting a real
|
|
/// web host or database — no <c>Environment.Exit</c> call inside the tested unit
|
|
/// itself (only Program.cs's thin top-level wrapper performs the real process exit),
|
|
/// so a fake <see cref="IRestoreService"/> is enough to prove the CLI contract.
|
|
/// </summary>
|
|
public sealed class RestoreModeRunnerTests
|
|
{
|
|
[Fact]
|
|
public void IsRestoreMode_recognizes_the_flag_alone()
|
|
{
|
|
Assert.True(RestoreModeRunner.IsRestoreMode(["--restore"]));
|
|
}
|
|
|
|
[Fact]
|
|
public void IsRestoreMode_recognizes_the_flag_alongside_other_arguments()
|
|
{
|
|
Assert.True(RestoreModeRunner.IsRestoreMode(["--restore", "--backup-id=x"]));
|
|
}
|
|
|
|
[Fact]
|
|
public void IsRestoreMode_is_false_for_ordinary_startup_args()
|
|
{
|
|
Assert.False(RestoreModeRunner.IsRestoreMode([]));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Missing_required_arguments_returns_usage_exit_code_without_touching_the_restore_service()
|
|
{
|
|
var services = new ServiceCollection()
|
|
.AddSingleton<IRestoreService>(new FakeRestoreService(shouldSucceed: true))
|
|
.BuildServiceProvider();
|
|
using var output = new StringWriter();
|
|
|
|
var exitCode = await RestoreModeRunner.RunAsync(["--restore"], services, output, CancellationToken.None);
|
|
|
|
Assert.Equal(2, exitCode);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_successful_restore_returns_exit_code_zero()
|
|
{
|
|
var services = new ServiceCollection()
|
|
.AddSingleton<IRestoreService>(new FakeRestoreService(shouldSucceed: true))
|
|
.BuildServiceProvider();
|
|
using var output = new StringWriter();
|
|
|
|
var exitCode = await RestoreModeRunner.RunAsync(
|
|
["--restore", "--backup-id=abc", "--target-connection=conn", "--target-attachments=/tmp/x"],
|
|
services, output, CancellationToken.None);
|
|
|
|
Assert.Equal(0, exitCode);
|
|
Assert.Contains("GESLAAGD", output.ToString(), StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_failed_restore_returns_a_non_zero_exit_code_and_reports_the_mismatch()
|
|
{
|
|
var services = new ServiceCollection()
|
|
.AddSingleton<IRestoreService>(new FakeRestoreService(shouldSucceed: false))
|
|
.BuildServiceProvider();
|
|
using var output = new StringWriter();
|
|
|
|
var exitCode = await RestoreModeRunner.RunAsync(
|
|
["--restore", "--backup-id=abc", "--target-connection=conn", "--target-attachments=/tmp/x"],
|
|
services, output, CancellationToken.None);
|
|
|
|
Assert.Equal(1, exitCode);
|
|
Assert.Contains("MISLUKT", output.ToString(), StringComparison.Ordinal);
|
|
Assert.Contains("AFWIJKING", output.ToString(), StringComparison.Ordinal);
|
|
}
|
|
|
|
private sealed class FakeRestoreService(bool shouldSucceed) : IRestoreService
|
|
{
|
|
public Task<RestoreReport> RestoreAsync(string backupId, RestoreTarget target, CancellationToken cancellationToken)
|
|
{
|
|
var tables = shouldSucceed
|
|
? new List<RestoreTableReport> { new("pages", 3, 3) }
|
|
: new List<RestoreTableReport> { new("pages", 2, 3) };
|
|
|
|
return Task.FromResult(new RestoreReport(backupId, shouldSucceed, tables, shouldSucceed ? 1 : 0, 1, []));
|
|
}
|
|
}
|
|
}
|