using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using NSubstitute; using NSubstitute.ExceptionExtensions; using Schaad.OpenData.Weather; using Schaad.OpenData.Weather.Models; using Schaad.OpenData.Weather.Source; using Schaad.OpenData.Weather.Storage; using static Schaad.OpenData.Tests.WeatherTestData; namespace Schaad.OpenData.Tests; public class WeatherRefresherTestShould { private readonly WeatherOptions settings = new() { Stations = ["GUT", "STK"] }; private readonly MeteoSwissClient client; private readonly WeatherCacheStorage storage; private readonly WeatherDataStore store = new(); private readonly TimeProvider timeProvider = Substitute.For(); private readonly WeatherRefresher refresher; public WeatherRefresherTestShould() { var options = Options.Create(settings); client = Substitute.For(new HttpClient(), options); client.FetchStationsAsync(Arg.Any>(), Arg.Any()) .Returns([Station(), Station("STK", "Steckborn")]); client.FetchRecentAsync(Arg.Any(), Arg.Any()) .Returns(call => [Reading(call.Arg(), Now)]); client.FetchHistoryAsync(Arg.Any(), Arg.Any(), Arg.Any()) .Returns(call => [Reading(call.Arg(), Now.AddDays(-7))]); storage = Substitute.For(options); timeProvider.GetUtcNow().Returns(Now); refresher = new WeatherRefresher(client, storage, store, options, timeProvider, NullLogger.Instance); } private static WeatherSnapshot Covered(DateTimeOffset? stationsFetched) => new( [Station(), Station("STK", "Steckborn")], [Reading("GUT", Now.AddDays(-7)), Reading("GUT", Now.AddMinutes(-10)), Reading("STK", Now.AddDays(-7)), Reading("STK", Now.AddMinutes(-10))], Now.AddMinutes(-10), stationsFetched); [Fact] public async Task FetchStationsAndBackfillSevenDaysWhenCacheIsEmpty() { await refresher.RefreshAsync(CancellationToken.None); await client.Received(1).FetchStationsAsync( Arg.Is>(a => a.SequenceEqual(new[] { "GUT", "STK" })), Arg.Any()); await client.Received(1).FetchHistoryAsync("GUT", Now.AddDays(-7), Arg.Any()); await client.Received(1).FetchHistoryAsync("STK", Now.AddDays(-7), Arg.Any()); store.Current.Stations.Count.ShouldBe(2); store.Current.Readings.Count.ShouldBe(4); store.Current.StationsFetched.ShouldBe(Now); await storage.Received(1).SaveAsync( store.Current, Arg.Is>(days => days.Order().SequenceEqual(new[] { new DateOnly(2026, 11, 11), new DateOnly(2026, 11, 18) })), Arg.Any()); } [Fact] public async Task FetchOnlyRecentValuesWhenCacheIsCovered() { store.Replace(Covered(Now.AddHours(-1))); await refresher.RefreshAsync(CancellationToken.None); await client.DidNotReceive().FetchStationsAsync(Arg.Any>(), Arg.Any()); await client.DidNotReceive().FetchHistoryAsync(Arg.Any(), Arg.Any(), Arg.Any()); await client.Received(2).FetchRecentAsync(Arg.Any(), Arg.Any()); store.Current.Readings.Count.ShouldBe(6); } [Fact] public async Task RefreshStationMetadataOnceADayWhenRefreshing() { store.Replace(Covered(Now.AddDays(-1))); await refresher.RefreshAsync(CancellationToken.None); await client.Received(1).FetchStationsAsync(Arg.Any>(), Arg.Any()); } [Fact] public async Task KeepOtherStationsWhenOneStationFails() { store.Replace(Covered(Now.AddHours(-1))); client.FetchRecentAsync("GUT", Arg.Any()).ThrowsAsync(new HttpRequestException("404")); await refresher.RefreshAsync(CancellationToken.None); store.Current.Readings.Where(r => r.Time == Now).Select(r => r.StationAbbr).ShouldBe(["STK"]); } [Fact] public void RequireRefreshWhenLastRefreshIsOlderThanInterval() { store.Replace(new WeatherSnapshot([], [], Now.AddMinutes(-11), null)); refresher.IsRefreshDue(TimeSpan.FromMinutes(10)).ShouldBeTrue(); refresher.IsRefreshDue(TimeSpan.FromMinutes(15)).ShouldBeFalse(); } }