package com.flagship.abox.manager.testing.fakes

import com.flagship.abox.manager.domain.DeviceDescriptor
import com.flagship.abox.manager.domain.DeviceId
import com.flagship.abox.manager.domain.DeviceName
import com.flagship.abox.manager.domain.DeviceType
import com.flagship.abox.manager.domain.DomainResult
import com.flagship.abox.manager.domain.ProviderProfileId
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Test

class FakeDeviceCatalogTest {
    @Test
    fun defaultDevicesAreIsolatedByProviderProfile() = runTest {
        val gateway = FakeDeviceCatalog()
        val aboxProfile = ProviderProfileId("abox")
        val localProfile = ProviderProfileId("local")

        val aboxDevices = (gateway.observeDevices(aboxProfile).first() as DomainResult.Success).value
        val localDevices = (gateway.observeDevices(localProfile).first() as DomainResult.Success).value

        assertEquals(3, aboxDevices.size)
        assertEquals(3, localDevices.size)
        assertNotEquals(aboxDevices.first().id, localDevices.first().id)
        assertEquals(aboxProfile, aboxDevices.first().profileId)
        assertEquals(localProfile, localDevices.first().profileId)
    }

    @Test(expected = IllegalArgumentException::class)
    fun setDevicesRejectsAnotherProvidersData() {
        val gateway = FakeDeviceCatalog()
        gateway.setDevices(
            ProviderProfileId("one"),
            listOf(
                DeviceDescriptor(
                    id = DeviceId("two:socket"),
                    profileId = ProviderProfileId("two"),
                    name = DeviceName("socket"),
                    type = DeviceType.SOCKET,
                ),
            ),
        )
    }
}
