diff --git a/src/Codebreaker.ViewModels.Tests/Codebreaker.ViewModels.Tests.csproj b/src/Codebreaker.ViewModels.Tests/Codebreaker.ViewModels.Tests.csproj index abfcec5..7b01af9 100644 --- a/src/Codebreaker.ViewModels.Tests/Codebreaker.ViewModels.Tests.csproj +++ b/src/Codebreaker.ViewModels.Tests/Codebreaker.ViewModels.Tests.csproj @@ -1,22 +1,21 @@ - net7.0 + net8.0 enable enable - false - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs index c65d98a..b86373e 100644 --- a/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs +++ b/src/Codebreaker.ViewModels.Tests/GamePageViewModelTests.cs @@ -6,55 +6,94 @@ namespace CodeBreaker.ViewModels.Tests; public class GamePageViewModelTests { - private readonly GamePageViewModel _viewModel; + private static readonly string[] s_code = ["Green", "Blue", "Yellow", "Orange"]; + private static readonly (string[] GuessPegs, string[] KeyPegs, bool HasEnded, bool IsVictory)[] s_moves = [ + (["Red", "Red", "Red", "Red"], [], false, false), + (["Green", "Green", "Green", "Green"], ["Black"], false, false), + (["Blue", "Green", "Green", "Green"], ["White", "White"], false, false), + (["Green", "Blue", "Blue", "Blue"], ["Black", "Black"], false, false), + (["Green", "Blue", "Yellow", "Yellow"], ["Black", "Black", "Black"], false, false), + (["Green", "Blue", "Yellow", "Purple"], ["Black", "Black", "Black"], false, false), + (["Green", "Blue", "Yellow", "Orange"], ["Black", "Black", "Black", "Black"], true, true), + ]; + + private readonly Mock _gamesClientMock; + private readonly Mock _infoBarServiceMock; public GamePageViewModelTests() { - (Guid GameType, int NumberCodes, int MaxMoves, IDictionary FieldValues) returnValue = + (Guid Id, int NumberCodes, int MaxMoves, IDictionary FieldValues) gameReturnValue = (Guid.NewGuid(), 4, 12, new Dictionary() { - { "colors", new string[] { "Black", "White", "Red", "Green", "Blue", "Yellow" } } + { "colors", ["Black", "White", "Red", "Green", "Blue", "Yellow"] } }); - Mock gameClient = new(); - gameClient.Setup( - client => client.StartGameAsync(GameType.Game6x4, "Test", CancellationToken.None)).ReturnsAsync(returnValue); - - Mock> options = new(); - options.Setup(o => o.Value).Returns(new GamePageViewModelOptions()); + _gamesClientMock = new(); + // Setup the StartGameAsync method of the GamesClient. + _gamesClientMock.Setup(client => client.StartGameAsync(GameType.Game6x4, "Test", CancellationToken.None)).ReturnsAsync(gameReturnValue); - Mock dialogService = new(); - Mock infoBarService = new(); + // Setup the SetMoveAsync method of the GamesClient. + for (int i = 0; i < s_moves.Length; i++) + { + var move = s_moves[i]; + _gamesClientMock.Setup(client => client.SetMoveAsync(gameReturnValue.Id, "Test", GameType.Game6x4, i + 1, move.GuessPegs, CancellationToken.None)) + .ReturnsAsync((move.KeyPegs, move.HasEnded, move.IsVictory)); + } - _viewModel = new GamePageViewModel(gameClient.Object, options.Object, dialogService.Object, infoBarService.Object); + _infoBarServiceMock = new(); } [Fact] - public async Task TestGameModeStartedAfterStart() + public async Task TestGameStart() { - _viewModel.Name = "Test"; - await _viewModel.StartGameCommand.ExecuteAsync(null); + var viewModel = new GamePageViewModel(_gamesClientMock.Object, _infoBarServiceMock.Object); + viewModel.Username = "Test"; + await viewModel.StartGameCommand.ExecuteAsync(null); - Assert.Equal(GameMode.Started, _viewModel.GameStatus); + Assert.Equal(4, viewModel.SelectedFields.Length); + Assert.All(viewModel.SelectedFields, field => Assert.NotNull(field)); + Assert.NotNull(viewModel.Game); } [Fact] - public async Task TestInProgressNotificationAfterStart() + public async Task TestIsLoadingNotificationAfterStart() { - List expected = new() { true, false }; - _viewModel.Name = "Test"; - List inProgressValues = new(); + var viewModel = new GamePageViewModel(_gamesClientMock.Object, _infoBarServiceMock.Object); + List expectedIsLoadingValues = [true, false]; + List actualInProgressValues = []; + viewModel.Username = "Test"; - _viewModel.PropertyChanged += (sender, e) => + viewModel.PropertyChanged += (sender, e) => { - if (e.PropertyName is "InProgress") - { - inProgressValues.Add(_viewModel.InProgress); - } + if (e.PropertyName is nameof(GamePageViewModel.IsLoading)) + actualInProgressValues.Add(viewModel.IsLoading); }; - await _viewModel.StartGameCommand.ExecuteAsync(null); + await viewModel.StartGameCommand.ExecuteAsync(null); + + Assert.Equal(expectedIsLoadingValues, actualInProgressValues); + } + + [Fact] + public async Task TestMoves() + { + // Start game + var viewModel = new GamePageViewModel(_gamesClientMock.Object, _infoBarServiceMock.Object); + viewModel.Username = "Test"; + + await viewModel.StartGameCommand.ExecuteAsync(null); + + // Play game + foreach ((string[] guessPegs, _, _, _) in s_moves) + { + for (int i = 0; i < guessPegs.Length; i++) + viewModel.SelectedFields[i].Color = guessPegs[i]; + + await viewModel.MakeMoveCommand.ExecuteAsync(null); + } - Assert.Equal(expected, inProgressValues); + Assert.Equal(s_moves.Length, viewModel.Game?.Moves.Count); + Assert.NotNull(viewModel.Game?.EndTime); + Assert.True(viewModel.Game?.IsVictory); } }