Ejemplo n.º 1
0
 public static ICommandContext CreateCommandContext(
     MessageStore messageStore,
     HashSet <ulong> existingUserIds,
     ulong guildId,
     ulong messageChannelId,
     ulong userId,
     Action <Mock <IGuild>, IGuildTextChannel> updateMockGuild = null)
 {
     return(CreateCommandContext(
                messageStore,
                existingUserIds,
                guildId,
                messageChannelId,
                userId: userId,
                updateMockGuild: updateMockGuild,
                out _));
 }
Ejemplo n.º 2
0
        private static IGuildTextChannel CreateGuildTextChannel(
            MessageStore messageStore,
            IGuild guild,
            ulong messageChannelId,
            Action <Mock <IGuildTextChannel> > updateMock = null)
        {
            Mock <IGuildTextChannel> mockMessageChannel =
                CreateMockGuildTextChannel(messageStore, messageChannelId, updateMock);

            mockMessageChannel
            .Setup(channel => channel.Guild)
            .Returns(guild);

            // Okay to invoke twice?
            updateMock?.Invoke(mockMessageChannel);
            return(mockMessageChannel.Object);
        }
        private static void CreateHandler(
            HashSet <ulong> existingUserIds,
            ulong channelId,
            ulong userId,
            out BotCommandHandler handler,
            out GameState currentGame,
            out MessageStore messageStore)
        {
            GameStateManager manager = new GameStateManager();

            manager.TryCreate(DefaultChannelId, out currentGame);
            messageStore = new MessageStore();
            ICommandContext commandContext = CreateCommandContext(
                messageStore, existingUserIds, channelId, userId);

            handler = new BotCommandHandler(commandContext, manager, currentGame, Mock.Of <ILogger>());
        }
        public async Task ClearAllRemovesGame()
        {
            GameStateManager manager = new GameStateManager();

            manager.TryCreate(DefaultChannelId, out GameState currentGame);
            MessageStore    messageStore   = new MessageStore();
            ICommandContext commandContext = CreateCommandContext(
                messageStore, DefaultIds, DefaultChannelId, DefaultReaderId);
            BotCommandHandler handler = new BotCommandHandler(commandContext, manager, currentGame, Mock.Of <ILogger>());

            await handler.ClearAll();

            Assert.IsFalse(
                manager.TryGet(DefaultChannelId, out GameState game),
                "Game should have been removed from the manager.");
            Assert.AreEqual(1, messageStore.ChannelMessages.Count, "Unexpected number of messages sent.");
        }
Ejemplo n.º 5
0
        private static Mock <IGuildTextChannel> CreateMockGuildTextChannel(
            MessageStore messageStore,
            ulong messageChannelId,
            Action <Mock <IGuildTextChannel> > updateMock = null)
        {
            Mock <IGuildTextChannel> mockMessageChannel = new Mock <IGuildTextChannel>();
            Mock <IUserMessage>      mockUserMessage    = new Mock <IUserMessage>();

            mockMessageChannel
            .Setup(channel => channel.Id)
            .Returns(messageChannelId);
            mockMessageChannel
            .Setup(channel => channel.SendMessageAsync(It.IsAny <string>(), false, null, It.IsAny <RequestOptions>()))
            .Returns <string, bool, Embed, RequestOptions>((message, isTTS, embed, options) =>
            {
                messageStore.ChannelMessages.Add(message);
                return(Task.FromResult(mockUserMessage.Object));
            });
            mockMessageChannel
            .Setup(channel => channel.SendMessageAsync(null, false, It.IsAny <Embed>(), It.IsAny <RequestOptions>()))
            .Returns <string, bool, Embed, RequestOptions>((message, isTTS, embed, options) =>
            {
                messageStore.ChannelEmbeds.Add(GetMockEmbedText(embed));
                return(Task.FromResult(mockUserMessage.Object));
            });
            mockMessageChannel
            .Setup(channel => channel.Name)
            .Returns("gameChannel");
            mockMessageChannel
            .Setup(channel => channel.SendFileAsync(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(), It.IsAny <Embed>(), It.IsAny <RequestOptions>(), It.IsAny <bool>()))
            .Returns <Stream, string, string, bool, Embed, RequestOptions, bool>(
                (stream, filename, text, isTTS, embed, requestOptions, isSpoiler) =>
            {
                messageStore.Files.Add((stream, filename, text));
                return(Task.FromResult(mockUserMessage.Object));
            });

            updateMock?.Invoke(mockMessageChannel);
            return(mockMessageChannel);
        }
