public void Start_ThrowsArgumentNullWhenBotIsNull() { var socket = new StubWebSocket(WebSocketState.Open); var client = new SnakeClient(socket); Assert.Throws <ArgumentNullException>(() => client.Start(null, true)); }
public void Start_SendsRegisterMoveRequestFromBot(Direction direction, string expectedDirectionString) { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.PlayerRegistered } }); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.GameStarting } }); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.MapUpdated }, { "map", _sampleMapJson } }); var client = new SnakeClient(socket, Mock.Of <IGameObserver>()); client.Start(new StubSnakeBot(direction), true); var moveMessage = socket.OutgoingJson.Last(); Assert.Equal("se.cygni.snake.api.request.RegisterMove", (string)moveMessage["type"]); Assert.Equal(expectedDirectionString, (string)moveMessage["direction"]); Assert.Equal(_sampleMapJson["worldTick"], (string)moveMessage["gameTick"]); }
public void Start_RequestsMoveFromSnakeBotOnMapUpdate() { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.PlayerRegistered } }); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.GameStarting } }); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.MapUpdated }, { "map", _sampleMapJson } }); bool receivedCall = false; var bot = new StubSnakeBot(map => { receivedCall = true; return(Direction.Down); }); var client = new SnakeClient(socket); client.Start(bot); Assert.True(receivedCall); }
public void Start_ThrowsInvalidOperationWhenSocketIsNotOpen(WebSocketState state) { var socket = new StubWebSocket(state); var client = new SnakeClient(socket); Assert.Throws <InvalidOperationException>(() => client.Start(new StubSnakeBot(), true)); }
public void Start_RequestsMoveFromSnakeBotOnMapUpdate() { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.PlayerRegistered } }); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.GameStarting } }); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.MapUpdated }, { "map", _sampleMapJson } }); var bot = new Mock <StubSnakeBot>(); bot.Setup(b => b.GetNextMove(It.IsAny <Map>())); var client = new SnakeClient(socket); client.Start(bot.Object, true); bot.Verify(o => o.GetNextMove(It.IsAny <Map>()), Times.Once); }
public void Start_ThrowsInvalidOperationWhenServerSaysPlayerNameIsInvalid() { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.InvalidPlayerName }, { "reason", "taken" } }); var client = new SnakeClient(socket); Assert.Throws <InvalidOperationException>(() => client.Start(new StubSnakeBot(), true)); }
public void Start_SendsStartGameRequestAfterServerHasConfirmedRegistration() { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.PlayerRegistered } }); var client = new SnakeClient(socket, Mock.Of <IGameObserver>()); client.Start(new StubSnakeBot(), true); var startGameMessage = socket.OutgoingJson[2]; Assert.Equal("se.cygni.snake.api.request.StartGame", (string)startGameMessage["type"]); }
public void Start_RequestsMoveFromBotEventIfNoGameStartingMessageHasBeenReceived() { // Not an actual requirement, but this test is here for completness. var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(JObject.Parse(TestResources.GetResourceText("map-update.json", Encoding.UTF8))); var client = new SnakeClient(socket, Mock.Of <IGameObserver>()); var mockSnake = new Mock <StubSnakeBot>(); mockSnake.Setup(s => s.GetNextMove(It.IsAny <Map>())); client.Start(mockSnake.Object, true); mockSnake.Verify(s => s.GetNextMove(It.IsAny <Map>()), Times.Once()); }
public void Start_DoesNotSendStartGameRequestAfterServerHasConfirmedRegistrationWhenAutoStartDisabled() { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.PlayerRegistered } }); var client = new SnakeClient(socket, new StubGameObserver()); client.Start(new StubSnakeBot() { AutoStart = false }); Assert.Equal(2, socket.OutgoingJson.Count); }
public void Start_RequestsMoveFromBotEventIfNoGameStartingMessageHasBeenReceived() { // Not an actual requirement, but this test is here for completness. var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(JObject.Parse(TestResources.GetResourceText("map-update.json", Encoding.UTF8))); bool receivedCall = false; var bot = new StubSnakeBot(map => { receivedCall = true; return(Direction.Down); }); var client = new SnakeClient(socket); client.Start(bot); Assert.True(receivedCall); }
public void Start_NotifiesObserverWhenGameIsStarting() { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.PlayerRegistered } }); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.GameStarting } }); var observer = new StubGameObserver(); var client = new SnakeClient(socket, observer); client.Start(new StubSnakeBot()); Assert.Equal(1, observer.GameStartCalls); }
public void Start_SendsRegisterPlayerRequest() { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.PlayerRegistered } }); var client = new SnakeClient(socket, Mock.Of <IGameObserver>()); var bot = new StubSnakeBot(); client.Start(bot, true); var registerMessage = socket.OutgoingJson[0]; Assert.Equal("se.cygni.snake.api.request.RegisterPlayer", (string)registerMessage["type"]); Assert.Equal(bot.Name, (string)registerMessage["playerName"]); }
public void Start_NotifiesObserverWhenGameHasEnded() { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.GameEnded }, { "map", _sampleMapJson } }); var observer = new StubGameObserver(); var client = new SnakeClient(socket, observer); client.Start(new StubSnakeBot()); Assert.Equal(observer.GameEndCalls, 1); }
public void Start_NotifiesObserverWhenSnakeHasDied() { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.SnakeDead }, { "playerId", "snake-id" }, { "deathReason", "CollisionWithWall" }, }); var observer = new StubGameObserver(); var client = new SnakeClient(socket, observer); client.Start(new StubSnakeBot()); Assert.Equal(1, observer.SnakeDiedCalls); }
public void Start_NotifiesObserverWhenGameIsStarting() { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.PlayerRegistered } }); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.GameStarting } }); var observer = new Mock <IGameObserver>(); observer.Setup(o => o.OnGameStart()); var client = new SnakeClient(socket, observer.Object); client.Start(new StubSnakeBot(), true); observer.Verify(o => o.OnGameStart(), Times.Once); }
public void Start_NotifiesObserverWhenGameHasEnded() { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.GameEnded }, { "map", _sampleMapJson } }); var observer = new Mock <IGameObserver>(); observer.Setup(o => o.OnGameEnd(It.IsAny <Map>())); var client = new SnakeClient(socket, observer.Object); client.Start(new StubSnakeBot(), true); observer.Verify(o => o.OnGameEnd(It.IsAny <Map>()), Times.Once); }
public void RegisterPlayer_StartGame_RegisterMove() { // Setting up socket with normal flow of events var socket = new StubWebSocket(WebSocketState.Open) { CloseWhenNoMoreMessages = true }; socket.IncomingJson.Enqueue(JObject.Parse(TestResources.GetResourceText("player-registered.json", Encoding.UTF8))); socket.IncomingJson.Enqueue(JObject.Parse(TestResources.GetResourceText("game-starting.json", Encoding.UTF8))); socket.IncomingJson.Enqueue(JObject.Parse(TestResources.GetResourceText("map-update.json", Encoding.UTF8))); socket.IncomingJson.Enqueue(JObject.Parse(TestResources.GetResourceText("game-ended.json", Encoding.UTF8))); var client = new SnakeClient(socket, Mock.Of <IGameObserver>()); client.Start(new StubSnakeBot(Direction.Left)); // Assert correct sequence of messages back to the socket. Assert.Equal(MessageType.RegisterPlayer, socket.OutgoingJson[0]["type"]); Assert.Equal(MessageType.StartGame, socket.OutgoingJson[1]["type"]); Assert.Equal(MessageType.RegisterMove, socket.OutgoingJson[2]["type"]); }
public void Start_NotifiesObserverWhenSnakeHasDied() { var socket = new StubWebSocket(WebSocketState.Open); socket.IncomingJson.Enqueue(new JObject { { "type", MessageType.SnakeDead }, { "playerId", "snake-id" }, { "deathReason", "CollisionWithWall" }, }); var observer = new Mock <IGameObserver>(); observer.Setup(o => o.OnSnakeDied(It.IsAny <string>(), It.IsAny <string>())); var client = new SnakeClient(socket, observer.Object); client.Start(new StubSnakeBot(), true); observer.Verify(o => o.OnSnakeDied( It.Is <string>(s => s.Equals("CollisionWithWall", StringComparison.Ordinal)), It.Is <string>(s => s.Equals("snake-id"))), Times.Once); }