using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using NSubstitute; using Schaad.OpenData.Vehicles; using Schaad.OpenData.Vehicles.Models; using Schaad.OpenData.Vehicles.Source; using Schaad.OpenData.Vehicles.Storage; using static Schaad.OpenData.Tests.VehicleTestData; namespace Schaad.OpenData.Tests; public class VehicleRefresherTestShould { private static readonly DateTimeOffset Now = new(2026, 11, 6, 12, 0, 0, TimeSpan.Zero); private static readonly IOptions Options = Microsoft.Extensions.Options.Options.Create(new VehicleOptions()); private static readonly DatasetVersion StockVersion = new("djs-stv-17", Now.AddDays(-50)); private static readonly DatasetVersion PopulationVersion = new("sk-stat-56", Now.AddDays(-120)); private readonly ThurgauVehicleClient client = Substitute.For(new HttpClient(), Options); private readonly MunicipalityBoundaryClient boundaryClient = Substitute.For(new HttpClient(), Options); private readonly VehicleCacheStorage storage = Substitute.For(Options); private readonly VehicleDataStore store = new(); private readonly TimeProvider timeProvider = Substitute.For(); private readonly VehicleRefresher refresher; public VehicleRefresherTestShould() { timeProvider.GetUtcNow().Returns(Now); client.GetVersionAsync("djs-stv-17", Arg.Any()).Returns(StockVersion); client.GetVersionAsync("sk-stat-56", Arg.Any()).Returns(PopulationVersion); boundaryClient.FetchAsync(Arg.Any(), Arg.Any()).Returns(call => Outline(call.Arg())); refresher = new VehicleRefresher(client, boundaryClient, storage, store, Options, timeProvider, NullLogger.Instance); } [Fact] public async Task FetchEverythingWhenCacheIsEmpty() { client.FetchStockAsync(Arg.Any()).Returns([Entry(id: "4566"), Entry(id: "4671", municipality: "Kreuzlingen")]); client.FetchPopulationAsync(Arg.Any()).Returns([new PopulationEntry(2024, "4566", 27000)]); await refresher.RefreshAsync(CancellationToken.None); var snapshot = store.Current; snapshot.Stock.Count.ShouldBe(2); snapshot.Population.Count.ShouldBe(1); snapshot.Outlines.Select(o => o.Id).ShouldBe(["4566", "4671"]); snapshot.StockVersion.ShouldBe(StockVersion); snapshot.PopulationVersion.ShouldBe(PopulationVersion); snapshot.LastRefresh.ShouldBe(Now); await storage.Received(1).SaveAsync(snapshot, Arg.Any()); } [Fact] public async Task SkipUnchangedDatasetsAndKnownOutlinesWhenRefreshing() { store.Replace(new VehicleSnapshot([Entry()], [new PopulationEntry(2024, "4566", 27000)], [Outline("4566")], StockVersion, PopulationVersion, Now.AddDays(-1))); await refresher.RefreshAsync(CancellationToken.None); await client.DidNotReceive().FetchStockAsync(Arg.Any()); await client.DidNotReceive().FetchPopulationAsync(Arg.Any()); await boundaryClient.DidNotReceive().FetchAsync(Arg.Any(), Arg.Any()); store.Current.LastRefresh.ShouldBe(Now); } [Fact] public async Task FetchStockAgainWhenDatasetWasModified() { store.Replace(new VehicleSnapshot([Entry()], [new PopulationEntry(2024, "4566", 27000)], [Outline("4566")], StockVersion with { Modified = Now.AddDays(-400) }, PopulationVersion, null)); client.FetchStockAsync(Arg.Any()).Returns([Entry(2025), Entry(2025, "4401", "Arbon")]); await refresher.RefreshAsync(CancellationToken.None); store.Current.Years.ShouldBe([2025]); store.Current.Outlines.Select(o => o.Id).ShouldBe(["4566", "4401"]); await client.DidNotReceive().FetchPopulationAsync(Arg.Any()); await boundaryClient.Received(1).FetchAsync("4401", Arg.Any()); } [Fact] public void RequireRefreshWhenLastRefreshIsOlderThanInterval() { store.Replace(new VehicleSnapshot([], [], [], null, null, Now.AddHours(-25))); refresher.IsRefreshDue(TimeSpan.FromDays(1)).ShouldBeTrue(); refresher.IsRefreshDue(TimeSpan.FromDays(2)).ShouldBeFalse(); } }