package com.github.nullptroma.wallenc.domain.vault.errors

import com.github.nullptroma.wallenc.domain.errors.WallencException
import com.github.nullptroma.wallenc.domain.vault.network.yandexdisk.YandexDiskAuthException
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import okhttp3.ResponseBody.Companion.toResponseBody
import retrofit2.HttpException
import retrofit2.Response
import java.io.FileNotFoundException
import java.io.IOException
import java.net.SocketTimeoutException

class VaultThrowableMappingTest {

    @Test
    fun mapsYandexDiskAuthToAuthFailed() {
        val mapped = YandexDiskAuthException("unauthorized").toVaultWallencException()
        assertTrue(mapped is WallencException.Auth.Failed)
    }

    @Test
    fun mapsHttpExceptionToNetworkHttpFailed() {
        val response = Response.error<String>(401, "".toResponseBody(null))
        val mapped = HttpException(response).toVaultWallencException()
        assertTrue(mapped is WallencException.Network.HttpFailed)
        assertEquals(401, (mapped as WallencException.Network.HttpFailed).statusCode)
    }

    @Test
    fun mapsMissingOAuthTokenIoToTokenMissing() {
        val mapped = IOException("Yandex OAuth token is missing").toVaultWallencException()
        assertTrue(mapped is WallencException.Auth.TokenMissing)
    }

    @Test
    fun mapsSocketTimeoutToOperationTimedOut() {
        val mapped = SocketTimeoutException("timeout").toVaultWallencException()
        assertTrue(mapped is WallencException.Network.OperationTimedOut)
    }

    @Test
    fun mapsFileNotFoundToStorageFileNotFound() {
        val mapped = FileNotFoundException("x").toVaultWallencException()
        assertTrue(mapped is WallencException.Storage.FileNotFound)
    }

    @Test
    fun mapsIllegalStateNotAFile() {
        val mapped = IllegalStateException("Not a file").toVaultWallencException()
        assertTrue(mapped is WallencException.Storage.NotAFile)
    }
}
