acdream/tests/AcDream.Launcher.Tests/LauncherUpdateViewModelTests.cs
Erik a01ff42640 feat(launcher): LU2/LU3 — one update question at startup, and it restarts itself
The update surface was a panel the user had to reason about: Check again,
Rollback client, Stage launcher, Install client, Cancel, Close, plus an
installed/available version table, a minimum-launcher-version sentence, and a
"restart required" banner they had to act on. Reaching it meant knowing to
press "Check for updates" in the header.

Now: the feed is checked once at startup. If nothing is out of date, nothing
appears. If something is, one dialog says what is new and offers Update or
Not now.

Launcher before client, deliberately. A client release can declare a minimum
launcher version, so updating the launcher first is what makes the client
update installable at all — and it means nobody is ever shown "install
launcher X or newer before the client update", which is not a sentence a
player should have to read.

A launcher update now restarts into the new build by itself. That reuses the
existing, proven handoff rather than inventing a second one: LauncherSelfUpdate
Bootstrap.TryApplyStagedUpdateNowAsync starts the staged payload in helper mode
against the CURRENT process, exactly as ordinary startup does, and the launcher
then shuts down. Restarting by spawning a fresh copy of the current launcher and
letting its startup notice the staged plan would look simpler and be wrong: the
helper would wait on the new copy while the old one still held its own
executable mapped, so the file replacement could fail. The staged-helper launch
is extracted into one private method both paths call, so they cannot drift.

Deleted: the header "Check for updates" button, OpenCommand, CheckCommand,
InstallClientCommand, StageLauncherCommand, RollbackCommand, CloseCommand, the
version table, IsLauncherMinimumBlocked/MinimumLauncherStatus, the restart
banner, and LauncherUpdatePhase plumbing through the view model.

NOT deleted — none of the safety changed: manifest validation, bounded verified
download, safe ZIP extraction, versioned install with an atomic current.json
switch, the update session barrier, and rollback all still live in
AcDream.Launcher.Core/Updates. Rollback simply has no button; it remains
reachable as Core API with its own tests. The complexity the user objected to
was the panel, not the machinery underneath it.

An unreachable feed stays silent. A friend with no internet must still reach
their characters, so a failed startup check shows nothing at all rather than an
error to dismiss.

Tests: LauncherUpdateViewModelTests rewritten against the new surface (8 tests
— nothing-to-do stays silent, client update installs, launcher update stages
then restarts without touching the client, no-restart-seam fallback, silent
offline, Not now, refused while a session runs, failed install reports why).
Tests for the deleted commands are removed with them, not skipped.
Launcher 59 passed, Launcher.Core 335 passed.

Campaign LU slices LU2 and LU3, landed together because the new prompt replaces
the old one in the same files.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-19 18:49:19 +02:00

311 lines
11 KiB
C#

