using System.Net; using System.Net.Http.Json; using System.Text.Json; using Xunit; namespace Observation.AcceptanceTests; public sealed class ObservationHistoryTests(ObservationApiFixture fixture) : IClassFixture { private sealed record Profile(Guid PatientId); private sealed record Observation(Guid ObservationId, Guid PatientId, DateTime MeasuredAt, int Systolic, int Diastolic, int Pulse, string? Context, string? Note); private async Task CreatePatientAsync() { using var response = await fixture.Client.PostAsJsonAsync("/api/patients", new { name = "History Example", dateOfBirth = "1980-01-01" }); Assert.Equal(HttpStatusCode.Created, response.StatusCode); return (await response.Content.ReadFromJsonAsync())!.PatientId; } private async Task RecordAsync(Guid patientId, string measuredAt, int systolic, int diastolic, int pulse) { using var response = await fixture.Client.PostAsJsonAsync($"/api/patients/{patientId}/observations", new { measuredAt, systolic, diastolic, pulse, context = "Resting", note = "Before breakfast" }); Assert.Equal(HttpStatusCode.Created, response.StatusCode); return (await response.Content.ReadFromJsonAsync())!; } private Task HistoryAsync(Guid patientId, string from, string to) => fixture.Client.GetFromJsonAsync( $"/api/patients/{patientId}/observations?from={Uri.EscapeDataString(from)}&to={Uri.EscapeDataString(to)}"); [Fact] public async Task Selected_period_returns_only_the_matching_observation_after_restart() { var patientId = await CreatePatientAsync(); await RecordAsync(patientId, "2026-09-01T08:00:00", 120, 80, 60); var expected = await RecordAsync(patientId, "2026-09-05T08:00:00", 125, 82, 65); await RecordAsync(patientId, "2026-09-10T08:00:00", 130, 84, 70); await RecordAsync(await CreatePatientAsync(), "2026-09-05T08:00:00", 140, 90, 75); await fixture.RestartHostsAsync(); var history = await HistoryAsync(patientId, "2026-09-03T00:00:00", "2026-09-08T23:59:59"); Assert.Equal(expected, Assert.Single(history!)); } [Fact] public async Task Results_are_chronological_and_include_both_period_boundaries() { var patientId = await CreatePatientAsync(); var last = await RecordAsync(patientId, "2026-09-10T08:00:00", 130, 84, 70); var first = await RecordAsync(patientId, "2026-09-01T08:00:00", 120, 80, 60); var middle = await RecordAsync(patientId, "2026-09-05T08:00:00", 125, 82, 65); Assert.Equal(new[] { first, middle, last }, await HistoryAsync(patientId, "2026-09-01T08:00:00", "2026-09-10T08:00:00")); Assert.Equal(first, Assert.Single((await HistoryAsync(patientId, "2026-09-01T08:00:00", "2026-09-01T08:00:00"))!)); } [Fact] public async Task No_matching_observations_returns_an_empty_array() { var patientId = await CreatePatientAsync(); await RecordAsync(await CreatePatientAsync(), "2026-09-05T08:00:00", 125, 82, 65); Assert.Empty((await HistoryAsync(patientId, "2026-09-03T00:00:00", "2026-09-08T23:59:59"))!); await RecordAsync(patientId, "2026-09-01T08:00:00", 120, 80, 60); Assert.Empty((await HistoryAsync(patientId, "2026-09-03T00:00:00", "2026-09-08T23:59:59"))!); } [Theory] [InlineData("from=2026-09-08T00:00:00&to=2026-09-03T00:00:00")] [InlineData("from=invalid&to=2026-09-08T00:00:00")] [InlineData("from=2026-09-03T00:00:00&to=invalid")] [InlineData("from=2026-09-03T00:00:00")] [InlineData("to=2026-09-08T00:00:00")] [InlineData("")] public async Task Invalid_or_missing_period_is_rejected_without_changing_observations(string query) { var patientId = await CreatePatientAsync(); var saved = await RecordAsync(patientId, "2026-09-05T08:00:00", 125, 82, 65); using var response = await fixture.Client.GetAsync($"/api/patients/{patientId}/observations?{query}"); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); using var problem = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); Assert.True(problem.RootElement.TryGetProperty("errors", out _)); Assert.Equal(saved, await fixture.Client.GetFromJsonAsync($"/api/observations/{saved.ObservationId}")); } [Fact] public async Task Offset_bounds_are_normalized_consistently_with_recorded_measurements() { var patientId = await CreatePatientAsync(); var saved = await RecordAsync(patientId, "2026-09-05T10:00:00+02:00", 125, 82, 65); Assert.Equal(saved, Assert.Single((await HistoryAsync(patientId, "2026-09-05T10:00:00+02:00", "2026-09-05T08:00:00Z"))!)); Assert.Empty((await HistoryAsync(patientId, "2026-09-05T08:00:01Z", "2026-09-05T11:00:00Z"))!); } }