Files
HomeAssistant/PowerMonitor.Manager.Tests/PowerMonitorOptionsTests.cs
T
gitea_admin cf8c9434c8 Clear the residual Qodana findings
- Drop the leftover unused using, parameter and date-time attribute reader
- Merge the remaining enum guard into its pattern
- Make the NordPool price DTO init-only
- Replace the four non-serializable theory matrices with single tests that
  iterate the same rows, so xUnit can still report them
- Replace Array.Empty with collection expressions and drop unused locals,
  redundant default arguments and duplicated tuple names
- Throw from the unreachable shower default instead of an empty section
- Cover the configurable PowerMonitor option overrides in a test so the
  init setters have a caller
2026-11-29 21:49:21 +02:00

100 lines
3.9 KiB
C#

using FluentAssertions;
using PowerMonitorManager.Models;
using PowerMonitorManager.Models.Consts;
namespace PowerMonitor.Manager.Tests;
public class PowerMonitorOptionsTests
{
[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);
}
public static TheoryData<string> CollectionProperties =>
[
nameof(PowerMonitorOptions.ExcludedSensorPatterns),
nameof(PowerMonitorOptions.EnforceSwitchOffEntityIds),
nameof(PowerMonitorOptions.PauseAndRestoreEntityIds)
];
[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");
}
}