✨ Add multiple downloads at the same time
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 31 May 2020 13:05:27 +0000 (15:05 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 31 May 2020 14:45:15 +0000 (16:45 +0200)
src/main/java/net/pterodactylus/xdcc/ui/stdin/DownloadCommand.kt
src/test/java/net/pterodactylus/xdcc/ui/stdin/DownloadCommandTest.kt

index 7915472..8636e06 100644 (file)
@@ -34,10 +34,11 @@ class DownloadCommand(private val core: Core) : Command {
                if (parameters.isEmpty()) {
                        return state
                }
-               val index = Ints.tryParse(parameters[0])
-               if (index != null && index < state.lastResults.size) {
-                       core.fetch(state.lastResults[index].bot(), state.lastResults[index].pack())
-               }
+               parameters
+                               .mapNotNull { Ints.tryParse(it) }
+                               .filter { it < state.lastResults.size }
+                               .map { state.lastResults[it] }
+                               .forEach { core.fetch(it.bot(), it.pack()) }
                return state
        }
 
index c02d5f9..cc4469d 100644 (file)
@@ -84,4 +84,32 @@ class DownloadCommandTest {
                assertThat(newState, equalTo(state))
        }
 
+       @Test
+       fun `executing command with multiple mixed parameters and last results will fetch correct packs from correct bots`() {
+               val bot1 = mock(Bot::class.java)
+               val bot2 = mock(Bot::class.java)
+               val bot3 = mock(Bot::class.java)
+               val pack1 = Pack("1", "2", "3")
+               val pack2 = Pack("1", "2", "3")
+               val pack3 = Pack("1", "2", "3")
+               val state = state.setLastResults(listOf(Result(core, bot1, pack1), Result(core, bot2, pack2), Result(core, bot3, pack3)))
+               command.execute(state, listOf("0", "a", "7", "3", "1", "2"), StringWriter())
+               verify(core).fetch(bot1, pack1)
+               verify(core).fetch(bot2, pack2)
+               verify(core).fetch(bot3, pack3)
+       }
+
+       @Test
+       fun `executing command with multiple mixed parameters and last results will return old state`() {
+               val bot1 = mock(Bot::class.java)
+               val bot2 = mock(Bot::class.java)
+               val bot3 = mock(Bot::class.java)
+               val pack1 = Pack("1", "2", "3")
+               val pack2 = Pack("1", "2", "3")
+               val pack3 = Pack("1", "2", "3")
+               val state = state.setLastResults(listOf(Result(core, bot1, pack1), Result(core, bot2, pack2), Result(core, bot3, pack3)))
+               val newState = command.execute(state, listOf("0", "a", "7", "3", "1", "2"), StringWriter())
+               assertThat(newState, equalTo(state))
+       }
+
 }