test(#515): add KeysViewModel.toggleCategory test (re-applied onto main, +cross-toggle independence) (#515)

This commit is contained in:
M57 authored and GitHub committed 2026-07-09 20:21:26 +03:00
1 parent bab0c3038d
commit c98cfee3cc
1 file changed
+92
@@ -1,13 +1,18 @@
package com.m57.hermescontrol.ui.keys
import com.m57.hermescontrol.data.model.EnvVarConfig
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
@@ -54,4 +59,91 @@ class KeysViewModelTest {
assertEquals("dummy_token_value_that_is_long_enough", viewModel.uiState.value.newKeyValue)
}
@Test
fun testToggleCategory() {
// Arrange
val viewModel = createViewModel()
// Seed initial categories via reflection (no test-only seeding path exists in source)
val uiStateField = KeysViewModel::class.java.getDeclaredField("_uiState")
uiStateField.isAccessible = true
val uiStateFlow =
uiStateField.get(viewModel) as MutableStateFlow<KeysUiState>
val initialCategories =
listOf(
CategorySection(
name = "LLM Providers",
vars =
mapOf(
"OPENAI_API_KEY" to
EnvVarConfig(
isSet = true,
category = "LLM Providers",
isPassword = true,
),
),
expanded = false,
),
CategorySection(
name = "Tool API Keys",
vars =
mapOf(
"WEATHER_API_KEY" to
EnvVarConfig(
isSet = true,
category = "Tool API Keys",
isPassword = true,
),
),
expanded = true,
),
)
uiStateFlow.update { it.copy(categories = initialCategories) }
// Act - expand an unexpanded category
viewModel.toggleCategory("LLM Providers")
// Assert
var categories = viewModel.uiState.value.categories
assertEquals(2, categories.size)
assertTrue(
"LLM Providers should be expanded",
categories.first { it.name == "LLM Providers" }.expanded,
)
assertTrue(
"Tool API Keys should remain expanded",
categories.first { it.name == "Tool API Keys" }.expanded,
)
// Act - collapse it again
viewModel.toggleCategory("LLM Providers")
// Assert
categories = viewModel.uiState.value.categories
assertEquals(2, categories.size)
assertFalse(
"LLM Providers should be collapsed",
categories.first { it.name == "LLM Providers" }.expanded,
)
assertTrue(
"Tool API Keys should remain expanded",
categories.first { it.name == "Tool API Keys" }.expanded,
)
// Act - toggle the other category, ensure first stays untouched
viewModel.toggleCategory("Tool API Keys")
// Assert
categories = viewModel.uiState.value.categories
assertFalse(
"LLM Providers should stay collapsed",
categories.first { it.name == "LLM Providers" }.expanded,
)
assertFalse(
"Tool API Keys should now be collapsed",
categories.first { it.name == "Tool API Keys" }.expanded,
)
}
}