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

import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * Decode/encode tests for the Mobile SSO bridge DTOs (PLAN.md §4.2). Provider
 * discovery is public (never secrets); the exchange body uses snake_case
 * `code_verifier` to match the backend.
 */
class SsoDtoTest {

    private val json = Json {
        ignoreUnknownKeys = true
        explicitNulls = false
        coerceInputValues = true
    }

    @Test fun providerDecodesWithOptionalFields() {
        val dto = json.decodeFromString<SsoProviderDto>(
            """{"id":"discord","name":"Discord","icon":"discord","loginUrl":"/auth/discord","priority":2}""",
        )
        assertEquals("discord", dto.id)
        assertEquals("Discord", dto.name)
        assertEquals(2, dto.priority)
    }

    @Test fun providerToleratesMissingOptionals() {
        val dto = json.decodeFromString<SsoProviderDto>("""{"id":"oidc","name":"Corp SSO"}""")
        assertNull(dto.icon)
        assertNull(dto.priority)
    }

    @Test fun exchangeRequestEncodesSnakeCaseVerifier() {
        val body = json.encodeToString(MobileSsoExchangeRequest(code = "abc123", codeVerifier = "v-e-r-i-f-i-e-r"))
        assertTrue(body.contains("\"code\":\"abc123\""))
        assertTrue(body.contains("\"code_verifier\":\"v-e-r-i-f-i-e-r\""))
    }
}
