20 files changed
+1065
-8
No files matched your search
@@ -0,0 +1,109 @@
|
||||
package com.m57.hermescontrol.ui.channels
|
||||
|
||||
import com.m57.hermescontrol.data.model.MessagingPlatform
|
||||
import com.m57.hermescontrol.data.model.MessagingPlatformResponse
|
||||
import com.m57.hermescontrol.data.model.MessagingPlatformUpdate
|
||||
import com.m57.hermescontrol.data.model.MessagingPlatformUpdateResponse
|
||||
import com.m57.hermescontrol.data.remote.ApiClient
|
||||
import com.m57.hermescontrol.data.remote.HermesApiService
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkObject
|
||||
import io.mockk.unmockkAll
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||
import kotlinx.coroutines.test.advanceUntilIdle
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import retrofit2.Response
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class ChannelsViewModelTest {
|
||||
private val testDispatcher = StandardTestDispatcher()
|
||||
private lateinit var mockApi: HermesApiService
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
Dispatchers.setMain(testDispatcher)
|
||||
mockkObject(ApiClient)
|
||||
mockApi = mockk(relaxed = true)
|
||||
every { ApiClient.hermesApi } returns mockApi
|
||||
coEvery { mockApi.getMessagingPlatforms() } returns Response.success(platformsResponse())
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
Dispatchers.resetMain()
|
||||
unmockkAll()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `hot served update clears restart requirement and refreshes platforms`() {
|
||||
coEvery { mockApi.configurePlatform("telegram", any()) } returns
|
||||
Response.success(
|
||||
MessagingPlatformUpdateResponse(
|
||||
ok = true,
|
||||
platform = "telegram",
|
||||
hotServed = true,
|
||||
),
|
||||
)
|
||||
|
||||
val viewModel = ChannelsViewModel()
|
||||
viewModel.loadPlatforms()
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
viewModel.configurePlatform("telegram", MessagingPlatformUpdate(env = mapOf("token" to "redacted")))
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
assertFalse(viewModel.uiState.value.restartNeeded)
|
||||
assertTrue(
|
||||
viewModel.uiState.value.toastMessage
|
||||
?.contains("reloaded live") == true,
|
||||
)
|
||||
coVerify { mockApi.configurePlatform("telegram", any()) }
|
||||
coVerify(atLeast = 2) { mockApi.getMessagingPlatforms() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `update without hot served preserves restart required behavior`() {
|
||||
coEvery { mockApi.configurePlatform("telegram", any()) } returns
|
||||
Response.success(MessagingPlatformUpdateResponse(ok = true, platform = "telegram"))
|
||||
|
||||
val viewModel = ChannelsViewModel()
|
||||
viewModel.loadPlatforms()
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
viewModel.configurePlatform("telegram", MessagingPlatformUpdate(enabled = true))
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
assertTrue(viewModel.uiState.value.restartNeeded)
|
||||
assertTrue(
|
||||
viewModel.uiState.value.toastMessage
|
||||
?.contains("restart the gateway") == true,
|
||||
)
|
||||
}
|
||||
|
||||
private fun platformsResponse(): MessagingPlatformResponse =
|
||||
MessagingPlatformResponse(
|
||||
envPath = "/tmp/.env",
|
||||
gatewayStartCommand = "hermes gateway start",
|
||||
platforms =
|
||||
listOf(
|
||||
MessagingPlatform(
|
||||
id = "telegram",
|
||||
name = "Telegram",
|
||||
enabled = true,
|
||||
configured = true,
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import com.m57.hermescontrol.data.model.ProfileInfo
|
||||
import com.m57.hermescontrol.data.model.ProfileSetupCommandResponse
|
||||
import com.m57.hermescontrol.data.model.ProfilesResponse
|
||||
import com.m57.hermescontrol.data.model.RenameProfileRequest
|
||||
import com.m57.hermescontrol.data.model.StatusResponse
|
||||
import com.m57.hermescontrol.data.remote.ApiClient
|
||||
import com.m57.hermescontrol.data.remote.HermesApiService
|
||||
import com.m57.hermescontrol.data.remote.NetworkError
|
||||
@@ -62,6 +63,7 @@ class ProfilesViewModelTest {
|
||||
coEvery { mockApi.getProfiles() } returns
|
||||
Response.success(ProfilesResponse(listOf(ProfileInfo(name = "default", is_default = true))))
|
||||
coEvery { mockApi.getActiveProfile() } returns Response.success(ActiveProfileResponse(active = "default"))
|
||||
coEvery { mockApi.getStatus() } returns Response.success(StatusResponse())
|
||||
}
|
||||
|
||||
@Before
|
||||
@@ -360,6 +362,53 @@ class ProfilesViewModelTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `loadProfiles displays normalized shared gateway profiles`() {
|
||||
coEvery { mockApi.getProfiles() } returns
|
||||
Response.success(
|
||||
ProfilesResponse(
|
||||
listOf(
|
||||
ProfileInfo(name = "default", is_default = true),
|
||||
ProfileInfo(name = "coding"),
|
||||
),
|
||||
),
|
||||
)
|
||||
coEvery { mockApi.getStatus() } returns
|
||||
Response.success(
|
||||
StatusResponse(
|
||||
gatewaySharedWith = listOf("default", "coding", "coding", " ", ""),
|
||||
),
|
||||
)
|
||||
|
||||
val vm = createViewModel()
|
||||
vm.loadProfiles()
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
assertEquals(listOf("default", "coding"), vm.uiState.value.sharedGatewayProfiles)
|
||||
assertNull(vm.uiState.value.errorMessage)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `loadProfiles keeps profiles usable when optional status request fails`() {
|
||||
coEvery { mockApi.getStatus() } returns errorResponse(404)
|
||||
|
||||
val vm = createViewModel()
|
||||
vm.loadProfiles()
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
assertEquals(
|
||||
listOf("default"),
|
||||
vm.uiState.value.profiles
|
||||
.map { it.name },
|
||||
)
|
||||
assertEquals("default", vm.uiState.value.activeProfileName)
|
||||
assertTrue(
|
||||
vm.uiState.value.sharedGatewayProfiles
|
||||
.isEmpty(),
|
||||
)
|
||||
assertNull(vm.uiState.value.errorMessage)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `cloneProfile success calls createProfile with clone_from and reloads`() {
|
||||
coEvery { mockApi.createProfile(any()) } returns Response.success(Unit)
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
package com.m57.hermescontrol.ui.system
|
||||
|
||||
import android.app.Application
|
||||
import com.m57.hermescontrol.data.model.GatewayMigrationPlan
|
||||
import com.m57.hermescontrol.data.model.GatewayMigrationProfile
|
||||
import com.m57.hermescontrol.data.model.GatewayMigrationStartResponse
|
||||
import com.m57.hermescontrol.data.remote.ApiClient
|
||||
import com.m57.hermescontrol.data.remote.HermesApiService
|
||||
import com.m57.hermescontrol.ui.common.ActionProgressPhase
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkObject
|
||||
import io.mockk.unmockkAll
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||
import kotlinx.coroutines.test.advanceUntilIdle
|
||||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import okhttp3.ResponseBody.Companion.toResponseBody
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import retrofit2.Response
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class GatewayMigrationViewModelTest {
|
||||
private val testDispatcher = StandardTestDispatcher()
|
||||
private val mockApp = mockk<Application>(relaxed = true)
|
||||
private lateinit var mockApi: HermesApiService
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
Dispatchers.setMain(testDispatcher)
|
||||
mockkObject(ApiClient)
|
||||
mockApi = mockk(relaxed = true)
|
||||
every { ApiClient.hermesApi } returns mockApi
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
Dispatchers.resetMain()
|
||||
unmockkAll()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unsupported migration plan is capability local`() {
|
||||
coEvery { mockApi.getGatewayMigrationPlan() } returns
|
||||
Response.error(404, "unsupported".toResponseBody(null))
|
||||
|
||||
val viewModel = SystemViewModel(mockApp, migrationDispatcher = testDispatcher)
|
||||
viewModel.loadMigrationPlan()
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
assertFalse(viewModel.uiState.value.migrationSupported == true)
|
||||
assertEquals(false, viewModel.uiState.value.migrationSupported)
|
||||
assertNull(viewModel.uiState.value.migrationPlan)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `migration plan exposes backend states without inventing eligibility rules`() {
|
||||
val plan =
|
||||
GatewayMigrationPlan(
|
||||
profiles = listOf(GatewayMigrationProfile(profile = "default")),
|
||||
eligible = false,
|
||||
blockers = emptyList(),
|
||||
notices = listOf("explicit migration is available"),
|
||||
)
|
||||
coEvery { mockApi.getGatewayMigrationPlan() } returns Response.success(plan)
|
||||
|
||||
val viewModel = SystemViewModel(mockApp, migrationDispatcher = testDispatcher)
|
||||
viewModel.loadMigrationPlan()
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
assertEquals(plan, viewModel.uiState.value.migrationPlan)
|
||||
assertEquals(
|
||||
false,
|
||||
viewModel.uiState.value.migrationPlan
|
||||
?.eligible,
|
||||
)
|
||||
assertTrue(
|
||||
viewModel.uiState.value.migrationPlan
|
||||
?.blockers
|
||||
?.isEmpty() == true,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `migration POST only starts action tracking and does not report completion`() {
|
||||
val plan = GatewayMigrationPlan(eligible = false)
|
||||
coEvery { mockApi.getGatewayMigrationPlan() } returns Response.success(plan)
|
||||
coEvery { mockApi.startGatewayMigration() } returns
|
||||
Response.success(
|
||||
GatewayMigrationStartResponse(
|
||||
ok = true,
|
||||
pid = 1234,
|
||||
name = "gateway-migrate",
|
||||
),
|
||||
)
|
||||
|
||||
val viewModel = SystemViewModel(mockApp, migrationDispatcher = testDispatcher)
|
||||
viewModel.loadMigrationPlan()
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
viewModel.startMigration()
|
||||
testDispatcher.scheduler.runCurrent()
|
||||
|
||||
assertTrue(viewModel.migrationProgress.state.value.visible)
|
||||
assertEquals(ActionProgressPhase.RUNNING, viewModel.migrationProgress.state.value.phase)
|
||||
assertEquals("gateway-migrate", viewModel.migrationProgress.state.value.actionName)
|
||||
coVerify { mockApi.startGatewayMigration() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `migration POST with an unsuccessful body is shown as rejected`() {
|
||||
coEvery { mockApi.getGatewayMigrationPlan() } returns Response.success(GatewayMigrationPlan())
|
||||
coEvery { mockApi.startGatewayMigration() } returns
|
||||
Response.success(
|
||||
GatewayMigrationStartResponse(
|
||||
ok = false,
|
||||
name = "gateway-migrate",
|
||||
),
|
||||
)
|
||||
|
||||
val viewModel = SystemViewModel(mockApp, migrationDispatcher = testDispatcher)
|
||||
viewModel.loadMigrationPlan()
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
viewModel.startMigration()
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
assertEquals(ActionProgressPhase.FAILED, viewModel.migrationProgress.state.value.phase)
|
||||
assertEquals("Migration was rejected by the backend", viewModel.migrationProgress.state.value.error)
|
||||
coVerify { mockApi.startGatewayMigration() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `already multiplexed plan never starts backend action`() {
|
||||
coEvery { mockApi.getGatewayMigrationPlan() } returns
|
||||
Response.success(GatewayMigrationPlan(alreadyMultiplexed = true))
|
||||
|
||||
val viewModel = SystemViewModel(mockApp, migrationDispatcher = testDispatcher)
|
||||
viewModel.loadMigrationPlan()
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
viewModel.startMigration()
|
||||
testDispatcher.scheduler.runCurrent()
|
||||
|
||||
assertFalse(viewModel.migrationProgress.state.value.visible)
|
||||
assertTrue(
|
||||
viewModel.uiState.value.toastMessage
|
||||
?.contains("already multiplexed") == true,
|
||||
)
|
||||
coVerify(exactly = 0) { mockApi.startGatewayMigration() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `migration with blockers never starts backend action`() {
|
||||
coEvery { mockApi.getGatewayMigrationPlan() } returns
|
||||
Response.success(GatewayMigrationPlan(blockers = listOf("duplicate credential")))
|
||||
|
||||
val viewModel = SystemViewModel(mockApp, migrationDispatcher = testDispatcher)
|
||||
viewModel.loadMigrationPlan()
|
||||
testDispatcher.scheduler.advanceUntilIdle()
|
||||
|
||||
viewModel.startMigration()
|
||||
testDispatcher.scheduler.runCurrent()
|
||||
|
||||
assertFalse(viewModel.migrationProgress.state.value.visible)
|
||||
assertTrue(
|
||||
viewModel.uiState.value.toastMessage
|
||||
?.contains("blocked") == true,
|
||||
)
|
||||
coVerify(exactly = 0) { mockApi.startGatewayMigration() }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user