Files
ersatztv/ErsatzTV.Tests/Application/MediaCollections/CollectionCustomOrderConcurrencyTests.cs
T
timothyandClaude Opus 4.8 c40ffefa99
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 5s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m11s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 9m51s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
feat(253): PR3 optimistic-concurrency fan-out — Diff + Scalar aggregates
Fans the frozen Block recipe (api-conventions §7a) across the five Diff/Scalar
replace-all endpoints, completing the #253 PR2→PR4 arc's implementable core:

- #6 Collection custom-order, #7 Playout alternate-schedules, #8 Playout templates
  (shared Playout.Version), #9 MultiCollection, #10 RerunCollection — each: pre-check
  412 as a standalone Either after validation (H2, subtype survives the Join flatten),
  unconditional Version++ (M1), guarded save, controller If-Match/ETag/400/412, SPA
  editor ETag round-trip + 412 conflict dialog.
- H1: the two Playout handlers' catch(Exception)→422 restructured so the guard's
  PreconditionFailedError returns before the catch (412, not 422).
- M2: RerunCollection/Collection refresh runs unconditionally on save; MultiCollection
  keeps its name-only→no-rebuild optimization by bumping on the first (name) save.
- H3: UpdateDefaultDecoHandler bulk-bumps Playout.Version via .SetProperty.
- Shared ConcurrencyHeaders.MalformedIfMatchProblem() for the 400 guard.
- Deferred (→ #269): same-root non-bulk sibling config writers' ETag rotation.

Tests: per-handler pre-check/bump concurrency tests (Playout ×2 incl. non-vacuous
racing-save backstop, Rerun, Multi incl. name-only-no-rebuild M2, Collection);
controller tests get a DefaultHttpContext for the header read/write. Docs:
api-conventions §7a fan-out status, spa-conventions §4a list-editor note, decisions.

Refs #253

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

83 lines
2.8 KiB
C#

using System.Threading.Channels;
using ErsatzTV.Application;
using ErsatzTV.Application.MediaCollections;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Infrastructure.Data;
using ErsatzTV.Tests.Support;
using LanguageExt;
using Microsoft.EntityFrameworkCore;
using NSubstitute;
using NUnit.Framework;
using Shouldly;
using Unit = LanguageExt.Unit;
using static LanguageExt.Prelude;
namespace ErsatzTV.Tests.Application.MediaCollections;
/// <summary>
/// #253 optimistic-concurrency contract on the Collection custom-order handler (#6): the pre-check
/// 412 (standalone Either after validation, not flattened to 422) and the unconditional Version bump.
/// </summary>
[TestFixture]
public class CollectionCustomOrderConcurrencyTests : MediaCollectionHandlerTestBase
{
private async Task SeedCollectionWithVersion(int id, int version, string name = "Collection")
{
await using TvContext context = Db.CreateContext();
context.Collections.Add(new Collection
{
Id = id,
Name = name,
Version = version,
MediaItems = [],
CollectionItems = []
});
await context.SaveChangesAsync();
}
private async Task<int> ReadVersion(int id)
{
await using TvContext context = Db.CreateContext();
return await context.Collections.Where(c => c.Id == id).Select(c => c.Version).SingleAsync();
}
private UpdateCollectionCustomOrderHandler MakeHandler()
{
IMediaCollectionRepository repo = Substitute.For<IMediaCollectionRepository>();
repo.PlayoutIdsUsingCollection(Arg.Any<int>()).Returns([]);
return new UpdateCollectionCustomOrderHandler(Db.Factory, repo, Worker);
}
private static BaseError LeftOf(Either<BaseError, Unit> either) =>
either.Match(Left: e => e, Right: _ => throw new AssertionException("Expected a Left result"));
[Test]
public async Task Stale_If_Match_Should_Fail_Precondition_And_Not_Mutate()
{
await SeedCollectionWithVersion(1, version: 2);
Either<BaseError, Unit> result = await MakeHandler().Handle(
new UpdateCollectionCustomOrder(1, [], Some(1)),
CancellationToken.None);
LeftOf(result).ShouldBeOfType<PreconditionFailedError>();
(await ReadVersion(1)).ShouldBe(2);
}
[Test]
public async Task Matching_If_Match_Should_Succeed_And_Bump()
{
await SeedCollectionWithVersion(1, version: 2);
Either<BaseError, Unit> result = await MakeHandler().Handle(
new UpdateCollectionCustomOrder(1, [], Some(2)),
CancellationToken.None);
result.IsRight.ShouldBeTrue();
(await ReadVersion(1)).ShouldBe(3);
}
}