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

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

/**
 * Unit tests for the live-feed event descriptions (PLAN.md §6.2), mirroring the
 * website's `lib/shardEvents.js`. Fields arrive as a [kotlinx.serialization.json.JsonObject]
 * — the shape of a live SSE frame.
 */
class ShardEventTextTest {

    private fun fields(literal: String) = Json.parseToJsonElement(literal).jsonObject

    @Test fun playerDeathWithKiller() {
        val text = ShardEventText.describe(
            "player.death",
            fields("""{"who":{"name":"Alice"},"killer":{"name":"a dragon"}}"""),
        )
        assertEquals("Alice was slain by a dragon", text)
    }

    @Test fun playerDeathWithoutKiller() {
        val text = ShardEventText.describe("player.death", fields("""{"who":"Bob"}"""))
        assertEquals("Bob was slain", text)
    }

    @Test fun economySupplyGroupsNumbers() {
        val text = ShardEventText.describe(
            "economy.supply",
            fields("""{"gold":123456789,"accounts":1200}"""),
        )
        assertEquals("Gold supply: 123,456,789 across 1,200 accounts", text)
    }

    @Test fun houseDecayReadsToAndRegion() {
        val text = ShardEventText.describe(
            "house.decay",
            fields("""{"name":"Keep","to":"IDOC","region":"Britain"}"""),
        )
        assertEquals("Keep is now IDOC — Britain", text)
    }

    @Test fun champUpdateActiveWithBoss() {
        val text = ShardEventText.describe(
            "champ.update",
            fields("""{"name":"Rikktor","status":"active","bossUp":true,"boss":"Rikktor"}"""),
        )
        assertEquals("Rikktor: boss is up (Rikktor)", text)
    }

    @Test fun cityUpdateWithGovernor() {
        val text = ShardEventText.describe(
            "city.update",
            fields("""{"city":"Trinsic","governor":{"name":"Dupre"}}"""),
        )
        assertEquals("Trinsic is governed by Dupre", text)
    }

    @Test fun presenceOnlineCount() {
        assertEquals(
            "58 players online",
            ShardEventText.describe("presence.online", fields("""{"count":58}""")),
        )
    }

    @Test fun unknownKindFallsBackToKind() {
        assertEquals("some.weird.kind", ShardEventText.describe("some.weird.kind", fields("{}")))
    }

    @Test fun missingActorReadsAsSomeone() {
        val text = ShardEventText.describe("mob.login", fields("{}"))
        assertTrue(text.startsWith("Someone"))
    }
}
