package eventDemo.contexts.game.application.notification import eventDemo.contexts.game.application.notification.models.ItsTheTurnOfNotification import eventDemo.contexts.game.application.notification.models.PilesShuffledNotification import eventDemo.contexts.game.application.notification.models.PlayerAsPlayACardNotification import eventDemo.contexts.game.application.notification.models.PlayerHavePassNotification import eventDemo.contexts.game.application.notification.models.PlayerWasReadyNotification import eventDemo.contexts.game.application.notification.models.TheGameWasStartedNotification import eventDemo.contexts.game.application.notification.models.WelcomeToTheGameNotification import eventDemo.contexts.game.application.notification.models.YourNewCardNotification import eventDemo.contexts.game.domain.events.CardIsPlayedEvent import eventDemo.contexts.game.domain.events.DrawFilledWithDiscardEvent import eventDemo.contexts.game.domain.events.GameStartedEvent import eventDemo.contexts.game.domain.events.NewPlayerEvent import eventDemo.contexts.game.domain.events.PlayerHaveDrawCardEvent import eventDemo.contexts.game.domain.events.PlayerReadyEvent import eventDemo.contexts.game.domain.game.Card import eventDemo.contexts.game.domain.game.DiscardPile import eventDemo.contexts.game.domain.game.DrawPile import eventDemo.contexts.game.domain.game.GameId import eventDemo.contexts.game.domain.game.Player import eventDemo.contexts.game.domain.game.PlayerHand import eventDemo.contexts.game.domain.game.PlayerList import eventDemo.contexts.game.domain.game.gameState.GameCreated import eventDemo.contexts.game.domain.game.gameState.GameStarted import eventDemo.sharedKernel.UserId import io.kotest.core.spec.style.FunSpec import io.kotest.matchers.shouldBe import org.junit.jupiter.api.assertInstanceOf class ToNotificationTest : FunSpec({ val player1 = Player( name = "Bob", userId = UserId(), hand = PlayerHand(setOf(Card.NumericCard(1, Card.Color.Red))), id = Player.PlayerId(), ) val player2 = Player( name = "John", userId = UserId(), hand = PlayerHand(setOf(Card.NumericCard(1, Card.Color.Red))), id = Player.PlayerId(), ) test("NewPlayerEvent") { val game = GameCreated( aggregateId = GameId(), version = 1, players = PlayerList(setOf(player1)), recordedEvents = setOf(), ) NewPlayerEvent( game.aggregateId, version = 2, player = player1, ).toNotification( game = game, currentUserId = player1.userId, ).let { it.toList().size shouldBe 1 // Check if the user is assertInstanceOf(it.first()).run { players.size shouldBe 1 players.first().name shouldBe "Bob" } } } test("PlayerReadyEvent") { val game = GameCreated( aggregateId = GameId(), version = 1, players = PlayerList(setOf(player1)), recordedEvents = setOf(), ) PlayerReadyEvent( game.aggregateId, version = 2, playerId = player1.id, ).toNotification( game = game, currentUserId = player1.userId, ).let { it.toList().size shouldBe 1 assertInstanceOf(it.first()).let { it.playerId shouldBe player1.id } } } test("PlayerHaveDrawCardEvent on current player") { val game = GameStarted( aggregateId = GameId(), players = PlayerList(setOf(player1, player2)), drawPile = DrawPile(), discardPile = DiscardPile(), lastPlayerId = player1.id, nextPlayerId = player2.id, currentColor = Card.Color.Red, version = 1, recordedEvents = setOf(), ) val card = Card.NumericCard(1, Card.Color.Blue) PlayerHaveDrawCardEvent( game.aggregateId, version = 2, playerId = player1.id, takenCards = setOf(card), ).toNotification( game = game, currentUserId = player1.userId, ).let { it.toList().size shouldBe 2 it.toList().let { notifications -> assertInstanceOf(notifications.first()).let { it.cards.first() shouldBe card } assertInstanceOf(notifications[1]).let { it.player.id shouldBe player2.id } } } } test("PlayerHaveDrawCardEvent on other player") { val game = GameStarted( aggregateId = GameId(), players = PlayerList(setOf(player1, player2)), drawPile = DrawPile(), discardPile = DiscardPile(), lastPlayerId = player1.id, nextPlayerId = player2.id, currentColor = Card.Color.Red, version = 1, recordedEvents = setOf(), ) val card = Card.NumericCard(1, Card.Color.Blue) PlayerHaveDrawCardEvent( game.aggregateId, version = 2, playerId = player1.id, takenCards = setOf(card), ).toNotification( game = game, currentUserId = player2.userId, ).let { it.toList().size shouldBe 2 it.toList().let { notifications -> assertInstanceOf(notifications.first()).let { it.playerId shouldBe player1.id } assertInstanceOf(notifications[1]).let { it.player.id shouldBe player2.id } } } } test("CardIsPlayedEvent on current player") { val game = GameStarted( aggregateId = GameId(), players = PlayerList(setOf(player1, player2)), drawPile = DrawPile(), discardPile = DiscardPile(), lastPlayerId = player2.id, nextPlayerId = player1.id, currentColor = Card.Color.Red, version = 1, recordedEvents = setOf(), ) val card = Card.NumericCard(1, Card.Color.Blue) CardIsPlayedEvent( game.aggregateId, version = 2, playerId = player1.id, card = card, ).toNotification( game = game, currentUserId = player1.userId, ).toList() .let { notifications -> notifications.size shouldBe 2 assertInstanceOf(notifications.first()).let { it.playerId shouldBe player1.id it.card shouldBe card } assertInstanceOf(notifications[1]).let { it.player.id shouldBe player1.id } } } test("CardIsPlayedEvent on other player") { val game = GameStarted( aggregateId = GameId(), players = PlayerList(setOf(player1, player2)), drawPile = DrawPile(), discardPile = DiscardPile(), lastPlayerId = player1.id, nextPlayerId = player2.id, currentColor = Card.Color.Red, version = 1, recordedEvents = setOf(), ) val card = Card.NumericCard(1, Card.Color.Blue) CardIsPlayedEvent( game.aggregateId, version = 2, playerId = player2.id, card = card, ).toNotification( game = game, currentUserId = player1.userId, ).let { it.toList().size shouldBe 2 it.toList().let { notifications -> assertInstanceOf(notifications.first()).let { it.playerId shouldBe player2.id it.card shouldBe card } assertInstanceOf(notifications[1]).let { it.player.id shouldBe player2.id } } } } test("DrawFilledWithDiscardEvent") { val game = GameStarted( aggregateId = GameId(), players = PlayerList(setOf(player1, player2)), drawPile = DrawPile(), discardPile = DiscardPile(), lastPlayerId = player1.id, nextPlayerId = player2.id, currentColor = Card.Color.Red, version = 1, recordedEvents = setOf(), ) DrawFilledWithDiscardEvent( game.aggregateId, version = 2, newDrawPile = DrawPile(), newDiscardPile = DiscardPile(), ).toNotification( game = game, currentUserId = player1.userId, ).let { it.toList().size shouldBe 1 assertInstanceOf(it.first()) } } test("GameStartedEvent") { val game = GameStarted( aggregateId = GameId(), players = PlayerList(setOf(player1, player2)), drawPile = DrawPile(), discardPile = DiscardPile(), lastPlayerId = player1.id, nextPlayerId = player2.id, currentColor = Card.Color.Red, version = 1, recordedEvents = setOf(), ) GameStartedEvent( game.aggregateId, version = 2, firstPlayer = player1.id, playersHans = mapOf( player1.id to player1.hand, player2.id to player2.hand, ), drawPile = DrawPile(), discardPile = DiscardPile(), ).toNotification( game = game, currentUserId = player1.userId, ).toList() .let { notifications -> notifications.size shouldBe 2 assertInstanceOf(notifications.first()).let { it.hand.size shouldBe 1 it.hand.first() shouldBe player1.hand.cards.first() } assertInstanceOf(notifications[1]).let { it.player.id shouldBe player2.id } } } })