<commit_before>package org.ccci.gto.android.common.db

import io.mockk.every
import io.mockk.slot
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class, ExperimentalStdlibApi::class)
class CoroutinesFlowDaoTest {
    private val callback = slot<Dao.InvalidationCallback>()
    private val dao = spyk<CoroutinesFlowDao> {
        every { services } returns mutableMapOf()
        every { registerInvalidationCallback(capture(callback)) } returns Unit
    }

    @Test
    fun verifyFindAsFlowMonitorsInvalidations() = runTest(UnconfinedTestDispatcher()) {
        every { dao.coroutineDispatcher } returns coroutineContext[CoroutineDispatcher]!!
        val job = dao.findAsFlow(String::class.java).launchIn(this)
        verify { dao.registerInvalidationCallback(any()) }
        verify(inverse = true) { dao.unregisterInvalidationCallback(any()) }

        job.cancel()
        verify { dao.unregisterInvalidationCallback(any()) }
    }

    @Test
    fun verifyFindAsFlow() = runTest {
        every { dao.coroutineDispatcher } returns coroutineContext[CoroutineDispatcher]!!
        val flow = dao.findAsFlow(String::class.java).launchIn(this)
        verify(exactly = 0) { dao.find(String::class.java) }

        // initial trigger
        advanceUntilIdle()
        verify { dao.registerInvalidationCallback(any()) }
        verify(exactly = 1) { dao.find(String::class.java) }

        // invalidation triggers processing
        callback.captured.onInvalidate(String::class.java)<commit_msg>make sure there isn't a race condition when starting findAsFlow()<commit_after>package org.ccci.gto.android.common.db

import io.mockk.every
import io.mockk.slot
import io.mockk.spyk
import io.mockk.verify
import io.mockk.verifyOrder
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class, ExperimentalStdlibApi::class)
class CoroutinesFlowDaoTest {
    private val callback = slot<Dao.InvalidationCallback>()
    private val dao = spyk<CoroutinesFlowDao> {
        every { services } returns mutableMapOf()
        every { registerInvalidationCallback(capture(callback)) } returns Unit
    }

    @Test
    fun `findAsFlow() - Monitors Invalidations`() = runTest(UnconfinedTestDispatcher()) {
        every { dao.coroutineDispatcher } returns coroutineContext[CoroutineDispatcher]!!
        val job = dao.findAsFlow(String::class.java).launchIn(this)
        verifyOrder {
            dao.registerInvalidationCallback(any())
            // Ensure we query data at least once after we register the invalidation callback to avoid a race condition.
            // We risk not emitting fresh data if an invalidation triggers after we last fetched data but before we
            // register the callback.
            dao.find(String::class.java)
        }
        verify(inverse = true) { dao.unregisterInvalidationCallback(any()) }

        job.cancel()
        verify { dao.unregisterInvalidationCallback(any()) }
    }

    @Test
    fun `findAsFlow()`() = runTest {
        every { dao.coroutineDispatcher } returns coroutineContext[CoroutineDispatcher]!!
        val flow = dao.findAsFlow(String::class.java).launchIn(this)
        verify(exactly = 0) { dao.find(String::class.java) }

        // initial trigger
        advanceUntilIdle()
        verify { dao.registerInvalidationCallback(any()) }
        verify(exactly = 1) { dao.find(String::class.java) }

        // invalidation triggers processing
        callback.captured.onInvalidate(String::class.java)
{'fraction_non_alphanumeric': 0.09596083231334149, 'fraction_numerical': 0.0009791921664626683, 'mean_word_length': 3.801410105757932, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6992662', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1195, 'n_words': 230}