| 1 | """A check of kit/room.py against the room protocol (docs/games.md §8.3), on a toy game with one human seat: a stale or |
| 2 | illegal answer is ignored, a handover re-asks with a new version and drops the old controller's late answer, a silent |
| 3 | seat gets auto's move, and stop ends the room. python3 kit/test_room.py (or pytest) |
| 4 | """ |
| 5 | import json, os, subprocess, sys |
| 6 | |
| 7 | KIT = os.path.dirname(os.path.abspath(__file__)) |
| 8 | GAME = f''' |
| 9 | import sys; sys.path.insert(0, {KIT!r}) |
| 10 | from room import Room, Stop |
| 11 | room = Room(None) |
| 12 | try: |
| 13 | for i in range(4): |
| 14 | room.event({{'seat': '0', 'move': room.ask('0', {{'i': i}}, ['a', 'b'], auto=lambda: 'auto')}}) |
| 15 | except Stop: |
| 16 | room.event({{'stopped': True}}) |
| 17 | room.end('done', {{'0': 0}}) |
| 18 | ''' |
| 19 | |
| 20 | |
| 21 | def test_room(): |
| 22 | env = {**os.environ, 'SS_ROOM': json.dumps({'limit': 0.5, 'seats': {'0': {'kind': 'human'}}})} |
| 23 | p = subprocess.Popen([sys.executable, '-c', GAME], env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) |
| 24 | |
| 25 | def said(): |
| 26 | line = p.stdout.readline() |
| 27 | assert line.startswith('@shoal '), line |
| 28 | return json.loads(line[7:]) |
| 29 | |
| 30 | def send(msg): |
| 31 | p.stdin.write(json.dumps(msg) + '\n') |
| 32 | p.stdin.flush() |
| 33 | |
| 34 | assert said() == {'to': '0', 'version': 1, 'obs': {'i': 0}, 'legal': ['a', 'b']} |
| 35 | send({'act': {'seat': '0', 'version': 0, 'action': 'a'}}) |
| 36 | send({'act': {'seat': '0', 'version': 1, 'action': 'z'}}) |
| 37 | send({'act': {'seat': '0', 'version': 1, 'action': 'b'}}) |
| 38 | assert said() == {'event': {'seat': '0', 'move': 'b'}} |
| 39 | assert said()['version'] == 2 |
| 40 | send({'control': {'seat': '0', 'kind': 'agent', 'name': 'ada-codex'}}) |
| 41 | send({'act': {'seat': '0', 'version': 2, 'action': 'b'}}) |
| 42 | assert said() == {'event': {'type': 'control', 'seat': '0', 'kind': 'agent', 'name': 'ada-codex'}} |
| 43 | assert said()['version'] == 3 |
| 44 | send({'act': {'seat': '0', 'version': 3, 'action': 'a'}}) |
| 45 | assert said() == {'event': {'seat': '0', 'move': 'a'}} |
| 46 | assert said()['version'] == 4 |
| 47 | assert said() == {'event': {'seat': '0', 'move': 'auto'}} |
| 48 | assert said()['version'] == 5 |
| 49 | send({'stop': True}) |
| 50 | assert said() == {'event': {'stopped': True}} |
| 51 | assert said() == {'end': {'result': 'done', 'scores': {'0': 0}}} |
| 52 | assert p.wait(5) == 0 |
| 53 | |
| 54 | |
| 55 | if __name__ == '__main__': |
| 56 | test_room() |
| 57 | print('room protocol: ok') |
| 58 | |
| 59 | |
| 60 | def test_bot_processes_are_private_to_seats(): |
| 61 | |
| 62 | from room import Room |
| 63 | class Child: |
| 64 | def __init__(self): |
| 65 | self.seen = [] |
| 66 | def ask(self, msg): |
| 67 | self.seen.append(msg) |
| 68 | return 'a' |
| 69 | def close(self): |
| 70 | pass |
| 71 | room = object.__new__(Room) |
| 72 | room.start, room.field, room.bots = lambda folder: Child(), 'field', {} |
| 73 | a = room._bot({'field': 'same'}, seat='0') |
| 74 | b = room._bot({'field': 'same'}, seat='1') |
| 75 | assert a is not b |
| 76 | assert room._bot({'field': 'same'}, seat='0') is a |
| 77 | a.ask({'hand': 'private'}) |
| 78 | assert b.seen == [] |
| 79 | room.close() |
| 80 | |
| 81 | |
| 82 | if __name__ == '__main__': |
| 83 | test_bot_processes_are_private_to_seats() |
| 84 | print('seat process isolation: ok') |