def test_removePlayerFailure_GameStarted(): game = Game() game.state = States.NIGHT message = get_state_change_message(game, False, Actions.REMOVE_PLAYER, None) assert message == f"The game has started. You can't leave now!"
def test_StartGame_CreatesChannelAndInvitesMafia(): repo = GameStateRepo() game = Game() testpId = 'test' testMafiaChannelId = 'testChannel' game.players = [createMafia(id=testpId)] game.state = States.NIGHT game.meta = {'channel_id': 'channel'} with patch('mafiaManageSlack.WebClient') as slackClientConstructor: with patch( 'mafiaManageSlack.get_state_change_message') as messageBuilder: with patch('data_access.dataRepos.boto3'): with patch('mafiaManageSlack.boto3'): slackClient = slackClientConstructor.return_value slackClient.conversations_create.return_value = { 'channel': { 'id': testMafiaChannelId } } mafiaManageSlack.lambda_handler( createSqsEvent({ 'state': repo._serializeGame(game), 'action': Actions.START_GAME, 'source': 'initiator', 'target': None }), None) slackClient.conversations_create.assert_called_with( name='mafia-secrets', is_private=True) slackClient.conversations_invite.assert_called_with( channel=testMafiaChannelId, users=testpId) slackClient.chat_postMessage.assert_called_with( channel=testMafiaChannelId, text= 'You are members of the local mafia. Rabble-rousers in the village have decided to make a stand against you. It is time you taught them a lesson...\nKill one of them using the command: /mafia kill @who-to-kill\nIf there is more than one member of the mafia you must all /mafia kill the same villager before they will be killed.' )
def test_MultipleMafiaMembers_MustAgreeOnWhoToKill(): player1 = createVillager('test1') player2 = createVillager('test2') mafia1 = createMafia('mafia1') mafia2 = createMafia('mafia2') state = Game() state.state = GameStates.NIGHT state.players = [player1, player2, createVillager(), createVillager(), mafia1, mafia2] systemUnderTest = getInstance(state) #mafia 1 wants to kill player 1 systemUnderTest.transition(Actions.MURDER, player1.id, mafia1.id) assert state.state == GameStates.NIGHT assert player1.state == PlayerStates.ALIVE #mafia 2 wants to kill player 2 systemUnderTest.transition(Actions.MURDER, player2.id, mafia2.id) assert state.state == GameStates.NIGHT assert player1.state == PlayerStates.ALIVE assert player2.state == PlayerStates.ALIVE #mafia 1 decides to agree and kill player2 instead systemUnderTest.transition(Actions.MURDER, player2.id, mafia1.id) assert state.state == GameStates.DAY assert player2.state == PlayerStates.DEAD
def test_GameStateMarshalling_AddPlayerAction_PlayerAdded(): state = Game() p_id = 'test' state.state = GameStates.MARSHALLING systemUnderTest = getInstance(state) success = systemUnderTest.transition(Actions.ADD_PLAYER, executor=p_id) assert p_id in [p.id for p in state.players] assert success
def test_GameStateMarshallingWithEnoughPlayers_GameStartAction_StateIsNight(): state = Game() state.state = GameStates.MARSHALLING state.players = [Player(), Player(), Player(), Player()] systemUnderTest = getInstance(state) success = systemUnderTest.transition(Actions.START_GAME) assert state.state == GameStates.NIGHT assert success
def test_GameStateMarshalling_AddPlayerWithDupeId_PlayerNotAdded(): state = Game() p_id = 'test' state.state = GameStates.MARSHALLING systemUnderTest = getInstance(state) systemUnderTest.transition(Actions.ADD_PLAYER, p_id) success = systemUnderTest.transition(Actions.ADD_PLAYER, p_id) assert len(state.players) == 1 assert not success
def test_GameStateNight_MurderAction_StateIsDayPlayerDead(): player = createVillager('test') mafia = createMafia('mafia') state = Game() state.state = GameStates.NIGHT state.players = [player, createVillager(), createVillager(), mafia] systemUnderTest = getInstance(state) systemUnderTest.transition(Actions.MURDER, player.id, mafia.id) assert state.state == GameStates.DAY assert player.state == PlayerStates.DEAD
def test_GameStateNight_MurderResultsInVillagerCountEqualingMafia_StateIsGameOver(): player = createVillager('test') mafia = createMafia('mafia') state = Game() state.state = GameStates.NIGHT state.players = [player, createVillager(), mafia] systemUnderTest = getInstance(state) systemUnderTest.transition(Actions.MURDER, player.id, mafia.id) assert state.state == GameStates.GAME_OVER assert player.state == PlayerStates.DEAD
def test_CanNotCastVoteIfOnTrial(): state = Game() state.state = GameStates.TRIAL systemUnderTest = getInstance(state) mafia = createMafia('test') villager1 = createVillager('v1') villager2 = createVillager('v2') mafia.state = PlayerStates.ON_TRIAL state.players = [mafia, villager1, villager2] systemUnderTest.transition(Actions.GUILTY, executor=mafia.id) assert mafia.vote == None
def test_GameStateMarshalling_RemovePlayerAction_PlayerRemoved(): state = Game() state.state = GameStates.MARSHALLING systemUnderTest = getInstance(state) player = createVillager('test') state.players = [player] success = systemUnderTest.transition(Actions.REMOVE_PLAYER, executor=player.id) assert player not in state.players assert success
def test_GameStateDay_OneAccuseAction_StateIsStillDay(): state = Game() state.state = GameStates.DAY systemUnderTest = getInstance(state) accused = createVillager('test') p1 = createVillager('p1') p2 = createVillager('p2') state.players = [accused, p1, p2] systemUnderTest.transition(Actions.ACCUSE, accused.id, p1.id) assert state.state == GameStates.DAY assert accused.state == PlayerStates.ALIVE
def test_DeadManCanNotAccuse(): state = Game() state.state = GameStates.DAY systemUnderTest = getInstance(state) dead_player = createVillager('test') live_player = createVillager('v1') dead_player.state=PlayerStates.DEAD state.players = [dead_player, live_player] result = systemUnderTest.transition(Actions.ACCUSE, live_player.id, dead_player.id) assert state.state == GameStates.DAY assert result == False
def test_VillagerCanNotMurder(): player1 = createVillager('test1') player2 = createVillager('test1') mafia = createMafia('mafia') state = Game() state.state = GameStates.NIGHT state.players = [player1, player2, mafia] systemUnderTest = getInstance(state) systemUnderTest.transition(Actions.MURDER, player2.id, player1.id) assert state.state == GameStates.NIGHT assert player2.state == PlayerStates.ALIVE
def test_GameStateDay_TwoAccuseActions_StateIsTrialPlayerIsOnTrial(): state = Game() state.state = GameStates.DAY systemUnderTest = getInstance(state) accused = createVillager('test') p1 = createVillager('p1') p2 = createVillager('p2') state.players = [accused, p1, p2] systemUnderTest.transition(Actions.ACCUSE, accused.id, p1.id) systemUnderTest.transition(Actions.ACCUSE, accused.id, p2.id) assert state.state == GameStates.TRIAL assert accused.state == PlayerStates.ON_TRIAL
def test_GameStateTrial_LastMafiaMemberFoundGuilty_StateIsGameOver(): state = Game() state.state = GameStates.TRIAL systemUnderTest = getInstance(state) mafia = createMafia('test') villager1 = createVillager('v1') villager2 = createVillager('v2') mafia.state = PlayerStates.ON_TRIAL state.players = [mafia, villager1, villager2] systemUnderTest.transition(Actions.GUILTY, executor='v1') systemUnderTest.transition(Actions.GUILTY, executor='v2') assert state.state == GameStates.GAME_OVER assert mafia.state == PlayerStates.DEAD
def test_GameStateMarshallingWithEnoughPlayers_GameStartAction_PlayersAssignedRoles( ): num_players = random.randint(4, 10) expected_mafia = num_players // 3 expected_villagers = num_players - expected_mafia state = Game() state.state = GameStates.MARSHALLING state.players = [Player() for x in range(num_players)] systemUnderTest = getInstance(state) systemUnderTest.transition(Actions.START_GAME) assert len([p for p in state.players if p.role == Roles.MAFIA]) == expected_mafia assert len([p for p in state.players if p.role == Roles.VILLAGER]) == expected_villagers
def test_GameStateTrial_FoundGuiltyAction_StateIsNightPlayerOnTrialIsDead(): state = Game() state.state = GameStates.TRIAL systemUnderTest = getInstance(state) player = createVillager('test') villager1 = createVillager('v1') villager2 = createVillager('v2') mafia = createMafia('m') player.state = PlayerStates.ON_TRIAL state.players = [player, villager2, villager1, mafia] systemUnderTest.transition(Actions.NOT_GUILTY, executor='v1') systemUnderTest.transition(Actions.GUILTY, executor='v2') systemUnderTest.transition(Actions.GUILTY, executor='m') assert state.state == GameStates.NIGHT assert player.state == PlayerStates.DEAD
def test_GameStateTrial_FoundNotGuiltyAction_StateIsDayPlayerOnTrialIsAlive(): state = Game() state.state = GameStates.TRIAL systemUnderTest = getInstance(state) player = createVillager('test') villager1 = createVillager('v1') villager2 = createVillager('v2') villager3 = createVillager('v3') villager3.state = PlayerStates.DEAD mafia = createMafia('m') player.state = PlayerStates.ON_TRIAL state.players = [player, villager2, villager1, mafia, villager3] systemUnderTest.transition(Actions.GUILTY, executor='m') assert state.state == GameStates.TRIAL systemUnderTest.transition(Actions.NOT_GUILTY, executor='v1') assert state.state == GameStates.TRIAL systemUnderTest.transition(Actions.NOT_GUILTY, executor='v2') assert state.state == GameStates.DAY assert player.state == PlayerStates.ALIVE
def test_playerMurderedSuccess(): game = Game() game.state = States.DAY p_id = "test" message = get_state_change_message(game, True, Actions.MURDER, target=p_id) assert message == f"Another beautiful morning! One that <@{p_id}> won't get to experience, for they are dead! Murdered in the night! One among you is the culprit!\n{build_how_to_accuse_message()}"
def test_addPlayerFailure_GameStarted(): game = Game() game.state = States.NIGHT message = get_state_change_message(game, False, Actions.ADD_PLAYER, None) assert message == f"The game has started. Maybe next time."