CI / Build and test (push) Successful in 11m36s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 12s
Release / Build, test and publish (push) Successful in 14m56s
CI / PostgreSQL 16 integration (push) Successful in 10m11s
CI / PostgreSQL 17 integration (push) Successful in 10m15s
CI / Docker image (push) Successful in 45s
Release / Server image (push) Successful in 4m55s
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
210 lines
8.3 KiB
C#
210 lines
8.3 KiB
C#
using System.IO;
|
|
using Chronos.Core.Updates;
|
|
using Chronos.Testing;
|
|
using Chronos.Ui.Mvvm;
|
|
using Chronos.Ui.Updates;
|
|
|
|
namespace Chronos.App.Tests.About;
|
|
|
|
/// <summary>The update notice: start-up opt-out, capture runs, portable copies, failures and cancellation of the download.</summary>
|
|
[Serial] // AsyncRelayCommand.Failed is a static event
|
|
public sealed class UpdateFlowTests(TestContext context)
|
|
{
|
|
private const string Page = "https://gitea.example/o/r/releases/tag/v1.0.0";
|
|
|
|
[Test]
|
|
public async Task TheStartUpCheckFollowsTheOptOut()
|
|
{
|
|
var host = new Host();
|
|
var flow = host.Flow();
|
|
Assert.Null(await flow.CheckOnStartAsync(enabled: false));
|
|
Assert.Equal(0, host.Checks);
|
|
Assert.Equal(UpdateState.Unknown, flow.State);
|
|
|
|
Assert.Equal("v1.0.0", (await flow.CheckOnStartAsync(enabled: true))?.Tag);
|
|
Assert.Equal(1, host.Checks);
|
|
Assert.Equal(UpdateState.Available, flow.State);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ACaptureRunNeverTouchesTheNetwork()
|
|
{
|
|
var host = new Host { IsCapture = true };
|
|
var flow = host.Flow();
|
|
Assert.Null(await flow.CheckOnStartAsync(enabled: true));
|
|
Assert.Null(await flow.CheckAsync(CancellationToken.None));
|
|
Assert.Equal(0, host.Checks);
|
|
|
|
flow.SetResult(Release(withSetup: true, withZip: true, withSums: true));
|
|
await RunAsync(flow.Download);
|
|
Assert.Equal(0, host.Downloads);
|
|
Assert.Empty(host.Opened);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ADownloadedSetupIsShownAndOfferedUntilANewerReleaseAppears()
|
|
{
|
|
var host = new Host { Downloaded = Path.Combine(context.CreateTempDirectory(), "Chronos-Setup-1.0.0.exe") };
|
|
File.WriteAllText(host.Downloaded, "setup");
|
|
var flow = host.Flow();
|
|
flow.SetResult(Release(withSetup: true, withZip: false, withSums: true));
|
|
Assert.Equal("Chronos-Setup-1.0.0.exe", flow.Package?.Name);
|
|
|
|
await RunAsync(flow.Download);
|
|
Assert.Equal((UpdateState.Downloaded, host.Downloaded), (flow.State, flow.PackagePath));
|
|
Assert.SequenceEqual(new[] { host.Downloaded }, host.Revealed);
|
|
Assert.False(flow.IsDownloadOffered);
|
|
|
|
flow.SetResult(Release(withSetup: true, withZip: false, withSums: true));
|
|
Assert.Equal(UpdateState.Downloaded, flow.State);
|
|
flow.SetResult(Release(withSetup: true, withZip: false, withSums: true) with { Version = new Version(1, 1, 0), Tag = "v1.1.0" });
|
|
Assert.Equal((UpdateState.Available, (string?)null), (flow.State, flow.PackagePath));
|
|
}
|
|
|
|
[Test]
|
|
public async Task APortableCopyNeverDownloadsTheSetup()
|
|
{
|
|
var host = new Host { IsPortable = true };
|
|
var flow = host.Flow();
|
|
flow.SetResult(Release(withSetup: true, withZip: false, withSums: true));
|
|
Assert.Null(flow.Package);
|
|
await RunAsync(flow.Download);
|
|
Assert.Equal(0, host.Downloads);
|
|
Assert.SequenceEqual(new[] { Page }, host.Opened);
|
|
|
|
flow.SetResult(Release(withSetup: true, withZip: true, withSums: true));
|
|
Assert.Equal("Chronos-win-x64-portable-1.0.0.zip", flow.Package?.Name);
|
|
await RunAsync(flow.Download);
|
|
Assert.Equal(1, host.Downloads);
|
|
Assert.Equal(UpdateState.Downloaded, flow.State);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AReleaseWithoutChecksumsOnlyOpensItsPage()
|
|
{
|
|
var host = new Host();
|
|
var flow = host.Flow();
|
|
flow.SetResult(Release(withSetup: true, withZip: true, withSums: false));
|
|
await RunAsync(flow.Download);
|
|
Assert.Equal(0, host.Downloads);
|
|
Assert.SequenceEqual(new[] { Page }, host.Opened);
|
|
Assert.Equal(UpdateState.Available, flow.State);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ARefusedDownloadIsReportedAndCanBeRetried()
|
|
{
|
|
var host = new Host { Failure = new UpdateVerificationException("SHA-256 mismatch") };
|
|
var flow = host.Flow();
|
|
flow.SetResult(Release(withSetup: true, withZip: false, withSums: true));
|
|
await RunAsync(flow.Download);
|
|
Assert.Equal(UpdateState.NotVerified, flow.State);
|
|
Assert.True(flow.IsDownloadOffered);
|
|
Assert.True(flow.Download.CanExecute(null));
|
|
Assert.True(flow.Check.CanExecute(null));
|
|
}
|
|
|
|
[Test]
|
|
public async Task AStalledDownloadFailsAndCanBeRetried()
|
|
{
|
|
var host = new Host { Failure = new TimeoutException("No data received for 60 s.") };
|
|
var flow = host.Flow();
|
|
flow.SetResult(Release(withSetup: true, withZip: false, withSums: true));
|
|
await RunAsync(flow.Download);
|
|
Assert.Equal(UpdateState.DownloadFailed, flow.State);
|
|
Assert.True(flow.IsDownloadOffered);
|
|
}
|
|
|
|
[Test]
|
|
public async Task AnUnexpectedFailureIsReportedAndNeverLeavesTheNoticeDownloading()
|
|
{
|
|
var host = new Host { Failure = new UriFormatException("bad asset link") };
|
|
var flow = host.Flow();
|
|
flow.SetResult(Release(withSetup: true, withZip: false, withSums: true));
|
|
var reported = new List<Exception>();
|
|
AsyncRelayCommand.Failed += reported.Add;
|
|
try
|
|
{
|
|
await RunAsync(flow.Download);
|
|
}
|
|
finally
|
|
{
|
|
AsyncRelayCommand.Failed -= reported.Add;
|
|
}
|
|
Assert.True(reported.Single() is UriFormatException, reported.Single().GetType().Name);
|
|
Assert.Equal(UpdateState.DownloadFailed, flow.State);
|
|
Assert.True(flow.Check.CanExecute(null));
|
|
Assert.True(flow.Download.CanExecute(null));
|
|
}
|
|
|
|
[Test]
|
|
public async Task CancellingADownloadOffersItAgain()
|
|
{
|
|
var host = new Host { Stall = true };
|
|
var flow = host.Flow();
|
|
flow.SetResult(Release(withSetup: true, withZip: false, withSums: true));
|
|
flow.Download.Execute(null);
|
|
await host.Started.Task.WaitAsync(TimeSpan.FromSeconds(10));
|
|
Assert.Equal(UpdateState.Downloading, flow.State);
|
|
Assert.False(flow.Check.CanExecute(null));
|
|
|
|
flow.CancelDownload();
|
|
await WaitAsync(flow.Download);
|
|
Assert.Equal(UpdateState.Available, flow.State);
|
|
Assert.True(flow.IsDownloadOffered);
|
|
Assert.True(flow.Check.CanExecute(null));
|
|
}
|
|
|
|
private static ReleaseInfo Release(bool withSetup, bool withZip, bool withSums)
|
|
{
|
|
const string Base = "https://gitea.example/o/r/releases/download/v1.0.0/";
|
|
var assets = new List<ReleaseAsset>();
|
|
if (withSetup) assets.Add(new ReleaseAsset("Chronos-Setup-1.0.0.exe", Base + "Chronos-Setup-1.0.0.exe", 1000));
|
|
if (withZip) assets.Add(new ReleaseAsset("Chronos-win-x64-portable-1.0.0.zip", Base + "Chronos-win-x64-portable-1.0.0.zip", 900));
|
|
if (withSums) assets.Add(new ReleaseAsset("SHA256SUMS", Base + "SHA256SUMS", 300));
|
|
return new ReleaseInfo(new Version(1, 0, 0), "v1.0.0", Page) { Assets = assets };
|
|
}
|
|
|
|
private static async Task RunAsync(AsyncRelayCommand command)
|
|
{
|
|
command.Execute(null);
|
|
await WaitAsync(command);
|
|
}
|
|
|
|
private static async Task WaitAsync(AsyncRelayCommand command)
|
|
{
|
|
for (var i = 0; i < 500 && command.IsRunning; i++) await Task.Delay(10);
|
|
Assert.False(command.IsRunning, "the command did not finish");
|
|
}
|
|
|
|
private sealed class Host
|
|
{
|
|
public bool IsCapture { get; init; }
|
|
public bool IsPortable { get; init; }
|
|
public Exception? Failure { get; init; }
|
|
public bool Stall { get; init; }
|
|
public string Downloaded { get; init; } = "Chronos-Setup-1.0.0.exe";
|
|
public int Checks { get; private set; }
|
|
public int Downloads { get; private set; }
|
|
public List<string> Opened { get; } = [];
|
|
public List<string> Revealed { get; } = [];
|
|
public TaskCompletionSource Started { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
public UpdateFlow Flow() => new(new UpdateHost(new Version(0, 9, 0), IsCapture, IsPortable,
|
|
_ =>
|
|
{
|
|
Checks++;
|
|
return Task.FromResult<ReleaseInfo?>(Release(withSetup: true, withZip: true, withSums: true));
|
|
},
|
|
async (_, _, ct) =>
|
|
{
|
|
Downloads++;
|
|
Started.TrySetResult();
|
|
if (Stall) await Task.Delay(Timeout.Infinite, ct);
|
|
await Task.Yield();
|
|
return Failure is null ? Downloaded : throw Failure;
|
|
},
|
|
Opened.Add, Revealed.Add));
|
|
}
|
|
}
|