test: make viewmodel and notification receiver tests deterministic (#1207)

This commit is contained in:
m57 authored and GitHub committed 2026-09-21 16:53:10 +03:00
1 parent 29fdb52978
commit fa2b388b24
9 files changed
+52 -66

No files matched your search

@@ -20,6 +20,7 @@ import com.m57.hermescontrol.ui.common.ToastHost
import com.m57.hermescontrol.ui.common.refreshOnChange
import com.m57.hermescontrol.ui.common.safeLaunchLoad
import com.m57.hermescontrol.ui.common.safeLaunchSwrLoad
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
@@ -99,6 +100,7 @@ class CronJobsViewModel :
val uiState: StateFlow<CronJobsUiState> = _uiState.asStateFlow()
private var loadJob: Job? = null
internal var ioDispatcher: CoroutineDispatcher = Dispatchers.IO
private val jobsCache = SwrCache<String, List<CronJob>>()
fun clearScopeOwnedState() {
@@ -166,7 +168,7 @@ class CronJobsViewModel :
}
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.pauseCronJob(id) }
}
val currentScope = runCatching { AuthManager.currentDataScope() }.getOrNull()
@@ -197,7 +199,7 @@ class CronJobsViewModel :
}
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.resumeCronJob(id) }
}
val currentScope = runCatching { AuthManager.currentDataScope() }.getOrNull()
@@ -218,7 +220,7 @@ class CronJobsViewModel :
fun triggerCronJob(id: String) {
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.triggerCronJob(id) }
}
when (result) {
@@ -241,7 +243,7 @@ class CronJobsViewModel :
}
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.deleteCronJob(id) }
}
val currentScope = runCatching { AuthManager.currentDataScope() }.getOrNull()
@@ -292,7 +294,7 @@ class CronJobsViewModel :
fun loadEditorBlueprints() {
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.getCronBlueprints() }
}
when (result) {
@@ -319,7 +321,7 @@ class CronJobsViewModel :
fun loadDeliveryTargets() {
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.getCronDeliveryTargets() }
}
if (result is NetworkResult.Success) {
@@ -345,7 +347,7 @@ class CronJobsViewModel :
}
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.getCronJob(id) }
}
when (result) {
@@ -462,7 +464,7 @@ class CronJobsViewModel :
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
if (editor.isNew && editor.selectedBlueprintKey != null) {
safeApiCall {
ApiClient.hermesApi.instantiateBlueprint(
@@ -33,6 +33,7 @@ import com.m57.hermescontrol.data.repository.KanbanRepositoryImpl
import com.m57.hermescontrol.data.ws.KanbanEventsClient
import com.m57.hermescontrol.data.ws.KanbanLiveStatus
import com.m57.hermescontrol.ui.common.ToastHost
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
@@ -166,6 +167,7 @@ class KanbanViewModel(
private var eventsBoard: String? = null
private var reloadJob: Job? = null
private var currentLoadGen: Int = 0
internal var ioDispatcher: CoroutineDispatcher = Dispatchers.IO
fun loadBoards() {
val endpoint = endpointProvider()
@@ -184,7 +186,7 @@ class KanbanViewModel(
coroutineScope {
val prefetchedBoardDeferred =
if (previouslySelectedId != null) {
async(Dispatchers.IO) {
async(ioDispatcher) {
repository.getBoard(
board = previouslySelectedId,
includeArchived = _uiState.value.includeArchived,
@@ -15,6 +15,7 @@ import com.m57.hermescontrol.ui.common.ToastHost
import com.m57.hermescontrol.ui.common.refreshOnChange
import com.m57.hermescontrol.ui.common.safeLaunchLoad
import com.m57.hermescontrol.ui.common.safeLaunchSwrLoad
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
@@ -40,6 +41,7 @@ class PairingViewModel :
val uiState: StateFlow<PairingUiState> = _uiState.asStateFlow()
private var launchJob: Job? = null
internal var ioDispatcher: CoroutineDispatcher = Dispatchers.IO
private val pairingCache = SwrCache<String, PairingResponse>()
fun clearScopeOwnedState() {
@@ -140,7 +142,7 @@ class PairingViewModel :
if (_uiState.value.actionKey != null) return
_uiState.update { it.copy(actionKey = actionKey) }
viewModelScope.launch {
val result = withContext(Dispatchers.IO) { apiCall() }
val result = withContext(ioDispatcher) { apiCall() }
when (result) {
is NetworkResult.Success -> {
_uiState.update { onSuccess(it.copy(actionKey = null)) }
@@ -21,6 +21,7 @@ import com.m57.hermescontrol.data.remote.safeApiCall
import com.m57.hermescontrol.ui.common.ToastHost
import com.m57.hermescontrol.ui.common.safeLaunchLoad
import com.m57.hermescontrol.ui.common.safeLaunchSwrLoad
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
@@ -98,6 +99,7 @@ class SkillsViewModel(
private var loadJob: Job? = null
private var searchJob: Job? = null
private val skillsCache = SwrCache<String, List<Skill>>()
internal var ioDispatcher: CoroutineDispatcher = Dispatchers.IO
fun clearScopeOwnedState() {
_uiState.update {
@@ -252,7 +254,7 @@ class SkillsViewModel(
}
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.scanHubSkill(identifier = identifier) }
}
when (result) {
@@ -307,7 +309,7 @@ class SkillsViewModel(
}
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.previewHubSkill(identifier = identifier) }
}
when (result) {
@@ -343,7 +345,7 @@ class SkillsViewModel(
}
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.installHubSkill(SkillHubInstallRequest(identifier = identifier)) }
}
when (result) {
@@ -381,7 +383,7 @@ class SkillsViewModel(
}
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.uninstallHubSkill(SkillHubUninstallRequest(name = name)) }
}
when (result) {
@@ -438,7 +440,7 @@ class SkillsViewModel(
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.toggleSkill(ToggleSkillRequest(skill.name, targetEnabled)) }
}
val currentScope = runCatching { AuthManager.currentDataScope() }.getOrNull()
@@ -489,7 +491,7 @@ class SkillsViewModel(
}
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.getSkillContent(skillName) }
}
when (result) {
@@ -522,7 +524,7 @@ class SkillsViewModel(
_uiState.update { it.copy(isSavingContent = true, saveContentSuccess = false) }
viewModelScope.launch {
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall {
ApiClient.hermesApi.saveSkillContent(
SaveSkillContentRequest(
@@ -560,7 +562,7 @@ class SkillsViewModel(
viewModelScope.launch {
_uiState.update { it.copy(toastMessage = "Updating skills from hub…") }
val result =
withContext(Dispatchers.IO) {
withContext(ioDispatcher) {
safeApiCall { ApiClient.hermesApi.updateSkillsFromHub() }
}
when (result) {