From 03f34c7acc7c3ad0ba8adcd0464f713559599473 Mon Sep 17 00:00:00 2001 From: PrimeBuild-pc Date: Mon, 3 Aug 2026 18:21:45 +0200 Subject: [PATCH] fix: consolidate process monitoring configuration --- .../ProcessPowerPlanAssociations.json | 3 - Models/ProcessMonitorConfiguration.cs | 14 ---- .../ProcessMonitorConfigurationTests.cs | 38 ++++++++++ .../ProcessMonitorServiceSettingsTests.cs | 69 +++++++++++++++++++ .../ProcessPowerPlanAssociationViewModel.cs | 18 ----- Views/ProcessPowerPlanAssociationView.xaml | 15 ---- 6 files changed, 107 insertions(+), 50 deletions(-) create mode 100644 Tests/ThreadPilot.Core.Tests/ProcessMonitorConfigurationTests.cs create mode 100644 Tests/ThreadPilot.Core.Tests/ProcessMonitorServiceSettingsTests.cs diff --git a/Configuration/ProcessPowerPlanAssociations.json b/Configuration/ProcessPowerPlanAssociations.json index f1bb261..88f1ec1 100644 --- a/Configuration/ProcessPowerPlanAssociations.json +++ b/Configuration/ProcessPowerPlanAssociations.json @@ -1,9 +1,6 @@ { "DefaultPowerPlanGuid": "", "DefaultPowerPlanName": "", - "IsEventBasedMonitoringEnabled": true, - "IsFallbackPollingEnabled": true, - "PollingIntervalSeconds": 5, "PreventDuplicatePowerPlanChanges": true, "PowerPlanChangeDelayMs": 250, "Associations": [ diff --git a/Models/ProcessMonitorConfiguration.cs b/Models/ProcessMonitorConfiguration.cs index 84afe74..c94bef8 100644 --- a/Models/ProcessMonitorConfiguration.cs +++ b/Models/ProcessMonitorConfiguration.cs @@ -12,15 +12,6 @@ public partial class ProcessMonitorConfiguration : ObservableObject [ObservableProperty] private string defaultPowerPlanName = string.Empty; - [ObservableProperty] - private bool isEventBasedMonitoringEnabled = true; - - [ObservableProperty] - private bool isFallbackPollingEnabled = true; - - [ObservableProperty] - private int pollingIntervalSeconds = 5; - [ObservableProperty] private bool preventDuplicatePowerPlanChanges = true; @@ -95,11 +86,6 @@ public List Validate() { var errors = new List(); - if (this.PollingIntervalSeconds < 1) - { - errors.Add("Polling interval must be at least 1 second"); - } - if (this.PowerPlanChangeDelayMs < 0) { errors.Add("Power plan change delay cannot be negative"); diff --git a/Tests/ThreadPilot.Core.Tests/ProcessMonitorConfigurationTests.cs b/Tests/ThreadPilot.Core.Tests/ProcessMonitorConfigurationTests.cs new file mode 100644 index 0000000..15f9e8d --- /dev/null +++ b/Tests/ThreadPilot.Core.Tests/ProcessMonitorConfigurationTests.cs @@ -0,0 +1,38 @@ +namespace ThreadPilot.Core.Tests +{ + using System.Text.Json; + using ThreadPilot.Models; + + public sealed class ProcessMonitorConfigurationTests + { + [Fact] + public void LegacyMonitoringFields_AreIgnoredWhenConfigurationIsDeserialized() + { + const string json = """ + { + "isEventBasedMonitoringEnabled": false, + "isFallbackPollingEnabled": false, + "pollingIntervalSeconds": 1, + "preventDuplicatePowerPlanChanges": false + } + """; + + var configuration = JsonSerializer.Deserialize(json, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + }); + + Assert.NotNull(configuration); + Assert.False(configuration.PreventDuplicatePowerPlanChanges); + } + + [Theory] + [InlineData("IsEventBasedMonitoringEnabled")] + [InlineData("IsFallbackPollingEnabled")] + [InlineData("PollingIntervalSeconds")] + public void DoesNotExposeDuplicateMonitoringProperties(string propertyName) + { + Assert.Null(typeof(ProcessMonitorConfiguration).GetProperty(propertyName)); + } + } +} diff --git a/Tests/ThreadPilot.Core.Tests/ProcessMonitorServiceSettingsTests.cs b/Tests/ThreadPilot.Core.Tests/ProcessMonitorServiceSettingsTests.cs new file mode 100644 index 0000000..08fef0e --- /dev/null +++ b/Tests/ThreadPilot.Core.Tests/ProcessMonitorServiceSettingsTests.cs @@ -0,0 +1,69 @@ +namespace ThreadPilot.Core.Tests +{ + using Microsoft.Extensions.Logging.Abstractions; + using Moq; + using ThreadPilot.Models; + using ThreadPilot.Services; + + public sealed class ProcessMonitorServiceSettingsTests + { + [Fact] + public async Task StartMonitoringAsync_WhenWmiDisabled_DoesNotReportWmiAsAvailable() + { + using var monitor = CreateMonitor(new ApplicationSettingsModel + { + EnableWmiMonitoring = false, + EnableFallbackPolling = false, + }); + + await monitor.StartMonitoringAsync(); + + Assert.False(monitor.IsWmiAvailable); + } + + [Fact] + public async Task StartMonitoringAsync_WhenFallbackDisabled_DoesNotActivateFallbackWhenWmiIsDisabled() + { + using var monitor = CreateMonitor(new ApplicationSettingsModel + { + EnableWmiMonitoring = false, + EnableFallbackPolling = false, + }); + + await monitor.StartMonitoringAsync(); + + Assert.False(monitor.IsFallbackPollingActive); + } + + [Fact] + public async Task StartMonitoringAsync_UsesFallbackPollingIntervalFromApplicationSettings() + { + using var monitor = CreateMonitor(new ApplicationSettingsModel + { + EnableWmiMonitoring = false, + EnableFallbackPolling = true, + FallbackPollingIntervalMs = 12345, + }); + var messages = new List(); + monitor.MonitoringStatusChanged += (_, status) => messages.Add(status.StatusMessage); + + await monitor.StartMonitoringAsync(); + + Assert.True(monitor.IsFallbackPollingActive); + Assert.Contains("Fallback polling started (interval: 12345ms)", messages); + } + + private static ProcessMonitorService CreateMonitor(ApplicationSettingsModel settings) + { + var processService = new Mock(MockBehavior.Strict); + processService.Setup(service => service.GetProcessesAsync()) + .ReturnsAsync(new System.Collections.ObjectModel.ObservableCollection()); + var settingsService = new Mock(MockBehavior.Strict); + settingsService.SetupGet(service => service.Settings).Returns(settings); + return new ProcessMonitorService( + processService.Object, + settingsService.Object, + NullLogger.Instance); + } + } +} diff --git a/ViewModels/ProcessPowerPlanAssociationViewModel.cs b/ViewModels/ProcessPowerPlanAssociationViewModel.cs index 46b4dde..7a8f32f 100644 --- a/ViewModels/ProcessPowerPlanAssociationViewModel.cs +++ b/ViewModels/ProcessPowerPlanAssociationViewModel.cs @@ -89,18 +89,6 @@ public partial class ProcessPowerPlanAssociationViewModel : BaseViewModel [ObservableProperty] private PowerPlanModel? defaultPowerPlan; - [ObservableProperty] - private bool isMonitoringEnabled = true; - - [ObservableProperty] - private bool isEventBasedMonitoringEnabled = true; - - [ObservableProperty] - private bool isFallbackPollingEnabled = true; - - [ObservableProperty] - private int pollingIntervalSeconds = 5; - [ObservableProperty] private bool preventDuplicatePowerPlanChanges = true; @@ -182,9 +170,6 @@ public async Task LoadDataAsync() // Load configuration settings var config = this.associationService.Configuration; - this.IsEventBasedMonitoringEnabled = config.IsEventBasedMonitoringEnabled; - this.IsFallbackPollingEnabled = config.IsFallbackPollingEnabled; - this.PollingIntervalSeconds = config.PollingIntervalSeconds; this.PreventDuplicatePowerPlanChanges = config.PreventDuplicatePowerPlanChanges; this.PowerPlanChangeDelayMs = config.PowerPlanChangeDelayMs; @@ -391,9 +376,6 @@ public async Task SaveConfigurationAsync() // Update configuration with current settings var config = this.associationService.Configuration; - config.IsEventBasedMonitoringEnabled = this.IsEventBasedMonitoringEnabled; - config.IsFallbackPollingEnabled = this.IsFallbackPollingEnabled; - config.PollingIntervalSeconds = this.PollingIntervalSeconds; config.PreventDuplicatePowerPlanChanges = this.PreventDuplicatePowerPlanChanges; config.PowerPlanChangeDelayMs = this.PowerPlanChangeDelayMs; diff --git a/Views/ProcessPowerPlanAssociationView.xaml b/Views/ProcessPowerPlanAssociationView.xaml index 47de0e7..78c68db 100644 --- a/Views/ProcessPowerPlanAssociationView.xaml +++ b/Views/ProcessPowerPlanAssociationView.xaml @@ -325,21 +325,6 @@ - - - - - - - - -