From 3dcc81cc434bb11e1c24e5da1b9b4e85c5b8033e Mon Sep 17 00:00:00 2001 From: Julian Lechner Date: Thu, 26 Nov 2026 09:41:32 +0100 Subject: [PATCH] fix(schedule): keep a requested song first when it is the only one in the slot With only the requested song before the next news, the ad and moderator break went in front of it. The swap message then showed the empty break title and its start time, and the song played later than chosen. Co-Authored-By: Claude Opus 5.5 --- src/Radiosender/Services/Scheduling/ScheduleGenerator.cs | 2 +- tests/Radiosender.Tests/ScheduleGeneratorTests.cs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Radiosender/Services/Scheduling/ScheduleGenerator.cs b/src/Radiosender/Services/Scheduling/ScheduleGenerator.cs index aa9f248..5e71a20 100644 --- a/src/Radiosender/Services/Scheduling/ScheduleGenerator.cs +++ b/src/Radiosender/Services/Scheduling/ScheduleGenerator.cs @@ -194,7 +194,7 @@ public class ScheduleGenerator(ScheduleOptions options, Random random) var pairs = Math.Max(adPieces.Count, moderatorPieces.Count); // Hot swap: requested song stays first, breaks start after it. - var firstBreakSong = keepFirstSongFirst && songs.Count > 1 ? 1 : 0; + var firstBreakSong = keepFirstSongFirst ? 1 : 0; // also when it is the only song: breaks then follow it var breakSlots = Math.Max(songs.Count - firstBreakSong, 1); var breaks = Math.Min(pairs, breakSlots); diff --git a/tests/Radiosender.Tests/ScheduleGeneratorTests.cs b/tests/Radiosender.Tests/ScheduleGeneratorTests.cs index 0ffe45c..226b904 100644 --- a/tests/Radiosender.Tests/ScheduleGeneratorTests.cs +++ b/tests/Radiosender.Tests/ScheduleGeneratorTests.cs @@ -265,6 +265,8 @@ public class ScheduleGeneratorTests try { swapped = Generator(seed).Replace(plan, index, Request, Library); } catch (ScheduleException) { continue; } // too little time left before the news + // Also when only the requested song fits: breaks go after it, not before. + Assert.Equal(Request, swapped[index].Song); Assert.Equal(plan[^1].End, swapped[^1].End); for (var i = 1; i < swapped.Count; i++) Assert.False(swapped[i - 1].Kind == ScheduleItemKind.Ad && swapped[i].Kind == ScheduleItemKind.Ad,