package xyz.cottongin.radio247.metadata

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

class ArtCacheTest {

    @Test
    fun get_returnsNullForMissingKey() {
        val cache = ArtCache()
        assertNull(cache.get("nonexistent"))
    }

    @Test
    fun put_and_get() {
        val cache = ArtCache()
        cache.put("key1", "https://example.com/art.jpg")
        assertEquals("https://example.com/art.jpg", cache.get("key1"))
    }

    @Test
    fun evicts_when_exceeding_maxSize() {
        val cache = ArtCache(maxSize = 2)
        cache.put("a", "url-a")
        cache.put("b", "url-b")
        cache.put("c", "url-c")
        assertNull(cache.get("a"))
        assertEquals("url-b", cache.get("b"))
        assertEquals("url-c", cache.get("c"))
    }

    @Test
    fun removeByValue_removesMatchingEntries() {
        val cache = ArtCache()
        cache.put("key1", "https://station.com/art.png")
        cache.put("key2", "https://other.com/art.png")
        cache.put("key3", "https://station.com/art.png")

        cache.removeByValue("https://station.com/art.png")

        assertNull(cache.get("key1"))
        assertEquals("https://other.com/art.png", cache.get("key2"))
        assertNull(cache.get("key3"))
    }

    @Test
    fun removeByValue_noOpWhenValueNotPresent() {
        val cache = ArtCache()
        cache.put("key1", "url-a")
        cache.removeByValue("url-b")
        assertEquals("url-a", cache.get("key1"))
    }

    @Test
    fun clear_removesAllEntries() {
        val cache = ArtCache()
        cache.put("a", "url-a")
        cache.put("b", "url-b")
        cache.put("c", "url-c")

        cache.clear()

        assertNull(cache.get("a"))
        assertNull(cache.get("b"))
        assertNull(cache.get("c"))
    }

    @Test
    fun clear_onEmptyCacheIsNoOp() {
        val cache = ArtCache()
        cache.clear()
        assertNull(cache.get("anything"))
    }
}
