Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit 281ad165 authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

test: cover install device preconditions

Pin the network and storage guard rails from the install refactor so user-facing failure signals keep their current behavior.
parent 0bf7b695
Loading
Loading
Loading
Loading
+118 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2026 e Foundation
 *
 * 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 <https://www.gnu.org/licenses/>.
 *
 */

package foundation.e.apps.installProcessor

import foundation.e.apps.R
import foundation.e.apps.data.event.AppEvent
import foundation.e.apps.data.install.AppManagerWrapper
import foundation.e.apps.data.install.models.AppInstall
import foundation.e.apps.data.install.notification.StorageNotificationManager
import foundation.e.apps.data.install.workmanager.AppInstallDevicePreconditions
import foundation.e.apps.data.install.wrapper.NetworkStatusChecker
import foundation.e.apps.data.install.wrapper.StorageSpaceChecker
import foundation.e.apps.domain.model.install.Status
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class AppInstallDevicePreconditionsTest {
    private lateinit var appManagerWrapper: AppManagerWrapper
    private lateinit var appEventDispatcher: FakeAppEventDispatcher
    private lateinit var storageNotificationManager: StorageNotificationManager
    private lateinit var storageSpaceChecker: StorageSpaceChecker
    private lateinit var networkStatusChecker: NetworkStatusChecker
    private lateinit var preconditions: AppInstallDevicePreconditions

    @Before
    fun setup() {
        appManagerWrapper = mockk(relaxed = true)
        appEventDispatcher = FakeAppEventDispatcher()
        storageNotificationManager = mockk(relaxed = true)
        storageSpaceChecker = mockk(relaxed = true)
        networkStatusChecker = mockk(relaxed = true)
        preconditions = AppInstallDevicePreconditions(
            appManagerWrapper,
            appEventDispatcher,
            storageNotificationManager,
            storageSpaceChecker,
            networkStatusChecker
        )
    }

    @Test
    fun canProceed_returnsFalseWhenNetworkUnavailable() = runTest {
        val appInstall = createInstall()
        every { networkStatusChecker.isNetworkAvailable() } returns false

        val result = preconditions.canProceed(appInstall)

        assertFalse(result)
        coVerify { appManagerWrapper.installationIssue(appInstall) }
        verify(exactly = 0) { storageSpaceChecker.spaceMissing(any()) }
        assertTrue(appEventDispatcher.events.any {
            it is AppEvent.NoInternetEvent && it.data == false
        })
    }

    @Test
    fun canProceed_returnsFalseWhenStorageIsMissing() = runTest {
        val appInstall = createInstall()
        every { networkStatusChecker.isNetworkAvailable() } returns true
        every { storageSpaceChecker.spaceMissing(appInstall) } returns 512L

        val result = preconditions.canProceed(appInstall)

        assertFalse(result)
        verify { storageNotificationManager.showNotEnoughSpaceNotification(appInstall) }
        coVerify { appManagerWrapper.installationIssue(appInstall) }
        assertTrue(appEventDispatcher.events.any {
            it is AppEvent.ErrorMessageEvent && it.data == R.string.not_enough_storage
        })
    }

    @Test
    fun canProceed_returnsTrueWhenNetworkAndStorageChecksPass() = runTest {
        val appInstall = createInstall()
        every { networkStatusChecker.isNetworkAvailable() } returns true
        every { storageSpaceChecker.spaceMissing(appInstall) } returns 0L

        val result = preconditions.canProceed(appInstall)

        assertTrue(result)
        coVerify(exactly = 0) { appManagerWrapper.installationIssue(any()) }
        verify(exactly = 0) { storageNotificationManager.showNotEnoughSpaceNotification(any()) }
        assertTrue(appEventDispatcher.events.isEmpty())
    }

    private fun createInstall() = AppInstall(
        id = "123",
        status = Status.AWAITING,
        name = "Example App",
        packageName = "com.example.app",
        appSize = 1024L
    )
}