fix(69): auto-tune review fixes — null channels, orphaned SmartCollection, oversized preview names, doc drift

- CreateAutoTunedChannelsRequest.ToCommand(): guard null Channels (was NREing on
  a request body that omits "channels", causing HTTP 500).
- CreateAutoTunedChannelsHandler.CreateOne: when CreateChannelFromLineup returns
  Left (Skipped/Failed), roll back the just-created SmartCollection via
  DeleteSmartCollection so retries don't fail on SmartCollection-name uniqueness.
  Best-effort; the delete result does not change the outcome.
- PreviewAutoTuneChannelsHandler: filter out proposals whose generated name
  exceeds the 50-char Channel.Name limit before number allocation, so numbers
  aren't wasted on proposals that can never be created.
- docs/superpowers/specs/2026-07-16-auto-tuning-design.md: fix field-name drift
  in JSON examples (proposedNumber -> number, error -> reason) to match the
  actual AutoTuneProposal/AutoTuneChannelOutcome DTOs.

Refs #69
This commit is contained in:
2026-07-16 22:18:52 +02:00
parent 0320735f47
commit f31476e012
6 changed files with 57 additions and 10 deletions
@@ -89,6 +89,11 @@ public class CreateAutoTunedChannelsHandler(ISender mediator)
foreach (BaseError error in channelResult.LeftToSeq())
{
// Roll back the smart collection we just created so a retry of this
// axis/value doesn't fail on SmartCollection-name uniqueness. Best-effort;
// the primary outcome below is still Skipped/Failed regardless of the delete result.
await mediator.Send(new DeleteSmartCollection(smartCollection.Id), cancellationToken);
AutoTuneOutcomeStatus status = error.Value.Contains(NumberTakenError, StringComparison.Ordinal)
? AutoTuneOutcomeStatus.Skipped
: AutoTuneOutcomeStatus.Failed;
@@ -45,14 +45,21 @@ public class PreviewAutoTuneChannelsHandler(IDbContextFactory<TvContext> dbConte
.Select(c => c.Name).ToListAsync(cancellationToken))
.ToHashSet(StringComparer.OrdinalIgnoreCase);
List<string> numbers = AutoTuneNumberAllocator.Allocate(
request.StartingNumber, raw.Count, existingNumbers);
// Drop entries whose generated name would be rejected at create time (Channel name <= 50
// chars) before number allocation, so numbers aren't wasted on proposals that can never
// be created.
List<(AutoTuneAxis Axis, string Value, int Count, string Name)> survivors = raw
.Select(r => (r.Axis, r.Value, r.Count, Name: AutoTuneAxisMap.GenerateName(r.Axis, r.Value)))
.Where(r => r.Name.Length <= 50)
.ToList();
var proposals = new List<AutoTuneProposal>(raw.Count);
for (int i = 0; i < raw.Count; i++)
List<string> numbers = AutoTuneNumberAllocator.Allocate(
request.StartingNumber, survivors.Count, existingNumbers);
var proposals = new List<AutoTuneProposal>(survivors.Count);
for (int i = 0; i < survivors.Count; i++)
{
(AutoTuneAxis axis, string value, int count) = raw[i];
string name = AutoTuneAxisMap.GenerateName(axis, value);
(AutoTuneAxis axis, string value, int count, string name) = survivors[i];
proposals.Add(new AutoTuneProposal(
axis, value, name, numbers[i], count, existingNames.Contains(name)));
}
@@ -32,6 +32,8 @@ public class CreateAutoTunedChannelsHandlerTests
return (Either<BaseError, SmartCollectionViewModel>)
new SmartCollectionViewModel(7, cmd.Name, cmd.Query);
});
_mediator.Send(Arg.Any<DeleteSmartCollection>(), Arg.Any<CancellationToken>())
.Returns((Either<BaseError, LanguageExt.Unit>)LanguageExt.Unit.Default);
}
[Test]
@@ -83,6 +85,23 @@ public class CreateAutoTunedChannelsHandlerTests
result.Results[0].Status.ShouldBe(AutoTuneOutcomeStatus.Skipped);
}
[Test]
public async Task Number_Collision_Rolls_Back_Orphaned_SmartCollection()
{
_mediator.Send(Arg.Any<CreateChannelFromLineup>(), Arg.Any<CancellationToken>())
.Returns((Either<BaseError, CreateChannelFromLineupResponseModel>)
BaseError.New("Channel number must be unique"));
var result = await Handle(new CreateAutoTunedChannels(3, "Auto-Tuned",
new List<AutoTuneChannelSelection> { new(AutoTuneAxis.TvGenre, "Comedy", "Comedy", "500") }));
result.Results[0].Status.ShouldBe(AutoTuneOutcomeStatus.Skipped);
await _mediator.Received().Send(
Arg.Is<DeleteSmartCollection>(d => d.SmartCollectionId == 7),
Arg.Any<CancellationToken>());
}
[Test]
public async Task Other_Errors_Are_Failed()
{
@@ -74,6 +74,22 @@ public class PreviewAutoTuneChannelsHandlerTests
proposals[0].ItemCount.ShouldBe(2);
}
[Test]
public async Task Excludes_Proposals_Whose_Generated_Name_Exceeds_50_Chars()
{
// "Movies" suffix (7 chars) pushes this over the 50-char Channel.Name limit.
const string longGenre = "A Really Really Long And Overly Descriptive Genre"; // 50 chars, +" Movies" = 57
await SeedMovieWithGenre(movieId: 1, metadataId: 1, genre: longGenre);
await SeedMovieWithGenre(movieId: 2, metadataId: 2, genre: longGenre);
var result = await Handle(new PreviewAutoTuneChannels(
new List<AutoTuneAxis> { AutoTuneAxis.MovieGenre }, MinItems: 2, StartingNumber: 500));
List<AutoTuneProposal> proposals = RightOf(result);
proposals.ShouldNotContain(p => p.Value == longGenre);
proposals.ShouldBeEmpty();
}
[Test]
public async Task Empty_Axes_Is_Error()
{
@@ -17,7 +17,7 @@ public record CreateAutoTunedChannelsRequest(
List<AutoTunedChannelRequest> Channels)
{
public CreateAutoTunedChannels ToCommand() =>
new(TemplateId, Group, Channels.Select(c => c.ToCommand()).ToList());
new(TemplateId, Group, (Channels ?? []).Select(c => c.ToCommand()).ToList());
}
public record AutoTunedChannelRequest(
@@ -87,9 +87,9 @@ Response — a list of proposed channels:
```
[
{ "axis": "TvShow", "value": "The Office", "name": "The Office",
"proposedNumber": 500, "itemCount": 201, "alreadyExists": false },
"number": 500, "itemCount": 201, "alreadyExists": false },
{ "axis": "MovieGenre", "value": "Action", "name": "Action Movies",
"proposedNumber": 501, "itemCount": 42, "alreadyExists": false },
"number": 501, "itemCount": 42, "alreadyExists": false },
...
]
```
@@ -117,7 +117,7 @@ Response — partial-success list, mirroring the existing `ResetAllPlayoutsResul
"results": [
{ "name": "The Office", "status": "Created", "channelId": 88 },
{ "name": "Action Movies", "status": "Skipped", "reason": "number 501 already taken" },
{ "name": "Sci-Fi", "status": "Failed", "error": "…" }
{ "name": "Sci-Fi", "status": "Failed", "reason": "…" }
],
"createdCount": 1, "skippedCount": 1, "failedCount": 1
}