Remove the MCP server and split NordPool today/tomorrow fetch decisions
- Drop the HomeAssistant.McpServer wiring from opencode.json along with the Cursor live-validation hook, rule and skill; update AGENTS.md and README - Rename ShouldFetchNewPrices to ShouldFetchTodaysPrices and add a separate ShouldFetchTomorrowPrice that waits until the CET publish hour (TomorrowPricesPublishHour) - Inject TimeProvider into NordPoolPriceService, use the CET zone for the current date and record fetch timestamps from it - Rework the NordPool tests around FakeTimeProvider and cover fetching only today before the publish hour
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Text.Json;
|
||||
using AutoMapper;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Time.Testing;
|
||||
using Moq;
|
||||
using NordPool.Models;
|
||||
using NordPool.Models.Dtos;
|
||||
@@ -18,13 +19,16 @@ public class NordPoolPriceServiceTests
|
||||
{
|
||||
private readonly Mock<IMapper> _mapperMock;
|
||||
private readonly NordPoolPriceService _sut;
|
||||
private readonly FakeTimeProvider _timeProvider;
|
||||
|
||||
public NordPoolPriceServiceTests()
|
||||
{
|
||||
_mapperMock = new Mock<IMapper>();
|
||||
var loggerMock = new Mock<ILogger<NordPoolPriceService>>();
|
||||
|
||||
_sut = new NordPoolPriceService(_mapperMock.Object, loggerMock.Object);
|
||||
// 2026-12-01 10:00 UTC = 11:00 CET — before tomorrow's prices are published
|
||||
_timeProvider = new FakeTimeProvider(new DateTimeOffset(2026, 12, 1, 10, 0, 0, TimeSpan.Zero));
|
||||
_sut = new NordPoolPriceService(_mapperMock.Object, loggerMock.Object, _timeProvider);
|
||||
}
|
||||
|
||||
#region GetTimeQuarterIndex
|
||||
@@ -542,7 +546,7 @@ public class NordPoolPriceServiceTests
|
||||
public void UpdateCurrentPrice_WhenSameQuarterIndex_ShouldReturnCachedPrice()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndex = NordPoolPriceService.GetTimeQuarterIndex(DateTime.UtcNow);
|
||||
var expectedIndex = NordPoolPriceService.GetTimeQuarterIndex(_timeProvider.GetUtcNow().UtcDateTime);
|
||||
|
||||
var prices = new List<NordPoolDayPrice>();
|
||||
|
||||
@@ -616,7 +620,7 @@ public class NordPoolPriceServiceTests
|
||||
public void UpdateCurrentPrice_WhenCurrentQuarterHasPrice_ShouldStoreThatPrice(double expectedPrice)
|
||||
{
|
||||
// Arrange — negative and zero spot prices are valid NordPool scenarios
|
||||
var index = NordPoolPriceService.GetTimeQuarterIndex(DateTime.UtcNow);
|
||||
var index = NordPoolPriceService.GetTimeQuarterIndex(_timeProvider.GetUtcNow().UtcDateTime);
|
||||
|
||||
var prices = NordPoolTestData.CreateIntervals(
|
||||
NordPoolConsts.MaxDailyIntervals,
|
||||
@@ -638,7 +642,7 @@ public class NordPoolPriceServiceTests
|
||||
public void UpdateCurrentPrice_WhenCurrentIndexIsOutsidePricesRange_ShouldKeepPreviousPriceButAdvanceIndex()
|
||||
{
|
||||
// Arrange — fewer prices than the current quarter index (e.g. partial day payload)
|
||||
var index = NordPoolPriceService.GetTimeQuarterIndex(DateTime.UtcNow);
|
||||
var index = NordPoolPriceService.GetTimeQuarterIndex(_timeProvider.GetUtcNow().UtcDateTime);
|
||||
|
||||
var prices = Enumerable.Range(0, index)
|
||||
.Select(i => new NordPoolDayPrice { Price = 10.0 + i })
|
||||
@@ -660,7 +664,7 @@ public class NordPoolPriceServiceTests
|
||||
public void UpdateCurrentPrice_WhenQuarterIndexUnchangedButPricesChanged_ShouldKeepCachedPrice()
|
||||
{
|
||||
// Arrange
|
||||
var index = NordPoolPriceService.GetTimeQuarterIndex(DateTime.UtcNow);
|
||||
var index = NordPoolPriceService.GetTimeQuarterIndex(_timeProvider.GetUtcNow().UtcDateTime);
|
||||
|
||||
var prices = Enumerable.Range(0, NordPoolConsts.MaxDailyIntervals)
|
||||
.Select(_ => new NordPoolDayPrice { Price = 99.0 })
|
||||
@@ -682,7 +686,7 @@ public class NordPoolPriceServiceTests
|
||||
public void UpdateCurrentPrice_WhenOnlyCurrentQuarterPriceIsNull_ShouldKeepPreviousPriceAndAdvanceIndex()
|
||||
{
|
||||
// Arrange
|
||||
var index = NordPoolPriceService.GetTimeQuarterIndex(DateTime.UtcNow);
|
||||
var index = NordPoolPriceService.GetTimeQuarterIndex(_timeProvider.GetUtcNow().UtcDateTime);
|
||||
|
||||
var prices = Enumerable.Range(0, NordPoolConsts.MaxDailyIntervals)
|
||||
.Select(i => new NordPoolDayPrice { Price = i == index ? null : 10.0 })
|
||||
@@ -705,9 +709,9 @@ public class NordPoolPriceServiceTests
|
||||
#region FetchPricesAsync
|
||||
|
||||
[Fact]
|
||||
public async Task FetchPricesAsync_WhenNoCachedPrices_ShouldFetchTodayAndTomorrowViaDelegate()
|
||||
public async Task FetchPricesAsync_WhenNoCachedPricesBeforePublishHour_ShouldOnlyFetchToday()
|
||||
{
|
||||
// Arrange
|
||||
// Arrange — 11:00 CET, tomorrow's prices are not published yet
|
||||
var requestedDates = new List<DateOnly>();
|
||||
|
||||
Task<NordPoolPrice> Fetch(DateOnly date)
|
||||
@@ -721,26 +725,67 @@ public class NordPoolPriceServiceTests
|
||||
await _sut.FetchPricesAsync(Fetch);
|
||||
|
||||
// Assert
|
||||
requestedDates.Should().HaveCount(2);
|
||||
requestedDates[1].Should().Be(requestedDates[0].AddDays(1));
|
||||
_sut.TodayNordPoolPrice.Should().NotBeNull();
|
||||
_sut.TomorrowNordPoolPrice.Should().NotBeNull();
|
||||
requestedDates.Should().ContainSingle().Which.Should().Be(new DateOnly(2026, 12, 1));
|
||||
_sut.TodayNordPoolPrice!.Prices![0].Price.Should().Be(1.5);
|
||||
_sut.TomorrowNordPoolPrice.Should().BeNull();
|
||||
_sut.LastFetchedDate.Should().Be(new DateOnly(2026, 12, 1));
|
||||
_sut.LastUpdatedDate.Should().Be(_timeProvider.GetUtcNow().UtcDateTime);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FetchPricesAsync_WhenNoCachedPricesAfterPublishHour_ShouldFetchTodayAndTomorrow()
|
||||
{
|
||||
// Arrange — 13:30 CET, tomorrow's prices are published
|
||||
_timeProvider.SetUtcNow(new DateTimeOffset(2026, 12, 1, 12, 30, 0, TimeSpan.Zero));
|
||||
var requestedDates = new List<DateOnly>();
|
||||
|
||||
Task<NordPoolPrice> Fetch(DateOnly date)
|
||||
{
|
||||
requestedDates.Add(date);
|
||||
|
||||
return Task.FromResult(CreatePrices(1.5));
|
||||
}
|
||||
|
||||
// Act
|
||||
await _sut.FetchPricesAsync(Fetch);
|
||||
|
||||
// Assert
|
||||
requestedDates.Should().Equal(new DateOnly(2026, 12, 1), new DateOnly(2026, 12, 2));
|
||||
_sut.TodayNordPoolPrice!.Prices![0].Price.Should().Be(1.5);
|
||||
_sut.TomorrowNordPoolPrice!.Prices![0].Price.Should().Be(1.5);
|
||||
_sut.LastFetchedDate.Should().Be(requestedDates[0]);
|
||||
_sut.LastUpdatedDate.Should().NotBeNull();
|
||||
_sut.LastFetchedDate.Should().Be(new DateOnly(2026, 12, 1));
|
||||
_sut.LastUpdatedDate.Should().Be(_timeProvider.GetUtcNow().UtcDateTime);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FetchPricesAsync_WhenTomorrowPricesMissingBeforePublishHour_ShouldKeepTodayAndSkipTomorrow()
|
||||
{
|
||||
// Arrange — today is cached, tomorrow is not published yet
|
||||
_sut.TodayNordPoolPrice = CreatePrices(1.0);
|
||||
_sut.LastFetchedDate = new DateOnly(2026, 12, 1);
|
||||
var requestedDates = new List<DateOnly>();
|
||||
|
||||
// Act
|
||||
await _sut.FetchPricesAsync(date =>
|
||||
{
|
||||
requestedDates.Add(date);
|
||||
|
||||
return Task.FromResult(CreatePrices(3.0));
|
||||
});
|
||||
|
||||
// Assert
|
||||
requestedDates.Should().BeEmpty();
|
||||
_sut.TodayNordPoolPrice!.Prices![0].Price.Should().Be(1.0);
|
||||
_sut.TomorrowNordPoolPrice.Should().BeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task FetchPricesAsync_WhenCachedPricesAlreadyPresent_ShouldNotCallDelegate()
|
||||
{
|
||||
// Arrange
|
||||
var cetTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
||||
var todayCet = DateOnly.FromDateTime(TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, cetTimeZone));
|
||||
|
||||
_sut.TodayNordPoolPrice = CreatePrices(1.0);
|
||||
_sut.TomorrowNordPoolPrice = CreatePrices(2.0);
|
||||
_sut.LastFetchedDate = todayCet;
|
||||
_sut.LastFetchedDate = new DateOnly(2026, 12, 1);
|
||||
var delegateCalled = false;
|
||||
|
||||
// Act
|
||||
@@ -759,7 +804,7 @@ public class NordPoolPriceServiceTests
|
||||
|
||||
#endregion
|
||||
|
||||
#region ShouldFetchNewPrices
|
||||
#region ShouldFetchTodaysPrices
|
||||
|
||||
private static NordPoolPrice CreatePrices(params double?[] values)
|
||||
{
|
||||
@@ -770,38 +815,33 @@ public class NordPoolPriceServiceTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scenarios that do not depend on the time of day (tomorrow's prices are published after 13:00,
|
||||
/// so the "tomorrow missing" cases are covered separately below). xUnit cannot serialize
|
||||
/// <see cref="NordPoolPrice" /> rows, so the matrix runs inside a single test.
|
||||
/// xUnit cannot serialize <see cref="NordPoolPrice" /> rows, so the matrix runs inside a single test.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ShouldFetchNewPrices_ShouldMatchScenario()
|
||||
public void ShouldFetchTodaysPrices_ShouldMatchScenario()
|
||||
{
|
||||
var today = DateOnly.FromDateTime(DateTime.Now);
|
||||
var today = DateOnly.FromDateTime(_timeProvider.GetUtcNow().UtcDateTime);
|
||||
|
||||
(string Scenario, DateOnly? LastFetchedDate, NordPoolPrice? TodayPrice, NordPoolPrice? TomorrowPrice, bool
|
||||
Expected)[] cases =
|
||||
(string Scenario, DateOnly? LastFetchedDate, NordPoolPrice? TodayPrice, bool Expected)[] cases =
|
||||
[
|
||||
("no fetch date yet", null, CreatePrices(12.50), CreatePrices(18.71), true),
|
||||
("last fetch was yesterday", today.AddDays(-1), CreatePrices(12.50), CreatePrices(18.71), true),
|
||||
("last fetch date is in the future", today.AddDays(1), CreatePrices(12.50), CreatePrices(18.71), false),
|
||||
("today prices are null", today, new NordPoolPrice { Prices = null }, CreatePrices(18.71), true),
|
||||
("today prices are empty", today, new NordPoolPrice { Prices = [] }, CreatePrices(18.71), true),
|
||||
("today and tomorrow prices present", today, CreatePrices(12.50), CreatePrices(18.71), false),
|
||||
("price values are null but entries exist", today, CreatePrices((double?)null),
|
||||
CreatePrices((double?)null), false),
|
||||
("all price payloads are null", today, null, null, true)
|
||||
("no fetch date yet", null, CreatePrices(12.50), true),
|
||||
("last fetch was yesterday", today.AddDays(-1), CreatePrices(12.50), true),
|
||||
("last fetch date is in the future", today.AddDays(1), CreatePrices(12.50), false),
|
||||
("today prices are null", today, new NordPoolPrice { Prices = null }, true),
|
||||
("today prices are empty", today, new NordPoolPrice { Prices = [] }, true),
|
||||
("today prices present", today, CreatePrices(12.50), false),
|
||||
("price values are null but entries exist", today, CreatePrices((double?)null), false),
|
||||
("all price payloads are null", today, null, true)
|
||||
];
|
||||
|
||||
foreach (var (scenario, lastFetchedDate, todayPrice, tomorrowPrice, expected) in cases)
|
||||
foreach (var (scenario, lastFetchedDate, todayPrice, expected) in cases)
|
||||
{
|
||||
// Arrange
|
||||
_sut.LastFetchedDate = lastFetchedDate;
|
||||
_sut.TodayNordPoolPrice = todayPrice;
|
||||
_sut.TomorrowNordPoolPrice = tomorrowPrice;
|
||||
|
||||
// Act
|
||||
var result = _sut.ShouldFetchNewPrices(DateOnly.FromDateTime(DateTime.Now));
|
||||
var result = _sut.ShouldFetchTodaysPrices(today);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expected, $"scenario: {scenario}");
|
||||
@@ -809,45 +849,19 @@ public class NordPoolPriceServiceTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldFetchNewPrices_WhenTomorrowPricesAreNull_ShouldDependOnHourOfDay()
|
||||
public void ShouldFetchTodaysPrices_WhenTomorrowPricesAreMissing_ShouldNotDependOnTomorrowState()
|
||||
{
|
||||
// Arrange — tomorrow's prices are not published before 13:00, so cached data is acceptable until then
|
||||
var today = DateOnly.FromDateTime(DateTime.Now);
|
||||
// Arrange — tomorrow's prices are irrelevant to today's fetch decision
|
||||
var today = new DateOnly(2026, 12, 1);
|
||||
_sut.LastFetchedDate = today;
|
||||
|
||||
_sut.TodayNordPoolPrice = new NordPoolPrice
|
||||
{
|
||||
Prices = new List<NordPoolDayPrice> { new NordPoolDayPrice { Price = 12.50 } }
|
||||
};
|
||||
|
||||
_sut.TodayNordPoolPrice = CreatePrices(12.50);
|
||||
_sut.TomorrowNordPoolPrice = new NordPoolPrice { Prices = null };
|
||||
|
||||
// Act
|
||||
var result = _sut.ShouldFetchNewPrices(today);
|
||||
var result = _sut.ShouldFetchTodaysPrices(today);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(DateTime.Now.Hour >= 13);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldFetchNewPrices_WhenTomorrowPricesAreEmpty_ShouldDependOnHourOfDay()
|
||||
{
|
||||
// Arrange
|
||||
var today = DateOnly.FromDateTime(DateTime.Now);
|
||||
_sut.LastFetchedDate = today;
|
||||
|
||||
_sut.TodayNordPoolPrice = new NordPoolPrice
|
||||
{
|
||||
Prices = new List<NordPoolDayPrice> { new NordPoolDayPrice { Price = 12.50 } }
|
||||
};
|
||||
|
||||
_sut.TomorrowNordPoolPrice = new NordPoolPrice { Prices = new List<NordPoolDayPrice>() };
|
||||
|
||||
// Act
|
||||
var result = _sut.ShouldFetchNewPrices(today);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(DateTime.Now.Hour >= 13);
|
||||
result.Should().BeFalse();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user