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

import org.junit.Assert.assertEquals
import org.junit.Test

/** Unit tests for the live board merge helper (PLAN.md §6.2). */
class LiveBoardTest {

    private data class Row(val id: String, val name: String)

    private fun board() = LiveBoard<Row> { it.id }

    @Test fun seedReplacesContents() {
        val b = board()
        b.seed(listOf(Row("1", "a"), Row("2", "b")))
        b.seed(listOf(Row("3", "c")))
        assertEquals(listOf(Row("3", "c")), b.values())
    }

    @Test fun upsertInsertsThenUpdatesInPlace() {
        val b = board()
        b.seed(listOf(Row("1", "a")))
        b.upsert(Row("2", "b"))
        b.upsert(Row("1", "a2")) // update existing — no reorder, no duplicate
        assertEquals(listOf(Row("1", "a2"), Row("2", "b")), b.values())
    }

    @Test fun removeDropsById() {
        val b = board()
        b.seed(listOf(Row("1", "a"), Row("2", "b")))
        b.remove("1")
        assertEquals(listOf(Row("2", "b")), b.values())
    }

    @Test fun removeMissingIsNoOp() {
        val b = board()
        b.seed(listOf(Row("1", "a")))
        b.remove("nope")
        assertEquals(listOf(Row("1", "a")), b.values())
    }

    @Test fun insertionOrderPreserved() {
        val b = board()
        b.upsert(Row("b", "1"))
        b.upsert(Row("a", "2"))
        b.upsert(Row("c", "3"))
        assertEquals(listOf("b", "a", "c"), b.values().map { it.id })
    }
}
