// 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.myicek.models
import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
class ModelSerializationTest {
private val json = Json { ignoreUnknownKeys = true }
@Test
fun testAccountDeserialization() {
val input = """{"name":"default","label":"Default"}"""
val account = json.decodeFromString(input)
assertEquals("default", account.name)
assertEquals("Default", account.label)
}
@Test
fun testEventDeserialization() {
val input = """
{
"id_event": 761040,
"agegroup": "U13 (Elite)",
"name": "U13 Elite - HC Ajoie",
"title": "U13 (Elite)\nGame\nRaiffeisen Arena",
"opponent": "HC Ajoie",
"place": "Raiffeisen Arena, Porrentruy",
"start": "2024-11-10T14:00:00",
"end": "2024-11-10T16:15:00",
"color": "#e4222e",
"event": "Jeu"
}
""".trimIndent()
val event = json.decodeFromString(input)
assertEquals("761040", event.idEvent)
assertEquals("U13 (Elite)", event.agegroup)
assertEquals("U13 Elite - HC Ajoie", event.name)
assertEquals("HC Ajoie", event.opponent)
assertEquals("#e4222e", event.color)
assertEquals("Jeu", event.eventType)
}
@Test
fun testEventDeserializationWithNullFields() {
val input = """
{
"id_event": "12345",
"agegroup": "U15",
"name": "",
"title": "Practice",
"opponent": "",
"place": "Les Vernets",
"start": "2024-11-10T14:00:00",
"end": "2024-11-10T15:30:00"
}
""".trimIndent()
val event = json.decodeFromString(input)
assertEquals("12345", event.idEvent)
assertEquals("", event.name)
assertEquals("", event.opponent)
assertNull(event.color)
assertEquals("", event.eventType)
}
@Test
fun testPlayerWithIntNumber() {
val input = """
{"position":"Gardien","number":1,"fname":"Jean","lname":"Dupont","dob":"2012-03-15"}
""".trimIndent()
val player = json.decodeFromString(input)
assertEquals("Gardien", player.position)
assertEquals("1", player.number)
assertEquals("Jean", player.fname)
}
@Test
fun testPlayerWithStringNumber() {
val input = """
{"position":"Attaquant","number":"27","fname":"Pierre","lname":"Martin","dob":"2012-05-20"}
""".trimIndent()
val player = json.decodeFromString(input)
assertEquals("27", player.number)
}
@Test
fun testPlayerWithNullNumber() {
val input = """
{"position":null,"number":null,"fname":"Paul","lname":"Durand","dob":"2012-01-10"}
""".trimIndent()
val player = json.decodeFromString(input)
assertNull(player.position)
assertNull(player.number)
}
@Test
fun testStaffDeserialization() {
val input = """
{"role":"Coach","fname":"Jean","lname":"Dupont"}
""".trimIndent()
val staff = json.decodeFromString(input)
assertEquals("Coach", staff.role)
assertEquals("Jean", staff.fname)
assertEquals("Dupont", staff.lname)
}
@Test
fun testConvocationDeserialization() {
val input = """
{
"available": [
{"position":"Gardien","number":1,"fname":"Jean","lname":"Dupont","dob":"2012-03-15"}
],
"staff": [
{"role":"Coach","fname":"Marc","lname":"Blanc"}
]
}
""".trimIndent()
val convocation = json.decodeFromString(input)
assertEquals(1, convocation.available.size)
assertEquals("1", convocation.available[0].number)
assertEquals(1, convocation.staff.size)
assertEquals("Coach", convocation.staff[0].role)
}
@Test
fun testConvocationWithEmptyArrays() {
val input = """{"available":[],"staff":[]}"""
val convocation = json.decodeFromString(input)
assertEquals(0, convocation.available.size)
assertEquals(0, convocation.staff.size)
}
@Test
fun testEventDetailDeserialization() {
val input = """
{
"title": "U13 (Elite) - Saison HC Ajoie",
"type": "games",
"place": "Raiffeisen Arena, Porrentruy",
"time_start": "2024-11-10T14:00:00",
"time_end": "2024-11-10T16:15:00",
"is_away": "1",
"convocation": {
"available": [],
"staff": []
}
}
""".trimIndent()
val detail = json.decodeFromString(input)
assertEquals("U13 (Elite) - Saison HC Ajoie", detail.title)
assertEquals("games", detail.type)
assertEquals("2024-11-10T14:00:00", detail.timeStart)
assertEquals(0, detail.convocation.available.size)
}
@Test
fun testUserInfoDeserialization() {
val input = """{"email":"rene@luria.ch","sub":"abc123"}"""
val userInfo = json.decodeFromString(input)
assertEquals("rene@luria.ch", userInfo.email)
}
@Test
fun testEventRoundTripSerialization() {
val event = Event(
idEvent = "761040",
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 = "Jeu",
)
val jsonString = json.encodeToString(Event.serializer(), event)
val decoded = json.decodeFromString(jsonString)
assertEquals(event, decoded)
}
}