@@ -25,13 +25,12 @@ import com.mapbox.navigation.copilot.internal.PushStatus
2525import com.mapbox.navigation.copilot.internal.PushStatusObserver
2626import com.mapbox.navigation.copilot.work.HistoryUploadWorker
2727import com.mapbox.navigation.copilot.work.HistoryUploadWorker.Companion.putCopilotSession
28- import com.mapbox.navigation.utils.internal.logD
28+ import com.mapbox.navigation.testing.LoggingFrontendTestRule
2929import io.mockk.CapturingSlot
3030import io.mockk.coEvery
3131import io.mockk.every
3232import io.mockk.mockk
3333import io.mockk.mockkObject
34- import io.mockk.mockkStatic
3534import io.mockk.slot
3635import io.mockk.unmockkAll
3736import io.mockk.verify
@@ -41,6 +40,7 @@ import org.junit.Assert.assertEquals
4140import org.junit.Assert.assertNotNull
4241import org.junit.Assert.assertTrue
4342import org.junit.Before
43+ import org.junit.Rule
4444import org.junit.Test
4545import java.io.File
4646
@@ -49,6 +49,9 @@ import java.io.File
4949@OptIn(ExperimentalPreviewMapboxNavigationAPI ::class )
5050class HistoryUploadWorkerTest {
5151
52+ @get:Rule
53+ val loggerRule = LoggingFrontendTestRule ()
54+
5255 private lateinit var mockedContext: Context
5356 private lateinit var mockedUploadServiceInterface: HttpServiceInterface
5457 private val mockedPushStatusObserver = mockk<PushStatusObserver >(relaxUnitFun = true )
@@ -74,12 +77,6 @@ class HistoryUploadWorkerTest {
7477
7578 @Before
7679 fun setup () {
77- mockkStatic(" com.mapbox.navigation.utils.internal.LoggerProviderKt" )
78- every { logD(msg = any(), category = any()) } answers {
79- // // uncomment to verify log printout
80- // println(secondArg<String?>() + " " + firstArg())
81- }
82-
8380 // Mock File operations
8481 mockkObject(HistoryAttachmentsUtils )
8582 coEvery { rename(any(), any()) } coAnswers {
@@ -191,25 +188,6 @@ class HistoryUploadWorkerTest {
191188 )
192189 }
193190
194- @Test
195- fun `remove history file not called - FAILED` () = runBlocking {
196- givenUploadServiceAnswer(
197- uploadStatus(
198- state = TransferState .FAILED ,
199- error = mockk<TransferError >(relaxed = true ) {
200- every { code } returns mockk<TransferErrorCode >()
201- },
202- ),
203- )
204-
205- HistoryUploadWorker (mockedContext, workerParams(stubCopilotSession))
206- .doWork()
207-
208- verify(exactly = 0 ) {
209- delete(any())
210- }
211- }
212-
213191 @Test
214192 fun `Result retry - FAILED` () = runBlocking {
215193 givenUploadServiceAnswer(
@@ -280,7 +258,55 @@ class HistoryUploadWorkerTest {
280258 }
281259
282260 @Test
283- fun `remove history file - runAttemptCount greater or equal than MAX_RUN_ATTEMPT_COUNT` () =
261+ fun `remove history file not called - FAILED, runAttemptCount = 1` () = runBlocking {
262+ givenUploadServiceAnswer(
263+ uploadStatus(
264+ state = TransferState .FAILED ,
265+ error = mockk<TransferError >(relaxed = true ) {
266+ every { code } returns mockk<TransferErrorCode >()
267+ },
268+ ),
269+ )
270+
271+ HistoryUploadWorker (mockedContext, workerParams(stubCopilotSession, runAttemptCount = 1 ))
272+ .doWork()
273+
274+ verify(exactly = 0 ) {
275+ delete(any())
276+ }
277+ }
278+
279+ @Test
280+ fun `remove history file - FAILED, runAttemptCount = MAX_RUN_ATTEMPT_COUNT` () =
281+ runBlocking {
282+ givenUploadServiceAnswer(
283+ uploadStatus(
284+ state = TransferState .FAILED ,
285+ error = mockk<TransferError >(relaxed = true ) {
286+ every { code } returns mockk<TransferErrorCode >()
287+ },
288+ ),
289+ )
290+
291+ HistoryUploadWorker (
292+ mockedContext,
293+ workerParams(
294+ stubCopilotSession,
295+ runAttemptCount = HistoryUploadWorker .MAX_RUN_ATTEMPT_COUNT ,
296+ ),
297+ ).doWork()
298+
299+ val deletedFiles = mutableListOf<File >()
300+ verify(exactly = 2 ) {
301+ delete(capture(deletedFiles))
302+ }
303+ assertEquals(2 , deletedFiles.size)
304+ assertNotNull(deletedFiles.first { it.name.endsWith(" pbf.gz" ) })
305+ assertNotNull(deletedFiles.first { it.name.endsWith(" metadata.json" ) })
306+ }
307+
308+ @Test
309+ fun `remove history file - FINISHED 204` () =
284310 runBlocking {
285311 givenUploadServiceAnswer(
286312 uploadStatus(
@@ -290,7 +316,7 @@ class HistoryUploadWorkerTest {
290316 ),
291317 )
292318
293- HistoryUploadWorker (mockedContext, workerParams(stubCopilotSession, 8 ))
319+ HistoryUploadWorker (mockedContext, workerParams(stubCopilotSession))
294320 .doWork()
295321
296322 val deletedFiles = mutableListOf<File >()
@@ -314,8 +340,10 @@ class HistoryUploadWorkerTest {
314340 ),
315341 )
316342
317- val result = HistoryUploadWorker (mockedContext, workerParams(stubCopilotSession, 8 ))
318- .doWork()
343+ val result = HistoryUploadWorker (
344+ mockedContext,
345+ workerParams(stubCopilotSession, HistoryUploadWorker .MAX_RUN_ATTEMPT_COUNT ),
346+ ).doWork()
319347
320348 assertTrue(result is ListenableWorker .Result .Failure )
321349 }
0 commit comments