Skip to content

Commit

Permalink
Add some more test cases and bugfixes to queuing
Browse files Browse the repository at this point in the history
- Make `to` parameter optional in remove range command
  • Loading branch information
DRSchlaubi committed Mar 15, 2024
1 parent b9167c3 commit 9bba2cb
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 11 deletions.
2 changes: 1 addition & 1 deletion music/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
subprojects {
version = "3.5.11-SNAPSHOT"
version = "3.5.12-SNAPSHOT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.kotlindiscord.kord.extensions.commands.application.slash.EphemeralSla
import com.kotlindiscord.kord.extensions.commands.application.slash.EphemeralSlashCommandContext
import com.kotlindiscord.kord.extensions.commands.application.slash.ephemeralSubCommand
import com.kotlindiscord.kord.extensions.commands.converters.impl.int
import com.kotlindiscord.kord.extensions.commands.converters.impl.optionalInt
import dev.kord.common.entity.Snowflake
import dev.schlaubi.mikbot.plugin.api.util.safeGuild
import dev.schlaubi.mikmusic.core.MusicModule
Expand All @@ -25,7 +26,7 @@ class RemoveRangeSongArguments : Arguments() {
name = "from"
description = "commands.remove.arguments.from.description"
}
val to by int {
val to by optionalInt {
name = "to"
description = "commands.remove.arguments.to.description"
}
Expand Down Expand Up @@ -76,10 +77,11 @@ suspend fun MusicModule.removeCommand() = ephemeralControlSlashCommand {
description = "commands.remove.range.description"

doRemove {
if (arguments.to < arguments.from) {
val to = arguments.to ?: arguments.from
if (to < arguments.from) {
throw DiscordRelayedException(translate("commands.remove.range.invalid_range_end"))
}
val range = arguments.from..arguments.to
val range = arguments.from..to

musicPlayer.queue.removeQueueEntries(range)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ class Queue(private var tracksList: MutableList<QueuedTrack> = mutableListOf())
val song = tracksList.getOrNull(fromIndex) ?: return null
if (swap) {
val toValue = tracksList.getOrNull(toIndex) ?: return null
tracksList[to] = song
tracksList[from] = toValue
tracksList[toIndex] = song
tracksList[fromIndex] = toValue
} else {
tracksList.add(to, song)
tracksList.removeAt(from)
tracksList.add(toIndex, song)
tracksList.removeAt(fromIndex)
}

return song.track
Expand All @@ -72,16 +72,18 @@ class Queue(private var tracksList: MutableList<QueuedTrack> = mutableListOf())
val trackIndex = runCatching {
order.removeAt(index)
}.getOrNull() ?: return null
if (trackIndex <= nextIndex) nextIndex--
return tracksList[trackIndex].track
}

fun removeQueueEntries(range: IntRange): Int {
val queueSize = order.size
if (range.first >= 0 && range.last <= order.size) {
val before = order.subList(0, range.first - 1) // inclusive
val after = order.subList(range.last, queueSize)
val after = order.subList(range.last + 1, queueSize) // inclusive
val combined = before + after
order = LinkedList(combined)
if (range.last <= nextIndex) nextIndex -= range.count() + 1
}

return queueSize - order.size
Expand Down
75 changes: 73 additions & 2 deletions music/player/src/test/kotlin/QueueTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,88 @@ class QueueTest {
queue.shuffle = false
assertEquals(newTracks + tracks, queue.tracks)
}

@Test
fun `test removal of tracks`() {
val queue = makeMockQueue()
val tracks = queue.tracks.toList()

queue.removeQueueEntry(5)
assertEquals(tracks.filterIndexed { index, _ -> index != 5 }, queue.tracks, "Track was not removed")

val track = mockTrack(11)
queue.addTracks(track)
repeat(queue.tracks.size - 1) { queue.poll() }
assertEquals(track, queue.poll())
}

@Test
fun `test removal of tracks in range`() {
val queue = makeMockQueue()
val tracks = queue.tracks.toList()

queue.removeQueueEntries(5..8)
// removeQueueEntries is inclusive
assertEquals(tracks.filterIndexed { index, _ -> index !in 4..8 }, queue.tracks, "Track was not removed")

val track = mockTrack(11)
queue.addTracks(track)
repeat(queue.tracks.size - 1) { queue.poll() }
assertEquals(track, queue.poll())
}

@Test
fun `test swap of tracks`() {
val queue = makeMockQueue()
val tracks = queue.tracks.toList()

queue.moveQueuedEntry(5, 7, swap = true)
assertEquals(tracks[7], queue.tracks[5])
assertEquals(tracks[5], queue.tracks[7])
}

@Test
fun `test move of tracks`() {
val queue = makeMockQueue()
val tracks = queue.tracks.toList()

queue.moveQueuedEntry(5, 7, swap = false)
assertEquals(tracks[5], queue.tracks[6])
}

@Test
fun `test swap of tracks in shuffle`() {
val queue = makeMockQueue()
queue.shuffle = true
queue.addTracks(MutableList(10) { mockTrack(it + 11) })
val tracks = queue.tracks.toList()

queue.moveQueuedEntry(5, 7, swap = true)
assertEquals(tracks[7], queue.tracks[5])
assertEquals(tracks[5], queue.tracks[7])
}

@Test
fun `test move of tracks in shuffle`() {
val queue = makeMockQueue()
queue.shuffle = true
queue.addTracks(MutableList(10) { mockTrack(it + 11) })
val tracks = queue.tracks.toList()

queue.moveQueuedEntry(5, 7, swap = false)
assertEquals(tracks[5], queue.tracks[6])
}
}

private fun makeMockQueue() = Queue(MutableList(10, ::mockTrack))

private fun mockTrack(num: Int): SimpleQueuedTrack {
private fun mockTrack(num: Int, author: Snowflake = Snowflake.min): SimpleQueuedTrack {
val track = Track(
"$num",
TrackInfo("", false, "", 0, false, 0, "$num", null, "", null, null),
emptyJsonObject(),
emptyJsonObject()
)

return SimpleQueuedTrack(track, Snowflake.min)
return SimpleQueuedTrack(track, author)
}

0 comments on commit 9bba2cb

Please sign in to comment.