package com.ponzischeme89.memby.update

import android.content.pm.PackageInstaller
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * The wording is the feature here: a failed self-update on a television has no logcat and
 * no support channel, so the sentence on screen is the whole diagnosis.
 */
class InstallStatusMessageTest {

    @Test
    fun `blocked install names where the permission lives`() {
        val message = installStatusMessage(PackageInstaller.STATUS_FAILURE_BLOCKED, null)
        assertTrue(message, message.contains("Unknown sources"))
    }

    @Test
    fun `conflicting signature tells the viewer to reinstall`() {
        val message = installStatusMessage(PackageInstaller.STATUS_FAILURE_CONFLICT, null)
        assertTrue(message, message.contains("Uninstall Memby"))
    }

    @Test
    fun `an aborted install invites another attempt rather than reading as broken`() {
        val message = installStatusMessage(PackageInstaller.STATUS_FAILURE_ABORTED, null)
        assertTrue(message, message.contains("try again", ignoreCase = true))
    }

    @Test
    fun `an unrecognised failure carries the system's own message`() {
        assertEquals(
            "The update could not be installed (INSTALL_FAILED_VERSION_DOWNGRADE).",
            installStatusMessage(PackageInstaller.STATUS_FAILURE, "INSTALL_FAILED_VERSION_DOWNGRADE"),
        )
    }

    @Test
    fun `an unrecognised failure with no message still says something`() {
        assertEquals(
            "The update could not be installed.",
            installStatusMessage(PackageInstaller.STATUS_FAILURE, "  "),
        )
    }

    @Test
    fun `success is reported, because the app may live long enough to show it`() {
        assertTrue(installStatusMessage(PackageInstaller.STATUS_SUCCESS, null).contains("installed"))
    }
}
