from aiohttp.test_utils import AioHTTPTestCase import practice EMPTY = {"history": [[], []], "turn": 0} class PracticeTest(AioHTTPTestCase): async def get_application(self): return practice.make_app() async def play(self, body): res = await self.client.post("/api/play", json=body) return res.status, await res.json() async def test_page_served(self): res = await self.client.get("/") self.assertEqual(res.status, 200) self.assertIn("practice.js", await res.text()) async def test_human_move(self): status, s = await self.play({**EMPTY, "cell": 4}) self.assertEqual(status, 200) self.assertEqual(s["board"][4], 0) self.assertEqual(s["history"], [[4], []]) self.assertEqual(s["turn"], 1) async def test_perfect_computer_blocks(self): for _ in range(10): status, s = await self.play({"history": [[0, 1], [4]], "turn": 1, "accuracy": 1}) self.assertEqual(status, 200) self.assertEqual(s["computerMove"], {"cell": 2, "how": "perfect"}) async def test_computer_takes_the_win(self): status, s = await self.play({"history": [[0, 1], [4, 8]], "turn": 0, "accuracy": 1}) self.assertEqual(s["winner"], 0) self.assertEqual(s["winLine"], [0, 1, 2]) self.assertEqual(s["computerMove"], {"cell": 2, "how": "perfect"}) async def test_human_move_has_no_computer_move(self): status, s = await self.play({**EMPTY, "cell": 4}) self.assertIsNone(s["computerMove"]) async def test_rejects_bad_requests(self): for body in [{}, [], {"history": [[0], [0]], "turn": 0}, {**EMPTY, "cell": 9}, {**EMPTY, "accuracy": "lots"}]: status, s = await self.play(body) self.assertEqual(status, 400, body) self.assertIn("error", s) async def test_no_moves_after_a_win(self): status, s = await self.play({"history": [[0, 1, 2], [4, 5]], "turn": 1}) self.assertEqual(status, 400) self.assertEqual(s["error"], "The round is over")