using Microsoft.Extensions.Options; using Schaad.OpenData.Traffic; using Schaad.OpenData.Traffic.Models; using Schaad.OpenData.Traffic.Storage; namespace Schaad.OpenData.Tests; public sealed class TrafficCacheStorageTestShould : IDisposable { private static readonly DateOnly Day = new(2026, 10, 27); private readonly string dataPath = Path.Combine(Path.GetTempPath(), "odv-tests-" + Guid.NewGuid().ToString("N")); private readonly TrafficCacheStorage storage; public TrafficCacheStorageTestShould() { storage = new TrafficCacheStorage(Options.Create(new TrafficOptions { DataPath = dataPath })); } public void Dispose() { if (Directory.Exists(dataPath)) { Directory.Delete(dataPath, true); } } [Fact] public async Task ReturnEmptySnapshotWhenCacheDoesNotExist() { var snapshot = await storage.LoadAsync(CancellationToken.None); snapshot.Stations.ShouldBeEmpty(); snapshot.Dates.ShouldBeEmpty(); snapshot.LastRefresh.ShouldBeNull(); } [Fact] public async Task RestoreSavedDataWhenLoading() { var refreshedAt = new DateTimeOffset(2026, 11, 6, 12, 0, 0, TimeSpan.Zero); var geometry = new StationGeometry(1, true, [new DirectionGeometry("nach A", [TestData.Origin, TestData.Meters(10, 0)])]); var snapshot = new TrafficSnapshot( [TestData.Station(1, "nach A")], [TestData.Count(1, "nach A", Day, 8, 42, 3)], [geometry], refreshedAt); await storage.SaveAsync(snapshot, [Day], CancellationToken.None); await storage.SaveGeometryAsync(snapshot, CancellationToken.None); var loaded = await storage.LoadAsync(CancellationToken.None); loaded.Stations[1].Directions.ShouldBe(["nach A"]); loaded.CountsByDate[Day].ShouldHaveSingleItem().Counts.ShouldBe(TestData.Cars(42, 3)); loaded.Geometry[1].Directions.ShouldHaveSingleItem().Path.Count.ShouldBe(2); loaded.LastRefresh.ShouldBe(refreshedAt); } [Fact] public async Task WriteOneFilePerDayWhenSaving() { var snapshot = new TrafficSnapshot([], [TestData.Count(1, "nach A", Day, 8, 1)], [], null); await storage.SaveAsync(snapshot, [Day], CancellationToken.None); File.Exists(Path.Combine(dataPath, "traffic", "thurgau", "counts", "2026-10-27.json")).ShouldBeTrue(); } }