namespace ThreadPilot.Core.Tests { using System.Collections.ObjectModel; using Moq; using ThreadPilot.Models; using ThreadPilot.Services; public sealed class SystemTrayStatusUpdaterTests { [Fact] public async Task UpdateContextMenuAsync_DiagnosticsHidden_DoesNotResolvePerformanceService() { var harness = new Harness(); var updater = harness.CreateUpdater(performanceFactory: () => throw new InvalidOperationException("Performance service should not be resolved.")); await updater.UpdateContextMenuAsync(harness.Tray.Object, Harness.PassThroughDispatcher); harness.Tray.Verify(x => x.UpdatePowerPlans(It.IsAny>(), It.IsAny()), Times.Once); harness.Tray.Verify(x => x.UpdateProfiles(It.IsAny>()), Times.Once); harness.Tray.Verify(x => x.UpdateSystemStatus("Balanced"), Times.Once); harness.Tray.Verify(x => x.UpdateSystemStatus(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } [Fact] public async Task UpdateContextMenuAsync_MarshalsEveryTrayMutationThroughTheDispatcher() { var harness = new Harness(); var updater = harness.CreateUpdater(performanceFactory: () => throw new InvalidOperationException("Performance service should not be resolved.")); var dispatchedCallCount = 0; var undispatchedTrayCalls = 0; var insideDispatcher = false; harness.Tray .Setup(x => x.UpdatePowerPlans(It.IsAny>(), It.IsAny())) .Callback(() => { if (!insideDispatcher) { undispatchedTrayCalls++; } }); harness.Tray .Setup(x => x.UpdateProfiles(It.IsAny>())) .Callback(() => { if (!insideDispatcher) { undispatchedTrayCalls++; } }); harness.Tray .Setup(x => x.UpdateSystemStatus(It.IsAny())) .Callback(() => { if (!insideDispatcher) { undispatchedTrayCalls++; } }); await updater.UpdateContextMenuAsync(harness.Tray.Object, action => { dispatchedCallCount++; insideDispatcher = true; try { action(); } finally { insideDispatcher = false; } return Task.CompletedTask; }); Assert.Equal(0, undispatchedTrayCalls); Assert.Equal(3, dispatchedCallCount); } [Fact] public async Task UpdateStatusAsync_DiagnosticsHidden_DoesNotRequestLightweightMetrics() { var harness = new Harness(); var updater = harness.CreateUpdater(performanceFactory: () => throw new InvalidOperationException("Performance service should not be resolved.")); var updated = await updater.UpdateStatusAsync(harness.Tray.Object, action => { action(); return Task.CompletedTask; }); Assert.True(updated); Assert.False(updater.ShouldRunPerformanceStatusUpdates); harness.Tray.Verify(x => x.UpdateSystemStatus("Balanced"), Times.Once); harness.Tray.Verify(x => x.UpdateSystemStatus(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); } private sealed class Harness { public static Task PassThroughDispatcher(Action action) { action(); return Task.CompletedTask; } public Mock Tray { get; } = new(MockBehavior.Strict); public Mock PowerPlan { get; } = new(MockBehavior.Strict); public Harness() { var activePlan = new PowerPlanModel { Guid = "balanced", Name = "Balanced", IsActive = true }; this.PowerPlan .Setup(x => x.GetPowerPlansAsync()) .ReturnsAsync(new ObservableCollection { activePlan }); this.PowerPlan .Setup(x => x.GetActivePowerPlan()) .ReturnsAsync(activePlan); this.Tray .Setup(x => x.UpdatePowerPlans(It.IsAny>(), It.IsAny())); this.Tray .Setup(x => x.UpdateProfiles(It.IsAny>())); this.Tray .Setup(x => x.UpdateSystemStatus(It.IsAny())); } public SystemTrayStatusUpdater CreateUpdater(Func performanceFactory) => new( this.PowerPlan.Object, new Lazy(performanceFactory)); } } }