コード例 #1
0
        public async Task NoMessageWhenNonPromptedPlayerWithdraws()
        {
            this.CreateHandler(
                out MessageHandler handler,
                out GameState state,
                out IGuildUser firstPlayerUser,
                out _,
                out IGuildTextChannel channel,
                out MessageStore messageStore);
            IGuildUser secondPlayerUser = CommandMocks.CreateGuildUser(DefaultSecondPlayerId);

            await handler.HandlePlayerMessage(state, firstPlayerUser, channel, "buzz");

            await handler.HandlePlayerMessage(state, secondPlayerUser, channel, "buzz");

            messageStore.VerifyChannelMessages(firstPlayerUser.Mention);
            messageStore.Clear();

            await handler.HandlePlayerMessage(state, secondPlayerUser, channel, "wd");

            messageStore.VerifyChannelMessages();

            Assert.IsTrue(state.TryGetNextPlayer(out ulong nextPlayerId), "Couldn't get another player");
            Assert.AreEqual(DefaultPlayerId, nextPlayerId, "Unexpected user prompted");

            state.ScorePlayer(0);
            Assert.IsFalse(state.TryGetNextPlayer(out _), "Second player should've been withdrawn");
        }
コード例 #2
0
        public async Task ExportToFileWhenGeneratorFails()
        {
            const string errorMessage = "Error!";
            IOptionsMonitor <BotConfiguration> options = CommandMocks.CreateConfigurationOptionsMonitor();
            Mock <IFileScoresheetGenerator>    mockScoresheetGenerator = new Mock <IFileScoresheetGenerator>();

            Task <IResult <Stream> > result = Task.FromResult <IResult <Stream> >(new FailureResult <Stream>(errorMessage));

            mockScoresheetGenerator
            .Setup(generator => generator.TryCreateScoresheet(It.IsAny <GameState>(), It.IsAny <string>(), It.IsAny <string>()))
            .Returns(result);

            this.CreateHandler(
                options,
                mockScoresheetGenerator.Object,
                out ReaderCommandHandler handler,
                out GameState currentGame,
                out MessageStore messageStore);
            await handler.ExportToFileAsync();

            string readerName = $"User_{DefaultReaderId}";

            mockScoresheetGenerator
            .Verify(generator => generator.TryCreateScoresheet(currentGame, readerName, It.IsAny <string>()),
                    Times.Once());
            messageStore.VerifyChannelMessages($"Export failed. Error: {errorMessage}");
        }
コード例 #3
0
        private void CreateHandler(
            IOptionsMonitor <BotConfiguration> options,
            IFileScoresheetGenerator scoresheetGenerator,
            out ReaderCommandHandler handler,
            out GameState game,
            out MessageStore messageStore)
        {
            messageStore = new MessageStore();
            ICommandContext commandContext = CommandMocks.CreateCommandContext(
                messageStore,
                DefaultIds,
                DefaultGuildId,
                DefaultChannelId,
                userId: DefaultReaderId,
                updateMockGuild: UpdateMockGuild,
                out _);
            GameStateManager manager = new GameStateManager();

            manager.TryCreate(DefaultChannelId, out game);
            game.TeamManager = new ByCommandTeamManager();
            IDatabaseActionFactory dbActionFactory = CommandMocks.CreateDatabaseActionFactory(
                this.botConfigurationfactory);

            handler = new ReaderCommandHandler(commandContext, manager, options, dbActionFactory, scoresheetGenerator);
        }
コード例 #4
0
        private void InitializeHandler(
            ulong voiceChannelId    = 9999,
            string voiceChannelName = "Voice",
            IGoogleSheetsGeneratorFactory googleSheetsGeneratorFactory = null)
        {
            this.MessageStore = new MessageStore();
            ICommandContext commandContext = CommandMocks.CreateCommandContext(
                this.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));
                mockGuild
                .Setup(guild => guild.Roles)
                .Returns(DefaultRoles.Select((role, index) =>
                {
                    Mock <IRole> mockRole = new Mock <IRole>();
                    mockRole
                    .Setup(r => r.Name)
                    .Returns(role);
                    mockRole
                    .Setup(r => r.Id)
                    .Returns((ulong)index);
                    return(mockRole.Object);
                }).ToArray());
            },
                out IGuildTextChannel guildTextChannel);

            this.GuildTextChannel = guildTextChannel;
            IOptionsMonitor <BotConfiguration> options         = CommandMocks.CreateConfigurationOptionsMonitor();
            IDatabaseActionFactory             dbActionFactory = CommandMocks.CreateDatabaseActionFactory(
                this.botConfigurationfactory);

            this.GoogleSheetsGeneratorFactory = googleSheetsGeneratorFactory ?? (new Mock <IGoogleSheetsGeneratorFactory>()).Object;

            this.Handler = new AdminCommandHandler(commandContext, dbActionFactory, this.GoogleSheetsGeneratorFactory);
        }
        private void CreateHandler(out BotOwnerCommandHandler handler, out MessageStore messageStore)
        {
            messageStore = new MessageStore();
            ICommandContext commandContext = CommandMocks.CreateCommandContext(
                messageStore,
                DefaultIds,
                DefaultGuildId,
                DefaultChannelId,
                DefaultReaderId);
            IDatabaseActionFactory dbActionFactory = CommandMocks.CreateDatabaseActionFactory(
                this.botConfigurationfactory);
            IOptionsMonitor <BotConfiguration> options = CommandMocks.CreateConfigurationOptionsMonitor();

            handler = new BotOwnerCommandHandler(commandContext, options, dbActionFactory);
        }
