Files
HomeAssistant/PowerMonitor.Manager.Tests/PowerMonitorOptionsTests.cs
T
gitea_admin 5777e641f6 Add shared test data builders and Qodana configuration
- Extract per-monitor *TestData helper classes and rework tests to use them
- Add boiler run-time tracker abstraction (implementation pending)
- Drop unused NordPool init/update-result types and stale PLAN.md
- Add qodana.yaml with the inspection set to keep the analysis focused
2026-11-29 21:30:13 +02:00

92 lines
3.7 KiB
C#

using FluentAssertions;
using PowerMonitorManager.Models;
using PowerMonitorManager.Models.Consts;
namespace PowerMonitor.Manager.Tests;
public class PowerMonitorOptionsTests
{
/// <summary>
/// Expected defaults for every public, configurable option. Only public properties are
/// inspected here - no private state is read through reflection.
/// </summary>
public static TheoryData<string, object> ExpectedDefaults => new()
{
{ nameof(PowerMonitorOptions.Enabled), false },
{ nameof(PowerMonitorOptions.MainMeterEntityId), string.Empty },
{ nameof(PowerMonitorOptions.PollIntervalSeconds), PowerMonitorConsts.DefaultPollIntervalSeconds },
{ nameof(PowerMonitorOptions.SmoothingWindowSize), PowerMonitorConsts.DefaultSmoothingWindowSize },
{ nameof(PowerMonitorOptions.AnomalyThresholdWatts), PowerMonitorConsts.DefaultAnomalyThresholdWatts },
{ nameof(PowerMonitorOptions.HighPowerThresholdWatts), PowerMonitorConsts.DefaultHighPowerThresholdWatts },
{ nameof(PowerMonitorOptions.EnforceLimitEnabled), true },
{ nameof(PowerMonitorOptions.PowerLimitWatts), PowerMonitorConsts.DefaultPowerLimitWatts },
{ nameof(PowerMonitorOptions.RecoverySeconds), PowerMonitorConsts.DefaultRecoverySeconds },
{ nameof(PowerMonitorOptions.LimitCooldownSeconds), PowerMonitorConsts.DefaultLimitCooldownSeconds }
};
[Theory]
[MemberData(nameof(ExpectedDefaults))]
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 => new()
{
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 SectionName_ShouldBePowerMonitor()
{
PowerMonitorOptions.SectionName.Should().Be("PowerMonitor");
}
}