Files
moonchess/tests/test_practice.py
T
tukaunuandClaude Opus 5.5 ce6b71eb35 Serve practice mode from the main server, add mode switch, fix touch
Practice mode is now at /practice on server.py instead of a separate
server, with a button in the bottom-right corner of each page to the
other mode. Touch: no double-tap zoom, no sticky hover after a tap, no
long-press selection on the board, and a pressed look on tapped squares.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-22 01:47:27 +02:00

60 lines
2.3 KiB
Python

from aiohttp.test_utils import AioHTTPTestCase
import server
EMPTY = {"history": [[], []], "turn": 0}
class PracticeTest(AioHTTPTestCase):
async def get_application(self):
server.rooms.clear()
return server.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("/practice")
self.assertEqual(res.status, 200)
self.assertIn("practice.js", await res.text())
async def test_pages_link_to_each_other(self):
self.assertIn('href="/practice"', await (await self.client.get("/")).text())
self.assertIn('href="/"', await (await self.client.get("/practice")).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")