package com.ponzischeme89.memby.ui

import com.ponzischeme89.memby.data.HomeCache
import com.ponzischeme89.memby.data.model.BaseItem
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class HomeUiStateTest {
    @Test
    fun cachedContentIsShownWhileOnlyMissingRowsLoad() {
        val cache = HomeCache(
            continueWatching = listOf(BaseItem(id = "resume")),
            favorites = listOf(BaseItem(id = "favorite")),
        )

        val state = HomeUiState.from(cache)

        assertFalse(HomeSection.CONTINUE in state.loading)
        assertFalse(HomeSection.FAVORITES in state.loading)
        assertTrue(HomeSection.LATEST in state.loading)
    }

    /**
     * A cache written by the build that still had two rows must not lose its Next Up
     * episodes on the first launch after the update — that cache is what the launcher
     * draws before any network response arrives.
     */
    @Test
    fun cachedNextUpItemsFoldIntoContinueWatching() {
        val cache = HomeCache(
            continueWatching = listOf(BaseItem(id = "resume"), BaseItem(id = "same")),
            nextUp = listOf(BaseItem(id = "same", name = "Next copy"), BaseItem(id = "next")),
        )

        val state = HomeUiState.from(cache)

        assertEquals(
            listOf("resume", "same", "next"),
            state.continueWatching.map(BaseItem::id),
        )
    }
}