コード例 #6
0
        private void CreateHandler(
            out MessageHandler handler,
            out GameState state,
            out IGuildUser playerUser,
            out IGuildUser readerUser,
            out IGuildTextChannel channel,
            out MessageStore messageStore)
        {
            messageStore = new MessageStore();
            IDatabaseActionFactory dbActionFactory = CommandMocks.CreateDatabaseActionFactory(
                this.botConfigurationfactory);
            IOptionsMonitor <BotConfiguration> options = CommandMocks.CreateConfigurationOptionsMonitor();

            handler = new MessageHandler(
                options, dbActionFactory, CommandMocks.CreateHubContext(), new Mock <ILogger>().Object);

            playerUser = CommandMocks.CreateGuildUser(DefaultPlayerId);
            readerUser = CommandMocks.CreateGuildUser(DefaultReaderId);
            CommandMocks.CreateGuild(
                messageStore,
                DefaultIds,
                DefaultGuildId,
                DefaultChannelId,
                (mockGuild, textChannel) =>
            {
                Mock <IVoiceChannel> mockVoiceChannel = new Mock <IVoiceChannel>();
                mockVoiceChannel.Setup(voiceChannel => voiceChannel.Id).Returns(DefaultVoiceChannelId);
                mockVoiceChannel.Setup(voiceChannel => voiceChannel.Name).Returns("Voice");
                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));
            },
                null,
                out channel);
            state = new GameState()
            {
                ReaderId    = DefaultReaderId,
                TeamManager = new ByCommandTeamManager()
            };
        }
コード例 #7
0
        public async Task ExportToFileGuildLimit()
        {
            const int guildLimit = 2;
            IOptionsMonitor <BotConfiguration> options = CommandMocks.CreateConfigurationOptionsMonitor();

            options.CurrentValue.DailyGuildExportLimit = guildLimit;
            Mock <IFileScoresheetGenerator> mockScoresheetGenerator = new Mock <IFileScoresheetGenerator>();

            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("scoresheet")))
            {
                Task <IResult <Stream> > result = Task.FromResult <IResult <Stream> >(new SuccessResult <Stream>(stream));
                mockScoresheetGenerator
                .Setup(generator => generator.TryCreateScoresheet(It.IsAny <GameState>(), It.IsAny <string>(), It.IsAny <string>()))
                .Returns(result);

                this.CreateHandler(
                    options,
                    mockScoresheetGenerator.Object,
                    out ReaderCommandHandler handler,
                    out GameState currentGame,
                    out MessageStore messageStore);

                for (int i = 0; i < guildLimit; i++)
                {
                    await handler.ExportToFileAsync();
                }

                messageStore.VerifyChannelMessages();
                messageStore.Clear();

                await handler.ExportToFileAsync();

                string readerName = $"User_{DefaultReaderId}";
                Assert.AreEqual(1, messageStore.ChannelMessages.Count, "Unexpected number of messages");
                string message = messageStore.ChannelMessages.First();
                Assert.IsTrue(
                    message.Contains("The server has already exceeded", StringComparison.InvariantCultureIgnoreCase),
                    $"Couldn't find information on the user limit in the message '{message}'");
                Assert.AreEqual(0, messageStore.Files.Count, "No files should've been attached");
            }
        }
コード例 #8
0
        public async Task ExportToFileSucceeds()
        {
            const string streamText = "scoresheet";
            IOptionsMonitor <BotConfiguration> options = CommandMocks.CreateConfigurationOptionsMonitor();
            Mock <IFileScoresheetGenerator>    mockScoresheetGenerator = new Mock <IFileScoresheetGenerator>();

            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(streamText)))
            {
                Task <IResult <Stream> > result = Task.FromResult <IResult <Stream> >(new SuccessResult <Stream>(stream));
                mockScoresheetGenerator
                .Setup(generator => generator.TryCreateScoresheet(It.IsAny <GameState>(), It.IsAny <string>(), It.IsAny <string>()))
                .Returns(result);

                this.CreateHandler(
                    options,
                    mockScoresheetGenerator.Object,
                    out ReaderCommandHandler handler,
                    out GameState currentGame,
                    out MessageStore messageStore);
                await handler.ExportToFileAsync();

                string readerName = $"User_{DefaultReaderId}";
                mockScoresheetGenerator
                .Verify(generator => generator.TryCreateScoresheet(currentGame, readerName, It.IsAny <string>()),
                        Times.Once());
                messageStore.VerifyChannelMessages();
                Assert.AreEqual(1, messageStore.Files.Count, "Unexpected number of file attachments");
                (Stream resultStream, string filename, string text) = messageStore.Files.First();
                Assert.AreEqual($"Scoresheet_{readerName}_1.xlsx", filename, "Unexpected filename");

                resultStream.Position = 0;
                Assert.AreEqual(streamText.Length, resultStream.Length, "Unexpected stream length");
                byte[] resultBytes = new byte[streamText.Length];
                resultStream.Read(resultBytes, 0, resultBytes.Length);
                string resultString = Encoding.UTF8.GetString(resultBytes);
                Assert.AreEqual(streamText, resultString, "Unexpected result from the stream");
            }
        }
コード例 #9
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.");
        }
コード例 #10
0
        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);
        }