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_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_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_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_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_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_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_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_RecordReceived_GenerateMessageAndBroadcastToChannel(): repo = GameStateRepo() game = Game() testExecutorId = 'source' testTargetId = 'target' testMainChannelId = 'testChannel' game.meta = {'channel_id' : testMainChannelId} with patch('mafiaManageSlack.WebClient') as slackClientConstructor: with patch('mafiaManageSlack.get_state_change_message') as messageBuilder: with patch('mafiaManageSlack.boto3'): slackClient = slackClientConstructor.return_value mafiaManageSlack.lambda_handler(createSqsEvent({'state':repo._serializeGame(game), 'action':'ACTION', 'source': testExecutorId, 'target':testTargetId}), None) slackClient.chat_postMessage.assert_called_with(channel=testMainChannelId, text=messageBuilder.return_value) messageBuilder.assert_called_with(ANY, True, 'ACTION', testExecutorId, testTargetId)
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_addPlayerFailure_AlreadyJoined(): p_id = "test" game = Game() game.players.append(Player(p_id)) message = get_state_change_message(game, False, Actions.ADD_PLAYER, p_id) assert message == f"You can't join if you're already in."
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_FullGame_VillageWins(): game = Game() manager = getInstance(game) print('game marshalling no players yet.') manager.printGameState() manager.transition(Actions.ADD_PLAYER, executor='1') manager.transition(Actions.ADD_PLAYER, executor='2') manager.transition(Actions.ADD_PLAYER, executor='3') manager.transition(Actions.ADD_PLAYER, executor='4') print('enough players have joined to start') manager.printGameState() manager.transition(Actions.START_GAME) manager = getInstance(game) print('game begins') manager.printGameState() mafia = [p for p in manager.gameState.players if p.role == Roles.MAFIA][0] to_kill = [ p for p in manager.gameState.players if p.role == Roles.VILLAGER and p.state == PlayerStates.ALIVE ][0] manager.transition(Actions.MURDER, to_kill.id, mafia.id) manager = getInstance(game) print('the mafia has killed a villager') manager.printGameState() to_accuse = [ p for p in manager.gameState.players if p.role == Roles.MAFIA and p.state == PlayerStates.ALIVE ][0] living_villagers = [ p for p in manager.gameState.players if p.role == Roles.VILLAGER and p.state == PlayerStates.ALIVE ] accuser1, accuser2 = living_villagers[0], living_villagers[1] manager.transition(Actions.ACCUSE, to_accuse.id, accuser1.id) manager.transition(Actions.ACCUSE, to_accuse.id, accuser2.id) manager = getInstance(game) print('the villagers accuse a member of the mafia') manager.printGameState() manager.transition(Actions.GUILTY, executor='1') manager.transition(Actions.GUILTY, executor='2') manager.transition(Actions.GUILTY, executor='3') manager.transition(Actions.GUILTY, executor='4') print('the murderer has been executed') print( 'game over village wins. organized crime has been purged from the town' ) manager.printGameState() assert game.state == GameStates.GAME_OVER
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_FullGame_MafiaWins(): game = Game() manager = getInstance(game) print('game marshalling no players yet.') manager.printGameState() manager.transition(Actions.ADD_PLAYER, executor='1') manager.transition(Actions.ADD_PLAYER, executor='2') manager.transition(Actions.ADD_PLAYER, executor='3') manager.transition(Actions.ADD_PLAYER, executor='4') print('enough players have joined to start') manager.printGameState() manager.transition(Actions.START_GAME) print('game begins') manager.printGameState() manager = getInstance(game) mafia = [p for p in manager.gameState.players if p.role == Roles.MAFIA][0] to_kill = [ p for p in manager.gameState.players if p.role == Roles.VILLAGER and p.state == PlayerStates.ALIVE ][0] manager.transition(Actions.MURDER, to_kill.id, mafia.id) print('the mafia has killed a villager') manager.printGameState() manager = getInstance(game) villagers = [ p for p in manager.gameState.players if p.role == Roles.VILLAGER and p.state == PlayerStates.ALIVE ] to_accuse = villagers[0] accuser = villagers[1] manager.transition(Actions.ACCUSE, to_accuse.id, accuser.id) manager.transition(Actions.ACCUSE, to_accuse.id, mafia.id) print('the villagers accuse one of their own') manager.printGameState() manager = getInstance(game) manager.transition(Actions.GUILTY, executor='1') manager.transition(Actions.GUILTY, executor='2') manager.transition(Actions.GUILTY, executor='3') manager.transition(Actions.GUILTY, executor='4') print('an innocent man has died') print('game over mafia wins. they control the town') manager.printGameState() assert game.state == GameStates.GAME_OVER
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_startGameSuccess(): message = get_state_change_message(Game(), True, Actions.START_GAME, None) assert message == f"The game is starting now! If you are in the mafia you will be notified...\n\nNight falls on the village. It is peaceful here, but not for long. The mafia is up to something.\n{build_roster_message(Game())}"
def test_defaultGameState(): systemUnderTest = Game() assert systemUnderTest.state == States.MARSHALLING assert len(systemUnderTest.players) == 0
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."