package eventDemo.app.command import eventDemo.app.command.command.GameCommand import eventDemo.app.command.command.IWantToJoinTheGameCommand import eventDemo.app.entity.GameId import eventDemo.app.entity.Player import eventDemo.app.eventListener.PlayerNotificationEventListener import eventDemo.app.eventListener.ReactionEventListener import eventDemo.app.notification.Notification import eventDemo.app.notification.WelcomeToTheGameNotification import eventDemo.configuration.appKoinModule import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.collections.shouldContain import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.launch import org.koin.dsl.koinApplication import kotlin.test.assertIs @OptIn(DelicateCoroutinesApi::class) class GameCommandHandlerTest : FunSpec({ test("handle a command should execute the command") { koinApplication { modules(appKoinModule) }.koin.apply { val commandHandler by inject() val notificationListener by inject() val gameId = GameId() val player = Player("Tesla") val channelCommand = Channel(Channel.BUFFERED) val channelNotification = Channel(Channel.BUFFERED) ReactionEventListener(get(), get(), get()).init() notificationListener.startListening(channelNotification, player) GlobalScope.launch { commandHandler.handle(player, channelCommand, channelNotification) } channelCommand.send(IWantToJoinTheGameCommand(IWantToJoinTheGameCommand.Payload(gameId, player))) assertIs(channelNotification.receive()).let { it.players shouldContain player } } } })