Ejemplo n.º 1
0
    async def test_game(
        self,
        cog: EventsCog,
        message: discord.Message,
        add_user: Callable[..., User],
    ) -> None:
        player1 = add_user()
        player2 = add_user()
        users = [mock_discord_object(player1), mock_discord_object(player2)]
        with mock_operations(lfg_action, users=users):
            lfg_action.safe_followup_channel.return_value = message

            await self.run(
                cog.game,
                players=f"<@{player1.xid}><@{player2.xid}>",
                format=cast(int, GameFormat.LEGACY.value),
            )

        game = DatabaseSession.query(Game).one()
        assert game.status == GameStatus.STARTED.value
        admin = DatabaseSession.query(User).get(self.interaction.user.id)
        assert admin is not None and admin.game_id is None
        players = DatabaseSession.query(User).filter(User.xid != self.interaction.user.id).all()
        assert len(players) == 2
        for player in players:
            assert player.game_id == game.id
Ejemplo n.º 2
0
    async def test_watch_and_unwatch(self, cog: WatchCog,
                                     add_user: Callable[..., User]) -> None:
        target_user = add_user()
        target_member = cast(discord.Member, mock_discord_object(target_user))

        await self.run(cog.watch, target=target_member, note="note")
        self.interaction.response.send_message.assert_called_once_with(
            f"Watching <@{target_member.id}>.",
            ephemeral=True,
        )

        watch = DatabaseSession.query(Watch).one()
        assert watch.to_dict() == {
            "guild_xid": self.guild.xid,
            "user_xid": target_member.id,
            "note": "note",
        }

        self.interaction.response.send_message.reset_mock()
        await self.run(cog.unwatch, target=target_member)
        self.interaction.response.send_message.assert_called_once_with(
            f"No longer watching <@{target_member.id}>.",
            ephemeral=True,
        )

        watch = DatabaseSession.query(Watch).one_or_none()
        assert not watch
Ejemplo n.º 3
0
    async def test_game_with_banned_player(
        self,
        cog: EventsCog,
        add_user: Callable[..., User],
    ) -> None:
        player = add_user()
        banned = add_user(banned=True)
        users = [mock_discord_object(player), mock_discord_object(banned)]
        with mock_operations(lfg_action, users=users):
            await self.run(
                cog.game,
                players=f"<@{player.xid}><@{banned.xid}>",
                format=cast(int, GameFormat.LEGACY.value),
            )

            lfg_action.safe_followup_channel.assert_called_once_with(
                self.interaction,
                f"Some of the players you mentioned can not be added to a game: <@{banned.xid}>",
            )
Ejemplo n.º 4
0
    async def test_score_for_other_user(self, cog: ScoreCog, add_user: Callable[..., User]) -> None:
        target_user = add_user()
        target_member = cast(discord.Member, mock_discord_object(target_user))
        await self.run(cog.score, user=target_member)

        assert self.last_send_message("embed") == {
            "author": {"name": f"Record of games played on {self.guild.name}"},
            "color": self.settings.EMBED_COLOR,
            "description": f"<@{target_member.id}> has played 0 games on this server.\n"
            "View more [details on spellbot.io]"
            f"(https://bot.spellbot.io/g/{self.guild.xid}/u/{target_member.id}).",
            "thumbnail": {"url": self.settings.ICO_URL},
            "type": "rich",
        }
Ejemplo n.º 5
0
    async def test_game_with_one_player(
        self,
        cog: EventsCog,
        add_user: Callable[..., User],
    ) -> None:
        player = add_user()
        users = [mock_discord_object(player)]
        with mock_operations(lfg_action, users=users):
            await self.run(
                cog.game,
                players=f"<@{player.xid}>",
                format=cast(int, GameFormat.LEGACY.value),
            )

            lfg_action.safe_followup_channel.assert_called_once_with(
                self.interaction,
                "You can't create a Legacy game with 1 players.",
            )
Ejemplo n.º 6
0
 async def target(self, add_user: Callable[..., User]) -> discord.Member:
     return cast(discord.Member, mock_discord_object(add_user()))
