-
-
Notifications
You must be signed in to change notification settings - Fork 401
tests: Add adapter-level tests for merels via game_handler (fixes #433) #891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+171
−67
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| from typing import Any, List, Tuple | ||
| from typing import Dict | ||
|
|
||
| from typing_extensions import override | ||
|
|
||
| from zulip_bots.game_handler import GameInstance | ||
| from zulip_bots.test_lib import BotTestCase, DefaultTests | ||
|
|
||
| from .libraries.constants import EMPTY_BOARD | ||
|
|
@@ -9,7 +10,8 @@ | |
| class TestMerelsBot(BotTestCase, DefaultTests): | ||
| bot_name = "merels" | ||
|
|
||
| def test_no_command(self): | ||
| def test_no_command(self) -> None: | ||
| # Out-of-game message for arbitrary input. | ||
| message = dict( | ||
| content="magic", type="stream", sender_email="[email protected]", sender_full_name="boo" | ||
| ) | ||
|
|
@@ -18,76 +20,178 @@ def test_no_command(self): | |
| res["content"], "You are not in a game at the moment. Type `help` for help." | ||
| ) | ||
|
|
||
| # FIXME: Add tests for computer moves | ||
| # FIXME: Add test lib for game_handler | ||
| def test_parse_board_identity_empty_board(self) -> None: | ||
| # Merels parse_board is identity; verify with the canonical empty board. | ||
| bot, _ = self._get_handlers() | ||
| self.assertEqual(bot.game_message_handler.parse_board(EMPTY_BOARD), EMPTY_BOARD) | ||
|
|
||
| # Test for unchanging aspects within the game | ||
| # Player Color, Start Message, Moving Message | ||
| def test_static_responses(self) -> None: | ||
| model, message_handler = self._get_game_handlers() | ||
| self.assertNotEqual(message_handler.get_player_color(0), None) | ||
| self.assertNotEqual(message_handler.game_start_message(), None) | ||
| self.assertEqual( | ||
| message_handler.alert_move_message("foo", "moved right"), "foo :moved right" | ||
|
|
||
| class GameAdapterTestLib: | ||
| """Small helpers for driving GameAdapter-based bots in tests.""" | ||
|
|
||
| def send( | ||
| self, | ||
| bot, | ||
| bot_handler, | ||
| content: str, | ||
| *, | ||
| user: str = "[email protected]", | ||
| user_name: str = "foo", | ||
| ) -> None: | ||
| bot.handle_message( | ||
| self.make_request_message(content, user=user, user_name=user_name), | ||
| bot_handler, | ||
| ) | ||
|
|
||
| # Test to see if the attributes exist | ||
| def test_has_attributes(self) -> None: | ||
| model, message_handler = self._get_game_handlers() | ||
| # Attributes from the Merels Handler | ||
| self.assertTrue(hasattr(message_handler, "parse_board") is not None) | ||
| self.assertTrue(hasattr(message_handler, "get_player_color") is not None) | ||
| self.assertTrue(hasattr(message_handler, "alert_move_message") is not None) | ||
| self.assertTrue(hasattr(message_handler, "game_start_message") is not None) | ||
| self.assertTrue(hasattr(message_handler, "alert_move_message") is not None) | ||
| # Attributes from the Merels Model | ||
| self.assertTrue(hasattr(model, "determine_game_over") is not None) | ||
| self.assertTrue(hasattr(model, "contains_winning_move") is not None) | ||
| self.assertTrue(hasattr(model, "make_move") is not None) | ||
|
|
||
| def test_parse_board(self) -> None: | ||
| board = EMPTY_BOARD | ||
| expect_response = EMPTY_BOARD | ||
| self._test_parse_board(board, expect_response) | ||
|
|
||
| def test_add_user_to_cache(self): | ||
| self.add_user_to_cache("Name") | ||
|
|
||
| def test_setup_game(self): | ||
| self.setup_game() | ||
|
|
||
| def add_user_to_cache(self, name: str, bot: Any = None) -> Any: | ||
| if bot is None: | ||
| bot, bot_handler = self._get_handlers() | ||
| message = { | ||
| "sender_email": f"{name}@example.com", | ||
| "sender_full_name": f"{name}", | ||
| def replies(self, bot_handler): | ||
| # Return the bot message 'content' fields from the transcript. | ||
| return [m["content"] for (_method, m) in bot_handler.transcript] | ||
|
|
||
| def send_and_collect( | ||
| self, | ||
| bot, | ||
| bot_handler, | ||
| content: str, | ||
| *, | ||
| user: str = "[email protected]", | ||
| user_name: str = "foo", | ||
| ): | ||
| bot_handler.reset_transcript() | ||
| self.send(bot, bot_handler, content, user=user, user_name=user_name) | ||
| return self.replies(bot_handler) | ||
|
|
||
|
|
||
| # Note: Merels has no vs-computer mode (in merels.py, supports_computer=False). | ||
| # If computer mode is added in the future, add adapter-level tests here. | ||
|
|
||
|
|
||
| class TestMerelsAdapter(BotTestCase, DefaultTests, GameAdapterTestLib): | ||
| """Adapter-focused tests (mirrors connect_four); use stable fragment assertions.""" | ||
|
|
||
| bot_name = "merels" | ||
|
|
||
| @override | ||
| def make_request_message( | ||
| self, content: str, user: str = "[email protected]", user_name: str = "foo" | ||
| ) -> Dict[str, str]: | ||
| # Provide stream metadata consumed by GameAdapter. | ||
| return { | ||
| "sender_email": user, | ||
| "sender_full_name": user_name, | ||
| "content": content, | ||
| "type": "stream", | ||
| "display_recipient": "general", | ||
| "subject": "merels-test-topic", | ||
| } | ||
| bot.add_user_to_cache(message) | ||
| return bot | ||
|
|
||
| def setup_game(self) -> None: | ||
| bot = self.add_user_to_cache("foo") | ||
| self.add_user_to_cache("baz", bot) | ||
| instance = GameInstance( | ||
| bot, False, "test game", "abc123", ["[email protected]", "[email protected]"], "test" | ||
|
|
||
| def test_help_is_merels_help(self) -> None: | ||
| bot, bot_handler = self._get_handlers() | ||
|
|
||
| bot_handler.reset_transcript() | ||
| bot.handle_message(self.make_request_message("help"), bot_handler) | ||
|
|
||
| responses = [m for (_method, m) in bot_handler.transcript] | ||
| self.assertTrue(responses, "No bot response to 'help'") | ||
| help_text = responses[0]["content"] | ||
|
|
||
| # Assert on stable fragments to avoid brittle exact matches. | ||
| self.assertIn("Merels Bot Help", help_text) | ||
| self.assertIn("start game", help_text) | ||
| self.assertIn("play game", help_text) | ||
| self.assertIn("quit", help_text) | ||
| self.assertIn("rules", help_text) | ||
| # Present today; OK if dropped in future wording changes. | ||
| self.assertIn("leaderboard", help_text) | ||
| self.assertIn("cancel game", help_text) | ||
|
|
||
| def test_start_game_emits_invite(self) -> None: | ||
| bot, bot_handler = self._get_handlers() | ||
| bot_handler.reset_transcript() | ||
|
|
||
| bot.handle_message( | ||
| self.make_request_message("start game", user="[email protected]", user_name="foo"), | ||
| bot_handler, | ||
| ) | ||
| bot.instances.update({"abc123": instance}) | ||
| instance.start() | ||
| return bot | ||
|
|
||
| def _get_game_handlers(self) -> Tuple[Any, Any]: | ||
| contents = [m["content"] for (_method, m) in bot_handler.transcript] | ||
| self.assertTrue(contents, "No bot reply recorded for 'start game'") | ||
| first = contents[0] | ||
| self.assertIn("wants to play", first) | ||
| self.assertIn("Merels", first) | ||
| self.assertIn("join", first) | ||
|
|
||
| def test_join_starts_game_emits_start_message(self) -> None: | ||
| bot, bot_handler = self._get_handlers() | ||
| return bot.model, bot.game_message_handler | ||
| expected_fragment = bot.game_message_handler.game_start_message() | ||
|
|
||
| def _test_parse_board(self, board: str, expected_response: str) -> None: | ||
| model, message_handler = self._get_game_handlers() | ||
| response = message_handler.parse_board(board) | ||
| self.assertEqual(response, expected_response) | ||
| bot_handler.reset_transcript() | ||
| bot.handle_message( | ||
| self.make_request_message("start game", "[email protected]", "foo"), bot_handler | ||
| ) | ||
| bot.handle_message(self.make_request_message("join", "[email protected]", "bar"), bot_handler) | ||
|
|
||
| def _test_determine_game_over( | ||
| self, board: List[List[int]], players: List[str], expected_response: str | ||
| ) -> None: | ||
| model, message_handler = self._get_game_handlers() | ||
| response = model.determine_game_over(players) | ||
| self.assertEqual(response, expected_response) | ||
| contents = [m["content"] for (_method, m) in bot_handler.transcript] | ||
| self.assertTrue( | ||
| any(expected_fragment in c for c in contents), | ||
| "Merels start message not found after 'join'", | ||
| ) | ||
|
|
||
| def test_message_handler_helpers(self) -> None: | ||
| bot, _ = self._get_handlers() | ||
|
|
||
| # Identity parse_board. | ||
| self.assertEqual( | ||
| bot.game_message_handler.parse_board("sample_board_repr"), "sample_board_repr" | ||
| ) | ||
|
|
||
| # Token color in allowed set. | ||
| self.assertIn( | ||
| bot.game_message_handler.get_player_color(0), | ||
| (":o_button:", ":cross_mark_button:"), | ||
| ) | ||
| self.assertIn( | ||
| bot.game_message_handler.get_player_color(1), | ||
| (":o_button:", ":cross_mark_button:"), | ||
| ) | ||
|
|
||
| # Basic move alert format. | ||
| self.assertEqual( | ||
| bot.game_message_handler.alert_move_message("foo", "move 1,1"), | ||
| "foo :move 1,1", | ||
| ) | ||
|
|
||
| def test_move_after_join_invokes_make_move_and_replies(self) -> None: | ||
| """ | ||
| After start/join, Merels begins in placement (Phase 1). Use 'put v,h' | ||
| and assert the adapter emits an acknowledgement. Try both players to | ||
| avoid assuming turn order. | ||
| """ | ||
| bot, bot_handler = self._get_handlers() | ||
|
|
||
| # Start 2P game. | ||
| _ = self.send_and_collect( | ||
| bot, bot_handler, "start game", user="[email protected]", user_name="foo" | ||
| ) | ||
| _ = self.send_and_collect(bot, bot_handler, "join", user="[email protected]", user_name="bar") | ||
|
|
||
| # Stable oracles from the handler's formatter. | ||
| ack_foo = bot.game_message_handler.alert_move_message("foo", "put 1,1") | ||
| ack_bar = bot.game_message_handler.alert_move_message("bar", "put 1,1") | ||
|
|
||
| # Try current player first (unknown), then the other. | ||
| contents_foo = self.send_and_collect( | ||
| bot, bot_handler, "put 1,1", user="[email protected]", user_name="foo" | ||
| ) | ||
| joined = " ".join(contents_foo) | ||
|
|
||
| if (ack_foo not in joined) and (ack_bar not in joined) and (":put 1,1" not in joined): | ||
| contents_bar = self.send_and_collect( | ||
| bot, bot_handler, "put 1,1", user="[email protected]", user_name="bar" | ||
| ) | ||
| joined += " " + " ".join(contents_bar) | ||
|
|
||
| # Assert the adapter produced a placement acknowledgement. | ||
| self.assertTrue( | ||
| any(h in joined for h in (":put 1,1", ack_foo, ack_bar)), | ||
| f"No placement acknowledgement found in: {joined}", | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do your new tests do either of these things?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just pushed a commit that addresses the test lib for game_handler. However, I did not add tests for computer moves because in merels.py, it shows that supports_computer = False, so I left a comment in test_merels.py to clarify that. I assumed I wasn't supposed to modify merels.py, which is why I went this route.