Implement US-07 context-filtered observation history

This commit is contained in:
Steffen committed 2026-10-05 12:28:04 +02:00
1 parent ac6b42f32e
commit ce03038ce1
8 files changed
+235 -11

No files matched your search

@@ -0,0 +1,179 @@
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
using HealthApp.TestSupport;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace Observation.AcceptanceTests;
public sealed class ContextFilteredHistoryTests(ObservationApiFixture fixture) : IClassFixture<ObservationApiFixture>
{
private sealed record Profile(Guid PatientId);
private sealed record ContextData(string Setting, string ActivityCondition, string? DeviceDetails);
private sealed record Observation(Guid ObservationId, Guid PatientId, DateTime MeasuredAt,
string? Context, ContextData MeasurementContext);
private const string Period = "from=2026-09-01T08%3A00%3A00&to=2026-09-10T08%3A00%3A00";
private async Task<Guid> Patient()
{
using var response = await fixture.Client.PostAsJsonAsync("/api/patients", new { name = "Filtered history", dateOfBirth = "1980-01-01" });
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
return (await response.Content.ReadFromJsonAsync<Profile>())!.PatientId;
}
private async Task<Observation> Record(Guid patient, string time, string? setting, string? activity, string? legacy = null, bool includeContext = true, bool completeContext = false)
{
using var response = await fixture.Client.PostAsJsonAsync($"/api/patients/{patient}/observations", new
{
measuredAt = time, systolic = 128, diastolic = 82, pulse = 68, context = legacy,
measurementContext = includeContext ? new
{
setting, activityCondition = activity, deviceDetails = "Saved device",
restDurationMinutes = completeContext ? (decimal?)5 : null,
bodyPosition = completeContext ? "Seated" : null,
measuredArm = completeContext ? "Left" : null,
cuffDetails = completeContext ? "Saved cuff" : null,
additionalCircumstances = completeContext ? "Usual routine" : null
} : null
});
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
return (await response.Content.ReadFromJsonAsync<Observation>())!;
}
private string PathFor(Guid patient, string selection = "", string period = Period) =>
$"/api/patients/{patient}/observations?{period}{selection}";
[Theory]
[InlineData("&setting=Home", "Home", "All", 2)]
[InlineData("&activityCondition=AtRest", "All", "AtRest", 2)]
[InlineData("&setting=Home&activityCondition=AtRest", "Home", "AtRest", 1)]
public async Task Independent_and_combined_selections_return_matching_context_and_metadata(string selection,
string settingHeader, string activityHeader, int expectedCount)
{
var patient = await Patient();
var homeRest = await Record(patient, "2026-09-05T08:00:00", "Home", "AtRest");
var homeAfter = await Record(patient, "2026-09-06T08:00:00", "Home", "AfterActivity");
var clinicRest = await Record(patient, "2026-09-07T08:00:00", "Clinic", "AtRest");
await Record(patient, "2026-09-08T08:00:00", null, null, "Home, resting? Morning reading");
using var response = await fixture.Client.GetAsync(PathFor(patient, selection));
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(settingHeader, Assert.Single(response.Headers.GetValues("X-Measurement-Setting")));
Assert.Equal(activityHeader, Assert.Single(response.Headers.GetValues("X-Activity-Condition")));
var history = (await response.Content.ReadFromJsonAsync<Observation[]>())!;
Assert.Equal(expectedCount, history.Length);
var expected = new[] { homeRest, homeAfter, clinicRest }.Where(o =>
(settingHeader == "All" || o.MeasurementContext.Setting == settingHeader) &&
(activityHeader == "All" || o.MeasurementContext.ActivityCondition == activityHeader)).ToArray();
Assert.Equal(expected, history);
Assert.All(history, o => Assert.Equal("Saved device", o.MeasurementContext.DeviceDetails));
}
[Theory]
[InlineData("&setting=Unknown", "Unknown", "All")]
[InlineData("&activityCondition=Unknown", "All", "Unknown")]
[InlineData("&setting=Unknown&activityCondition=Unknown", "Unknown", "Unknown")]
public async Task Unknown_selections_match_unknown_categories_without_interpreting_legacy_text(string selection,
string settingHeader, string activityHeader)
{
var patient = await Patient();
var unknown = await Record(patient, "2026-09-05T08:00:00", null, null, "Home resting?");
var partial = await Record(patient, "2026-09-06T08:00:00", "Clinic", null);
await Record(patient, "2026-09-07T08:00:00", "Home", "AtRest");
using var response = await fixture.Client.GetAsync(PathFor(patient, selection));
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(settingHeader, Assert.Single(response.Headers.GetValues("X-Measurement-Setting")));
Assert.Equal(activityHeader, Assert.Single(response.Headers.GetValues("X-Activity-Condition")));
var result = (await response.Content.ReadFromJsonAsync<Observation[]>())!;
Assert.Equal(activityHeader == "Unknown" && settingHeader == "All" ? new[] { unknown, partial } : new[] { unknown }, result);
Assert.Equal("Home resting?", result[0].Context);
}
[Fact]
public async Task No_selection_retains_all_contexts_in_chronological_order_after_restart()
{
var patient = await Patient();
var last = await Record(patient, "2026-09-07T08:00:00", null, null, includeContext: false);
var first = await Record(patient, "2026-09-05T08:00:00", "Home", "AtRest", completeContext: true);
var middle = await Record(patient, "2026-09-06T08:00:00", "Clinic", null);
await fixture.RestartHostsAsync();
using var response = await fixture.Client.GetAsync(PathFor(patient));
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(new[] { first, middle, last }, await response.Content.ReadFromJsonAsync<Observation[]>());
Assert.Equal("All", Assert.Single(response.Headers.GetValues("X-Measurement-Setting")));
Assert.Equal("All", Assert.Single(response.Headers.GetValues("X-Activity-Condition")));
}
[Fact]
public async Task Empty_filtered_history_is_an_empty_array_with_applied_selection_metadata()
{
var patient = await Patient();
await Record(patient, "2026-09-05T08:00:00", "Home", "AtRest");
using var response = await fixture.Client.GetAsync(PathFor(patient, "&setting=Other&activityCondition=AfterActivity"));
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("[]", await response.Content.ReadAsStringAsync());
Assert.Equal("Other", Assert.Single(response.Headers.GetValues("X-Measurement-Setting")));
Assert.Equal("AfterActivity", Assert.Single(response.Headers.GetValues("X-Activity-Condition")));
}
[Fact]
public async Task Filtered_history_preserves_patient_scope_inclusive_boundaries_and_equal_bounds()
{
var patient = await Patient();
var last = await Record(patient, "2026-09-10T08:00:00", "Home", "AtRest");
var first = await Record(patient, "2026-09-01T08:00:00", "Home", "AtRest");
await Record(patient, "2026-09-01T07:59:59", "Home", "AtRest");
await Record(patient, "2026-09-10T08:00:01", "Home", "AtRest");
await Record(patient, "2026-09-05T08:00:00", "Home", "AfterActivity");
await Record(await Patient(), "2026-09-05T08:00:00", "Home", "AtRest");
using var response = await fixture.Client.GetAsync(PathFor(patient, "&setting=Home&activityCondition=AtRest"));
Assert.Equal(new[] { first, last }, await response.Content.ReadFromJsonAsync<Observation[]>());
using var equal = await fixture.Client.GetAsync(PathFor(patient, "&setting=Home", "from=2026-09-01T10%3A00%3A00%2B02%3A00&to=2026-09-01T08%3A00%3A00Z"));
Assert.Equal(HttpStatusCode.OK, equal.StatusCode);
Assert.Equal(first, Assert.Single((await equal.Content.ReadFromJsonAsync<Observation[]>())!));
}
[Theory]
[InlineData("&setting=Unsupported")]
[InlineData("&activityCondition=Unsupported")]
[InlineData("&setting=All")]
[InlineData("&activityCondition=1")]
public async Task Unsupported_selections_are_rejected_without_changing_observations(string selection)
{
var patient = await Patient();
var observation = await Record(patient, "2026-09-05T08:00:00", "Home", "AtRest");
await using var db = fixture.CreateObservationDbContext();
var before = await db.Observations.CountAsync();
using var response = await fixture.Client.GetAsync(PathFor(patient, selection));
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
using var problem = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
Assert.True(problem.RootElement.TryGetProperty("errors", out _));
Assert.Equal(before, await db.Observations.CountAsync());
Assert.Equal(observation, await fixture.Client.GetFromJsonAsync<Observation>($"/api/observations/{observation.ObservationId}"));
}
[Fact]
public async Task Professional_reads_follow_assignments_and_other_patient_tokens_cannot_use_filters_to_gain_access()
{
var patient = await Patient();
var expected = await Record(patient, "2026-09-05T08:00:00", "Home", "AtRest");
var other = await Patient();
fixture.AdditionalGatewayConfiguration["Access:ProfessionalAssignments:professional:0"] = patient.ToString();
await fixture.RestartHostsAsync();
foreach (var (account, status) in new[]
{
("professional", HttpStatusCode.OK), ("professional-empty", HttpStatusCode.NotFound),
(fixture.Authentication.PatientAccounts[other], HttpStatusCode.NotFound)
})
{
using var request = new HttpRequestMessage(HttpMethod.Get, PathFor(patient, "&setting=Home&activityCondition=AtRest"));
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await AuthenticatedTestClient.LoginAsync(fixture.RawClient, account));
using var response = await fixture.RawClient.SendAsync(request);
Assert.Equal(status, response.StatusCode);
if (status == HttpStatusCode.OK)
Assert.Equal(expected, Assert.Single((await response.Content.ReadFromJsonAsync<Observation[]>())!));
else Assert.Empty(await response.Content.ReadAsStringAsync());
}
}
}