using System.Net; using System.Text; using Microsoft.Extensions.Options; using Schaad.OpenData.Hydrology; using Schaad.OpenData.Hydrology.Models; using Schaad.OpenData.Hydrology.Source; namespace Schaad.OpenData.Tests; public class HydrodatenPlotClientTestShould { private sealed class StubHandler(Dictionary bodies) : HttpMessageHandler { public List Requests { get; } = []; protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var url = request.RequestUri!.AbsoluteUri; Requests.Add(url); return Task.FromResult(bodies.TryGetValue(url, out var body) ? new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(body, Encoding.UTF8, "application/json") } : new HttpResponseMessage(HttpStatusCode.NotFound)); } } private const string LevelUrl = "https://hydro.example/plots/p_q_7days/2181_p_q_7days_de.json"; private const string TemperatureUrl = "https://hydro.example/plots/temperature_7days/2181_temperature_7days_de.json"; private const string LevelPlot = """ {"plot":{"layout":{},"data":[ {"name":"Wasserstand","type":"scatter","x":["2026-11-12T00:00:00.000+01:00","2026-11-12T00:05:00.000+01:00","2026-11-12T00:10:00.000+01:00"],"y":[455.835,455.839,455.843]}, {"name":"Abfluss","type":"scatter","x":["2026-11-12T00:00:00.000+01:00","2026-11-12T00:05:00.000+01:00","2026-11-12T00:10:00.000+01:00"],"y":[28.243,28.576,null]} ]},"hoverInfo":{}} """; private const string TemperaturePlot = """ {"plot":{"layout":{},"data":[ {"name":"Temperatur","type":"scatter","x":["2026-11-12T00:00:00.000+01:00","2026-11-12T00:20:00.000+01:00"],"y":[10.37,10.36]} ]},"hoverInfo":{}} """; private static HydrodatenPlotClient Client(StubHandler handler) => new(new HttpClient(handler), Options.Create(new HydrologyOptions { PlotsUrl = "https://hydro.example/plots/" })); [Fact] public async Task CombineTracesOnTenMinuteMarksWhenFetchingRecentValues() { var handler = new StubHandler(new() { [LevelUrl] = LevelPlot, [TemperatureUrl] = TemperaturePlot }); var readings = await Client(handler).FetchRecentAsync("2181", CancellationToken.None); var offset = TimeSpan.FromHours(1); readings.ShouldBe([ new HydroReading("2181", new DateTimeOffset(2026, 11, 12, 0, 0, 0, offset), 455.835, 28.243, 10.37, null), new HydroReading("2181", new DateTimeOffset(2026, 11, 12, 0, 10, 0, offset), 455.843, null, null, null), new HydroReading("2181", new DateTimeOffset(2026, 11, 12, 0, 20, 0, offset), null, null, 10.36, null), ]); } [Fact] public async Task ReturnLevelsOnlyWhenTemperaturePlotIsMissing() { var handler = new StubHandler(new() { [LevelUrl] = LevelPlot }); var readings = await Client(handler).FetchRecentAsync("2181", CancellationToken.None); readings.Count.ShouldBe(2); readings.ShouldAllBe(r => r.WaterTemperature == null); handler.Requests.ShouldBe([LevelUrl, TemperatureUrl]); } [Fact] public async Task SkipRequestWhenStationIdIsNotNumeric() { var handler = new StubHandler([]); var readings = await Client(handler).FetchRecentAsync("../2181", CancellationToken.None); readings.ShouldBeEmpty(); handler.Requests.ShouldBeEmpty(); } }