@@ -15,11 +15,13 @@ import io.ktor.server.testing.*
1515import io.mockk.clearAllMocks
1616import io.mockk.every
1717import io.mockk.mockk
18+ import io.mockk.verify
1819import jamule.AmuleClient
1920import jamule.model.AmuleTransferringFile
2021import jamule.model.DownloadCommand
2122import jamule.model.FileStatus
2223import kotlinx.serialization.json.Json
24+ import java.nio.file.Files
2325
2426class TorrentApiTest : StringSpec ({
2527 val amuleClient = mockk<AmuleClient >()
@@ -117,21 +119,69 @@ class TorrentApiTest : StringSpec({
117119 }
118120 }
119121
120- " should delete torrent" {
122+ " should delete torrent when downloading " {
121123 testApplication {
122124 application {
123125 torrentApi(amuleClient, categoryStore, finishedPath)
124126 configureForTest()
125127 }
128+ categoryStore.store("test", testMagnetLink.amuleHexHash())
126129 every {
127130 amuleClient.sendDownloadCommand(testMagnetHash, DownloadCommand .DELETE )
128131 } returns Result .success(Unit )
132+ every {
133+ amuleClient.getDownloadQueue()
134+ } returns Result .success(
135+ listOf(
136+ MockTransferringFile (
137+ fileHashHexString = testMagnetLink.amuleHexHash(),
138+ fileName = testMagnetLink.name,
139+ sizeFull = testMagnetLink.size,
140+ )
141+ )
142+ )
143+ client.submitForm(formParameters = Parameters .build {
144+ append("hashes", testMagnetLink.amuleHexHash())
145+ append("deleteFiles", "true")
146+ }, url = "/api/v2/torrents/delete").apply {
147+ this.status shouldBe HttpStatusCode .OK
148+ }
149+ verify { amuleClient.sendDownloadCommand(testMagnetHash, DownloadCommand .DELETE ) }
150+ categoryStore.getCategory(testMagnetLink.amuleHexHash()) shouldBe null
151+ }
152+ }
153+
154+ " should delete file when not downloading" {
155+ testApplication {
156+ application {
157+ torrentApi(amuleClient, categoryStore, finishedPath)
158+ configureForTest()
159+ }
160+ categoryStore.store("test", testMagnetLink.amuleHexHash())
161+ every {
162+ amuleClient.sendDownloadCommand(testMagnetHash, DownloadCommand .DELETE )
163+ } returns Result .success(Unit )
164+ val randomTemporaryFile = Files .createTempFile("test", "test")
165+ every { amuleClient.getSharedFiles() } returns Result .success(
166+ listOf(
167+ MockTransferringFile (
168+ fileHashHexString = testMagnetLink.amuleHexHash(),
169+ fileName = testMagnetLink.name,
170+ sizeFull = testMagnetLink.size,
171+ filePath = randomTemporaryFile.toAbsolutePath().toString()
172+ )
173+ )
174+ )
175+ every { amuleClient.getDownloadQueue() } returns Result .success(emptyList())
129176 client.submitForm(formParameters = Parameters .build {
130177 append("hashes", testMagnetLink.amuleHexHash())
131- append("deleteFiles", "test ")
178+ append("deleteFiles", "true ")
132179 }, url = "/api/v2/torrents/delete").apply {
133180 this.status shouldBe HttpStatusCode .OK
134181 }
182+ verify(exactly = 0) { amuleClient.sendDownloadCommand(testMagnetHash, DownloadCommand .DELETE ) }
183+ categoryStore.getCategory(testMagnetLink.amuleHexHash()) shouldBe null
184+ Files .exists(randomTemporaryFile) shouldBe false
135185 }
136186 }
137187
0 commit comments