package com.hermes.app.core.analytics

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
import java.util.UUID
import kotlinx.datetime.Instant
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34])
class HermesAnalyticsTrackerTest {
    @Test
    fun convertsPendingEventsToBatchRequest() {
        val tracker = HermesAnalyticsTracker()
        val event = HermesTrackedEvent(
            id = UUID.fromString("00000000-0000-0000-0000-000000000001"),
            name = "screen_viewed",
            attributes = mapOf("screen_name" to "feed"),
            timestamp = Instant.fromEpochMilliseconds(1_000),
        )

        val batch = tracker.toBatchRequest(listOf(event))

        assertEquals(1, batch.events.size)
        assertEquals("screen_viewed", batch.events.first().eventName)
        assertEquals("feed", batch.events.first().attributes!!.first().value)
    }

    @Test
    fun marksDeliveredEventsAsRemoved() {
        val tracker = HermesAnalyticsTracker()

        tracker.track("screen_viewed", mapOf("screen_name" to "feed"))
        val trackedId = tracker.pendingEventsSnapshot().first().id
        tracker.markDelivered(setOf(trackedId))

        assertTrue(tracker.pendingEventsSnapshot().isEmpty())
    }
}
