|
| 1 | +namespace ThreadPilot.Core.Tests |
| 2 | +{ |
| 3 | + using Microsoft.Extensions.Logging.Abstractions; |
| 4 | + using Moq; |
| 5 | + using ThreadPilot.Models; |
| 6 | + using ThreadPilot.Services; |
| 7 | + using ThreadPilot.ViewModels; |
| 8 | + |
| 9 | + public sealed class PerformanceViewModelDiagnosticsTests |
| 10 | + { |
| 11 | + [Fact] |
| 12 | + public async Task InitializeAsync_DoesNotStartLiveMonitoringOrScanProcesses() |
| 13 | + { |
| 14 | + var harness = new Harness(); |
| 15 | + var viewModel = harness.CreateViewModel(); |
| 16 | + |
| 17 | + await viewModel.InitializeAsync(); |
| 18 | + |
| 19 | + harness.Performance.Verify(x => x.StartMonitoringAsync(), Times.Never); |
| 20 | + harness.Performance.Verify(x => x.GetSystemMetricsAsync(It.IsAny<bool>()), Times.Never); |
| 21 | + harness.Performance.Verify(x => x.GetTopCpuProcessesAsync(It.IsAny<int>()), Times.Never); |
| 22 | + harness.Performance.Verify(x => x.GetTopMemoryProcessesAsync(It.IsAny<int>()), Times.Never); |
| 23 | + harness.PowerPlan.Verify(x => x.GetActivePowerPlan(), Times.Never); |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public async Task ActivateDiagnosticsAsync_LoadsSnapshotWithoutStartingLiveMonitoring() |
| 28 | + { |
| 29 | + var harness = new Harness(); |
| 30 | + var viewModel = harness.CreateViewModel(); |
| 31 | + |
| 32 | + await viewModel.ActivateDiagnosticsAsync(); |
| 33 | + |
| 34 | + harness.Performance.Verify(x => x.GetSystemMetricsAsync(false), Times.Once); |
| 35 | + harness.Performance.Verify(x => x.GetHistoricalDataAsync(TimeSpan.FromHours(1)), Times.Once); |
| 36 | + harness.Performance.Verify(x => x.GetTopCpuProcessesAsync(25), Times.Once); |
| 37 | + harness.Performance.Verify(x => x.GetTopMemoryProcessesAsync(25), Times.Once); |
| 38 | + harness.PowerPlan.Verify(x => x.GetActivePowerPlan(), Times.Once); |
| 39 | + harness.Performance.Verify(x => x.StartMonitoringAsync(), Times.Never); |
| 40 | + Assert.False(viewModel.IsMonitoring); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public async Task SuspendBackgroundMonitoringAsync_StopsLiveMonitoringAndDoesNotAutoResume() |
| 45 | + { |
| 46 | + var harness = new Harness(); |
| 47 | + var viewModel = harness.CreateViewModel(); |
| 48 | + |
| 49 | + await viewModel.StartMonitoringCommand.ExecuteAsync(null); |
| 50 | + await viewModel.SuspendBackgroundMonitoringAsync(); |
| 51 | + await viewModel.ResumeBackgroundMonitoringAsync(); |
| 52 | + |
| 53 | + harness.Performance.Verify(x => x.StartMonitoringAsync(), Times.Once); |
| 54 | + harness.Performance.Verify(x => x.StopMonitoringAsync(), Times.Once); |
| 55 | + Assert.False(viewModel.IsMonitoring); |
| 56 | + Assert.Equal("Stopped", viewModel.MonitoringStateText); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void ShowAdvancedDiagnostics_DefaultsToHidden() |
| 61 | + { |
| 62 | + Assert.False(AppNavigationOptions.ShowAdvancedDiagnostics); |
| 63 | + } |
| 64 | + |
| 65 | + private sealed class Harness |
| 66 | + { |
| 67 | + public Mock<IPerformanceMonitoringService> Performance { get; } = new(MockBehavior.Strict); |
| 68 | + |
| 69 | + public Mock<IProcessService> Process { get; } = new(MockBehavior.Strict); |
| 70 | + |
| 71 | + public Mock<IProcessPowerPlanAssociationService> Associations { get; } = new(MockBehavior.Strict); |
| 72 | + |
| 73 | + public Mock<IPowerPlanService> PowerPlan { get; } = new(MockBehavior.Strict); |
| 74 | + |
| 75 | + public Mock<IProcessMonitorManagerService> ProcessMonitorManager { get; } = new(MockBehavior.Strict); |
| 76 | + |
| 77 | + public Mock<ISystemTweaksService> SystemTweaks { get; } = new(MockBehavior.Strict); |
| 78 | + |
| 79 | + public Harness() |
| 80 | + { |
| 81 | + this.Performance |
| 82 | + .Setup(x => x.GetSystemMetricsAsync(It.IsAny<bool>())) |
| 83 | + .ReturnsAsync(new SystemPerformanceMetrics()); |
| 84 | + this.Performance |
| 85 | + .Setup(x => x.GetHistoricalDataAsync(It.IsAny<TimeSpan>())) |
| 86 | + .ReturnsAsync(new List<SystemPerformanceMetrics>()); |
| 87 | + this.Performance |
| 88 | + .Setup(x => x.GetTopCpuProcessesAsync(It.IsAny<int>())) |
| 89 | + .ReturnsAsync(new List<ProcessPerformanceInfo>()); |
| 90 | + this.Performance |
| 91 | + .Setup(x => x.GetTopMemoryProcessesAsync(It.IsAny<int>())) |
| 92 | + .ReturnsAsync(new List<ProcessPerformanceInfo>()); |
| 93 | + this.Performance |
| 94 | + .Setup(x => x.StartMonitoringAsync()) |
| 95 | + .Returns(Task.CompletedTask); |
| 96 | + this.Performance |
| 97 | + .Setup(x => x.StopMonitoringAsync()) |
| 98 | + .Returns(Task.CompletedTask); |
| 99 | + |
| 100 | + this.Associations |
| 101 | + .Setup(x => x.GetAssociationsAsync()) |
| 102 | + .ReturnsAsync(Array.Empty<ProcessPowerPlanAssociation>()); |
| 103 | + |
| 104 | + this.PowerPlan |
| 105 | + .Setup(x => x.GetActivePowerPlan()) |
| 106 | + .ReturnsAsync(new PowerPlanModel { Guid = "balanced", Name = "Balanced" }); |
| 107 | + } |
| 108 | + |
| 109 | + public PerformanceViewModel CreateViewModel() => |
| 110 | + new( |
| 111 | + this.Performance.Object, |
| 112 | + this.Process.Object, |
| 113 | + this.Associations.Object, |
| 114 | + this.PowerPlan.Object, |
| 115 | + this.ProcessMonitorManager.Object, |
| 116 | + this.SystemTweaks.Object, |
| 117 | + NullLogger<PerformanceViewModel>.Instance); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments