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 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(); 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(); installer.Outcome += outcomes.Add; await Assert.ThrowsAsync(() => 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(() => 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(() => 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 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; } } }