package com.ponzischeme89.memby.update

import com.ponzischeme89.memby.data.model.GatewayUpdate
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test

class ServerUpdateServiceTest {
    @Test
    fun `returns mandatory server decision without a session dependency`() = runTest {
        val service = ServerUpdateService.forTest {
            GatewayUpdate(
                status = GatewayUpdate.STATUS_MANDATORY,
                version = "0.3.0",
                downloadUrl = "https://memby.test/updates/memby.apk",
            )
        }

        val update = service.check().getOrThrow()

        assertEquals("0.3.0", update?.version)
        assertTrue(update?.isMandatory == true)
    }

    @Test
    fun `non-actionable response is no update`() = runTest {
        val service = ServerUpdateService.forTest {
            GatewayUpdate(status = GatewayUpdate.STATUS_NONE)
        }

        assertNull(service.check().getOrThrow())
    }

    @Test
    fun `network failure is reported without manufacturing a blocking update`() = runTest {
        val service = ServerUpdateService.forTest { error("offline") }

        assertTrue(service.check().isFailure)
    }
}
