Files
Kool 27e7b04913 Harden persistence, monitoring, and shutdown reliability (#46)
Addresses the audit findings across persistent-rule recovery, settings merge behavior, CPU identity, monitoring lifecycle, notifications, autostart, tray updates, and shutdown disposal. Includes regression coverage and the corrected disabled-monitoring view. CI DevSecOps and CodeQL pass.
2026-08-12 01:00:17 +02:00

136 lines
5.4 KiB
C#

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<IEnumerable<PowerPlanModel>>(), It.IsAny<PowerPlanModel?>()), Times.Once);
harness.Tray.Verify(x => x.UpdateProfiles(It.IsAny<IEnumerable<string>>()), Times.Once);
harness.Tray.Verify(x => x.UpdateSystemStatus("Balanced"), Times.Once);
harness.Tray.Verify(x => x.UpdateSystemStatus(It.IsAny<string>(), It.IsAny<double>(), It.IsAny<double>()), 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<IEnumerable<PowerPlanModel>>(), It.IsAny<PowerPlanModel?>()))
.Callback(() =>
{
if (!insideDispatcher)
{
undispatchedTrayCalls++;
}
});
harness.Tray
.Setup(x => x.UpdateProfiles(It.IsAny<IEnumerable<string>>()))
.Callback(() =>
{
if (!insideDispatcher)
{
undispatchedTrayCalls++;
}
});
harness.Tray
.Setup(x => x.UpdateSystemStatus(It.IsAny<string>()))
.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<string>(), It.IsAny<double>(), It.IsAny<double>()), Times.Never);
}
private sealed class Harness
{
public static Task PassThroughDispatcher(Action action)
{
action();
return Task.CompletedTask;
}
public Mock<ISystemTrayService> Tray { get; } = new(MockBehavior.Strict);
public Mock<IPowerPlanService> 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<PowerPlanModel> { activePlan });
this.PowerPlan
.Setup(x => x.GetActivePowerPlan())
.ReturnsAsync(activePlan);
this.Tray
.Setup(x => x.UpdatePowerPlans(It.IsAny<IEnumerable<PowerPlanModel>>(), It.IsAny<PowerPlanModel?>()));
this.Tray
.Setup(x => x.UpdateProfiles(It.IsAny<IEnumerable<string>>()));
this.Tray
.Setup(x => x.UpdateSystemStatus(It.IsAny<string>()));
}
public SystemTrayStatusUpdater CreateUpdater(Func<IPerformanceMonitoringService> performanceFactory) =>
new(
this.PowerPlan.Object,
new Lazy<IPerformanceMonitoringService>(performanceFactory));
}
}
}