using MeterVault.Core.Costing; using MeterVault.Core.Domain; namespace MeterVault.Core.Tests; public sealed class TariffResolverTests { private static Tariff Unit(double value, TariffScope scope, int? scopeId, DateOnly from, DateOnly? to = null) => new() { Component = TariffComponent.UnitPrice, Value = value, Unit = "EUR/kWh", ScopeType = scope, ScopeId = scopeId, ValidFrom = from, ValidTo = to, }; [Fact] public void Picks_the_price_valid_for_the_date() { // Electricity price history from the Strom sheet. var tariffs = new[] { Unit(0.16, TariffScope.EnergyType, 1, new DateOnly(2022, 9, 1), new DateOnly(2022, 12, 31)), Unit(0.44, TariffScope.EnergyType, 1, new DateOnly(2023, 1, 1), new DateOnly(2023, 4, 30)), Unit(0.37, TariffScope.EnergyType, 1, new DateOnly(2023, 5, 1)), }; Assert.Equal(0.16, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 5, 1, new DateOnly(2022, 10, 15))); Assert.Equal(0.44, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 5, 1, new DateOnly(2023, 2, 15))); Assert.Equal(0.37, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 5, 1, new DateOnly(2024, 6, 15))); } [Fact] public void Meter_scope_overrides_energy_type_and_global() { var tariffs = new[] { Unit(0.30, TariffScope.Global, null, new DateOnly(2023, 1, 1)), Unit(0.40, TariffScope.EnergyType, 1, new DateOnly(2023, 1, 1)), Unit(0.50, TariffScope.Meter, 7, new DateOnly(2023, 1, 1)), }; Assert.Equal(0.50, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 7, 1, new DateOnly(2023, 6, 1))); Assert.Equal(0.40, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 8, 1, new DateOnly(2023, 6, 1))); Assert.Equal(0.30, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 8, 2, new DateOnly(2023, 6, 1))); } [Fact] public void Returns_zero_when_nothing_applies() { var tariffs = new[] { Unit(0.25, TariffScope.EnergyType, 1, new DateOnly(2024, 1, 1)) }; Assert.Equal(0d, TariffResolver.ResolveValue(tariffs, TariffComponent.UnitPrice, 1, 1, new DateOnly(2023, 1, 1))); } }