Clears the still-live findings from #172 (verified against main; #2/#4/#7 and the auth/search/Trakt tail were already deliberate-documented or fixed since 2026-07-07). - Null/empty Name → 500 (10 create/replace handlers). Block/Template/DecoTemplate/Deco Create+Replace/Update + UpdateFFmpegProfile did `request.Name.Length > 50` on a client-nullable string → unhandled NullReferenceException → HTTP 500 (no global exception filter). Now `string.IsNullOrWhiteSpace(request.Name) || .Length > 50` → 422; also rejects empty/whitespace names, matching the group-create handlers' NotEmpty behavior. CreatePlaylist coalesces null→"" at the DTO so it was an empty-name persist, not a 500; guarded the same way. - ReplaceTemplateItems overlap validation iterated with an `item == otherItem` record value-equality skip, so two exact-duplicate items were value-equal and bypassed the intersection check (both persisted). Now index-based (i != j) so duplicates register as a self-intersection and are rejected 422. - Trimmed the unreachable 404 ProducesResponseType from POST /api/blocks/groups and POST /api/templates/groups (a create has no parent lookup that can 404); v1.json regenerated. - Regression tests: all 10 name-guard paths + the duplicate-items path (19 cases). - Docs: decisions.md entry + api-conventions.md §3b null-safe-validation bullet. fixes #172 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
215 lines
7.2 KiB
C#
215 lines
7.2 KiB
C#
using ErsatzTV.Application.FFmpegProfiles;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Errors;
|
|
using ErsatzTV.Core.FFmpeg;
|
|
using ErsatzTV.Core.Interfaces.Repositories;
|
|
using ErsatzTV.Core.Interfaces.Search;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Tests.Support;
|
|
using LanguageExt;
|
|
using NSubstitute;
|
|
using NUnit.Framework;
|
|
using Shouldly;
|
|
using static LanguageExt.Prelude;
|
|
using Unit = LanguageExt.Unit;
|
|
|
|
namespace ErsatzTV.Tests.Application.FFmpegProfiles;
|
|
|
|
[TestFixture]
|
|
public class FFmpegProfileHandlerTests
|
|
{
|
|
private InMemoryTvContext _db = null!;
|
|
private IConfigElementRepository _configElementRepository = null!;
|
|
private ISearchTargets _searchTargets = null!;
|
|
|
|
[SetUp]
|
|
public async Task SetUp()
|
|
{
|
|
_db = await InMemoryTvContext.CreateAsync();
|
|
_configElementRepository = Substitute.For<IConfigElementRepository>();
|
|
_configElementRepository.GetValue<int>(Arg.Any<ConfigElementKey>(), Arg.Any<CancellationToken>())
|
|
.Returns(Option<int>.None);
|
|
_searchTargets = Substitute.For<ISearchTargets>();
|
|
}
|
|
|
|
[TearDown]
|
|
public async Task TearDown() => await _db.DisposeAsync();
|
|
|
|
[Test]
|
|
public async Task Create_Should_Return_NotFoundError_When_Resolution_Missing()
|
|
{
|
|
var handler = new CreateFFmpegProfileHandler(_db.Factory, _searchTargets);
|
|
|
|
Either<BaseError, CreateFFmpegProfileResult> result =
|
|
await handler.Handle(MakeCreate(resolutionId: 999), CancellationToken.None);
|
|
|
|
LeftOf(result).ShouldBeOfType<NotFoundError>();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Update_Should_Return_NotFoundError_When_Profile_Missing()
|
|
{
|
|
var handler = new UpdateFFmpegProfileHandler(_db.Factory, _searchTargets);
|
|
|
|
Either<BaseError, UpdateFFmpegProfileResult> result =
|
|
await handler.Handle(MakeUpdate(999), CancellationToken.None);
|
|
|
|
LeftOf(result).ShouldBeOfType<NotFoundError>();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Update_Should_Return_NotFoundError_When_Resolution_Missing()
|
|
{
|
|
await SeedProfile(1);
|
|
var handler = new UpdateFFmpegProfileHandler(_db.Factory, _searchTargets);
|
|
|
|
Either<BaseError, UpdateFFmpegProfileResult> result =
|
|
await handler.Handle(MakeUpdate(1, resolutionId: 999), CancellationToken.None);
|
|
|
|
LeftOf(result).ShouldBeOfType<NotFoundError>();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Delete_Should_Return_NotFoundError_When_Profile_Missing()
|
|
{
|
|
var handler = new DeleteFFmpegProfileHandler(_db.Factory, _configElementRepository, _searchTargets);
|
|
|
|
Either<BaseError, Unit> result = await handler.Handle(new DeleteFFmpegProfile(999), CancellationToken.None);
|
|
|
|
LeftOf(result).ShouldBeOfType<NotFoundError>();
|
|
}
|
|
|
|
[Test]
|
|
public async Task Update_Should_Reject_Null_Name()
|
|
{
|
|
// Issue #172 FIX A: the handler guarded a client-nullable Name with a bare Name.Length
|
|
// check, so a null name threw a NullReferenceException (HTTP 500). The guard now returns a
|
|
// validation failure (Left) instead of throwing.
|
|
await SeedProfile(1);
|
|
await SeedResolution(1);
|
|
var handler = new UpdateFFmpegProfileHandler(_db.Factory, _searchTargets);
|
|
|
|
// Resolution seeded so the resolution-exists gate passes and the null name is the sole
|
|
// failure: the guard must return a Left (validation failure) rather than throwing an NRE.
|
|
Either<BaseError, UpdateFFmpegProfileResult> result =
|
|
await handler.Handle(MakeUpdate(1) with { Name = null! }, CancellationToken.None);
|
|
|
|
LeftOf(result).ShouldBeAssignableTo<BaseError>();
|
|
}
|
|
|
|
private static BaseError LeftOf<TR>(Either<BaseError, TR> either) =>
|
|
either.Match(Left: e => e, Right: _ => throw new AssertionException("Expected a Left result"));
|
|
|
|
private async Task SeedResolution(int id)
|
|
{
|
|
await using TvContext context = _db.CreateContext();
|
|
context.Resolutions.Add(new Resolution { Id = id, Name = "1920x1080", Width = 1920, Height = 1080 });
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
private async Task SeedProfile(int id)
|
|
{
|
|
await using TvContext context = _db.CreateContext();
|
|
context.FFmpegProfiles.Add(new FFmpegProfile
|
|
{
|
|
Id = id,
|
|
Name = "Default",
|
|
ThreadCount = 1,
|
|
NormalizeAudio = true,
|
|
NormalizeVideo = true,
|
|
HardwareAcceleration = HardwareAccelerationKind.None,
|
|
VaapiDisplay = "drm",
|
|
VaapiDriver = VaapiDriver.Default,
|
|
VaapiDevice = "/dev/dri/renderD128",
|
|
ResolutionId = 1,
|
|
ScalingBehavior = ScalingBehavior.ScaleAndPad,
|
|
PadMode = FilterMode.Software,
|
|
VideoFormat = FFmpegProfileVideoFormat.H264,
|
|
VideoProfile = string.Empty,
|
|
VideoPreset = string.Empty,
|
|
BitDepth = FFmpegProfileBitDepth.EightBit,
|
|
VideoBitrate = 2_000,
|
|
VideoBufferSize = 4_000,
|
|
TonemapAlgorithm = FFmpegProfileTonemapAlgorithm.Linear,
|
|
AudioFormat = FFmpegProfileAudioFormat.Aac,
|
|
AudioBitrate = 192,
|
|
AudioBufferSize = 384,
|
|
NormalizeLoudnessMode = NormalizeLoudnessMode.Off,
|
|
AudioChannels = 2,
|
|
AudioSampleRate = 48_000,
|
|
NormalizeFramerate = false,
|
|
NormalizeColors = false,
|
|
DeinterlaceVideo = false
|
|
});
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
private static CreateFFmpegProfile MakeCreate(int resolutionId) =>
|
|
new(
|
|
"Default",
|
|
1,
|
|
true,
|
|
true,
|
|
HardwareAccelerationKind.None,
|
|
"drm",
|
|
VaapiDriver.Default,
|
|
"/dev/dri/renderD128",
|
|
null,
|
|
resolutionId,
|
|
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 UpdateFFmpegProfile MakeUpdate(int id, int resolutionId = 1) =>
|
|
new(
|
|
id,
|
|
"Default",
|
|
1,
|
|
true,
|
|
true,
|
|
HardwareAccelerationKind.None,
|
|
"drm",
|
|
VaapiDriver.Default,
|
|
"/dev/dri/renderD128",
|
|
null,
|
|
resolutionId,
|
|
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);
|
|
}
|