Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,14 @@ internal class ServerManagerImpl @Inject constructor(

private suspend fun getServerIdSanitize(serverId: Int): Int? {
return if (serverId == SERVER_ID_ACTIVE) {
localStorage.getInt(PREF_ACTIVE_SERVER)
?: serverDao.getLastServerId()
val storedActiveServerId = localStorage.getInt(PREF_ACTIVE_SERVER)
if (storedActiveServerId != null && serverDao.get(storedActiveServerId) == null) {
Timber.w("Active server ID $storedActiveServerId no longer exists, removing from storage")
localStorage.remove(PREF_ACTIVE_SERVER)
serverDao.getLastServerId()
} else {
storedActiveServerId ?: serverDao.getLastServerId()
}
} else {
serverId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,53 @@ class ServerManagerImplTest {

assertNull(result)
}

@Test
fun `Given stale active server ID when getServer with SERVER_ID_ACTIVE then removes stale ID and returns last server`() = runTest {
val staleServerId = 5
val lastServerId = 7
val lastServer = createServer(id = lastServerId)

coEvery { localStorage.getInt("active_server") } returns staleServerId
coEvery { serverDao.get(staleServerId) } returns null // Server no longer exists
coEvery { localStorage.remove("active_server") } just Runs
coEvery { serverDao.getLastServerId() } returns lastServerId
coEvery { serverDao.get(lastServerId) } returns lastServer

val result = serverManager.getServer(SERVER_ID_ACTIVE)

assertEquals(lastServer, result)
coVerify { localStorage.remove("active_server") }
}

@Test
fun `Given stale active server ID and no other servers when getServer with SERVER_ID_ACTIVE then removes stale ID and returns null`() = runTest {
val staleServerId = 5

coEvery { localStorage.getInt("active_server") } returns staleServerId
coEvery { serverDao.get(staleServerId) } returns null // Server no longer exists
coEvery { localStorage.remove("active_server") } just Runs
coEvery { serverDao.getLastServerId() } returns null // No other servers

val result = serverManager.getServer(SERVER_ID_ACTIVE)

assertNull(result)
coVerify { localStorage.remove("active_server") }
}

@Test
fun `Given valid active server ID when getServer with SERVER_ID_ACTIVE then does not remove from localStorage`() = runTest {
val activeServerId = 3
val server = createServer(id = activeServerId)

coEvery { localStorage.getInt("active_server") } returns activeServerId
coEvery { serverDao.get(activeServerId) } returns server

val result = serverManager.getServer(SERVER_ID_ACTIVE)

assertEquals(server, result)
coVerify(exactly = 0) { localStorage.remove("active_server") }
}
}

@Nested
Expand Down
Loading