package com.ponzischeme89.memby.ui.settings

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

class VersionHistoryTest {

    @Test
    fun `parses versions dates and bullets`() {
        val releases = parseChangelog(
            """
            # Changelog

            Prose before the first release is format documentation, not history.

            ## 0.2.16 — 2026-08-03
            - Signed-in devices can be renamed.
            - MDBList ratings.

            ## 0.1.53
            - Renamed to Memby.
            """.trimIndent(),
        )

        assertEquals(listOf("0.2.16", "0.1.53"), releases.map { it.version })
        assertEquals("2026-08-03", releases[0].date)
        assertEquals(2, releases[0].changes.size)
        assertEquals("MDBList ratings.", releases[0].changes[1])
        // A release with no date renders without one rather than showing a placeholder.
        assertEquals("", releases[1].date)
    }

    @Test
    fun `joins a bullet wrapped across lines`() {
        val releases = parseChangelog(
            """
            ## 1.0.0 - 2026-01-01
            - A change long enough that the file
              wraps it onto a second line.
            - A second change.
            """.trimIndent(),
        )

        assertEquals(
            listOf(
                "A change long enough that the file wraps it onto a second line.",
                "A second change.",
            ),
            releases.single().changes,
        )
    }

    @Test
    fun `ignores text that is not a release`() {
        assertTrue(parseChangelog("# Changelog\n\nNothing released yet.\n").isEmpty())
    }

    @Test
    fun `the shipped changelog is readable and newest first`() {
        val releases = MembyReleaseHistory
        assertTrue("CHANGELOG.md parsed to no releases", releases.size >= 2)
        assertTrue(releases.all { it.changes.isNotEmpty() })
        assertEquals(
            "the About page shows the file's order verbatim",
            releases.map { it.version }.sortedByDescending { version ->
                version.split('.').fold(0) { acc, part -> acc * 1_000 + part.toInt() }
            },
            releases.map { it.version },
        )
    }
}
