Files
Chronos/tests/Titano.App.Tests/About/UpdateFlowTests.cs
T
Claude ffa05d9f69 fix(app): resumable, cancellable update download; portable copies get the portable zip
- UpdateFlow owns the update notice (check, download, cancel) as a plain class;
  AboutViewModel only maps it to text and commands.
- A portable copy downloads and verifies Titano-win-x64-portable-<v>.zip instead
  of the setup; a release without the package or SHA256SUMS opens its page.
- The download fails after 60 s without data instead of after 30 minutes,
  keeps its .part file and resumes it with a range request; Cancel stops it.
- Older release folders under %TEMP%\Titano\updates are removed after a
  verified download; an unexpected exception never leaves "Downloading".
- Tooltips show the package size; stale machine translations of the three
  rewritten tooltips are removed so they fall back to English.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-03 10:00:54 +00:00

209 lines
8.3 KiB
C#

using System.IO;
using Titano.App.About;
using Titano.Testing;
using Titano.Ui.Mvvm;
namespace Titano.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://github.com/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(), "Titano-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("Titano-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("Titano-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://github.com/o/r/releases/download/v1.0.0/";
var assets = new List<ReleaseAsset>();
if (withSetup) assets.Add(new ReleaseAsset("Titano-Setup-1.0.0.exe", Base + "Titano-Setup-1.0.0.exe", 1000));
if (withZip) assets.Add(new ReleaseAsset("Titano-win-x64-portable-1.0.0.zip", Base + "Titano-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; } = "Titano-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));
}
}