Ejemplo n.º 7
0
    async def test_score(self, cog: ScoreCog, user: User, channel: Channel) -> None:
        await self.run(cog.score)

        assert self.last_send_message("embed") == {
            "author": {"name": f"Record of games played on {self.guild.name}"},
            "color": self.settings.EMBED_COLOR,
            "description": f"<@{user.xid}> has played 0 games on this server.\n"
            "View more [details on spellbot.io]"
            f"(https://bot.spellbot.io/g/{self.guild.xid}/u/{user.xid}).",
            "thumbnail": {"url": self.settings.ICO_URL},
            "type": "rich",
        }

        game = self.factories.game.create(
            seats=2,
            guild_xid=self.guild.xid,
            channel_xid=channel.xid,
        )
        self.factories.play.create(user_xid=user.xid, game_id=game.id)

        self.interaction.response.send_message.reset_mock()
        await self.run(cog.score)

        assert self.last_send_message("embed") == {
            "author": {"name": f"Record of games played on {self.guild.name}"},
            "color": self.settings.EMBED_COLOR,
            "description": f"<@{user.xid}> has played 1 game on this server.\n"
            "View more [details on spellbot.io]"
            f"(https://bot.spellbot.io/g/{self.guild.xid}/u/{user.xid}).",
            "thumbnail": {"url": self.settings.ICO_URL},
            "type": "rich",
        }

        game = self.factories.game.create(
            seats=2,
            guild_xid=self.guild.xid,
            channel_xid=channel.xid,
        )
        self.factories.play.create(user_xid=user.xid, game_id=game.id)

        self.interaction.response.send_message.reset_mock()
        await self.run(cog.score)
        assert self.last_send_message("embed") == {
            "author": {"name": f"Record of games played on {self.guild.name}"},
            "color": self.settings.EMBED_COLOR,
            "description": f"<@{user.xid}> has played 2 games on this server.\n"
            "View more [details on spellbot.io]"
            f"(https://bot.spellbot.io/g/{self.guild.xid}/u/{user.xid}).",
            "thumbnail": {"url": self.settings.ICO_URL},
            "type": "rich",
        }

        new_guild = build_guild(2)
        new_channel = build_channel(new_guild, 2)
        discord_user = mock_discord_object(user)
        new_interaction = build_interaction(new_guild, new_channel, discord_user)
        await self.run(cog.score, interaction=new_interaction)

        send_message = new_interaction.response.send_message
        send_message.assert_called_once()
        embed = send_message.call_args.kwargs.get("embed")
        assert embed is not None
        assert embed.to_dict() == {
            "author": {"name": f"Record of games played on {new_guild.name}"},
            "color": self.settings.EMBED_COLOR,
            "description": f"<@{user.xid}> has played 0 games on this server.\n"
            "View more [details on spellbot.io]"
            f"(https://bot.spellbot.io/g/{new_guild.id}/u/{user.xid}).",
            "thumbnail": {"url": self.settings.ICO_URL},
            "type": "rich",
        }
Ejemplo n.º 8
0
    async def test_lfg_fully_seated(
        self,
        cog: LookingForGameCog,
        add_channel: Callable[..., Channel],
    ):
        channel = add_channel(default_seats=2, xid=self.interaction.channel_id)
        game = self.factories.game.create(
            guild=self.guild,
            channel=channel,
            seats=2,
            message_xid=123,
        )
        other_user = self.factories.user.create(xid=self.interaction.user.id +
                                                1,
                                                game=game)
        other_player = mock_discord_object(other_user)

        with mock_operations(lfg_action, users=[other_player]):
            message = MagicMock(spec=discord.Message)
            message.id = game.message_xid
            lfg_action.safe_get_partial_message.return_value = message

            await self.run(cog.lfg)

            DatabaseSession.expire_all()
            game = DatabaseSession.query(Game).one()
            mock_call = lfg_action.safe_update_embed
            assert mock_call.call_args_list[0].kwargs["embed"].to_dict() == {
                "color":
                self.settings.EMBED_COLOR,
                "description":
                ("Please check your Direct Messages for your SpellTable link.\n\n"
                 f"{self.guild.motd}\n\n{channel.motd}"),
                "fields": [
                    {
                        "inline":
                        False,
                        "name":
                        "Players",
                        "value":
                        f"<@{self.interaction.user.id}>, <@{other_player.id}>",
                    },
                    {
                        "inline": True,
                        "name": "Format",
                        "value": "Commander"
                    },
                    {
                        "inline": True,
                        "name": "Started at",
                        "value": f"<t:{game.started_at_timestamp}>",
                    },
                ],
                "footer": {
                    "text": f"SpellBot Game ID: #SB{game.id}"
                },
                "thumbnail": {
                    "url": self.settings.THUMB_URL
                },
                "title":
                "**Your game is ready!**",
                "type":
                "rich",
            }