package com.ponzischeme89.memby.data

import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import retrofit2.HttpException
import retrofit2.Response
import java.io.IOException

class SessionValidationTest {
    @Test
    fun `server restart preserves saved session`() {
        assertTrue(shouldPreserveSessionAfterValidationFailure(IOException("offline")))
    }

    @Test
    fun `maintenance response preserves saved session`() {
        assertTrue(shouldPreserveSessionAfterValidationFailure(httpFailure(503)))
    }

    @Test
    fun `explicit unauthorized response may invalidate saved session`() {
        assertFalse(shouldPreserveSessionAfterValidationFailure(httpFailure(401)))
    }

    private fun httpFailure(code: Int): HttpException =
        HttpException(Response.error<Any>(code, "error".toResponseBody()))
}
