namespace ThreadPilot.Core.Tests { using System.Diagnostics; using Moq; using ThreadPilot.Models; using ThreadPilot.Services; public sealed class ConfigurationBackupTests { [Fact] public async Task ExportRoundTrip_PreservesPersistentRules() { var harness = new Harness(); var backup = await harness.Service.CaptureAsync(harness.Settings.Object.Settings); var imported = ConfigurationBackupService.Parse(ConfigurationBackupService.Serialize(backup)); Assert.Equal("old", Assert.Single(imported!.PersistentProcessRules!).Id); } [Fact] public async Task LegacyBundle_KeepsPersistentRules() { var harness = new Harness(); var backup = await harness.Service.CaptureAsync(harness.Settings.Object.Settings); backup.SchemaVersion = "2.0"; backup.PersistentProcessRules = null; await harness.Service.ApplyAsync(ConfigurationBackupService.Parse(ConfigurationBackupService.Serialize(backup))!); harness.Rules.Verify(x => x.SaveAsync(It.IsAny>()), Times.Never); } [Fact] public async Task FailedImport_RestoresAllPreviousConfiguration() { var harness = new Harness(); harness.Associations.SetupSequence(x => x.ReplaceConfigurationAsync(It.IsAny())) .ReturnsAsync(false).ReturnsAsync(true); var backup = await harness.Service.CaptureAsync(harness.Settings.Object.Settings); backup.PersistentProcessRules = [new() { Id = "new", ProcessName = "new.exe" }]; await Assert.ThrowsAsync(() => harness.Service.ApplyAsync(backup)); harness.Rules.Verify(x => x.SaveAsync(It.Is>(r => r.Single().Id == "old")), Times.Once); harness.Settings.Verify(x => x.UpdateSettingsAsync(It.IsAny()), Times.Once); } [Fact] public async Task InvalidRule_IsRejectedBeforeAnyWrite() { var harness = new Harness(); var backup = await harness.Service.CaptureAsync(harness.Settings.Object.Settings); backup.PersistentProcessRules = [new() { ProcessName = "game.exe", Priority = ProcessPriorityClass.RealTime }]; await Assert.ThrowsAsync(() => harness.Service.ApplyAsync(backup)); harness.Rules.Verify(x => x.SaveAsync(It.IsAny>()), Times.Never); harness.Associations.Verify(x => x.ReplaceConfigurationAsync(It.IsAny()), Times.Never); } [Fact] public void IncompleteBundle_DoesNotFallBackToLegacySettingsImport() { Assert.Throws(() => ConfigurationBackupService.Parse("{\"settings\":{}}")); } private sealed class Harness { public Mock Settings { get; } = new(); public Mock Associations { get; } = new(); public Mock Rules { get; } = new(); public ConfigurationBackupService Service { get; } public Harness() { this.Settings.SetupGet(x => x.Settings).Returns(new ApplicationSettingsModel()); this.Associations.SetupGet(x => x.Configuration).Returns(new ProcessMonitorConfiguration()); this.Associations.Setup(x => x.ReplaceConfigurationAsync(It.IsAny())).ReturnsAsync(true); this.Rules.Setup(x => x.LoadAsync()).ReturnsAsync([new() { Id = "old", ProcessName = "old.exe" }]); this.Service = new(this.Settings.Object, this.Associations.Object, this.Rules.Object); } } } }