package com.ponzischeme89.memby.data.analytics

import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class JourneyAnalyticsTest {
    @Test
    fun `events stay ordered and contain no arbitrary characters`() {
        val analytics = JourneyAnalytics(userId = "user-1", now = { 1_700_000_000_000L }, journeyId = "journey-1")
        analytics.track(
            category = "content", action = "open", screen = "home screen",
            feature = "latest_movies", itemId = "item/unsafe", itemType = "Movie",
        )
        val events = analytics.drain()

        assertEquals(listOf(0, 1), events.map { it.sequence })
        assertEquals("user-1", events.last().userId)
        assertEquals("homescreen", events.last().screen)
        assertEquals("itemunsafe", events.last().itemId)
    }

    @Test
    fun `ending records one terminal event and rejects later actions`() {
        val analytics = JourneyAnalytics(userId = "user-1", journeyId = "journey-1")
        analytics.drain()
        analytics.end("details")
        analytics.end("player")
        analytics.track("navigation", "open", feature = "search")
        val events = analytics.drain()

        assertEquals(1, events.size)
        assertEquals("journey_end", events.single().action)
        assertTrue(events.single().screen == "details")
        assertFalse(events.any { it.feature == "search" })
    }
}
