24729ee267
- Reindent appsettings.json and the MSBuild project files - Collapse single-statement guard blocks and tidy using and member order - Normalize XML doc indentation and file endings - Reorder BatteryMonitor test-data helpers and move the exclusion tests into their own region - Carry the same formatting through the boiler config, control, entity and test files
99 lines
3.9 KiB
C#
99 lines
3.9 KiB
C#
using FluentAssertions;
|
|
using PowerMonitorManager.Models;
|
|
using PowerMonitorManager.Models.Consts;
|
|
|
|
namespace PowerMonitor.Manager.Tests;
|
|
|
|
public class PowerMonitorOptionsTests
|
|
{
|
|
public static TheoryData<string> CollectionProperties =>
|
|
[
|
|
nameof(PowerMonitorOptions.ExcludedSensorPatterns),
|
|
nameof(PowerMonitorOptions.EnforceSwitchOffEntityIds),
|
|
nameof(PowerMonitorOptions.PauseAndRestoreEntityIds)
|
|
];
|
|
|
|
[Theory]
|
|
[InlineData(nameof(PowerMonitorOptions.Enabled), false)]
|
|
[InlineData(nameof(PowerMonitorOptions.MainMeterEntityId), "")]
|
|
[InlineData(nameof(PowerMonitorOptions.PollIntervalSeconds), PowerMonitorConsts.DefaultPollIntervalSeconds)]
|
|
[InlineData(nameof(PowerMonitorOptions.SmoothingWindowSize), PowerMonitorConsts.DefaultSmoothingWindowSize)]
|
|
[InlineData(nameof(PowerMonitorOptions.AnomalyThresholdWatts), PowerMonitorConsts.DefaultAnomalyThresholdWatts)]
|
|
[InlineData(nameof(PowerMonitorOptions.HighPowerThresholdWatts), PowerMonitorConsts.DefaultHighPowerThresholdWatts)]
|
|
[InlineData(nameof(PowerMonitorOptions.EnforceLimitEnabled), true)]
|
|
[InlineData(nameof(PowerMonitorOptions.PowerLimitWatts), PowerMonitorConsts.DefaultPowerLimitWatts)]
|
|
[InlineData(nameof(PowerMonitorOptions.RecoverySeconds), PowerMonitorConsts.DefaultRecoverySeconds)]
|
|
[InlineData(nameof(PowerMonitorOptions.LimitCooldownSeconds), PowerMonitorConsts.DefaultLimitCooldownSeconds)]
|
|
public void Defaults_ShouldExposeExpectedValue(string propertyName, object expected)
|
|
{
|
|
// Arrange
|
|
var options = new PowerMonitorOptions();
|
|
var property = typeof(PowerMonitorOptions).GetProperty(propertyName);
|
|
|
|
// Act
|
|
var actual = property!.GetValue(options);
|
|
|
|
// Assert
|
|
property.Should().NotBeNull($"PowerMonitorOptions.{propertyName} should be a public property");
|
|
actual.Should().Be(expected);
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(CollectionProperties))]
|
|
public void Defaults_ShouldExposeEmptyCollection(string propertyName)
|
|
{
|
|
// Arrange
|
|
var options = new PowerMonitorOptions();
|
|
|
|
// Act
|
|
var value = typeof(PowerMonitorOptions).GetProperty(propertyName)!.GetValue(options);
|
|
|
|
// Assert
|
|
value.Should().BeOfType<string[]>();
|
|
((string[])value).Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void Defaults_ShouldNotShareCollectionInstancesBetweenOptionObjects()
|
|
{
|
|
// Arrange - two option objects must not mutate each other's array contents.
|
|
var first = new PowerMonitorOptions();
|
|
var second = new PowerMonitorOptions();
|
|
|
|
// Act
|
|
first.ExcludedSensorPatterns = ["sensor.custom_*"];
|
|
first.EnforceSwitchOffEntityIds = ["switch.custom"];
|
|
first.PauseAndRestoreEntityIds = ["switch.custom_monitor"];
|
|
|
|
// Assert
|
|
first.ExcludedSensorPatterns.Should().NotBeSameAs(second.ExcludedSensorPatterns);
|
|
first.EnforceSwitchOffEntityIds.Should().NotBeSameAs(second.EnforceSwitchOffEntityIds);
|
|
first.PauseAndRestoreEntityIds.Should().NotBeSameAs(second.PauseAndRestoreEntityIds);
|
|
second.ExcludedSensorPatterns.Should().BeEmpty();
|
|
second.EnforceSwitchOffEntityIds.Should().BeEmpty();
|
|
second.PauseAndRestoreEntityIds.Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void Options_ShouldAcceptConfiguredOverrides()
|
|
{
|
|
// Arrange - appsettings.json overrides these values, so the setters must stay writable.
|
|
var options = new PowerMonitorOptions
|
|
{
|
|
PollIntervalSeconds = 1,
|
|
AnomalyThresholdWatts = 250.0,
|
|
HighPowerThresholdWatts = 5_500.0
|
|
};
|
|
|
|
// Assert
|
|
options.PollIntervalSeconds.Should().Be(1);
|
|
options.AnomalyThresholdWatts.Should().Be(250.0);
|
|
options.HighPowerThresholdWatts.Should().Be(5_500.0);
|
|
}
|
|
|
|
[Fact]
|
|
public void SectionName_ShouldBePowerMonitor()
|
|
{
|
|
PowerMonitorOptions.SectionName.Should().Be("PowerMonitor");
|
|
}
|
|
} |