Files
ThreadPilot/Tests/ThreadPilot.Core.Tests/PerformanceViewModelDiagnosticsTests.cs
T
Prime Build 266834cbdf Reduce Performance tab to optional diagnostics (#17)
Reduce Performance tab to optional diagnostics

Hide the Performance/Diagnostics surface from the primary UX by default and remove hidden startup/tray runtime cost.

- Hide Diagnostics from primary navigation and tray by default
- Lazy-create PerformanceViewModel only when Diagnostics is opened
- Stop initializing diagnostics snapshots during app startup
- Gate tray CPU/RAM metrics behind the advanced diagnostics flag
- Avoid starting the tray lightweight metrics timer when diagnostics are hidden
- Lazily allocate PerformanceMonitoringService counters on first metrics use
- Stop live diagnostics when inactive, minimized or tray-hidden
- Preserve diagnostics internals for future selected-process summary work
- Add tests for lazy diagnostics creation, tray metrics gating and explicit diagnostics activation

No Process tab redesign, CPU affinity changes, memory priority changes, persistent rules changes, version bump or tag changes.
2026-05-21 23:47:21 +02:00

121 lines
5.2 KiB
C#

namespace ThreadPilot.Core.Tests
{
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using ThreadPilot.Models;
using ThreadPilot.Services;
using ThreadPilot.ViewModels;
public sealed class PerformanceViewModelDiagnosticsTests
{
[Fact]
public async Task InitializeAsync_DoesNotStartLiveMonitoringOrScanProcesses()
{
var harness = new Harness();
var viewModel = harness.CreateViewModel();
await viewModel.InitializeAsync();
harness.Performance.Verify(x => x.StartMonitoringAsync(), Times.Never);
harness.Performance.Verify(x => x.GetSystemMetricsAsync(It.IsAny<bool>()), Times.Never);
harness.Performance.Verify(x => x.GetTopCpuProcessesAsync(It.IsAny<int>()), Times.Never);
harness.Performance.Verify(x => x.GetTopMemoryProcessesAsync(It.IsAny<int>()), Times.Never);
harness.PowerPlan.Verify(x => x.GetActivePowerPlan(), Times.Never);
}
[Fact]
public async Task ActivateDiagnosticsAsync_LoadsSnapshotWithoutStartingLiveMonitoring()
{
var harness = new Harness();
var viewModel = harness.CreateViewModel();
await viewModel.ActivateDiagnosticsAsync();
harness.Performance.Verify(x => x.GetSystemMetricsAsync(false), Times.Once);
harness.Performance.Verify(x => x.GetHistoricalDataAsync(TimeSpan.FromHours(1)), Times.Once);
harness.Performance.Verify(x => x.GetTopCpuProcessesAsync(25), Times.Once);
harness.Performance.Verify(x => x.GetTopMemoryProcessesAsync(25), Times.Once);
harness.PowerPlan.Verify(x => x.GetActivePowerPlan(), Times.Once);
harness.Performance.Verify(x => x.StartMonitoringAsync(), Times.Never);
Assert.False(viewModel.IsMonitoring);
}
[Fact]
public async Task SuspendBackgroundMonitoringAsync_StopsLiveMonitoringAndDoesNotAutoResume()
{
var harness = new Harness();
var viewModel = harness.CreateViewModel();
await viewModel.StartMonitoringCommand.ExecuteAsync(null);
await viewModel.SuspendBackgroundMonitoringAsync();
await viewModel.ResumeBackgroundMonitoringAsync();
harness.Performance.Verify(x => x.StartMonitoringAsync(), Times.Once);
harness.Performance.Verify(x => x.StopMonitoringAsync(), Times.Once);
Assert.False(viewModel.IsMonitoring);
Assert.Equal("Stopped", viewModel.MonitoringStateText);
}
[Fact]
public void ShowAdvancedDiagnostics_DefaultsToHidden()
{
Assert.False(AppNavigationOptions.ShowAdvancedDiagnostics);
}
private sealed class Harness
{
public Mock<IPerformanceMonitoringService> Performance { get; } = new(MockBehavior.Strict);
public Mock<IProcessService> Process { get; } = new(MockBehavior.Strict);
public Mock<IProcessPowerPlanAssociationService> Associations { get; } = new(MockBehavior.Strict);
public Mock<IPowerPlanService> PowerPlan { get; } = new(MockBehavior.Strict);
public Mock<IProcessMonitorManagerService> ProcessMonitorManager { get; } = new(MockBehavior.Strict);
public Mock<ISystemTweaksService> SystemTweaks { get; } = new(MockBehavior.Strict);
public Harness()
{
this.Performance
.Setup(x => x.GetSystemMetricsAsync(It.IsAny<bool>()))
.ReturnsAsync(new SystemPerformanceMetrics());
this.Performance
.Setup(x => x.GetHistoricalDataAsync(It.IsAny<TimeSpan>()))
.ReturnsAsync(new List<SystemPerformanceMetrics>());
this.Performance
.Setup(x => x.GetTopCpuProcessesAsync(It.IsAny<int>()))
.ReturnsAsync(new List<ProcessPerformanceInfo>());
this.Performance
.Setup(x => x.GetTopMemoryProcessesAsync(It.IsAny<int>()))
.ReturnsAsync(new List<ProcessPerformanceInfo>());
this.Performance
.Setup(x => x.StartMonitoringAsync())
.Returns(Task.CompletedTask);
this.Performance
.Setup(x => x.StopMonitoringAsync())
.Returns(Task.CompletedTask);
this.Associations
.Setup(x => x.GetAssociationsAsync())
.ReturnsAsync(Array.Empty<ProcessPowerPlanAssociation>());
this.PowerPlan
.Setup(x => x.GetActivePowerPlan())
.ReturnsAsync(new PowerPlanModel { Guid = "balanced", Name = "Balanced" });
}
public PerformanceViewModel CreateViewModel() =>
new(
this.Performance.Object,
this.Process.Object,
this.Associations.Object,
this.PowerPlan.Object,
this.ProcessMonitorManager.Object,
this.SystemTweaks.Object,
NullLogger<PerformanceViewModel>.Instance);
}
}
}