Ejemplo n.º 6
0
        public async Task ClearAllRemovesGame()
        {
            GameStateManager manager = new GameStateManager();

            manager.TryCreate(DefaultChannelId, out GameState currentGame);
            MessageStore    messageStore   = new MessageStore();
            ICommandContext commandContext = CommandMocks.CreateCommandContext(
                messageStore, DefaultIds, DefaultGuildId, DefaultChannelId, DefaultReaderId);
            IDatabaseActionFactory dbActionFactory = CommandMocks.CreateDatabaseActionFactory(
                this.botConfigurationfactory);
            IOptionsMonitor <BotConfiguration> options             = CommandMocks.CreateConfigurationOptionsMonitor();
            IFileScoresheetGenerator           scoresheetGenerator = (new Mock <IFileScoresheetGenerator>()).Object;

            ReaderCommandHandler handler = new ReaderCommandHandler(
                commandContext, manager, options, dbActionFactory, scoresheetGenerator);

            await handler.ClearAllAsync();

            Assert.IsFalse(
                manager.TryGet(DefaultChannelId, out _),
                "Game should have been removed from the manager.");
            Assert.AreEqual(1, messageStore.ChannelMessages.Count, "Unexpected number of messages sent.");
        }
        private void CreateHandler(
            ulong voiceChannelId,
            string voiceChannelName,
            out AdminCommandHandler handler,
            out MessageStore messageStore,
            out IGuildTextChannel guildTextChannel)
        {
            messageStore = new MessageStore();
            ICommandContext commandContext = CommandMocks.CreateCommandContext(
                messageStore,
                DefaultIds,
                DefaultGuildId,
                DefaultChannelId,
                DefaultReaderId,
                (mockGuild, textChannel) =>
            {
                Mock <IVoiceChannel> mockVoiceChannel = new Mock <IVoiceChannel>();
                mockVoiceChannel.Setup(voiceChannel => voiceChannel.Id).Returns(voiceChannelId);
                mockVoiceChannel.Setup(voiceChannel => voiceChannel.Name).Returns(voiceChannelName);
                mockGuild
                .Setup(guild => guild.GetVoiceChannelAsync(It.IsAny <ulong>(), It.IsAny <CacheMode>(), It.IsAny <RequestOptions>()))
                .Returns(Task.FromResult(mockVoiceChannel.Object));

                List <IVoiceChannel> voiceChannels = new List <IVoiceChannel>()
                {
                    mockVoiceChannel.Object
                };
                mockGuild
                .Setup(guild => guild.GetVoiceChannelsAsync(It.IsAny <CacheMode>(), It.IsAny <RequestOptions>()))
                .Returns(Task.FromResult <IReadOnlyCollection <IVoiceChannel> >(voiceChannels));
            },
                out guildTextChannel);
            IDatabaseActionFactory dbActionFactory = CommandMocks.CreateDatabaseActionFactory(
                this.botConfigurationfactory);

            handler = new AdminCommandHandler(commandContext, dbActionFactory);
        }
Ejemplo n.º 8
0
 private void CreateHandler(
     out ReaderCommandHandler handler, out GameState game, out MessageStore messageStore)
 {
     this.CreateHandler(DefaultIds, out handler, out game, out messageStore);
 }