package moe.lava.neon.tests.api

import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import moe.lava.neon.api.ApiClient
import moe.lava.neon.api.endpoints.getExperiments
import moe.lava.neon.common.captcha.CaptchaResponse

class CaptchaTest : FunSpec({
    val mock = DiscordApiMock()
    val client = ApiClient(mock.engine, false)

    val (captchaReq, captchaRes) = mock.generateCaptcha()
    mock.isCaptchaEnabled = true

    test("captcha should not be handled") {
        val res = client.getExperiments().response
        res.status.value shouldBe 400
    }

    test("captcha should be handled") {
        client.setCaptchaHandler { req ->
            captchaRes
                .takeIf { req == captchaReq }
                ?: CaptchaResponse.Failed(Throwable())
        }
        val res = client.getExperiments().response
        res.status.value shouldBe 200
    }
})
