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

import com.runicgateway.app.data.api.dto.VersionDto
import com.runicgateway.app.data.repository.ConnectionRepository.Companion.evaluateVersion
import com.runicgateway.app.data.repository.ConnectionRepository.VersionVerdict
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * The connect-probe version guard (§3): a Runic Gateway backend on the app's
 * supported API is accepted; a wrong service identity or an unsupported API
 * version is refused clearly rather than mis-rendered.
 */
class ConnectionVersionGuardTest {

    @Test
    fun `matching service and api is Ok`() {
        val v = VersionDto(service = "runic-gateway", api = "v1", server = "1.2.3")
        assertEquals(VersionVerdict.Ok, evaluateVersion(v))
    }

    @Test
    fun `service id is case-insensitive and trimmed`() {
        val v = VersionDto(service = "  Runic-Gateway  ", api = "V1", server = "x")
        assertEquals(VersionVerdict.Ok, evaluateVersion(v))
    }

    @Test
    fun `wrong service is NotRunicGateway`() {
        val v = VersionDto(service = "some-other-app", api = "v1", server = "x")
        assertEquals(VersionVerdict.NotRunicGateway, evaluateVersion(v))
    }

    @Test
    fun `blank api is lenient (older backend) and accepted`() {
        val v = VersionDto(service = "runic-gateway", api = "", server = "x")
        assertEquals(VersionVerdict.Ok, evaluateVersion(v))
    }

    @Test
    fun `future api version is a mismatch carrying the server value`() {
        val v = VersionDto(service = "runic-gateway", api = "v2", server = "x")
        val verdict = evaluateVersion(v)
        assertTrue(verdict is VersionVerdict.Mismatch)
        assertEquals("v2", (verdict as VersionVerdict.Mismatch).serverApi)
    }
}
