/*
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package com.runicgateway.app.core.result

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

/**
 * The [ApiResult] helpers: [map] transforms an [ApiResult.Ok] and passes the two
 * failure variants through unchanged; [isShardUnavailable] is the 503 "shard down"
 * signal the player screens render as offline.
 */
class ApiResultExtrasTest {

    @Test fun mapTransformsOkBody() {
        val mapped = ApiResult.Ok(listOf(1, 2, 3)).map { it.size }
        assertEquals(ApiResult.Ok(3), mapped)
    }

    @Test fun mapPassesFailuresThroughUnchanged() {
        val http: ApiResult<Int> = ApiResult.HttpError(500, "boom")
        assertSame(http, http.map { it + 1 })

        val cause = RuntimeException("offline")
        val network: ApiResult<Int> = ApiResult.NetworkError(cause)
        val out = network.map { it + 1 }
        assertTrue(out is ApiResult.NetworkError)
        assertSame(cause, (out as ApiResult.NetworkError).cause)
    }

    @Test fun isShardUnavailableOnlyForHttp503() {
        assertTrue(ApiResult.HttpError(503).isShardUnavailable())
        assertFalse(ApiResult.HttpError(500).isShardUnavailable())
        assertFalse(ApiResult.Ok(Unit).isShardUnavailable())
        assertFalse(ApiResult.NetworkError(RuntimeException()).isShardUnavailable())
    }
}
