Backend plumbing for the SPA transcoding editors (#143 blocker): - FFmpeg profile response DTO round-trip: add NormalizeAudio, NormalizeVideo, PadMode, TargetLoudness, NormalizeColors, ResolutionId (keep Resolution name for compat); DeinterlaceVideo now bool (was bool?). #nullable enable on the DTO. - New GET /api/ffmpeg/hardware-acceleration-kinds wrapping GetSupportedHardwareAccelerationKinds (returns enum names as strings). - Filler preset CRUD: GET-by-id/POST/PUT/DELETE on FillerPresetController with full request/response DTOs; new FillerPresetFullResponseModel + GetFillerPresetByIdForApi. CreateFillerPreset now returns the new id (CreateFillerPresetResult) to match sibling Create commands and enable a 201+Location; updated the one Blazor call site. - Watermark CRUD: GET-by-id/POST/PUT/DELETE on WatermarkController with full DTOs (path+contentType, all 15 fields); new WatermarkFullResponseModel + GetWatermarkByIdForApi. - Startup: register WatermarkLocation/WatermarkSize as OpenAPI string enums (they live in ErsatzTV.FFmpeg.State and were documented as ints, mismatching the Newtonsoft StringEnumConverter runtime serialization). - Tests: full CRUD controller tests for filler + watermark; contract-test entries (404/401/422) for the new mutating endpoints; regenerated v1.json. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
287 lines
9.6 KiB
C#
287 lines
9.6 KiB
C#
using System.Reflection;
|
|
using ErsatzTV.Application.FFmpegProfiles;
|
|
using ErsatzTV.Controllers.Api;
|
|
using ErsatzTV.Controllers.Api.Requests;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Api.FFmpegProfiles;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Errors;
|
|
using ErsatzTV.Core.FFmpeg;
|
|
using LanguageExt;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Routing;
|
|
using NSubstitute;
|
|
using NUnit.Framework;
|
|
using Shouldly;
|
|
using static LanguageExt.Prelude;
|
|
using Unit = LanguageExt.Unit;
|
|
|
|
namespace ErsatzTV.Tests.Controllers;
|
|
|
|
[TestFixture]
|
|
public class FFmpegProfileControllerTests
|
|
{
|
|
private FFmpegProfileController _controller = null!;
|
|
private IMediator _mediator = null!;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_mediator = Substitute.For<IMediator>();
|
|
_controller = new FFmpegProfileController(_mediator);
|
|
}
|
|
|
|
[Test]
|
|
public void Controller_Should_Expose_Idiomatic_Rest_Routes()
|
|
{
|
|
ShouldHaveActionRoute("GET", "/api/ffmpeg/profiles");
|
|
ShouldHaveActionRoute("GET", "/api/ffmpeg/profiles/{id:int}");
|
|
ShouldHaveActionRoute("POST", "/api/ffmpeg/profiles");
|
|
ShouldHaveActionRoute("PUT", "/api/ffmpeg/profiles/{id:int}");
|
|
ShouldHaveActionRoute("DELETE", "/api/ffmpeg/profiles/{id:int}");
|
|
}
|
|
|
|
[Test]
|
|
public void Mutations_Should_Use_Request_Dtos_For_Wire_Contract()
|
|
{
|
|
ParameterInfo createRequest = typeof(FFmpegProfileController)
|
|
.GetMethod(nameof(FFmpegProfileController.AddOne))!
|
|
.GetParameters()
|
|
.Single(p => p.Name == "request");
|
|
ParameterInfo updateRequest = typeof(FFmpegProfileController)
|
|
.GetMethod(nameof(FFmpegProfileController.UpdateOne))!
|
|
.GetParameters()
|
|
.Single(p => p.Name == "request");
|
|
|
|
createRequest.ParameterType.ShouldBe(typeof(CreateFFmpegProfileRequest));
|
|
updateRequest.ParameterType.ShouldBe(typeof(UpdateFFmpegProfileRequest));
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetById_Should_Return_200_For_Some()
|
|
{
|
|
FFmpegFullProfileResponseModel vm = MakeVm(4);
|
|
_mediator.Send(Arg.Any<GetFFmpegFullProfileByIdForApi>(), Arg.Any<CancellationToken>())
|
|
.Returns(Option<FFmpegFullProfileResponseModel>.Some(vm));
|
|
|
|
IActionResult result = await _controller.GetById(4, CancellationToken.None);
|
|
|
|
result.ShouldBeOfType<OkObjectResult>().Value.ShouldBe(vm);
|
|
}
|
|
|
|
[Test]
|
|
public async Task GetById_Should_Return_404_For_None()
|
|
{
|
|
_mediator.Send(Arg.Any<GetFFmpegFullProfileByIdForApi>(), Arg.Any<CancellationToken>())
|
|
.Returns(Option<FFmpegFullProfileResponseModel>.None);
|
|
|
|
IActionResult result = await _controller.GetById(4, CancellationToken.None);
|
|
|
|
var notFound = result.ShouldBeOfType<NotFoundObjectResult>();
|
|
var problemDetails = notFound.Value.ShouldBeOfType<ProblemDetails>();
|
|
problemDetails.Status.ShouldBe(404);
|
|
problemDetails.Title.ShouldBe("Resource not found");
|
|
}
|
|
|
|
[Test]
|
|
public async Task Create_Should_Return_201_With_Location_And_Body()
|
|
{
|
|
_mediator.Send(Arg.Any<CreateFFmpegProfile>(), Arg.Any<CancellationToken>())
|
|
.Returns(Right<BaseError, CreateFFmpegProfileResult>(new CreateFFmpegProfileResult(7)));
|
|
FFmpegFullProfileResponseModel vm = MakeVm(7);
|
|
_mediator.Send(Arg.Any<GetFFmpegFullProfileByIdForApi>(), Arg.Any<CancellationToken>())
|
|
.Returns(Option<FFmpegFullProfileResponseModel>.Some(vm));
|
|
|
|
IActionResult result = await _controller.AddOne(MakeCreateRequest(), CancellationToken.None);
|
|
|
|
var created = result.ShouldBeOfType<CreatedResult>();
|
|
created.StatusCode.ShouldBe(201);
|
|
created.Location.ShouldBe("/api/ffmpeg/profiles/7");
|
|
created.Value.ShouldBe(vm);
|
|
}
|
|
|
|
[Test]
|
|
public async Task Create_Should_Return_422_On_Validation_Error()
|
|
{
|
|
_mediator.Send(Arg.Any<CreateFFmpegProfile>(), Arg.Any<CancellationToken>())
|
|
.Returns(Left<BaseError, CreateFFmpegProfileResult>(BaseError.New("bad")));
|
|
|
|
IActionResult result = await _controller.AddOne(MakeCreateRequest(), CancellationToken.None);
|
|
|
|
result.ShouldBeOfType<UnprocessableEntityObjectResult>();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Create_Should_Return_404_For_NotFoundError()
|
|
{
|
|
_mediator.Send(Arg.Any<CreateFFmpegProfile>(), Arg.Any<CancellationToken>())
|
|
.Returns(Left<BaseError, CreateFFmpegProfileResult>(new NotFoundError("missing")));
|
|
|
|
IActionResult result = await _controller.AddOne(MakeCreateRequest(), CancellationToken.None);
|
|
|
|
result.ShouldBeOfType<NotFoundObjectResult>();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Update_Should_Return_200_And_Map_Route_Id_To_Command()
|
|
{
|
|
_mediator.Send(Arg.Any<UpdateFFmpegProfile>(), Arg.Any<CancellationToken>())
|
|
.Returns(Right<BaseError, UpdateFFmpegProfileResult>(new UpdateFFmpegProfileResult(8)));
|
|
FFmpegFullProfileResponseModel vm = MakeVm(8);
|
|
_mediator.Send(Arg.Any<GetFFmpegFullProfileByIdForApi>(), Arg.Any<CancellationToken>())
|
|
.Returns(Option<FFmpegFullProfileResponseModel>.Some(vm));
|
|
|
|
IActionResult result = await _controller.UpdateOne(8, MakeUpdateRequest(), CancellationToken.None);
|
|
|
|
result.ShouldBeOfType<OkObjectResult>().Value.ShouldBe(vm);
|
|
await _mediator.Received(1).Send(
|
|
Arg.Is<UpdateFFmpegProfile>(c => c.FFmpegProfileId == 8),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Test]
|
|
public async Task Update_Should_Return_404_For_NotFoundError()
|
|
{
|
|
_mediator.Send(Arg.Any<UpdateFFmpegProfile>(), Arg.Any<CancellationToken>())
|
|
.Returns(Left<BaseError, UpdateFFmpegProfileResult>(new NotFoundError("missing")));
|
|
|
|
IActionResult result = await _controller.UpdateOne(99, MakeUpdateRequest(), CancellationToken.None);
|
|
|
|
result.ShouldBeOfType<NotFoundObjectResult>();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_Should_Return_204_On_Success()
|
|
{
|
|
_mediator.Send(Arg.Any<DeleteFFmpegProfile>(), Arg.Any<CancellationToken>())
|
|
.Returns(Right<BaseError, Unit>(Unit.Default));
|
|
|
|
IActionResult result = await _controller.DeleteProfileAsync(9, CancellationToken.None);
|
|
|
|
result.ShouldBeOfType<NoContentResult>();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_Should_Return_404_For_NotFoundError()
|
|
{
|
|
_mediator.Send(Arg.Any<DeleteFFmpegProfile>(), Arg.Any<CancellationToken>())
|
|
.Returns(Left<BaseError, Unit>(new NotFoundError("missing")));
|
|
|
|
IActionResult result = await _controller.DeleteProfileAsync(9, CancellationToken.None);
|
|
|
|
result.ShouldBeOfType<NotFoundObjectResult>();
|
|
}
|
|
|
|
private static void ShouldHaveActionRoute(string httpMethod, string route)
|
|
{
|
|
bool exists = typeof(FFmpegProfileController)
|
|
.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
|
|
.SelectMany(m => m.GetCustomAttributes<HttpMethodAttribute>(inherit: true))
|
|
.Any(a => a.HttpMethods.Contains(httpMethod) && a.Template == route);
|
|
|
|
exists.ShouldBeTrue($"Missing route {httpMethod} {route}");
|
|
}
|
|
|
|
private static FFmpegFullProfileResponseModel MakeVm(int id) =>
|
|
new(
|
|
id,
|
|
"Default",
|
|
1,
|
|
true,
|
|
true,
|
|
HardwareAccelerationKind.None,
|
|
"drm",
|
|
VaapiDriver.Default,
|
|
"/dev/dri/renderD128",
|
|
null,
|
|
1,
|
|
"HD",
|
|
ScalingBehavior.ScaleAndPad,
|
|
FilterMode.Software,
|
|
FFmpegProfileVideoFormat.H264,
|
|
string.Empty,
|
|
string.Empty,
|
|
false,
|
|
FFmpegProfileBitDepth.EightBit,
|
|
2_000,
|
|
4_000,
|
|
FFmpegProfileTonemapAlgorithm.Linear,
|
|
FFmpegProfileAudioFormat.Aac,
|
|
192,
|
|
384,
|
|
NormalizeLoudnessMode.Off,
|
|
null,
|
|
2,
|
|
48_000,
|
|
false,
|
|
true,
|
|
false);
|
|
|
|
private static CreateFFmpegProfileRequest MakeCreateRequest() =>
|
|
new(
|
|
"Default",
|
|
1,
|
|
true,
|
|
true,
|
|
HardwareAccelerationKind.None,
|
|
"drm",
|
|
VaapiDriver.Default,
|
|
"/dev/dri/renderD128",
|
|
null,
|
|
1,
|
|
ScalingBehavior.ScaleAndPad,
|
|
FilterMode.Software,
|
|
FFmpegProfileVideoFormat.H264,
|
|
string.Empty,
|
|
string.Empty,
|
|
false,
|
|
FFmpegProfileBitDepth.EightBit,
|
|
2_000,
|
|
4_000,
|
|
FFmpegProfileTonemapAlgorithm.Linear,
|
|
FFmpegProfileAudioFormat.Aac,
|
|
192,
|
|
384,
|
|
NormalizeLoudnessMode.Off,
|
|
null,
|
|
2,
|
|
48_000,
|
|
false,
|
|
false,
|
|
false);
|
|
|
|
private static UpdateFFmpegProfileRequest MakeUpdateRequest() =>
|
|
new(
|
|
"Default",
|
|
1,
|
|
true,
|
|
true,
|
|
HardwareAccelerationKind.None,
|
|
"drm",
|
|
VaapiDriver.Default,
|
|
"/dev/dri/renderD128",
|
|
null,
|
|
1,
|
|
ScalingBehavior.ScaleAndPad,
|
|
FilterMode.Software,
|
|
FFmpegProfileVideoFormat.H264,
|
|
string.Empty,
|
|
string.Empty,
|
|
false,
|
|
FFmpegProfileBitDepth.EightBit,
|
|
2_000,
|
|
4_000,
|
|
FFmpegProfileTonemapAlgorithm.Linear,
|
|
FFmpegProfileAudioFormat.Aac,
|
|
192,
|
|
384,
|
|
NormalizeLoudnessMode.Off,
|
|
null,
|
|
2,
|
|
48_000,
|
|
false,
|
|
false,
|
|
false);
|
|
}
|