Files
ersatztv/ErsatzTV.Tests/Controllers/TroubleshootControllerTests.cs
T
timothyandClaude Opus 4.8 5e5f0af684 fix(235-A): normalize async-op error contracts on Maintenance + Troubleshoot controllers (#235)
Slice A of the async-op API contract normalization.

MaintenanceController:
- EmptyTrash error path: was 500 text/plain (error.ToString()); now maps the
  BaseError Left through ApiResults.ToErrorResult() -> 404 (NotFoundError) / 422
  ProblemDetails. Success stays 200 OkResult. Added ProducesResponseType 200 + 422.
- CleanArtwork: fire-and-forget enqueue of DeleteOrphanedArtwork was a silent 200;
  now returns 202 Accepted (AcceptedResult) since it queues background work.
  Added ProducesResponseType 202. (Controller does not derive from ControllerBase,
  so results are built directly as before.)

TroubleshootController.TroubleshootPlayback (GET|HEAD /api/troubleshoot/playback.m3u8):
- Two bare body-less NotFound() call sites conflated "not found" with "prepare/
  playback failure". Both now return a ProblemDetails body:
  * prepare-failure (result.IsLeft): mapped through error.ToErrorResult() -> 404 for
    NotFoundError (unknown media item/channel) else 422 for a validation BaseError.
  * terminal fall-through (prepare ok but no playable output): kept 404 with a
    distinguishing ApiResults.NotFoundProblem(...) detail.
- Added ProducesResponseType 404 + 422 (409 already present).

Consumer check: the SPA (PlaybackTroubleshootingScreen) feeds the playback.m3u8 URL
straight to hls.js via HlsPlayer, which never inspects the HTTP status code — playback
state is surfaced via the separate /api/troubleshoot/playback/status poll. So the
404->422 split for the validation subcase is safe; no player code branches on the
status code.

Tests: MaintenanceControllerTests (200/422/202 + enqueue assertion),
TroubleshootControllerTests (prepare 404 NotFoundError, 422 validation). All green;
Api error-metadata/contract/security scans still pass.

Note: OpenAPI artifacts (v1.json / v1.d.ts) intentionally NOT regenerated here — the
orchestrator regenerates once after all #235 slices merge.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 17:54:21 +02:00

401 lines
15 KiB
C#

using System.IO.Abstractions;
using System.Reflection;
using System.Text.Json;
using System.Threading.Channels;
using ErsatzTV.Application;
using ErsatzTV.Application.Channels;
using ErsatzTV.Application.MediaItems;
using ErsatzTV.Application.Troubleshooting;
using ErsatzTV.Application.Troubleshooting.Queries;
using ErsatzTV.Controllers.Api;
using ErsatzTV.Controllers.Api.Requests;
using ErsatzTV.Core;
using ErsatzTV.Core.Api.Troubleshooting;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Interfaces.FFmpeg;
using ErsatzTV.Core.Interfaces.Locking;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Core.Interfaces.Troubleshooting;
using ErsatzTV.Core.Notifications;
using ErsatzTV.Core.Troubleshooting;
using LanguageExt;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using NSubstitute;
using NUnit.Framework;
using Shouldly;
namespace ErsatzTV.Tests.Controllers;
[TestFixture]
public class TroubleshootControllerTests
{
private TroubleshootController _controller = null!;
private IMediator _mediator = null!;
private IEntityLocker _entityLocker = null!;
private TroubleshootingPlaybackStatusStore _statusStore = null!;
[SetUp]
public void SetUp()
{
_mediator = Substitute.For<IMediator>();
_entityLocker = Substitute.For<IEntityLocker>();
_statusStore = new TroubleshootingPlaybackStatusStore();
_controller = new TroubleshootController(
System.Threading.Channels.Channel.CreateUnbounded<IFFmpegWorkerRequest>().Writer,
Substitute.For<IFileSystem>(),
Substitute.For<IConfigElementRepository>(),
Substitute.For<ITroubleshootingNotifier>(),
_entityLocker,
_statusStore,
_mediator);
}
private static MediaItemInfo FakeMediaItemInfo() =>
new(
1,
"Title",
"Movie",
"LocalLibrary",
null,
"Movies",
MediaItemState.Normal,
TimeSpan.FromMinutes(90),
"1:1",
"16:9",
"24000/1001",
VideoScanKind.Progressive,
null,
1920,
1080,
[],
[]);
[Test]
public void Controller_Should_Expose_Idiomatic_Rest_Route_For_Info()
{
MethodInfo action = typeof(TroubleshootController).GetMethod(nameof(TroubleshootController.GetInfo))
?? throw new AssertionException("Missing action GetInfo");
var attribute = action.GetCustomAttributes<HttpGetAttribute>().Single();
attribute.Template.ShouldBe("api/troubleshoot/info");
attribute.Name.ShouldBe("GetTroubleshootingInfo");
}
[Test]
public async Task GetInfo_Should_Serialize_General_Section_And_Carry_Platform_Capabilities()
{
var info = new TroubleshootingInfo(
"1.2.3",
new Dictionary<string, string> { ["ETV_FOO"] = "bar" },
[],
[],
[],
new ErsatzTV.Application.FFmpegProfiles.FFmpegSettingsViewModel(),
[],
[],
[],
false,
false,
"nvidia output",
"qsv output",
"vaapi output",
"videotoolbox output");
_mediator.Send(Arg.Any<GetTroubleshootingInfo>(), Arg.Any<CancellationToken>())
.Returns(info);
TroubleshootingInfoResponseModel result = await _controller.GetInfo(CancellationToken.None);
result.NvidiaCapabilities.ShouldBe("nvidia output");
result.QsvCapabilities.ShouldBe("qsv output");
result.VaapiCapabilities.ShouldBe("vaapi output");
result.VideoToolboxCapabilities.ShouldBe("videotoolbox output");
using JsonDocument document = JsonDocument.Parse(result.GeneralJson);
document.RootElement.GetProperty("Version").GetString().ShouldBe("1.2.3");
document.RootElement.GetProperty("Environment").GetProperty("ETV_FOO").GetString().ShouldBe("bar");
document.RootElement.GetProperty("AviSynth").GetProperty("Demuxer").GetBoolean().ShouldBeFalse();
}
[Test]
public void Controller_Should_Expose_Idiomatic_Rest_Route_For_ValidateSchedule()
{
MethodInfo action = typeof(TroubleshootController).GetMethod(nameof(TroubleshootController.ValidateSchedule))
?? throw new AssertionException("Missing action ValidateSchedule");
var attribute = action.GetCustomAttributes<HttpPostAttribute>().Single();
attribute.Template.ShouldBe("api/troubleshoot/validate-schedule");
attribute.Name.ShouldBe("ValidateSequentialSchedule");
}
[Test]
public void ValidateSchedule_Should_Use_Stable_Request_Dto()
{
MethodInfo action = typeof(TroubleshootController).GetMethod(nameof(TroubleshootController.ValidateSchedule))
?? throw new AssertionException("Missing action ValidateSchedule");
action.GetParameters()[0].ParameterType.ShouldBe(typeof(ValidateSequentialScheduleRequest));
}
[TestCase("")]
[TestCase(" ")]
public async Task ValidateSchedule_Should_Return_400_For_Empty_Yaml(string yaml)
{
IActionResult result = await _controller.ValidateSchedule(
new ValidateSequentialScheduleRequest(yaml, false),
CancellationToken.None);
var badRequest = result.ShouldBeOfType<BadRequestObjectResult>();
badRequest.Value.ShouldBeOfType<ProblemDetails>().Status.ShouldBe(400);
await _mediator.DidNotReceive().Send(Arg.Any<ValidateSequentialSchedule>(), Arg.Any<CancellationToken>());
}
[Test]
public async Task ValidateSchedule_Should_Map_Result_To_Response()
{
_mediator.Send(Arg.Any<ValidateSequentialSchedule>(), Arg.Any<CancellationToken>())
.Returns(new ValidateSequentialScheduleViewModel(false, ["boom"], "{}"));
IActionResult result = await _controller.ValidateSchedule(
new ValidateSequentialScheduleRequest("content: []", true),
CancellationToken.None);
var response = result.ShouldBeOfType<OkObjectResult>().Value
.ShouldBeOfType<ValidateSequentialScheduleResponseModel>();
response.IsValid.ShouldBeFalse();
response.Messages.ShouldBe(["boom"]);
response.Json.ShouldBe("{}");
await _mediator.Received(1).Send(
Arg.Is<ValidateSequentialSchedule>(q => q.Yaml == "content: []" && q.IsImport),
Arg.Any<CancellationToken>());
}
[Test]
public void Controller_Should_Expose_Idiomatic_Rest_Route_For_StreamSelectors()
{
MethodInfo action = typeof(TroubleshootController).GetMethod(nameof(TroubleshootController.GetStreamSelectors))
?? throw new AssertionException("Missing action GetStreamSelectors");
var attribute = action.GetCustomAttributes<HttpGetAttribute>().Single();
attribute.Template.ShouldBe("api/troubleshoot/playback/stream-selectors");
attribute.Name.ShouldBe("GetTroubleshootingStreamSelectors");
}
[Test]
public async Task GetStreamSelectors_Should_Return_Selector_Names()
{
_mediator.Send(Arg.Any<GetChannelStreamSelectors>(), Arg.Any<CancellationToken>())
.Returns(["a.yml", "b.yml"]);
List<string> result = await _controller.GetStreamSelectors(CancellationToken.None);
result.ShouldBe(["a.yml", "b.yml"]);
await _mediator.Received(1).Send(Arg.Any<GetChannelStreamSelectors>(), Arg.Any<CancellationToken>());
}
[Test]
public void Controller_Should_Expose_Idiomatic_Rest_Route_For_Subtitles()
{
MethodInfo action = typeof(TroubleshootController).GetMethod(nameof(TroubleshootController.GetSubtitles))
?? throw new AssertionException("Missing action GetSubtitles");
var attribute = action.GetCustomAttributes<HttpGetAttribute>().Single();
attribute.Template.ShouldBe("api/troubleshoot/playback/subtitles/{mediaItemId:int}");
attribute.Name.ShouldBe("GetTroubleshootingSubtitles");
}
[Test]
public async Task GetSubtitles_Should_Return_404_For_Unknown_Media_Item()
{
_mediator.Send(Arg.Any<GetMediaItemInfo>(), Arg.Any<CancellationToken>())
.Returns(Either<BaseError, MediaItemInfo>.Left(BaseError.New("nope")));
IActionResult result = await _controller.GetSubtitles(999, CancellationToken.None);
var notFound = result.ShouldBeOfType<NotFoundObjectResult>();
notFound.Value.ShouldBeOfType<ProblemDetails>().Status.ShouldBe(404);
await _mediator.DidNotReceive().Send(Arg.Any<GetTroubleshootingSubtitles>(), Arg.Any<CancellationToken>());
}
[Test]
public async Task GetSubtitles_Should_Map_ViewModels_To_Response()
{
_mediator.Send(Arg.Any<GetMediaItemInfo>(), Arg.Any<CancellationToken>())
.Returns(Either<BaseError, MediaItemInfo>.Right(FakeMediaItemInfo()));
_mediator.Send(Arg.Any<GetTroubleshootingSubtitles>(), Arg.Any<CancellationToken>())
.Returns(new List<SubtitleViewModel> { new(7, "eng", "English", "subrip") });
IActionResult result = await _controller.GetSubtitles(1, CancellationToken.None);
var subtitles = result.ShouldBeOfType<OkObjectResult>().Value
.ShouldBeOfType<List<TroubleshootingSubtitleResponseModel>>();
subtitles.Count.ShouldBe(1);
subtitles[0].Id.ShouldBe(7);
subtitles[0].Language.ShouldBe("eng");
subtitles[0].Title.ShouldBe("English");
subtitles[0].Codec.ShouldBe("subrip");
}
[Test]
public void Controller_Should_Expose_Idiomatic_Rest_Route_For_PlaybackStatus()
{
MethodInfo action = typeof(TroubleshootController).GetMethod(nameof(TroubleshootController.GetPlaybackStatus))
?? throw new AssertionException("Missing action GetPlaybackStatus");
var attribute = action.GetCustomAttributes<HttpGetAttribute>().Single();
attribute.Template.ShouldBe("api/troubleshoot/playback/status");
attribute.Name.ShouldBe("GetTroubleshootingPlaybackStatus");
}
[Test]
public async Task TroubleshootPlayback_Should_Return_409_When_Already_Locked()
{
_entityLocker.IsTroubleshootingPlaybackLocked().Returns(true);
IActionResult result = await _controller.TroubleshootPlayback(
mediaItem: 1,
channel: 0,
ffmpegProfile: 1,
StreamingMode.HttpLiveStreamingSegmenter,
watermark: [],
graphicsElement: [],
streamSelector: string.Empty,
subtitleId: null,
seekSeconds: 0,
start: null,
CancellationToken.None);
ConflictObjectResult conflict = result.ShouldBeOfType<ConflictObjectResult>();
conflict.Value.ShouldBeOfType<ProblemDetails>().Status.ShouldBe(409);
// pre-check short-circuits before dispatching any work
await _mediator.DidNotReceive()
.Send(Arg.Any<PrepareTroubleshootingPlayback>(), Arg.Any<CancellationToken>());
}
[Test]
public async Task TroubleshootPlayback_Should_Return_404_ProblemDetails_When_Prepare_Not_Found()
{
_entityLocker.IsTroubleshootingPlaybackLocked().Returns(false);
_mediator.Send(Arg.Any<PrepareTroubleshootingPlayback>(), Arg.Any<CancellationToken>())
.Returns(Either<BaseError, PlayoutItemResult>.Left(
new ErsatzTV.Core.Errors.NotFoundError("no such media item")));
IActionResult result = await _controller.TroubleshootPlayback(
mediaItem: 999,
channel: 0,
ffmpegProfile: 1,
StreamingMode.HttpLiveStreamingSegmenter,
watermark: [],
graphicsElement: [],
streamSelector: string.Empty,
subtitleId: null,
seekSeconds: 0,
start: null,
CancellationToken.None);
var notFound = result.ShouldBeOfType<NotFoundObjectResult>();
notFound.Value.ShouldBeOfType<ProblemDetails>().Status.ShouldBe(404);
}
[Test]
public async Task TroubleshootPlayback_Should_Return_422_ProblemDetails_When_Prepare_Fails_Validation()
{
_entityLocker.IsTroubleshootingPlaybackLocked().Returns(false);
_mediator.Send(Arg.Any<PrepareTroubleshootingPlayback>(), Arg.Any<CancellationToken>())
.Returns(Either<BaseError, PlayoutItemResult>.Left(BaseError.New("unable to prepare")));
IActionResult result = await _controller.TroubleshootPlayback(
mediaItem: 1,
channel: 0,
ffmpegProfile: 1,
StreamingMode.HttpLiveStreamingSegmenter,
watermark: [],
graphicsElement: [],
streamSelector: string.Empty,
subtitleId: null,
seekSeconds: 0,
start: null,
CancellationToken.None);
var unprocessable = result.ShouldBeOfType<UnprocessableEntityObjectResult>();
unprocessable.Value.ShouldBeOfType<ProblemDetails>().Status.ShouldBe(422);
}
[Test]
public async Task GetPlaybackStatus_Should_Report_Idle_When_No_Result_And_Unlocked()
{
_entityLocker.IsTroubleshootingPlaybackLocked().Returns(false);
TroubleshootingPlaybackStatusResponseModel result =
await _controller.GetPlaybackStatus(CancellationToken.None);
result.State.ShouldBe("idle");
result.ExitCode.ShouldBeNull();
result.Speed.ShouldBeNull();
}
[Test]
public async Task GetPlaybackStatus_Should_Report_Running_When_Locked()
{
_entityLocker.IsTroubleshootingPlaybackLocked().Returns(true);
TroubleshootingPlaybackStatusResponseModel result =
await _controller.GetPlaybackStatus(CancellationToken.None);
result.State.ShouldBe("running");
}
[Test]
public async Task GetPlaybackStatus_Should_Report_Completed_With_ExitCode_And_Speed()
{
_entityLocker.IsTroubleshootingPlaybackLocked().Returns(false);
_statusStore.RecordCompletion(0, 1.5);
TroubleshootingPlaybackStatusResponseModel result =
await _controller.GetPlaybackStatus(CancellationToken.None);
result.State.ShouldBe("completed");
result.ExitCode.ShouldBe(0);
result.Speed.ShouldBe(1.5);
}
[Test]
public async Task GetPlaybackStatus_Should_Report_Failed_For_NonZero_ExitCode()
{
_entityLocker.IsTroubleshootingPlaybackLocked().Returns(false);
_statusStore.RecordCompletion(1, Option<double>.None);
TroubleshootingPlaybackStatusResponseModel result =
await _controller.GetPlaybackStatus(CancellationToken.None);
result.State.ShouldBe("failed");
result.ExitCode.ShouldBe(1);
result.Speed.ShouldBeNull();
}
[Test]
public async Task RecordTroubleshootingPlaybackStatusHandler_Should_Record_Result_In_Store()
{
var store = new TroubleshootingPlaybackStatusStore();
var handler = new RecordTroubleshootingPlaybackStatusHandler(store);
store.CurrentResult.IsNone.ShouldBeTrue();
await handler.Handle(
new PlaybackTroubleshootingCompletedNotification(0, Option<Exception>.None, 2.0),
CancellationToken.None);
store.CurrentResult.IsSome.ShouldBeTrue();
TroubleshootingPlaybackResult recorded =
store.CurrentResult.IfNone(() => throw new AssertionException("Expected a recorded result"));
recorded.ExitCode.ShouldBe(0);
recorded.Speed.IfNone(-1).ShouldBe(2.0);
store.Reset();
store.CurrentResult.IsNone.ShouldBeTrue();
}
}