Files
FinalToken/tests/FinalToken.Core.Tests/StreamDeck/InstallerTests.cs
T
Florian Schmidt 15c7e684bb build: one version for the app and the plugin
D11 replaces an installed plugin only when the shipped manifest is
newer, but the manifest version was kept by hand and still said
1.0.0.1, the version of a test build installed before the snapshot
sharing and appPath fixes, so that stale plugin was never replaced.
Directory.Build.props now holds the only version, 1.0.1; publish.py
stamps it into the shipped manifest and the app shows it in its title.

Tests that waited on real time (file retry, popup expiry) use
controlled timers, and real process waits have wider margins.

Refs #1
2026-10-14 21:08:22 +02:00

130 lines
7.1 KiB
C#

using FinalToken.Core.StreamDeck;
using Xunit;
namespace FinalToken.Core.Tests.StreamDeck;
public sealed class InstallerTests : IDisposable
{
private readonly string root = Path.Combine(Path.GetTempPath(), "finaltoken-installer-" + Guid.NewGuid().ToString("N"));
private string Shipped => Path.Combine(root, "shipped");
private string Plugins => Path.Combine(root, "plugins");
private string Installed => Path.Combine(Plugins, PluginInstaller.FolderName);
public InstallerTests() => Write(Shipped, "1.0.0.0", "new");
[Fact]
public async Task ReleasePluginReportsOldDevelopmentCopyAsOutdated()
{
var shipped = Environment.GetEnvironmentVariable("FINALTOKEN_TEST_SHIPPED_PLUGIN");
if (shipped is null)
{
Write(Shipped, typeof(PluginInstaller).Assembly.GetName().Version!.ToString(), "new");
shipped = Shipped;
}
Write(Installed, "1.0.0.1", "old development copy");
var process = new FakeProcesses();
var installer = new PluginInstaller(shipped, new(Plugins, "fake", "fake"), process);
var status = await installer.GetStatusAsync(TestContext.Current.CancellationToken);
Assert.Equal(PluginInstallState.Outdated, status.State);
Assert.Equal(typeof(PluginInstaller).Assembly.GetName().Version!.ToString(), status.ShippedVersion);
Assert.Equal("1.0.0.1", status.InstalledVersion);
Assert.Equal(0, process.Stops); Assert.Equal(0, process.Starts);
}
[Fact]
public async Task FreshInstallDoesNotStartStoppedHost()
{
var process = new FakeProcesses();
var installer = Create(process);
Assert.Equal(PluginInstallState.NotInstalled, (await installer.GetStatusAsync(TestContext.Current.CancellationToken)).State);
await installer.InstallOrUpdateAsync(TestContext.Current.CancellationToken);
Assert.Equal("new", File.ReadAllText(Path.Combine(Installed, "binary")));
Assert.Equal(PluginInstallState.UpToDate, (await installer.GetStatusAsync(TestContext.Current.CancellationToken)).State);
Assert.Equal(0, process.Stops); Assert.Equal(0, process.Starts);
}
[Fact]
public async Task UpdateStopsAndRestartsRunningHost()
{
Write(Installed, "0.9.0.0", "old");
var process = new FakeProcesses { Running = true };
var installer = Create(process);
var outcomes = new List<InstallOutcome>(); installer.Outcome += outcomes.Add;
Assert.Equal(PluginInstallState.Outdated, (await installer.GetStatusAsync(TestContext.Current.CancellationToken)).State);
await installer.InstallOrUpdateAsync(TestContext.Current.CancellationToken);
Assert.Equal("new", File.ReadAllText(Path.Combine(Installed, "binary")));
Assert.Equal(1, process.Stops); Assert.Equal(1, process.Starts);
Assert.Contains(InstallOutcome.Updated, outcomes);
Assert.Single(Directory.GetDirectories(Plugins));
}
[Fact]
public async Task CurrentAndNewerVersionsAreNotReplaced()
{
Write(Installed, "2.0.0.0", "newer");
var process = new FakeProcesses { Running = true };
await Create(process).InstallOrUpdateAsync(TestContext.Current.CancellationToken);
Assert.Equal("newer", File.ReadAllText(Path.Combine(Installed, "binary")));
Assert.Equal(0, process.Stops);
}
[Fact]
public async Task RestartFailureRollsBackAndRestartsOldPlugin()
{
Write(Installed, "0.9.0.0", "old");
var process = new FakeProcesses { Running = true, FailFirstStart = true };
var installer = Create(process);
var outcomes = new List<InstallOutcome>(); installer.Outcome += outcomes.Add;
await Assert.ThrowsAsync<IOException>(() => installer.InstallOrUpdateAsync(TestContext.Current.CancellationToken));
Assert.Equal("old", File.ReadAllText(Path.Combine(Installed, "binary")));
Assert.Equal(2, process.Starts);
Assert.Contains(InstallOutcome.RolledBack, outcomes);
Assert.Single(Directory.GetDirectories(Plugins));
}
[Fact]
public async Task NoHostAndMissingShippedPluginHaveExplicitStatus()
{
var installer = new PluginInstaller(Shipped, null);
Assert.Equal(PluginInstallState.NoHostFound, (await installer.GetStatusAsync(TestContext.Current.CancellationToken)).State);
await installer.InstallOrUpdateAsync(TestContext.Current.CancellationToken);
var missing = new PluginInstaller(Path.Combine(root, "missing"), new(Plugins, "fake", "fake"));
Assert.Equal(PluginInstallState.NoShippedPlugin, (await missing.GetStatusAsync(TestContext.Current.CancellationToken)).State);
await Assert.ThrowsAsync<InvalidOperationException>(() => missing.InstallOrUpdateAsync(TestContext.Current.CancellationToken));
}
[Fact]
public async Task StopFailureKeepsOldFolderAndAttemptsRestart()
{
Write(Installed, "0.9.0.0", "old");
var process = new FakeProcesses { Running = true, FailStop = true };
await Assert.ThrowsAsync<IOException>(() => Create(process).InstallOrUpdateAsync(TestContext.Current.CancellationToken));
Assert.Equal("old", File.ReadAllText(Path.Combine(Installed, "binary")));
Assert.Equal(1, process.Starts);
}
[Theory]
[InlineData(InstallMode.Off, InstallAction.Nothing)]
[InlineData(InstallMode.Ask, InstallAction.Ask)]
[InlineData(InstallMode.Automatic, InstallAction.Install)]
public void PolicyHonorsInstallMode(InstallMode mode, InstallAction expected) =>
Assert.Equal(expected, InstallPolicy.Decide(mode, new(PluginInstallState.NotInstalled, "1"), null));
[Fact]
public void PolicyHonorsDismissedVersionAndUnavailableHost()
{
Assert.Equal(InstallAction.Nothing, InstallPolicy.Decide(InstallMode.Ask, new(PluginInstallState.Outdated, "1"), "1"));
Assert.Equal(InstallAction.Nothing, InstallPolicy.Decide(InstallMode.Automatic, new(PluginInstallState.NoHostFound), null));
}
private PluginInstaller Create(FakeProcesses process) => new(Shipped, new(Plugins, "fake", "fake"), process);
private static void Write(string directory, string version, string binary)
{
Directory.CreateDirectory(directory);
File.WriteAllText(Path.Combine(directory, "manifest.json"), "{\"Version\":\"" + version + "\"}");
File.WriteAllText(Path.Combine(directory, "binary"), binary);
}
public void Dispose() { if (Directory.Exists(root)) Directory.Delete(root, true); }
private sealed class FakeProcesses : IHostProcesses
{
public bool Running { get; init; }
public bool FailFirstStart { get; init; }
public bool FailStop { get; init; }
public int Stops { get; private set; }
public int Starts { get; private set; }
public Task<bool> IsRunningAsync(PluginHost host, CancellationToken ct) => Task.FromResult(Running);
public Task StopAsync(PluginHost host, CancellationToken ct) { Stops++; if (FailStop) throw new IOException("stop failed"); return Task.CompletedTask; }
public Task StartAsync(PluginHost host, CancellationToken ct) { Starts++; if (FailFirstStart && Starts == 1) throw new IOException("start failed"); return Task.CompletedTask; }
}
}