using System.Net; using System.Net.Http.Json; using Microsoft.EntityFrameworkCore; using Xunit; namespace Observation.AcceptanceTests; public sealed class DeleteObservationTests(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 = "Deletion 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) { using var response = await fixture.Client.PostAsJsonAsync($"/api/patients/{patientId}/observations", new { measuredAt, systolic = 128, diastolic = 82, pulse = 68, context = "Resting", note = "Before breakfast" }); Assert.Equal(HttpStatusCode.Created, response.StatusCode); return (await response.Content.ReadFromJsonAsync())!; } private Task HistoryAsync(Guid patientId) => fixture.Client.GetFromJsonAsync( $"/api/patients/{patientId}/observations?from=2026-09-01T00:00:00&to=2026-09-30T23:59:59"); [Fact] public async Task Deleted_observation_stays_absent_after_restart_and_other_observations_are_preserved() { var patientId = await CreatePatientAsync(); var earlier = await RecordAsync(patientId, "2026-09-10T08:00:00"); var deleted = await RecordAsync(patientId, "2026-09-12T08:00:00"); var other = await RecordAsync(await CreatePatientAsync(), "2026-09-13T08:00:00"); using var response = await fixture.Client.DeleteAsync($"/api/observations/{deleted.ObservationId}"); Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); Assert.Empty(await response.Content.ReadAsStringAsync()); await fixture.RestartHostsAsync(); using var retrieval = await fixture.Client.GetAsync($"/api/observations/{deleted.ObservationId}"); Assert.Equal(HttpStatusCode.NotFound, retrieval.StatusCode); Assert.Equal(earlier, Assert.Single((await HistoryAsync(patientId))!)); Assert.Equal(earlier, await fixture.Client.GetFromJsonAsync($"/api/observations/{earlier.ObservationId}")); Assert.Equal(earlier, await fixture.Client.GetFromJsonAsync( $"/api/patients/{patientId}/observations/latest")); Assert.Equal(other, await fixture.Client.GetFromJsonAsync($"/api/observations/{other.ObservationId}")); Assert.Equal(other, Assert.Single((await HistoryAsync(other.PatientId))!)); Assert.Equal(patientId, (await fixture.Client.GetFromJsonAsync($"/api/patients/{patientId}"))!.PatientId); await using var db = fixture.CreateObservationDbContext(); Assert.False(await db.Observations.AnyAsync(o => o.ObservationId == deleted.ObservationId)); } [Fact] public async Task Deleting_the_only_observation_leaves_empty_history_and_no_latest_observation() { var patientId = await CreatePatientAsync(); var observation = await RecordAsync(patientId, "2026-09-12T08:00:00"); using var response = await fixture.Client.DeleteAsync($"/api/observations/{observation.ObservationId}"); Assert.Equal(HttpStatusCode.NoContent, response.StatusCode); Assert.Empty((await HistoryAsync(patientId))!); using var latest = await fixture.Client.GetAsync($"/api/patients/{patientId}/observations/latest"); Assert.Equal(HttpStatusCode.NotFound, latest.StatusCode); using var repeated = await fixture.Client.DeleteAsync($"/api/observations/{observation.ObservationId}"); Assert.Equal(HttpStatusCode.NotFound, repeated.StatusCode); } [Fact] public async Task Missing_observation_returns_not_found_without_changing_existing_data() { var observation = await RecordAsync(await CreatePatientAsync(), "2026-09-12T08:00:00"); await using var db = fixture.CreateObservationDbContext(); var count = await db.Observations.CountAsync(); using var response = await fixture.Client.DeleteAsync($"/api/observations/{Guid.NewGuid()}"); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); Assert.Equal(count, await db.Observations.CountAsync()); Assert.Equal(observation, await fixture.Client.GetFromJsonAsync( $"/api/observations/{observation.ObservationId}")); Assert.Equal(observation, Assert.Single((await HistoryAsync(observation.PatientId))!)); } }