Files
ersatztv/ErsatzTV.Tests/Application/Plex/PlexHandlerTests.cs
T
timothy 4852c36268 feat(api): Plex media-source write API + lock-lifecycle fixes (#202 slice S2)
New PlexMediaSourcesController (/api/media-sources/plex) P1-P8 wrapping
existing MediatR commands: state GET, pin-flow, sign-out, per-server
libraries/path-replacements GET+PUT, and refresh — VMs projected to the
S0 shared DTOs, ApiResults mapping, 404 controller pre-checks, #215-style
409 lock guards, [EndpointGroupName("general")].

Lock-lifecycle hardening (the tricky part):
- TryCompletePlexPinFlowHandler now releases the Plex lock ONLY on its
  non-handoff exits (timeout-throw, poll exception, enqueue exception, the
  dead return-false) via try/catch — NOT an unconditional finally. On
  success the lock is handed off to SynchronizePlexMediaSources (the sole
  releaser after discovery); a finally would double-release and release
  before discovery, re-opening the finding-5 poll race. Fixes the latent
  leak where an abandoned pin flow wedged Plex locked until restart.
- StartPlexPinFlow controller compensates UnlockPlex on the Left branch AND
  any thrown dispatch/enqueue; only the Right/200 path holds the lock.
- SignOutOfPlexHandler wraps its work in try/finally { UnlockPlex() } — a
  terminal handler with no handoff, so unconditional release is correct.
- Post-save library sync enqueues SynchronizePlexLibraryByIdIfNeeded
  (Unlock:false) then SynchronizePlexNetworks (Unlock:true) — one lock, one
  release on the last message, compensating-unlock if the 2nd enqueue throws
  (corrects the Blazor Unlock-ordering bug, finding 6).

Data-integrity hardening:
- UpdatePlexPathReplacementsHandler rejects (422, no mutation) any positive
  Id not owned by the route source, blank RemotePath/LocalPath, and null
  list/items (findings 2c/8).
- MediaSourceRepository Plex path-replacement UPDATE gains
  AND PlexMediaSourceId = @id (Jellyfin/Emby untouched — slice S3).
- ReplaceLibraryPreferences controller validates the id set against the
  source's libraries (rejects unowned + Id=0), returns the reloaded list
  (ids change on disable).

Tests (NUnit/Shouldly/NSubstitute), 34 new, all green: pin-flow lock
released on thrown-cancellation/poll-throw/enqueue-throw AND held on
success (no double-release); sign-out finally-release under a throwing
dependency; cross-source/nonblank/null path-replacement 422s; library-prefs
id-not-owned 422; post-save enqueue exact messages + Unlock flags; full
controller route/404/409/422 coverage.

Refs #202
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 15:42:40 +02:00

258 lines
11 KiB
C#

using System.Threading.Channels;
using ErsatzTV.Application;
using ErsatzTV.Application.Plex;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Interfaces.Locking;
using ErsatzTV.Core.Interfaces.Plex;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Core.Interfaces.Search;
using ErsatzTV.Core.Plex;
using LanguageExt;
using NSubstitute;
using NUnit.Framework;
using Shouldly;
using static LanguageExt.Prelude;
using Unit = LanguageExt.Unit;
namespace ErsatzTV.Tests.Application.Plex;
[TestFixture]
public class PlexHandlerTests
{
// ----- TryCompletePlexPinFlowHandler: release ONLY on non-handoff exits (§C1.6 / finding 5) -----
[Test]
public async Task PinFlow_Should_Release_Lock_On_Thrown_Cancellation()
{
IPlexTvApiClient plexTvApiClient = Substitute.For<IPlexTvApiClient>();
var channel = Substitute.For<ChannelWriter<IPlexBackgroundServiceRequest>>();
IEntityLocker entityLocker = Substitute.For<IEntityLocker>();
using var externalCts = new CancellationTokenSource();
// poll returns false, then cancels the flow's token so Task.Delay THROWS (the real timeout path)
plexTvApiClient.TryCompletePinFlow(Arg.Any<PlexAuthPin>())
.Returns(_ =>
{
externalCts.Cancel();
return false;
});
var handler = new TryCompletePlexPinFlowHandler(plexTvApiClient, channel, entityLocker);
await Should.ThrowAsync<OperationCanceledException>(() =>
handler.Handle(new TryCompletePlexPinFlow(Pin()), externalCts.Token));
entityLocker.Received(1).UnlockPlex();
}
[Test]
public async Task PinFlow_Should_Release_Lock_On_Poll_Exception()
{
IPlexTvApiClient plexTvApiClient = Substitute.For<IPlexTvApiClient>();
var channel = Substitute.For<ChannelWriter<IPlexBackgroundServiceRequest>>();
IEntityLocker entityLocker = Substitute.For<IEntityLocker>();
plexTvApiClient.TryCompletePinFlow(Arg.Any<PlexAuthPin>())
.Returns<bool>(_ => throw new InvalidOperationException("poll boom"));
var handler = new TryCompletePlexPinFlowHandler(plexTvApiClient, channel, entityLocker);
await Should.ThrowAsync<InvalidOperationException>(() =>
handler.Handle(new TryCompletePlexPinFlow(Pin()), CancellationToken.None));
entityLocker.Received(1).UnlockPlex();
}
[Test]
public async Task PinFlow_Should_Release_Lock_On_Enqueue_Exception()
{
IPlexTvApiClient plexTvApiClient = Substitute.For<IPlexTvApiClient>();
var channel = Substitute.For<ChannelWriter<IPlexBackgroundServiceRequest>>();
IEntityLocker entityLocker = Substitute.For<IEntityLocker>();
plexTvApiClient.TryCompletePinFlow(Arg.Any<PlexAuthPin>()).Returns(true);
channel.WriteAsync(Arg.Any<IPlexBackgroundServiceRequest>(), Arg.Any<CancellationToken>())
.Returns<ValueTask>(_ => throw new InvalidOperationException("enqueue boom"));
var handler = new TryCompletePlexPinFlowHandler(plexTvApiClient, channel, entityLocker);
await Should.ThrowAsync<InvalidOperationException>(() =>
handler.Handle(new TryCompletePlexPinFlow(Pin()), CancellationToken.None));
entityLocker.Received(1).UnlockPlex();
}
[Test]
public async Task PinFlow_Success_Should_Not_Release_Lock_In_Handler()
{
IPlexTvApiClient plexTvApiClient = Substitute.For<IPlexTvApiClient>();
var channel = Substitute.For<ChannelWriter<IPlexBackgroundServiceRequest>>();
IEntityLocker entityLocker = Substitute.For<IEntityLocker>();
plexTvApiClient.TryCompletePinFlow(Arg.Any<PlexAuthPin>()).Returns(true);
var handler = new TryCompletePlexPinFlowHandler(plexTvApiClient, channel, entityLocker);
Either<BaseError, bool> result =
await handler.Handle(new TryCompletePlexPinFlow(Pin()), CancellationToken.None);
result.IsRight.ShouldBeTrue();
// the lock is handed off to SynchronizePlexMediaSources — the handler must NOT release it
entityLocker.DidNotReceive().UnlockPlex();
await channel.Received(1).WriteAsync(
Arg.Any<SynchronizePlexMediaSources>(),
Arg.Any<CancellationToken>());
}
// ----- SignOutOfPlexHandler: unconditional finally-release (§C6 / finding 7) -----
[Test]
public async Task SignOut_Should_Release_Lock_When_Dependency_Throws()
{
IMediaSourceRepository mediaSourceRepository = Substitute.For<IMediaSourceRepository>();
IPlexSecretStore plexSecretStore = Substitute.For<IPlexSecretStore>();
IEntityLocker entityLocker = Substitute.For<IEntityLocker>();
ISearchIndex searchIndex = Substitute.For<ISearchIndex>();
mediaSourceRepository.DeleteAllPlex()
.Returns<List<int>>(_ => throw new InvalidOperationException("delete boom"));
var handler = new SignOutOfPlexHandler(mediaSourceRepository, plexSecretStore, entityLocker, searchIndex);
await Should.ThrowAsync<InvalidOperationException>(() =>
handler.Handle(new SignOutOfPlex(), CancellationToken.None));
entityLocker.Received(1).UnlockPlex();
}
[Test]
public async Task SignOut_Should_Release_Lock_On_Success()
{
IMediaSourceRepository mediaSourceRepository = Substitute.For<IMediaSourceRepository>();
IPlexSecretStore plexSecretStore = Substitute.For<IPlexSecretStore>();
IEntityLocker entityLocker = Substitute.For<IEntityLocker>();
ISearchIndex searchIndex = Substitute.For<ISearchIndex>();
mediaSourceRepository.DeleteAllPlex().Returns(new List<int> { 1, 2 });
var handler = new SignOutOfPlexHandler(mediaSourceRepository, plexSecretStore, entityLocker, searchIndex);
Either<BaseError, Unit> result = await handler.Handle(new SignOutOfPlex(), CancellationToken.None);
result.IsRight.ShouldBeTrue();
entityLocker.Received(1).UnlockPlex();
}
// ----- UpdatePlexPathReplacementsHandler: ownership + nonblank + null validation (§C4b / findings 2c/8) -----
[Test]
public async Task PathReplacements_Should_Reject_CrossSource_Id_Without_Mutating()
{
IMediaSourceRepository mediaSourceRepository = Substitute.For<IMediaSourceRepository>();
mediaSourceRepository.GetPlex(1, Arg.Any<CancellationToken>())
.Returns(Option<PlexMediaSource>.Some(SourceWithReplacement(1, replacementId: 5)));
var handler = new UpdatePlexPathReplacementsHandler(mediaSourceRepository);
// id 99 is not owned by source 1 (its only replacement is id 5) → must not touch the repo
Either<BaseError, Unit> result = await handler.Handle(
new UpdatePlexPathReplacements(1, [new PlexPathReplacementItem(99, "/remote", "/local")]),
CancellationToken.None);
result.IsLeft.ShouldBeTrue();
await mediaSourceRepository.DidNotReceive().UpdatePathReplacements(
Arg.Any<int>(),
Arg.Any<List<PlexPathReplacement>>(),
Arg.Any<List<PlexPathReplacement>>(),
Arg.Any<List<PlexPathReplacement>>());
}
[Test]
public async Task PathReplacements_Should_Reject_Blank_Rows_Without_Mutating()
{
IMediaSourceRepository mediaSourceRepository = Substitute.For<IMediaSourceRepository>();
mediaSourceRepository.GetPlex(1, Arg.Any<CancellationToken>())
.Returns(Option<PlexMediaSource>.Some(SourceWithReplacement(1, replacementId: 5)));
var handler = new UpdatePlexPathReplacementsHandler(mediaSourceRepository);
Either<BaseError, Unit> result = await handler.Handle(
new UpdatePlexPathReplacements(1, [new PlexPathReplacementItem(0, " ", "/local")]),
CancellationToken.None);
result.IsLeft.ShouldBeTrue();
await mediaSourceRepository.DidNotReceive().UpdatePathReplacements(
Arg.Any<int>(),
Arg.Any<List<PlexPathReplacement>>(),
Arg.Any<List<PlexPathReplacement>>(),
Arg.Any<List<PlexPathReplacement>>());
}
[Test]
public async Task PathReplacements_Should_Reject_Null_Items_Without_Mutating()
{
IMediaSourceRepository mediaSourceRepository = Substitute.For<IMediaSourceRepository>();
mediaSourceRepository.GetPlex(1, Arg.Any<CancellationToken>())
.Returns(Option<PlexMediaSource>.Some(SourceWithReplacement(1, replacementId: 5)));
var handler = new UpdatePlexPathReplacementsHandler(mediaSourceRepository);
Either<BaseError, Unit> result = await handler.Handle(
new UpdatePlexPathReplacements(1, [null]),
CancellationToken.None);
result.IsLeft.ShouldBeTrue();
await mediaSourceRepository.DidNotReceive().UpdatePathReplacements(
Arg.Any<int>(),
Arg.Any<List<PlexPathReplacement>>(),
Arg.Any<List<PlexPathReplacement>>(),
Arg.Any<List<PlexPathReplacement>>());
}
[Test]
public async Task PathReplacements_Should_Accept_Owned_Rows_And_Mutate()
{
IMediaSourceRepository mediaSourceRepository = Substitute.For<IMediaSourceRepository>();
mediaSourceRepository.GetPlex(1, Arg.Any<CancellationToken>())
.Returns(Option<PlexMediaSource>.Some(SourceWithReplacement(1, replacementId: 5)));
mediaSourceRepository.UpdatePathReplacements(
Arg.Any<int>(),
Arg.Any<List<PlexPathReplacement>>(),
Arg.Any<List<PlexPathReplacement>>(),
Arg.Any<List<PlexPathReplacement>>())
.Returns(Unit.Default);
var handler = new UpdatePlexPathReplacementsHandler(mediaSourceRepository);
Either<BaseError, Unit> result = await handler.Handle(
new UpdatePlexPathReplacements(1, [new PlexPathReplacementItem(5, "/remote", "/local")]),
CancellationToken.None);
result.IsRight.ShouldBeTrue();
await mediaSourceRepository.Received(1).UpdatePathReplacements(
1,
Arg.Any<List<PlexPathReplacement>>(),
Arg.Any<List<PlexPathReplacement>>(),
Arg.Any<List<PlexPathReplacement>>());
}
private static PlexAuthPin Pin() => new(1, "code", "client-id");
private static PlexMediaSource SourceWithReplacement(int sourceId, int replacementId) =>
new()
{
Id = sourceId,
PathReplacements =
[
new PlexPathReplacement
{
Id = replacementId,
PlexPath = "/existing-remote",
LocalPath = "/existing-local",
PlexMediaSourceId = sourceId
}
]
};
}