// SPDX-License-Identifier: GPL-3.0-or-later
// MyIce Kotlin Multiplatform — schedule/convocation viewer
// Copyright (C) 2026 parano.ch
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
package ch.parano.myice.cache
import ch.parano.myice.models.Event
import com.russhwolf.settings.MapSettings
import com.russhwolf.settings.Settings
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
class ScheduleCacheTest {
private fun createCache(): Pair {
val settings = MapSettings()
return ScheduleCache(settings) to settings
}
private fun sampleEvent(id: String, eventType: String = "Jeu") = Event(
idEvent = id,
agegroup = "U13 (Elite)",
name = "U13 Elite - HC Ajoie",
title = "U13 (Elite)\nGame\nArena",
opponent = "HC Ajoie",
place = "Arena",
start = "2024-11-10T14:00:00",
end = "2024-11-10T16:15:00",
color = "#e4222e",
eventType = eventType,
)
@Test
fun testSaveAndLoadEvents() {
val (cache, _) = createCache()
val events = listOf(sampleEvent("1"), sampleEvent("2"))
cache.saveEvents("default", events)
val loaded = cache.loadEvents("default")
assertNotNull(loaded)
assertEquals(2, loaded.size)
assertEquals("1", loaded[0].idEvent)
assertEquals("2", loaded[1].idEvent)
}
@Test
fun testLoadEventsReturnsNullWhenEmpty() {
val (cache, _) = createCache()
assertNull(cache.loadEvents("default"))
}
@Test
fun testGetLastUpdated() {
val (cache, _) = createCache()
assertNull(cache.getLastUpdated("default"))
cache.saveEvents("default", listOf(sampleEvent("1")))
val timestamp = cache.getLastUpdated("default")
assertNotNull(timestamp)
assertTrue(timestamp > 0)
}
@Test
fun testSaveAndLoadFilters() {
val (cache, _) = createCache()
cache.saveFilters("default", agegroup = "U13 (Elite)", subgroup = "U13 Elite")
val (agegroup, subgroup) = cache.loadFilters("default")
assertEquals("U13 (Elite)", agegroup)
assertEquals("U13 Elite", subgroup)
}
@Test
fun testSaveFiltersWithNulls() {
val (cache, _) = createCache()
cache.saveFilters("default", agegroup = "U13", subgroup = "Elite")
cache.saveFilters("default", agegroup = null, subgroup = null)
val (agegroup, subgroup) = cache.loadFilters("default")
assertNull(agegroup)
assertNull(subgroup)
}
@Test
fun testClearCache() {
val (cache, _) = createCache()
cache.saveEvents("default", listOf(sampleEvent("1")))
cache.saveFilters("default", "U13", "Elite")
cache.clearCache("default")
assertNull(cache.loadEvents("default"))
val (agegroup, subgroup) = cache.loadFilters("default")
assertNull(agegroup)
assertNull(subgroup)
}
@Test
fun testClearAllCache() {
val (cache, _) = createCache()
cache.saveEvents("default", listOf(sampleEvent("1")))
cache.saveEvents("leonard", listOf(sampleEvent("2")))
cache.clearAllCache()
assertNull(cache.loadEvents("default"))
assertNull(cache.loadEvents("leonard"))
}
@Test
fun testTypeFilterPersistence() {
val (cache, _) = createCache()
cache.saveTypeFilter("default", "GAMES")
assertEquals("GAMES", cache.loadTypeFilter("default"))
}
@Test
fun testTypeFilterNullWhenNotSet() {
val (cache, _) = createCache()
assertNull(cache.loadTypeFilter("default"))
}
}