From 83c9122b6f820a759a1f096c71db4f51cdacc8e0 Mon Sep 17 00:00:00 2001 From: Timothy Date: Thu, 2 Jul 2026 21:27:40 +0200 Subject: [PATCH 1/4] wip: partial implementation salvaged from interrupted workflow run MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Untrusted draft — no build/test had run yet. Review before building on it. Co-Authored-By: Claude Fable 5 --- ErsatzTV.Application/Health/Mapper.cs | 25 ++++ .../Queries/GetAllHealthCheckResultsForApi.cs | 5 + .../GetAllHealthCheckResultsForApiHandler.cs | 32 +++++ .../Api/Health/HealthCheckResponseModel.cs | 7 ++ ...AllHealthCheckResultsForApiHandlerTests.cs | 111 ++++++++++++++++++ .../Controllers/HealthControllerTests.cs | 40 +++++++ ErsatzTV/Controllers/Api/HealthController.cs | 18 +++ 7 files changed, 238 insertions(+) create mode 100644 ErsatzTV.Application/Health/Mapper.cs create mode 100644 ErsatzTV.Application/Health/Queries/GetAllHealthCheckResultsForApi.cs create mode 100644 ErsatzTV.Application/Health/Queries/GetAllHealthCheckResultsForApiHandler.cs create mode 100644 ErsatzTV.Core/Api/Health/HealthCheckResponseModel.cs create mode 100644 ErsatzTV.Tests/Application/Health/GetAllHealthCheckResultsForApiHandlerTests.cs create mode 100644 ErsatzTV.Tests/Controllers/HealthControllerTests.cs create mode 100644 ErsatzTV/Controllers/Api/HealthController.cs diff --git a/ErsatzTV.Application/Health/Mapper.cs b/ErsatzTV.Application/Health/Mapper.cs new file mode 100644 index 000000000..50c5fa61a --- /dev/null +++ b/ErsatzTV.Application/Health/Mapper.cs @@ -0,0 +1,25 @@ +using ErsatzTV.Core.Api.Health; +using ErsatzTV.Core.Health; + +namespace ErsatzTV.Application.Health; + +internal static class Mapper +{ + internal static HealthCheckResponseModel ProjectToResponseModel(HealthCheckResult result) => + new( + result.Title, + GetStatus(result.Status), + result.Message, + result.Link.Match(l => l.Link, () => null)); + + private static string GetStatus(HealthCheckStatus status) => + status switch + { + HealthCheckStatus.Pass => "pass", + HealthCheckStatus.Fail => "fail", + HealthCheckStatus.Warning => "warn", + HealthCheckStatus.Info => "info", + HealthCheckStatus.NotApplicable => "info", + _ => throw new ArgumentOutOfRangeException(nameof(status)) + }; +} diff --git a/ErsatzTV.Application/Health/Queries/GetAllHealthCheckResultsForApi.cs b/ErsatzTV.Application/Health/Queries/GetAllHealthCheckResultsForApi.cs new file mode 100644 index 000000000..a3062a2f7 --- /dev/null +++ b/ErsatzTV.Application/Health/Queries/GetAllHealthCheckResultsForApi.cs @@ -0,0 +1,5 @@ +using ErsatzTV.Core.Api.Health; + +namespace ErsatzTV.Application.Health; + +public record GetAllHealthCheckResultsForApi : IRequest>; diff --git a/ErsatzTV.Application/Health/Queries/GetAllHealthCheckResultsForApiHandler.cs b/ErsatzTV.Application/Health/Queries/GetAllHealthCheckResultsForApiHandler.cs new file mode 100644 index 000000000..334d4cd28 --- /dev/null +++ b/ErsatzTV.Application/Health/Queries/GetAllHealthCheckResultsForApiHandler.cs @@ -0,0 +1,32 @@ +using ErsatzTV.Core.Api.Health; +using ErsatzTV.Core.Health; +using static ErsatzTV.Application.Health.Mapper; + +namespace ErsatzTV.Application.Health; + +public class GetAllHealthCheckResultsForApiHandler + : IRequestHandler> +{ + private readonly IHealthCheckService _healthCheckService; + + public GetAllHealthCheckResultsForApiHandler(IHealthCheckService healthCheckService) => + _healthCheckService = healthCheckService; + + public async Task> Handle( + GetAllHealthCheckResultsForApi request, + CancellationToken cancellationToken) + { + try + { + List results = await _healthCheckService.PerformHealthChecks(cancellationToken); + return results + .Filter(r => r.Status != HealthCheckStatus.NotApplicable) + .Map(ProjectToResponseModel) + .ToList(); + } + catch (Exception ex) when (ex is TaskCanceledException or OperationCanceledException) + { + return []; + } + } +} diff --git a/ErsatzTV.Core/Api/Health/HealthCheckResponseModel.cs b/ErsatzTV.Core/Api/Health/HealthCheckResponseModel.cs new file mode 100644 index 000000000..37be2a34c --- /dev/null +++ b/ErsatzTV.Core/Api/Health/HealthCheckResponseModel.cs @@ -0,0 +1,7 @@ +namespace ErsatzTV.Core.Api.Health; + +public record HealthCheckResponseModel( + string Title, + string Status, + string Detail, + string Link); diff --git a/ErsatzTV.Tests/Application/Health/GetAllHealthCheckResultsForApiHandlerTests.cs b/ErsatzTV.Tests/Application/Health/GetAllHealthCheckResultsForApiHandlerTests.cs new file mode 100644 index 000000000..d0e9a0ff4 --- /dev/null +++ b/ErsatzTV.Tests/Application/Health/GetAllHealthCheckResultsForApiHandlerTests.cs @@ -0,0 +1,111 @@ +using ErsatzTV.Application.Health; +using ErsatzTV.Core.Api.Health; +using ErsatzTV.Core.Health; +using LanguageExt; +using NSubstitute; +using NUnit.Framework; +using Shouldly; + +namespace ErsatzTV.Tests.Application.Health; + +[TestFixture] +public class GetAllHealthCheckResultsForApiHandlerTests +{ + private IHealthCheckService _healthCheckService = null!; + private GetAllHealthCheckResultsForApiHandler _handler = null!; + + [SetUp] + public void SetUp() + { + _healthCheckService = Substitute.For(); + _handler = new GetAllHealthCheckResultsForApiHandler(_healthCheckService); + } + + [Test] + public async Task Should_Map_Status_Codes_To_Lowercase_Strings() + { + var results = new List + { + new("Pass Check", HealthCheckStatus.Pass, "all good", "ok", Option.None), + new("Fail Check", HealthCheckStatus.Fail, "broken", "bad", Option.None), + new("Warn Check", HealthCheckStatus.Warning, "watch out", "warn", Option.None), + new("Info Check", HealthCheckStatus.Info, "fyi", "info", Option.None) + }; + + _healthCheckService.PerformHealthChecks(Arg.Any()).Returns(results); + + List response = + await _handler.Handle(new GetAllHealthCheckResultsForApi(), CancellationToken.None); + + response.Count.ShouldBe(4); + response.Select(r => r.Status).ShouldBe(["pass", "fail", "warn", "info"]); + } + + [Test] + public async Task Should_Filter_Out_NotApplicable_Results() + { + var results = new List + { + new("Pass Check", HealthCheckStatus.Pass, "all good", "ok", Option.None), + new("NA Check", HealthCheckStatus.NotApplicable, "skip", "skip", Option.None) + }; + + _healthCheckService.PerformHealthChecks(Arg.Any()).Returns(results); + + List response = + await _handler.Handle(new GetAllHealthCheckResultsForApi(), CancellationToken.None); + + response.Count.ShouldBe(1); + response[0].Title.ShouldBe("Pass Check"); + } + + [Test] + public async Task Should_Include_Link_When_Present() + { + var results = new List + { + new( + "Linked Check", + HealthCheckStatus.Warning, + "detail message", + "brief", + Option.Some(new HealthCheckLink("https://example.com/docs"))) + }; + + _healthCheckService.PerformHealthChecks(Arg.Any()).Returns(results); + + List response = + await _handler.Handle(new GetAllHealthCheckResultsForApi(), CancellationToken.None); + + response[0].Link.ShouldBe("https://example.com/docs"); + response[0].Detail.ShouldBe("detail message"); + } + + [Test] + public async Task Should_Have_Null_Link_When_Absent() + { + var results = new List + { + new("No Link Check", HealthCheckStatus.Pass, "detail", "brief", Option.None) + }; + + _healthCheckService.PerformHealthChecks(Arg.Any()).Returns(results); + + List response = + await _handler.Handle(new GetAllHealthCheckResultsForApi(), CancellationToken.None); + + response[0].Link.ShouldBeNull(); + } + + [Test] + public async Task Should_Return_Empty_List_On_Cancellation() + { + _healthCheckService.PerformHealthChecks(Arg.Any()) + .Returns>>(_ => throw new TaskCanceledException()); + + List response = + await _handler.Handle(new GetAllHealthCheckResultsForApi(), CancellationToken.None); + + response.ShouldBeEmpty(); + } +} diff --git a/ErsatzTV.Tests/Controllers/HealthControllerTests.cs b/ErsatzTV.Tests/Controllers/HealthControllerTests.cs new file mode 100644 index 000000000..113732a89 --- /dev/null +++ b/ErsatzTV.Tests/Controllers/HealthControllerTests.cs @@ -0,0 +1,40 @@ +using ErsatzTV.Application.Health; +using ErsatzTV.Controllers.Api; +using ErsatzTV.Core.Api.Health; +using MediatR; +using NSubstitute; +using NUnit.Framework; +using Shouldly; + +namespace ErsatzTV.Tests.Controllers; + +[TestFixture] +public class HealthControllerTests +{ + private IMediator _mediator = null!; + private HealthController _controller = null!; + + [SetUp] + public void SetUp() + { + _mediator = Substitute.For(); + _controller = new HealthController(_mediator); + } + + [Test] + public async Task GetAll_Should_Return_Results_From_Mediator() + { + var expected = new List + { + new("Check One", "pass", "all good", null), + new("Check Two", "fail", "broken", "https://example.com") + }; + + _mediator.Send(Arg.Any(), Arg.Any()) + .Returns(expected); + + List result = await _controller.GetAll(CancellationToken.None); + + result.ShouldBe(expected); + } +} diff --git a/ErsatzTV/Controllers/Api/HealthController.cs b/ErsatzTV/Controllers/Api/HealthController.cs new file mode 100644 index 000000000..3c865fd49 --- /dev/null +++ b/ErsatzTV/Controllers/Api/HealthController.cs @@ -0,0 +1,18 @@ +using ErsatzTV.Application.Health; +using ErsatzTV.Core.Api.Health; +using MediatR; +using Microsoft.AspNetCore.Mvc; + +namespace ErsatzTV.Controllers.Api; + +[ApiController] +[EndpointGroupName("general")] +public class HealthController(IMediator mediator) : ControllerBase +{ + [HttpGet("/api/health")] + [Tags("Health")] + [EndpointSummary("Get health check results")] + [ProducesResponseType(typeof(List), StatusCodes.Status200OK)] + public async Task> GetAll(CancellationToken cancellationToken) => + await mediator.Send(new GetAllHealthCheckResultsForApi(), cancellationToken); +} -- 2.47.3 From 29407f637be45ec7ffece7096f8503dc37eba973 Mon Sep 17 00:00:00 2001 From: Timothy Date: Fri, 3 Jul 2026 21:14:42 +0200 Subject: [PATCH 2/4] fix(api): align health endpoint with API conventions (#108) Fix the WIP health-check API slice to match established patterns: - HealthController: add Name="GetHealthChecks" route name and move [EndpointGroupName("general")] to method level, matching FillerPresetController/FFmpegProfileController exactly (the precedent for parameterless 200-only GET actions). - HealthCheckResponseModel: enable #nullable for the file and mark Link as string? since the mapper can emit null when HealthCheckResult.Link is None. - Mapper: fix a real bug - LanguageExt's Option.Match throws ResultIsNullException.ResultIsNull if either branch returns null (by design, to catch accidental nulls). The WIP's `Link.Match(l => l.Link, () => null)` crashed on every health check without a Link. Switch to MatchUnsafe, the LanguageExt- sanctioned way to intentionally produce a nullable result from Option. - HealthControllerTests: add the idiomatic-route-assertion test (route template + Name) and empty-list case, matching FillerPresetControllerTests. Verified: GetAllHealthCheckResultsForApiHandler already matches the existing GetAllHealthCheckResultsHandler's cancellation handling (both swallow TaskCanceledException/OperationCanceledException), so no change was needed there. Confirmed no OpenApi contract test enumerates all endpoints for error-response metadata (it's an explicit TestCase allowlist), so the new GET needed no new entries. Co-Authored-By: Claude Fable 5 --- ErsatzTV.Application/Health/Mapper.cs | 2 +- .../Api/Health/HealthCheckResponseModel.cs | 3 ++- .../Controllers/HealthControllerTests.cs | 25 +++++++++++++++++++ ErsatzTV/Controllers/Api/HealthController.cs | 4 +-- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/ErsatzTV.Application/Health/Mapper.cs b/ErsatzTV.Application/Health/Mapper.cs index 50c5fa61a..bc5bd56d8 100644 --- a/ErsatzTV.Application/Health/Mapper.cs +++ b/ErsatzTV.Application/Health/Mapper.cs @@ -10,7 +10,7 @@ internal static class Mapper result.Title, GetStatus(result.Status), result.Message, - result.Link.Match(l => l.Link, () => null)); + result.Link.MatchUnsafe(l => l.Link, () => null)); private static string GetStatus(HealthCheckStatus status) => status switch diff --git a/ErsatzTV.Core/Api/Health/HealthCheckResponseModel.cs b/ErsatzTV.Core/Api/Health/HealthCheckResponseModel.cs index 37be2a34c..39cf3ae39 100644 --- a/ErsatzTV.Core/Api/Health/HealthCheckResponseModel.cs +++ b/ErsatzTV.Core/Api/Health/HealthCheckResponseModel.cs @@ -1,7 +1,8 @@ +#nullable enable namespace ErsatzTV.Core.Api.Health; public record HealthCheckResponseModel( string Title, string Status, string Detail, - string Link); + string? Link); diff --git a/ErsatzTV.Tests/Controllers/HealthControllerTests.cs b/ErsatzTV.Tests/Controllers/HealthControllerTests.cs index 113732a89..ca8cbfc2c 100644 --- a/ErsatzTV.Tests/Controllers/HealthControllerTests.cs +++ b/ErsatzTV.Tests/Controllers/HealthControllerTests.cs @@ -1,7 +1,9 @@ +using System.Reflection; using ErsatzTV.Application.Health; using ErsatzTV.Controllers.Api; using ErsatzTV.Core.Api.Health; using MediatR; +using Microsoft.AspNetCore.Mvc.Routing; using NSubstitute; using NUnit.Framework; using Shouldly; @@ -21,6 +23,18 @@ public class HealthControllerTests _controller = new HealthController(_mediator); } + [Test] + public void Controller_Should_Expose_Idiomatic_Rest_Route() + { + MethodInfo action = typeof(HealthController).GetMethod(nameof(HealthController.GetAll)) + ?? throw new AssertionException("Missing action GetAll"); + + HttpMethodAttribute attribute = action.GetCustomAttributes(inherit: true).Single(); + attribute.HttpMethods.ShouldContain("GET"); + attribute.Template.ShouldBe("/api/health"); + attribute.Name.ShouldBe("GetHealthChecks"); + } + [Test] public async Task GetAll_Should_Return_Results_From_Mediator() { @@ -37,4 +51,15 @@ public class HealthControllerTests result.ShouldBe(expected); } + + [Test] + public async Task GetAll_Should_Return_Empty_List_When_None_Exist() + { + _mediator.Send(Arg.Any(), Arg.Any()) + .Returns([]); + + List result = await _controller.GetAll(CancellationToken.None); + + result.ShouldBeEmpty(); + } } diff --git a/ErsatzTV/Controllers/Api/HealthController.cs b/ErsatzTV/Controllers/Api/HealthController.cs index 3c865fd49..9398c1e03 100644 --- a/ErsatzTV/Controllers/Api/HealthController.cs +++ b/ErsatzTV/Controllers/Api/HealthController.cs @@ -6,12 +6,12 @@ using Microsoft.AspNetCore.Mvc; namespace ErsatzTV.Controllers.Api; [ApiController] -[EndpointGroupName("general")] public class HealthController(IMediator mediator) : ControllerBase { - [HttpGet("/api/health")] + [HttpGet("/api/health", Name = "GetHealthChecks")] [Tags("Health")] [EndpointSummary("Get health check results")] + [EndpointGroupName("general")] [ProducesResponseType(typeof(List), StatusCodes.Status200OK)] public async Task> GetAll(CancellationToken cancellationToken) => await mediator.Send(new GetAllHealthCheckResultsForApi(), cancellationToken); -- 2.47.3 From 272174ee75171215408f3878f94239f1884f5091 Mon Sep 17 00:00:00 2001 From: Timothy Date: Fri, 3 Jul 2026 21:15:38 +0200 Subject: [PATCH 3/4] chore(openapi): regenerate v1.json for health endpoint (#108) Co-Authored-By: Claude Fable 5 --- ErsatzTV/wwwroot/openapi/v1.json | 69 ++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/ErsatzTV/wwwroot/openapi/v1.json b/ErsatzTV/wwwroot/openapi/v1.json index 3df42d662..a8e70ff8e 100644 --- a/ErsatzTV/wwwroot/openapi/v1.json +++ b/ErsatzTV/wwwroot/openapi/v1.json @@ -1433,6 +1433,46 @@ } } }, + "/api/health": { + "get": { + "tags": [ + "Health" + ], + "summary": "Get health check results", + "operationId": "GetHealthChecks", + "responses": { + "200": { + "description": "OK", + "content": { + "text/plain": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/HealthCheckResponseModel" + } + } + }, + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/HealthCheckResponseModel" + } + } + }, + "text/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/HealthCheckResponseModel" + } + } + } + } + } + } + } + }, "/api/libraries/{id}/scan": { "post": { "tags": [ @@ -4349,6 +4389,32 @@ ], "type": "string" }, + "HealthCheckResponseModel": { + "required": [ + "title", + "status", + "detail", + "link" + ], + "type": "object", + "properties": { + "title": { + "type": "string" + }, + "status": { + "type": "string" + }, + "detail": { + "type": "string" + }, + "link": { + "type": [ + "null", + "string" + ] + } + } + }, "HlsSessionModel": { "required": [ "channelNumber", @@ -5946,6 +6012,9 @@ { "name": "Graphics Elements" }, + { + "name": "Health" + }, { "name": "Libraries" }, -- 2.47.3 From 922bec4b244551597bbcfeaec954a9bb2c39731b Mon Sep 17 00:00:00 2001 From: Timothy Date: Fri, 3 Jul 2026 21:18:41 +0200 Subject: [PATCH 4/4] refactor(api): throw on unmapped health status instead of silent NotApplicable fallthrough (#108) Review finding: NotApplicable is filtered by the handler before mapping, so a silent ->"info" arm would only mask a missing filter if the mapper is ever reused unfiltered. Co-Authored-By: Claude Fable 5 --- ErsatzTV.Application/Health/Mapper.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ErsatzTV.Application/Health/Mapper.cs b/ErsatzTV.Application/Health/Mapper.cs index bc5bd56d8..5e4ca919b 100644 --- a/ErsatzTV.Application/Health/Mapper.cs +++ b/ErsatzTV.Application/Health/Mapper.cs @@ -19,7 +19,6 @@ internal static class Mapper HealthCheckStatus.Fail => "fail", HealthCheckStatus.Warning => "warn", HealthCheckStatus.Info => "info", - HealthCheckStatus.NotApplicable => "info", - _ => throw new ArgumentOutOfRangeException(nameof(status)) + _ => throw new ArgumentOutOfRangeException(nameof(status), status, null) }; } -- 2.47.3