package xyz.kusoft.argusupdater

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

class ArgusSemanticVersionTest {
    @Test
    fun compareOrdersSemanticVersions() {
        assertTrue(ArgusSemanticVersion.compare("2.25", "2.24") > 0)
        assertEquals(0, ArgusSemanticVersion.compare("2.24", "2.24.0"))
        assertTrue(ArgusSemanticVersion.compare("v2.0.0", "1.9.9") > 0)
    }

    @Test
    fun compareHandlesPreReleaseAndBuildMetadataLikeArgusServer() {
        assertTrue(ArgusSemanticVersion.compare("2.0.0-beta.2", "2.0.0-beta.1") > 0)
        assertTrue(ArgusSemanticVersion.compare("2.0.0", "2.0.0-beta.1") > 0)
        assertEquals(0, ArgusSemanticVersion.compare("2.24+35", "2.24"))
    }

    @Test
    fun releaseResolvesVersionCodeFromAndroidFieldVersionAndFileName() {
        assertEquals(40L, release(androidVersionCode = 40L).resolvedVersionCode)
        assertEquals(41L, release(version = "2.25+41").resolvedVersionCode)
        assertEquals(42L, release(originalFileName = "aletheia-2.25-42.apk").resolvedVersionCode)
        assertNull(release(originalFileName = "aletheia-2.25.apk").resolvedVersionCode)
    }

    @Test
    fun updatePolicyFallsBackToVersionNameWhenArgusHasNoVersionCode() {
        val installedApp = ArgusInstalledApp(
            packageName = "com.aletheia.app",
            versionCode = 35L,
            versionName = "2.24"
        )

        assertTrue(ArgusUpdateVersionPolicy.compareReleaseToInstalled(release(version = "2.25"), installedApp)!! > 0)
        assertEquals(0, ArgusUpdateVersionPolicy.compareReleaseToInstalled(release(version = "2.24"), installedApp))
        assertTrue(ArgusUpdateVersionPolicy.compareReleaseToInstalled(release(version = "2.23"), installedApp)!! < 0)
    }

    @Test
    fun updatePolicyComparesReleaseVersionNotLegacyAndroidVersionCode() {
        val installedApp = ArgusInstalledApp(
            packageName = "com.aletheia.app",
            versionCode = 35L,
            versionName = "2.24"
        )

        assertEquals(
            0,
            ArgusUpdateVersionPolicy.compareReleaseToInstalled(
                release(version = "2.24", androidVersionCode = 99L),
                installedApp
            )
        )
    }

    private fun release(
        version: String = "2.24",
        androidVersionCode: Long? = null,
        originalFileName: String = "aletheia-2.24.apk"
    ): ArgusRelease =
        ArgusRelease(
            id = "release-id",
            version = version,
            channel = "stable",
            platform = "android",
            packageKind = "apk",
            androidVersionCode = androidVersionCode,
            downloadPath = "/api/apps/aletheia-kotlin/releases/release-id/download",
            originalFileName = originalFileName,
            contentType = "application/vnd.android.package-archive",
            packageSizeBytes = 123L,
            sha256 = "0".repeat(64),
            publishedAt = "2026-06-13T16:13:29+00:00",
            notes = null
        )
}
