From 411611215a74ae24a4bf3f7758374b27fcae34d8 Mon Sep 17 00:00:00 2001 From: Marius Date: Thu, 19 Mar 2026 16:27:37 +0200 Subject: [PATCH] test: add compact format verification and size limit tests for NordPool entity attributes Made-with: Cursor --- .../NordPoolEntityServiceTests.cs | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/NordPool.Manager.Tests/NordPoolEntityServiceTests.cs b/NordPool.Manager.Tests/NordPoolEntityServiceTests.cs index 15aa1ec..104e610 100644 --- a/NordPool.Manager.Tests/NordPoolEntityServiceTests.cs +++ b/NordPool.Manager.Tests/NordPoolEntityServiceTests.cs @@ -532,5 +532,82 @@ public class NordPoolEntityServiceTests Times.Once); } + [Fact] + public async Task UpdateEntitiesAsync_TodayTomorrowPrices_ShouldUseCompactFormat() + { + // Arrange + var todayPrice = new NordPoolPrice + { + Prices = [new NordPoolDayPrice + { + Start = new DateTime(2026, 3, 19, 10, 0, 0, DateTimeKind.Utc), + End = new DateTime(2026, 3, 19, 10, 15, 0, DateTimeKind.Utc), + Price = 12.0 + }] + }; + var context = CreateDefaultContext(todayPrice: todayPrice, tomorrowPrice: new NordPoolPrice { Prices = [] }); + + _entityManagerMock.Setup(em => em.SetEntityState(It.IsAny(), It.IsAny())) + .Returns(Task.CompletedTask); + _entityManagerMock.Setup(em => em.SetEntityAttributes(It.IsAny(), It.IsAny())) + .Returns(Task.CompletedTask); + + // Act + await _sut.UpdateEntitiesAsync(context); + + // Assert + _entityManagerMock.Verify( + em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.Is(o => + o.GetType().GetProperty("prices")!.GetValue(o) is List)), + Times.Once); + } + + [Fact] + public async Task UpdateEntitiesAsync_WithFullDayPrices_AttributesShouldBeUnderSizeLimit() + { + // Arrange + const int intervalsPerDay = 96; + const int haAttributeByteLimitBytes = 16_384; + var todayStart = new DateTime(2026, 3, 19, 0, 0, 0, DateTimeKind.Utc); + var tomorrowStart = new DateTime(2026, 3, 20, 0, 0, 0, DateTimeKind.Utc); + + var todayPrices = Enumerable.Range(0, intervalsPerDay).Select(i => new NordPoolDayPrice + { + Start = todayStart.AddMinutes(i * NordPoolConsts.MinutesPerInterval), + End = todayStart.AddMinutes((i + 1) * NordPoolConsts.MinutesPerInterval), + Price = 5.0 + i * 0.1 + }).ToList(); + + var tomorrowPrices = Enumerable.Range(0, intervalsPerDay).Select(i => new NordPoolDayPrice + { + Start = tomorrowStart.AddMinutes(i * NordPoolConsts.MinutesPerInterval), + End = tomorrowStart.AddMinutes((i + 1) * NordPoolConsts.MinutesPerInterval), + Price = 8.0 + i * 0.1 + }).ToList(); + + object? capturedAttributes = null; + _entityManagerMock.Setup(em => em.SetEntityState(It.IsAny(), It.IsAny())) + .Returns(Task.CompletedTask); + _entityManagerMock.Setup(em => em.SetEntityAttributes("sensor.nordpool_today_tomorrow_prices", It.IsAny())) + .Callback((_, attrs) => capturedAttributes = attrs) + .Returns(Task.CompletedTask); + _entityManagerMock.Setup(em => em.SetEntityAttributes(It.Is(id => id != "sensor.nordpool_today_tomorrow_prices"), It.IsAny())) + .Returns(Task.CompletedTask); + + var context = CreateDefaultContext( + todayPrice: new NordPoolPrice { Prices = todayPrices }, + tomorrowPrice: new NordPoolPrice { Prices = tomorrowPrices }); + + // Act + await _sut.UpdateEntitiesAsync(context); + + // Assert + capturedAttributes.Should().NotBeNull(); + var json = System.Text.Json.JsonSerializer.Serialize(capturedAttributes); + var byteCount = System.Text.Encoding.UTF8.GetByteCount(json); + byteCount.Should().BeLessThan(haAttributeByteLimitBytes, + $"today+tomorrow attribute payload is {byteCount} bytes, exceeding HA's {haAttributeByteLimitBytes}-byte limit"); + } + #endregion }