Files
HomeAssistant/PowerMonitor.Manager.Tests/PowerMonitorOptionsTests.cs
T
gitea_admin f919d6b25b Apply code formatting cleanup across the monitor projects
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests
- Expand single-statement guards and loops into braced blocks
- Realign wrapped method arguments, chained calls and continuations
- Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty`
- Flatten the boiler config expression and reindent the remaining touched files
2026-12-01 12:33:31 +02:00

103 lines
4.0 KiB
C#

#region
using FluentAssertions;
using PowerMonitorManager.Models;
using PowerMonitorManager.Models.Consts;
#endregion
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");
}
}