From 638c1ef6d1a66d7b788ffa7189b6ea441098ccef Mon Sep 17 00:00:00 2001 From: Marius Date: Thu, 19 Mar 2026 16:21:49 +0200 Subject: [PATCH] test: add size regression guard for compact NordPool price serialization Made-with: Cursor --- .../CompactNordPoolDayPriceTests.cs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/NordPool.Manager.Tests/CompactNordPoolDayPriceTests.cs b/NordPool.Manager.Tests/CompactNordPoolDayPriceTests.cs index e7adc41..7c84aea 100644 --- a/NordPool.Manager.Tests/CompactNordPoolDayPriceTests.cs +++ b/NordPool.Manager.Tests/CompactNordPoolDayPriceTests.cs @@ -188,4 +188,38 @@ public class CompactNordPoolDayPriceTests result.Should().BeEmpty(); } + + [Fact] + public void SerializedSize_WithMaxEntries_ShouldBeUnderHaAttributeLimit() + { + const int maxEntries = 192; + const int haAttributeByteLimitBytes = 16_384; + var dayStart = new DateTime(2026, 3, 19, 0, 0, 0, DateTimeKind.Utc); + var prices = new List(); + + for (var i = 0; i < maxEntries; i++) + { + var start = dayStart.AddMinutes(i * MinutesPerInterval); + prices.Add(new NordPoolDayPrice + { + Start = start, + End = start.AddMinutes(MinutesPerInterval), + Price = 10.0 + i * 0.01 + }); + } + + var compactPrices = prices.ToCompact(); + var attributePayload = new + { + prices = compactPrices, + prices_count = compactPrices.Count, + friendly_name = "NordPool today and tomorrow prices", + last_updated = DateTime.UtcNow.ToString("O") + }; + var json = JsonSerializer.Serialize(attributePayload); + var byteCount = System.Text.Encoding.UTF8.GetByteCount(json); + + byteCount.Should().BeLessThan(haAttributeByteLimitBytes, + $"serialized attribute payload is {byteCount} bytes, exceeding HA's {haAttributeByteLimitBytes}-byte limit"); + } }