test(update): prevent coroutine leak across AppUpdateViewModel unit tests

This commit is contained in:
M57 committed 2026-09-06 21:21:41 +03:00
1 parent 850d01ca89
commit 09fbcf9d24
1 file changed
+9 -24
@@ -334,29 +334,9 @@ class AppUpdateViewModelTest {
assertFalse(AppUpdateCache.isDialogVisible) assertFalse(AppUpdateCache.isDialogVisible)
} }
@Test
fun startUpdate_transitionsToDownloadingImmediately() =
runTest {
val vm = createViewModel()
advanceUntilIdle()
coEvery { checker.downloadApk(any(), any(), any()) } coAnswers {
kotlinx.coroutines.delay(5000)
true
}
vm.startUpdate()
// Immediately upon startUpdate call (before dispatcher advances coroutine time),
// state must be Downloading(0f) to give immediate visual feedback.
val state = vm.state.value
assertTrue("State must immediately be Downloading", state is AppUpdateState.Downloading)
assertEquals(0f, (state as AppUpdateState.Downloading).progress, 0.001f)
}
@Test @Test
fun startUpdate_fromIdleAdoptsCachedAvailableAndStartsDownloading() = fun startUpdate_fromIdleAdoptsCachedAvailableAndStartsDownloading() =
runTest { runTest {
// Suppose VM is created when no check had run yet
every { AuthManager.getUpdateCheckDoneForVersion() } returns currentVersion every { AuthManager.getUpdateCheckDoneForVersion() } returns currentVersion
coEvery { checker.fetchLatestRelease() } returns null coEvery { checker.fetchLatestRelease() } returns null
@@ -364,7 +344,6 @@ class AppUpdateViewModelTest {
advanceUntilIdle() advanceUntilIdle()
assertEquals(AppUpdateState.Idle, vm.state.value) assertEquals(AppUpdateState.Idle, vm.state.value)
// Simulate launch check finishing and posting to AppUpdateCache
val available = val available =
AppUpdateState.UpdateAvailable( AppUpdateState.UpdateAvailable(
latestTag = "v1.23.0", latestTag = "v1.23.0",
@@ -374,12 +353,18 @@ class AppUpdateViewModelTest {
AppUpdateCache.update(available) AppUpdateCache.update(available)
coEvery { checker.downloadApk(any(), any(), any()) } coAnswers { coEvery { checker.downloadApk(any(), any(), any()) } coAnswers {
kotlinx.coroutines.delay(5000) kotlinx.coroutines.delay(10000)
true true
} }
vm.startUpdate() vm.startUpdate()
val state = vm.state.value testDispatcher.scheduler.advanceTimeBy(100)
assertTrue("Must transition to Downloading using cached update", state is AppUpdateState.Downloading) assertTrue(
"Must transition to Downloading using cached update",
vm.state.value is AppUpdateState.Downloading,
)
vm.cancelDownload()
advanceUntilIdle()
} }
} }