using AcDream.Launcher.Core.Updates;
using AcDream.Launcher.ViewModels;
namespace AcDream.Launcher.Tests;
/// <summary>
/// LU2/LU3. The launcher asks about updates exactly once, at startup, and only
/// when something is actually out of date. These tests pin that shape and the
/// launcher-before-client ordering; the six-button panel they replace (Check
/// again / Rollback / Stage launcher / Install client / Cancel / Close) is
/// gone, so the tests for it are gone with it rather than skipped.
/// </summary>
public sealed class LauncherUpdateViewModelTests
{
[Fact]
public async Task NothingOutOfDateNeverShowsTheDialog()
{
var updater = new FakeUpdater
{
ClientUpdateAvailable = false,
LauncherUpdateAvailable = false,
};
using LauncherUpdateViewModel viewModel = Create(updater);
await viewModel.StartupCheckAsync();
Assert.False(viewModel.IsOpen);
Assert.Equal(0, updater.InstallCalls);
Assert.Equal(0, updater.StageCalls);
}
[Fact]
public async Task AClientUpdateAsksOnceAndInstallsOnUpdate()
{
var updater = new FakeUpdater
{
ClientUpdateAvailable = true,
LauncherUpdateAvailable = false,
};
int clientChanged = 0;
using LauncherUpdateViewModel viewModel = Create(
updater,
onClientChanged: () => clientChanged++);
await viewModel.StartupCheckAsync();
Assert.True(viewModel.IsOpen);
Assert.Contains("game", viewModel.Body, StringComparison.OrdinalIgnoreCase);
Assert.Contains("2.0.0", viewModel.Body, StringComparison.Ordinal);
await viewModel.UpdateCommand.ExecuteAsync();
Assert.Equal(1, updater.InstallCalls);
Assert.Equal(0, updater.StageCalls);
Assert.Equal(1, clientChanged);
Assert.False(viewModel.IsOpen);
Assert.Null(viewModel.Error);
}
/// <summary>
/// The launcher goes first even when both are behind: a client release can
/// declare a minimum launcher version, so updating the launcher first is
/// what makes the client update installable at all.
/// </summary>
[Fact]
public async Task ALauncherUpdateStagesThenRestartsWithoutTouchingTheClient()
{
var updater = new FakeUpdater
{
ClientUpdateAvailable = true,
LauncherUpdateAvailable = true,
};
int shutdowns = 0;
int applyCalls = 0;
using LauncherUpdateViewModel viewModel = Create(
updater,
applyLauncherUpdateAsync: _ =>
{
applyCalls++;
return Task.FromResult(true);
},
requestShutdown: () => shutdowns++);
await viewModel.StartupCheckAsync();
Assert.True(viewModel.IsOpen);
await viewModel.UpdateCommand.ExecuteAsync();
Assert.Equal(1, updater.StageCalls);
Assert.Equal(1, applyCalls);
Assert.Equal(1, shutdowns);
// The client is deliberately NOT touched in the same pass: the updated
// launcher checks again on its own next start.
Assert.Equal(0, updater.InstallCalls);
}
[Fact]
public async Task ALauncherUpdateThatCannotRestartTellsTheUserToReopen()
{
var updater = new FakeUpdater
{
ClientUpdateAvailable = false,
LauncherUpdateAvailable = true,
};
int shutdowns = 0;
using LauncherUpdateViewModel viewModel = Create(
updater,
applyLauncherUpdateAsync: _ => Task.FromResult(false),
requestShutdown: () => shutdowns++);
await viewModel.StartupCheckAsync();
await viewModel.UpdateCommand.ExecuteAsync();
Assert.Equal(1, updater.StageCalls);
Assert.Equal(0, shutdowns);
Assert.Contains("reopen", viewModel.Status, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// A friend with no internet must still reach their characters. An
/// unreachable feed is not an error the user has to dismiss.
/// </summary>
[Fact]
public async Task AnUnreachableFeedStaysSilent()
{
var updater = new FakeUpdater
{
CheckHandler = _ => Task.FromException<LauncherUpdateCheckResult>(
new LauncherUpdateException("The update feed is unreachable.")),
};
using LauncherUpdateViewModel viewModel = Create(updater);
await viewModel.StartupCheckAsync();
Assert.False(viewModel.IsOpen);
Assert.False(viewModel.HasError);
}
[Fact]
public async Task NotNowClosesWithoutUpdatingAnything()
{
var updater = new FakeUpdater { ClientUpdateAvailable = true };
using LauncherUpdateViewModel viewModel = Create(updater);
await viewModel.StartupCheckAsync();
Assert.True(viewModel.IsOpen);
viewModel.NotNowCommand.Execute(null);
Assert.False(viewModel.IsOpen);
Assert.Equal(0, updater.InstallCalls);
Assert.Equal(0, updater.StageCalls);
}
[Fact]
public async Task UpdatingIsRefusedWhileASessionIsRunning()
{
var updater = new FakeUpdater { ClientUpdateAvailable = true };
using LauncherUpdateViewModel viewModel = Create(updater, canMutate: () => false);
await viewModel.StartupCheckAsync();
Assert.True(viewModel.IsOpen);
Assert.False(viewModel.UpdateCommand.CanExecute(null));
}
[Fact]
public async Task AFailedInstallReportsTheReasonAndLeavesTheDialogOpen()
{
var updater = new FakeUpdater
{
ClientUpdateAvailable = true,
InstallHandler = (_, _) => Task.FromException<ClientVersionResolution>(
new LauncherUpdateException("The download did not match its digest.")),
};
using LauncherUpdateViewModel viewModel = Create(updater);
await viewModel.StartupCheckAsync();
await viewModel.UpdateCommand.ExecuteAsync();
Assert.True(viewModel.IsOpen);
Assert.True(viewModel.HasError);
Assert.Contains("digest", viewModel.Error!, StringComparison.Ordinal);
}
private static LauncherUpdateViewModel Create(
FakeUpdater updater,
Action? onClientChanged = null,
Func<bool>? canMutate = null,
Func<CancellationToken, Task<bool>>? applyLauncherUpdateAsync = null,
Action? requestShutdown = null) =>
new(
updater,
new ImmediateUiDispatcher(),
onClientChanged ?? (() => { }),
canOpen: () => true,
canMutate: canMutate ?? (() => true),
applyLauncherUpdateAsync: applyLauncherUpdateAsync,
requestShutdown: requestShutdown);
private sealed class FakeUpdater : ILauncherUpdater
{
private static readonly LauncherVersion One = LauncherVersion.Parse("1.0.0");
private static readonly LauncherVersion Two = LauncherVersion.Parse("2.0.0");
public ClientVersionResolution CurrentClient { get; private set; } =
Resolution(One);
public bool ClientUpdateAvailable { get; init; }
public bool LauncherUpdateAvailable { get; init; }
public bool MinimumSatisfied { get; init; } = true;
public int InstallCalls { get; private set; }
public int StageCalls { get; private set; }
public Func<CancellationToken, Task<LauncherUpdateCheckResult>>? CheckHandler
{
get;
init;
}
public Func<
IProgress<LauncherUpdateProgress>?,
CancellationToken,
Task<ClientVersionResolution>>? InstallHandler { get; init; }
public Task<ClientVersionResolution> InitializeAsync(
CancellationToken cancellationToken = default) =>
Task.FromResult(CurrentClient);
public Task<LauncherUpdateCheckResult> CheckAsync(
CancellationToken cancellationToken = default) =>
CheckHandler?.Invoke(cancellationToken) ?? Task.FromResult(CreateCheck());
public async Task<ClientVersionResolution> InstallClientAsync(
LauncherUpdateCheckResult check,
IProgress<LauncherUpdateProgress>? progress = null,
CancellationToken cancellationToken = default)
{
InstallCalls++;
if (InstallHandler is not null)
{
return await InstallHandler(progress, cancellationToken);
}
progress?.Report(new LauncherUpdateProgress(
LauncherUpdatePhase.DownloadingClient,
"Downloading fixture.",
5,
10));
CurrentClient = Resolution(Two);
return CurrentClient;
}
public Task<SelfUpdateStageResult> StageLauncherAsync(
LauncherUpdateCheckResult check,
IProgress<LauncherUpdateProgress>? progress = null,
CancellationToken cancellationToken = default)
{
StageCalls++;
progress?.Report(new LauncherUpdateProgress(
LauncherUpdatePhase.StagingLauncher,
"Staged fixture.",
1,
1));
return Task.FromResult(new SelfUpdateStageResult(
Two,
"pending.json",
"Launcher staged."));
}
public Task<ClientVersionResolution> RollbackClientAsync(
IProgress<LauncherUpdateProgress>? progress = null,
CancellationToken cancellationToken = default) =>
Task.FromResult(CurrentClient);
private LauncherUpdateCheckResult CreateCheck()
{
var artifact = new ReleaseArtifact(
new Uri("https://example.test/release.zip"),
new string('a', 64),
100);
var manifest = new ReleaseManifest(
Two,
MinimumSatisfied ? One : Two,
new Dictionary<string, ReleaseArtifact> { ["win-x64"] = artifact },
new Dictionary<string, ReleaseArtifact> { ["win-x64"] = artifact });
return new LauncherUpdateCheckResult(
manifest,
"win-x64",
One,
CurrentClient.Version,
ClientUpdateAvailable,
LauncherUpdateAvailable,
MinimumSatisfied,
"Fixture check.");
}
private static ClientVersionResolution Resolution(LauncherVersion version) =>
new(
ClientVersionState.Verified,
"Fixture client verified.",
version,
Path.Combine("fixture", version.Value),
null,
null);
}
}