perf(#1171): async AuthManager startup with readiness gate and lazy database (#1182)

Co-authored-by: hy4ri <hy4ri@users.noreply.github.com>
This commit is contained in:
M57andhy4ri authored and GitHub committed 2026-09-17 20:54:03 +03:00
1 parent 736433b326
commit 220779bf19
15 files changed
+413 -176

No files matched your search

@@ -1,6 +1,7 @@
package com.m57.hermescontrol.ui.chat
import com.m57.hermescontrol.ui.chat.fakes.FakeChatMessageDao
import kotlinx.coroutines.async
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
@@ -10,6 +11,41 @@ class ChatPersistenceRepositoryTest {
private lateinit var dao: FakeChatMessageDao
private lateinit var repository: ChatPersistenceRepository
@Test
fun providerIsLazyAndPendingWriteWaitsForDatabase() =
runTest {
val ready = kotlinx.coroutines.CompletableDeferred<Unit>()
var calls = 0
val lazyRepository =
ChatPersistenceRepository {
calls++
ready.await()
dao
}
assertEquals(0, calls)
val pending =
async {
lazyRepository.persistMessage(
ChatMessage(id = "pending", role = MessageRole.USER, content = "saved"),
"s",
)
}
kotlinx.coroutines.yield()
assertEquals(1, calls)
org.junit.Assert.assertFalse(pending.isCompleted)
ready.complete(Unit)
pending.await()
assertEquals("pending", dao.getMessagesForSession("s").single().id)
}
@Test
fun providerFailureIsNotReportedAsEmptyHistory() =
runTest {
val failure = IllegalStateException("database unavailable")
val failing = ChatPersistenceRepository { throw failure }
org.junit.Assert.assertSame(failure, runCatching { failing.loadMessages("s") }.exceptionOrNull())
}
@Before
fun setup() {
dao = FakeChatMessageDao()