update docs and prep for frontend
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StackExchange.Redis;
|
||||
|
||||
[Collection("Integration")]
|
||||
public class EncountersListTests : IAsyncLifetime
|
||||
{
|
||||
private readonly ApiFixture _fixture;
|
||||
private readonly HttpClient _client;
|
||||
private Guid _activeIcuEncounterId;
|
||||
private Guid _activeSurgeryEncounterId;
|
||||
private Guid _dischargedEncounterId;
|
||||
|
||||
public EncountersListTests(ApiFixture fixture)
|
||||
{
|
||||
_fixture = fixture;
|
||||
_client = fixture.CreateClient();
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
using var scope = _fixture.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
await DbResetHelper.ResetAsync(db);
|
||||
|
||||
var patientIcu = new Patient
|
||||
{
|
||||
Id = Guid.NewGuid(), Mrn = "MRN-WARD-001", FirstName = "Alice", LastName = "Icu",
|
||||
DateOfBirth = new DateOnly(1970, 3, 1), Gender = "F", CreatedAt = DateTimeOffset.UtcNow
|
||||
};
|
||||
var patientSurgery = new Patient
|
||||
{
|
||||
Id = Guid.NewGuid(), Mrn = "MRN-WARD-002", FirstName = "Bob", LastName = "Surgery",
|
||||
DateOfBirth = new DateOnly(1965, 7, 12), Gender = "M", CreatedAt = DateTimeOffset.UtcNow
|
||||
};
|
||||
|
||||
_activeIcuEncounterId = Guid.NewGuid();
|
||||
_activeSurgeryEncounterId = Guid.NewGuid();
|
||||
_dischargedEncounterId = Guid.NewGuid();
|
||||
|
||||
db.Patients.AddRange(patientIcu, patientSurgery);
|
||||
db.Encounters.AddRange(
|
||||
new Encounter
|
||||
{
|
||||
Id = _activeIcuEncounterId, PatientId = patientIcu.Id,
|
||||
EncounterType = EncounterType.Inpatient, Status = EncounterStatus.Active,
|
||||
Department = Department.Icu, AttendingPhysician = "Dr. Ward",
|
||||
RoomBed = "ICU-3", AdmittedAt = DateTimeOffset.UtcNow.AddHours(-2),
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
},
|
||||
new Encounter
|
||||
{
|
||||
Id = _activeSurgeryEncounterId, PatientId = patientSurgery.Id,
|
||||
EncounterType = EncounterType.Inpatient, Status = EncounterStatus.Active,
|
||||
Department = Department.Surgery, AttendingPhysician = "Dr. Ward",
|
||||
RoomBed = "S-12", AdmittedAt = DateTimeOffset.UtcNow.AddHours(-1),
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
},
|
||||
new Encounter
|
||||
{
|
||||
Id = _dischargedEncounterId, PatientId = patientSurgery.Id,
|
||||
EncounterType = EncounterType.Outpatient, Status = EncounterStatus.Discharged,
|
||||
Department = Department.Surgery, AttendingPhysician = "Dr. Ward",
|
||||
AdmittedAt = DateTimeOffset.UtcNow.AddDays(-3),
|
||||
DischargedAt = DateTimeOffset.UtcNow.AddDays(-2),
|
||||
CreatedAt = DateTimeOffset.UtcNow.AddDays(-3)
|
||||
});
|
||||
|
||||
db.News2Scores.Add(new News2Score
|
||||
{
|
||||
Id = Guid.NewGuid(), EncounterId = _activeIcuEncounterId, PatientId = patientIcu.Id,
|
||||
TotalScore = 7, RiskLevel = "HIGH", CalculatedAt = DateTimeOffset.UtcNow,
|
||||
RespRateScore = 1, Spo2Score = 0, SystolicBpScore = 1, HeartRateScore = 1,
|
||||
ConsciousnessScore = 0, TemperatureScore = 0, SupplementalO2Score = 0
|
||||
});
|
||||
|
||||
db.ClinicalAlerts.Add(new ClinicalAlert
|
||||
{
|
||||
Id = Guid.NewGuid(), EncounterId = _activeIcuEncounterId, PatientId = patientIcu.Id,
|
||||
AlertType = AlertType.News2Emergency, Severity = AlertSeverity.Critical,
|
||||
Details = "NEWS2 score 7", Status = AlertStatus.Open,
|
||||
TriggeredAt = DateTimeOffset.UtcNow
|
||||
});
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public Task DisposeAsync() => Task.CompletedTask;
|
||||
|
||||
[Fact]
|
||||
public async Task ListActiveEncounters_ReturnsSummariesWithPatientNames()
|
||||
{
|
||||
var resp = await _client.GetAsync("/api/v1/encounters?status=ACTIVE&page=1&pageSize=20");
|
||||
resp.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
var body = await resp.Content.ReadFromJsonAsync<JsonDocument>();
|
||||
var items = body!.RootElement.GetProperty("data").GetProperty("items");
|
||||
items.GetArrayLength().Should().Be(2);
|
||||
|
||||
var icu = items.EnumerateArray()
|
||||
.First(i => i.GetProperty("encounterId").GetGuid() == _activeIcuEncounterId);
|
||||
|
||||
icu.GetProperty("firstName").GetString().Should().Be("Alice");
|
||||
icu.GetProperty("lastName").GetString().Should().Be("Icu");
|
||||
icu.GetProperty("mrn").GetString().Should().Be("MRN-WARD-001");
|
||||
icu.GetProperty("roomBed").GetString().Should().Be("ICU-3");
|
||||
icu.GetProperty("news2Score").GetInt32().Should().Be(7);
|
||||
icu.GetProperty("openAlertCount").GetInt32().Should().Be(1);
|
||||
icu.GetProperty("qsofaScore").GetInt32().Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ListByDepartment_FiltersCorrectly()
|
||||
{
|
||||
var resp = await _client.GetAsync("/api/v1/encounters?status=ACTIVE&department=SURGERY");
|
||||
resp.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
var body = await resp.Content.ReadFromJsonAsync<JsonDocument>();
|
||||
var items = body!.RootElement.GetProperty("data").GetProperty("items");
|
||||
items.GetArrayLength().Should().Be(1);
|
||||
items[0].GetProperty("encounterId").GetGuid().Should().Be(_activeSurgeryEncounterId);
|
||||
items[0].GetProperty("firstName").GetString().Should().Be("Bob");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ListInvalidStatus_Returns400()
|
||||
{
|
||||
var resp = await _client.GetAsync("/api/v1/encounters?status=Active");
|
||||
resp.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StackExchange.Redis;
|
||||
|
||||
[Collection("Integration")]
|
||||
public class QsofaCurrentTests : IAsyncLifetime
|
||||
{
|
||||
private readonly ApiFixture _fixture;
|
||||
private readonly HttpClient _client;
|
||||
private Guid _encounterId;
|
||||
private Guid _patientId;
|
||||
|
||||
public QsofaCurrentTests(ApiFixture fixture)
|
||||
{
|
||||
_fixture = fixture;
|
||||
_client = fixture.CreateClient();
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
using var scope = _fixture.Services.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
await DbResetHelper.ResetAsync(db);
|
||||
|
||||
var patient = new Patient
|
||||
{
|
||||
Id = Guid.NewGuid(), Mrn = "MRN-QSOFA-API", FirstName = "qSOFA", LastName = "Api",
|
||||
DateOfBirth = new DateOnly(1960, 1, 1), Gender = "M", CreatedAt = DateTimeOffset.UtcNow
|
||||
};
|
||||
var encounter = new Encounter
|
||||
{
|
||||
Id = Guid.NewGuid(), PatientId = patient.Id, EncounterType = EncounterType.Inpatient,
|
||||
Status = EncounterStatus.Active, Department = Department.Icu,
|
||||
AttendingPhysician = "Dr. qSOFA", AdmittedAt = DateTimeOffset.UtcNow,
|
||||
CreatedAt = DateTimeOffset.UtcNow
|
||||
};
|
||||
db.Patients.Add(patient);
|
||||
db.Encounters.Add(encounter);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
_patientId = patient.Id;
|
||||
_encounterId = encounter.Id;
|
||||
|
||||
var redis = scope.ServiceProvider.GetRequiredService<IConnectionMultiplexer>();
|
||||
var cache = redis.GetDatabase();
|
||||
foreach (var key in QsofaCalculator.AllCriterionKeys(_encounterId))
|
||||
await cache.KeyDeleteAsync(key);
|
||||
}
|
||||
|
||||
public Task DisposeAsync() => Task.CompletedTask;
|
||||
|
||||
[Fact]
|
||||
public async Task Current_AfterBaselineVitals_ReturnsZeroCriteria()
|
||||
{
|
||||
using var scope = _fixture.Services.CreateScope();
|
||||
var detector = scope.ServiceProvider.GetRequiredService<QsofaDetector>();
|
||||
await detector.ProcessObservationAsync(_encounterId, _patientId, "RESP_RATE", 16m);
|
||||
|
||||
var resp = await _client.GetAsync($"/api/v1/encounters/{_encounterId}/qsofa/current");
|
||||
resp.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
var body = await resp.Content.ReadFromJsonAsync<JsonDocument>();
|
||||
body!.RootElement.GetProperty("data").GetProperty("activeCriteria").GetInt32()
|
||||
.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Current_AfterTwoCriteriaMet_ReturnsTwo()
|
||||
{
|
||||
using var scope = _fixture.Services.CreateScope();
|
||||
var detector = scope.ServiceProvider.GetRequiredService<QsofaDetector>();
|
||||
await detector.ProcessObservationAsync(_encounterId, _patientId, "RESP_RATE", 24m);
|
||||
await detector.ProcessObservationAsync(_encounterId, _patientId, "SYSTOLIC_BP", 95m);
|
||||
|
||||
var resp = await _client.GetAsync($"/api/v1/encounters/{_encounterId}/qsofa/current");
|
||||
resp.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
|
||||
var data = (await resp.Content.ReadFromJsonAsync<JsonDocument>())!
|
||||
.RootElement.GetProperty("data");
|
||||
data.GetProperty("activeCriteria").GetInt32().Should().Be(2);
|
||||
data.GetProperty("criteria").GetProperty("respRate").GetDecimal().Should().Be(24m);
|
||||
data.GetProperty("criteria").GetProperty("systolicBp").GetDecimal().Should().Be(95m);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Current_UnknownEncounter_Returns404()
|
||||
{
|
||||
var resp = await _client.GetAsync($"/api/v1/encounters/{Guid.NewGuid()}/qsofa/current");
|
||||
resp.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user