test: add size regression guard for compact NordPool price serialization
Made-with: Cursor
This commit is contained in:
@@ -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<NordPoolDayPrice>();
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user