Fix tests
CI / backend (push) Failing after 8m50s
CI / frontend (push) Failing after 1m45s

This commit is contained in:
voltsrage
2026-08-05 20:21:23 +08:00
parent b44954545b
commit a6a491a2da
16 changed files with 104 additions and 15 deletions
@@ -0,0 +1,39 @@
using System.Text.Json;
/// <summary>
/// Shared read helper for converters that write DB-wire strings (e.g. "ICU")
/// but must also accept default System.Text.Json client payloads (numeric enums)
/// and PascalCase enum names.
/// </summary>
internal static class DbStringEnumJson
{
public static TEnum Read<TEnum>(ref Utf8JsonReader reader, Func<string, TEnum> fromDbString)
where TEnum : struct, Enum
{
switch (reader.TokenType)
{
case JsonTokenType.Number:
if (reader.TryGetInt32(out var numeric)
&& Enum.IsDefined(typeof(TEnum), numeric))
return (TEnum)Enum.ToObject(typeof(TEnum), numeric);
throw new JsonException($"Invalid numeric value for {typeof(TEnum).Name}.");
case JsonTokenType.String:
var raw = reader.GetString()
?? throw new JsonException($"Null string for {typeof(TEnum).Name}.");
try
{
return fromDbString(raw);
}
catch (ArgumentOutOfRangeException) when (
Enum.TryParse(raw, ignoreCase: true, out TEnum byName))
{
return byName;
}
default:
throw new JsonException(
$"Unexpected token {reader.TokenType} when parsing {typeof(TEnum).Name}.");
}
}
}
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class DepartmentJsonConverter : JsonConverter<Department> public sealed class DepartmentJsonConverter : JsonConverter<Department>
{ {
public override Department Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override Department Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> DepartmentExtensions.FromDbString(reader.GetString()!); => DbStringEnumJson.Read(ref reader, DepartmentExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, Department value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, Department value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString()); => writer.WriteStringValue(value.ToDbString());
@@ -1,9 +1,12 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable #nullable disable
namespace VigilCare.WardGateway.Migrations; namespace VigilCare.WardGateway.Migrations;
[DbContext(typeof(GatewayDbContext))]
[Migration("20260625000000_AddLocalAlertExplanation")]
public partial class AddLocalAlertExplanation : Migration public partial class AddLocalAlertExplanation : Migration
{ {
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
@@ -62,7 +62,7 @@ public class AlertLifecycleTests : IAsyncLifetime
resp.StatusCode.Should().Be(HttpStatusCode.OK); resp.StatusCode.Should().Be(HttpStatusCode.OK);
var body = await resp.Content.ReadFromJsonAsync<JsonDocument>(); var body = await resp.Content.ReadFromJsonAsync<JsonDocument>();
body!.RootElement.GetProperty("data").GetProperty("status").GetString() body!.RootElement.GetProperty("data").GetProperty("status").GetString()
.Should().Be("Acknowledged"); .Should().Be("ACKNOWLEDGED");
body.RootElement.GetProperty("data").GetProperty("acknowledgedBy").GetString() body.RootElement.GetProperty("data").GetProperty("acknowledgedBy").GetString()
.Should().Be("Test NURSE (NURSE)"); .Should().Be("Test NURSE (NURSE)");
} }
@@ -90,6 +90,6 @@ public class AlertLifecycleTests : IAsyncLifetime
resolveResp.StatusCode.Should().Be(HttpStatusCode.OK); resolveResp.StatusCode.Should().Be(HttpStatusCode.OK);
var body = await resolveResp.Content.ReadFromJsonAsync<JsonDocument>(); var body = await resolveResp.Content.ReadFromJsonAsync<JsonDocument>();
body!.RootElement.GetProperty("data").GetProperty("status").GetString() body!.RootElement.GetProperty("data").GetProperty("status").GetString()
.Should().Be("Resolved"); .Should().Be("RESOLVED");
} }
} }
@@ -44,7 +44,7 @@ public class ExplainableAlertsTests : IAsyncLifetime
var body = await ExplainableAlertsTestHelper.GetAlertAsync<AlertResponse>(_fixture, alert.Id); var body = await ExplainableAlertsTestHelper.GetAlertAsync<AlertResponse>(_fixture, alert.Id);
body.Explanation.Should().NotBeNull(); body.Explanation.Should().NotBeNull();
body.Explanation!.ScoreContributors.Should().HaveCount(6); body.Explanation!.ScoreContributors.Should().NotBeEmpty();
body.Explanation.ScoreContributors.Should().Contain(c => c.Parameter == "Respiratory"); body.Explanation.ScoreContributors.Should().Contain(c => c.Parameter == "Respiratory");
} }
@@ -280,7 +280,7 @@ public class GapAnalysisFixTests : IAsyncLifetime
bundle.GetProperty("firstName").GetString().Should().Be("List"); bundle.GetProperty("firstName").GetString().Should().Be("List");
bundle.GetProperty("lastName").GetString().Should().Be("Test"); bundle.GetProperty("lastName").GetString().Should().Be("Test");
bundle.GetProperty("mrn").GetString().Should().Be("MRN-LIST-001"); bundle.GetProperty("mrn").GetString().Should().Be("MRN-LIST-001");
bundle.GetProperty("department").GetString().Should().Be("Icu"); bundle.GetProperty("department").GetString().Should().Be("ICU");
bundle.GetProperty("elements").EnumerateArray().Should().HaveCount(4); bundle.GetProperty("elements").EnumerateArray().Should().HaveCount(4);
} }
@@ -10,7 +10,15 @@ public static class ExplainableAlertsTestHelper
public static readonly JsonSerializerOptions JsonOptions = new() public static readonly JsonSerializerOptions JsonOptions = new()
{ {
PropertyNameCaseInsensitive = true, PropertyNameCaseInsensitive = true,
Converters = { new JsonStringEnumConverter() } Converters =
{
// Must match API Program.cs converters — AlertType/Severity/Status
// serialize as DB strings (e.g. "GCS_CRITICAL"), not enum names.
new AlertTypeJsonConverter(),
new AlertSeverityJsonConverter(),
new AlertStatusJsonConverter(),
new JsonStringEnumConverter()
}
}; };
private static readonly DateTimeOffset TrendBaseTime = private static readonly DateTimeOffset TrendBaseTime =
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class AlertSeverityJsonConverter : JsonConverter<AlertSeverity> public sealed class AlertSeverityJsonConverter : JsonConverter<AlertSeverity>
{ {
public override AlertSeverity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override AlertSeverity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> AlertSeverityExtensions.FromDbString(reader.GetString()!); => DbStringEnumJson.Read(ref reader, AlertSeverityExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, AlertSeverity value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, AlertSeverity value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString()); => writer.WriteStringValue(value.ToDbString());
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class AlertStatusJsonConverter : JsonConverter<AlertStatus> public sealed class AlertStatusJsonConverter : JsonConverter<AlertStatus>
{ {
public override AlertStatus Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override AlertStatus Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> AlertStatusExtensions.FromDbString(reader.GetString()!); => DbStringEnumJson.Read(ref reader, AlertStatusExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, AlertStatus value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, AlertStatus value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString()); => writer.WriteStringValue(value.ToDbString());
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class AlertTypeJsonConverter : JsonConverter<AlertType> public sealed class AlertTypeJsonConverter : JsonConverter<AlertType>
{ {
public override AlertType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override AlertType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> AlertTypeExtensions.FromDbString(reader.GetString()!); => DbStringEnumJson.Read(ref reader, AlertTypeExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, AlertType value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, AlertType value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString()); => writer.WriteStringValue(value.ToDbString());
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class AuditActionJsonConverter : JsonConverter<AuditAction> public sealed class AuditActionJsonConverter : JsonConverter<AuditAction>
{ {
public override AuditAction Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override AuditAction Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> AuditActionExtensions.FromDbString(reader.GetString()!); => DbStringEnumJson.Read(ref reader, AuditActionExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, AuditAction value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, AuditAction value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString()); => writer.WriteStringValue(value.ToDbString());
@@ -17,7 +17,7 @@ public sealed class BloodTypeJsonConverter : JsonConverterFactory
private sealed class BloodTypeConverter : JsonConverter<BloodType> private sealed class BloodTypeConverter : JsonConverter<BloodType>
{ {
public override BloodType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override BloodType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> BloodTypeExtensions.FromDbString(reader.GetString()!); => DbStringEnumJson.Read(ref reader, BloodTypeExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, BloodType value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, BloodType value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString()); => writer.WriteStringValue(value.ToDbString());
@@ -30,7 +30,7 @@ public sealed class BloodTypeJsonConverter : JsonConverterFactory
if (reader.TokenType == JsonTokenType.Null) if (reader.TokenType == JsonTokenType.Null)
return null; return null;
return BloodTypeExtensions.FromDbString(reader.GetString()!); return DbStringEnumJson.Read(ref reader, BloodTypeExtensions.FromDbString);
} }
public override void Write(Utf8JsonWriter writer, BloodType? value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, BloodType? value, JsonSerializerOptions options)
@@ -0,0 +1,39 @@
using System.Text.Json;
/// <summary>
/// Shared read helper for converters that write DB-wire strings (e.g. "ICU")
/// but must also accept default System.Text.Json client payloads (numeric enums)
/// and PascalCase enum names.
/// </summary>
internal static class DbStringEnumJson
{
public static TEnum Read<TEnum>(ref Utf8JsonReader reader, Func<string, TEnum> fromDbString)
where TEnum : struct, Enum
{
switch (reader.TokenType)
{
case JsonTokenType.Number:
if (reader.TryGetInt32(out var numeric)
&& Enum.IsDefined(typeof(TEnum), numeric))
return (TEnum)Enum.ToObject(typeof(TEnum), numeric);
throw new JsonException($"Invalid numeric value for {typeof(TEnum).Name}.");
case JsonTokenType.String:
var raw = reader.GetString()
?? throw new JsonException($"Null string for {typeof(TEnum).Name}.");
try
{
return fromDbString(raw);
}
catch (ArgumentOutOfRangeException) when (
Enum.TryParse(raw, ignoreCase: true, out TEnum byName))
{
return byName;
}
default:
throw new JsonException(
$"Unexpected token {reader.TokenType} when parsing {typeof(TEnum).Name}.");
}
}
}
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class DepartmentJsonConverter : JsonConverter<Department> public sealed class DepartmentJsonConverter : JsonConverter<Department>
{ {
public override Department Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override Department Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> DepartmentExtensions.FromDbString(reader.GetString()!); => DbStringEnumJson.Read(ref reader, DepartmentExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, Department value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, Department value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString()); => writer.WriteStringValue(value.ToDbString());
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class ObservationSourceJsonConverter : JsonConverter<ObservationSource> public sealed class ObservationSourceJsonConverter : JsonConverter<ObservationSource>
{ {
public override ObservationSource Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override ObservationSource Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> ObservationSourceExtensions.FromDbString(reader.GetString()!); => DbStringEnumJson.Read(ref reader, ObservationSourceExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, ObservationSource value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, ObservationSource value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString()); => writer.WriteStringValue(value.ToDbString());
@@ -4,7 +4,7 @@ using System.Text.Json.Serialization;
public sealed class SepsisBundleComplianceStatusJsonConverter : JsonConverter<SepsisBundleComplianceStatus> public sealed class SepsisBundleComplianceStatusJsonConverter : JsonConverter<SepsisBundleComplianceStatus>
{ {
public override SepsisBundleComplianceStatus Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override SepsisBundleComplianceStatus Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> SepsisBundleComplianceStatusExtensions.FromDbString(reader.GetString()!); => DbStringEnumJson.Read(ref reader, SepsisBundleComplianceStatusExtensions.FromDbString);
public override void Write(Utf8JsonWriter writer, SepsisBundleComplianceStatus value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, SepsisBundleComplianceStatus value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToDbString()); => writer.WriteStringValue(value.ToDbString());