| 1 | #!/usr/bin/env python3 |
| 2 | """Isolated end-to-end API tests. No changes to the application's real database.""" |
| 3 | import concurrent.futures |
| 4 | import os |
| 5 | import signal |
| 6 | import http.cookiejar |
| 7 | import json |
| 8 | from pathlib import Path |
| 9 | import shutil |
| 10 | import socket |
| 11 | import sqlite3 |
| 12 | import subprocess |
| 13 | import tempfile |
| 14 | import time |
| 15 | import urllib.request |
| 16 | import urllib.error |
| 17 | |
| 18 | WEB = Path(__file__).resolve().parents[1] |
| 19 | ARTIFACTS = WEB / 'tests/artifacts' |
| 20 | ARTIFACTS.mkdir(exist_ok=True) |
| 21 | checks = 0 |
| 22 | |
| 23 | def check(value, label): |
| 24 | global checks |
| 25 | checks += 1 |
| 26 | assert value, label |
| 27 | |
| 28 | with tempfile.TemporaryDirectory(prefix='api-', dir=ARTIFACTS) as tmp: |
| 29 | root = Path(tmp) |
| 30 | for name in ['api.php', 'index.php', 'auth.php', 'callback.php', 'router.php']: |
| 31 | shutil.copy(WEB / name, root / name) |
| 32 | shutil.copytree(WEB / 'lib', root / 'lib', ignore=shutil.ignore_patterns('tablebase')) |
| 33 | shutil.copytree(WEB / 'assets/avatars', root / 'assets/avatars') |
| 34 | (root / 'data').mkdir() |
| 35 | with socket.socket() as sock: |
| 36 | sock.bind(('127.0.0.1', 0)) |
| 37 | port = sock.getsockname()[1] |
| 38 | base = f'http://127.0.0.1:{port}' |
| 39 | log = (root / 'server.log').open('w') |
| 40 | process = subprocess.Popen(['php', '-S', f'127.0.0.1:{port}', '-t', str(root), str(root / 'router.php')], stdout=log, stderr=log, env={**os.environ, 'PHP_CLI_SERVER_WORKERS':'4'}, start_new_session=True) |
| 41 | class Client: |
| 42 | def __init__(self): |
| 43 | self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar())) |
| 44 | self.csrf = None |
| 45 | def get(self, path='/api.php'): |
| 46 | with self.opener.open(base + path) as r: |
| 47 | return json.load(r) |
| 48 | def state(self): |
| 49 | state = self.get() |
| 50 | self.csrf = state['csrf'] |
| 51 | return state |
| 52 | def post(self, action, expected=200, **fields): |
| 53 | req = urllib.request.Request(base + '/api.php', data=json.dumps({'action':action, **fields}).encode(), headers={'Content-Type':'application/json', 'X-CSRF-Token':self.csrf or ''}) |
| 54 | try: |
| 55 | response = self.opener.open(req) |
| 56 | except urllib.error.HTTPError as error: |
| 57 | response = error |
| 58 | with response: |
| 59 | check(response.status == expected, f'{action}: expected {expected}, got {response.status}') |
| 60 | return json.load(response) |
| 61 | def game(self, id): |
| 62 | return next(g for g in self.state()['games'] if g['id'] == id) |
| 63 | def game_action(self, action, id, **fields): |
| 64 | g = self.game(id) |
| 65 | return self.post(action, game=id, version=g['version'], **fields) |
| 66 | try: |
| 67 | for _ in range(100): |
| 68 | try: |
| 69 | urllib.request.urlopen(base + '/index.php').close() |
| 70 | break |
| 71 | except urllib.error.URLError: |
| 72 | time.sleep(.05) |
| 73 | config_path=root/'data/config.php' |
| 74 | check(config_path.is_file(),'First page request creates config.php') |
| 75 | generated=config_path.read_text() |
| 76 | check(all(f"'{key}' => ''" in generated for key in ['base_url','client_id','client_secret']),'Generated config fields are empty') |
| 77 | configured=generated.replace("'client_id' => ''", "'client_id' => 'fixture-client'")+'\n// Keep this server-specific comment.\n' |
| 78 | config_path.write_text(configured) |
| 79 | a, b = Client(), Client() |
| 80 | check(a.state()['user']['name'] == 'test_user_1', 'Local autologin') |
| 81 | check(config_path.read_text()==configured,'Existing configuration preserved byte-for-byte') |
| 82 | loaded=subprocess.check_output(['php','-r', |
| 83 | 'require $argv[1]; echo json_encode(config());',str(root/'lib/bootstrap.php')], |
| 84 | env={**os.environ,'MICROCHESS_BASE_URL':'https://example.test','MICROCHESS_CLIENT_ID':'environment-client','MICROCHESS_CLIENT_SECRET':'fixture-secret'},text=True) |
| 85 | check(json.loads(loaded)=={'base_url':'https://example.test','client_id':'fixture-client','client_secret':'fixture-secret'},'Empty fields use environment; configured fields take precedence') |
| 86 | b.state(); b.post('switch_user', user='test_user_2'); b.state() |
| 87 | check(b.state()['user']['name'] == 'test_user_2', 'Independent sessions') |
| 88 | check(a.state()['user']['rating']==b.state()['user']['rating']==1200, 'Accounts start at 1200 Elo') |
| 89 | avatars=json.loads((root/'assets/avatars/catalog.json').read_text()) |
| 90 | check(len(avatars)==91 and sum(v['group']=='fairy' for v in avatars)==67,'Only retained fairy and colored avatars are available') |
| 91 | check(len({Path(v['file']).stem.casefold() for v in avatars})==len(avatars),'PNG and SVG versions of a piece appear only once') |
| 92 | check(len({v['id'] for v in avatars})==len(avatars),'Avatar IDs are unique') |
| 93 | check(all((root/'assets/avatars'/v['file']).read_bytes().startswith(b'\x89PNG\r\n\x1a\n') for v in avatars),'Every selectable avatar has a local PNG') |
| 94 | initial_avatar=b.state()['user']['avatar'] |
| 95 | check(initial_avatar in {v['id'] for v in json.loads((root/'assets/avatars/catalog.json').read_text())},'Random default avatar') |
| 96 | a.post('set_avatar',avatar='blue-knight') |
| 97 | check(a.state()['user']['avatar']=='blue-knight','Avatar persists across requests') |
| 98 | check(b.state()['user']['avatar']==initial_avatar,'Avatar change affects only the signed-in user') |
| 99 | a.post('set_avatar',avatar='../../data/config.php',expected=409) |
| 100 | a.post('set_avatar',avatar='not-a-piece',expected=409) |
| 101 | a.post('set_avatar',avatar=['blue-knight'],expected=409) |
| 102 | b.post('set_avatar',avatar='purple-elephant') |
| 103 | check(b.state()['user']['avatar']=='purple-elephant','Fairy avatar can be selected') |
| 104 | b.post('set_avatar',avatar=None) |
| 105 | check(b.state()['user']['avatar'] is not None,'Random avatar reset') |
| 106 | a.post('switch_user', user='admin', expected=409) |
| 107 | old = a.csrf; a.csrf = 'wrong'; a.post('create_link', kind='match', expected=403); a.csrf = old |
| 108 | token = a.post('create_link', kind='friend')['url'].split('=')[1] |
| 109 | check(b.get('/api.php?invite='+token)['invite']['kind'] == 'friend', 'Friend request preview') |
| 110 | a.post('accept_link', token=token, expected=409) |
| 111 | b.post('accept_link', token=token) |
| 112 | check(len(a.state()['friends']) == len(b.state()['friends']) == 1, 'Mutual friendship') |
| 113 | check(b.state()['friends'][0]['avatar']=='blue-knight','Friends see selected avatar') |
| 114 | b.post('accept_link', token=token, expected=409) |
| 115 | a.post('challenge', friend='local:test_user_2') |
| 116 | g = b.state()['games'][0]; id = g['id'] |
| 117 | check(g['incoming'] and g['status']=='pending', 'Incoming challenge') |
| 118 | a.post('challenge', friend='local:test_user_2', expected=409) |
| 119 | a.game_action('accept', id, expected=409) |
| 120 | b.game_action('reject', id) |
| 121 | check(not a.state()['games'] and not b.state()['games'], 'Reject removes both tiles') |
| 122 | a.post('challenge', friend='local:test_user_2'); id = b.state()['games'][0]['id']; b.game_action('accept', id) |
| 123 | ga, gb = a.game(id), b.game(id) |
| 124 | check(gb['opponentAvatar']=='blue-knight','Game tile includes opponent avatar') |
| 125 | check(ga['color'] != gb['color'] and ga['yourTurn'] != gb['yourTurn'], 'Opposite colors and one turn') |
| 126 | white, black = (a,b) if ga['color']=='w' else (b,a) |
| 127 | black.game_action('move', id, from_=4, to=8, expected=409) |
| 128 | current = white.game(id) |
| 129 | white.post('move', game=id, version=current['version'], **{'from':15,'to':7}, expected=409) |
| 130 | move = current['legal'][0] |
| 131 | check('analysis' not in current, 'Polling carries no obsolete evaluation data') |
| 132 | response = white.post('move', game=id, version=current['version'], **move) |
| 133 | check(response['feedback']['model'] == 'tactical-estimate' and response['feedback']['ply'] == 1, 'Confirmed move returns live tactical estimate') |
| 134 | check(response['feedback']['quality'] in [None, 'good', 'poor'], 'Only supported feedback values') |
| 135 | white.post('move', game=id, version=current['version'], **move, expected=409) |
| 136 | check(black.game(id)['yourTurn'], 'Turn passes') |
| 137 | check(black.game(id)['turnOrder']>current['turnOrder'],'New turn receives a later persistent queue position') |
| 138 | black.game_action('resign', id) |
| 139 | check(a.game(id)['status']=='ended' and b.game(id)['winner']==white.state()['user']['name'], 'Resignation result') |
| 140 | check(white.state()['user']['rating']==1216 and black.state()['user']['rating']==1184, 'Equal rating win transfers 16 points') |
| 141 | black.game_action('resign', id, expected=409) |
| 142 | check(black.state()['user']['ratedGames']==1, 'Duplicate result cannot change rating twice') |
| 143 | check(sum(a.state()['friends'][0][k] for k in ['wins','losses','ties'])==1, 'Stats recorded once') |
| 144 | archive=white.get('/api.php?action=history') |
| 145 | check(len(archive)==1 and archive[0]['outcome']=='win','Winner has saved game') |
| 146 | saved_id=archive[0]['id'] |
| 147 | saved=white.get(f'/api.php?action=replay&id={saved_id}') |
| 148 | check(len(saved['positions'])==2 and saved['positions'][-1]['board']==white.game(id)['state']['board'],'Replay reconstructs opening and final board') |
| 149 | check(black.get('/api.php?action=history')[0]['outcome']=='loss','Both participants can access their result') |
| 150 | check(saved['color']=='w','Replay retains player orientation') |
| 151 | check(saved['whiteName']==white.state()['user']['name'] and saved['blackName']==black.state()['user']['name'],'Replay identifies both colors') |
| 152 | token=saved['shareToken'] |
| 153 | with urllib.request.urlopen(urllib.request.Request(base+'/api.php?action=shared_replay&token='+token,headers={'Host':'example.test'})) as response: |
| 154 | public=json.load(response) |
| 155 | check(public['positions']==saved['positions'] and public['outcome']=='White won','Anonymous shared replay contains original game') |
| 156 | check(not {'a','b','csrf','opponentId'} & public.keys(),'Public replay exposes no account identifiers or session data') |
| 157 | check(white.get('/api.php?action=shared_replay&token='+token)['positions']==saved['positions'],'Signed-in player can open a shared replay') |
| 158 | check(black.get('/api.php?action=shared_replay&token='+token)['outcome']=='White won','Opponent session can open the same shared replay') |
| 159 | with white.opener.open(base+'/?replay='+token) as response: |
| 160 | page=response.read().decode() |
| 161 | check('id="replay-page"' in page and 'id="your-games"' not in page,'Signed-in visitor gets the standalone replay page') |
| 162 | check(white.get(f'/api.php?action=replay&id={saved_id}')['shareToken']==token,'Share token remains stable') |
| 163 | try: |
| 164 | urllib.request.urlopen(urllib.request.Request(base+'/api.php?action=shared_replay&token='+'0'*48,headers={'Host':'example.test'})) |
| 165 | check(False,'Unknown shared token rejected') |
| 166 | except urllib.error.HTTPError as e: |
| 167 | check(e.code==404,'Unknown shared token rejected') |
| 168 | |
| 169 | old_color = a.game(id)['color'] |
| 170 | a.game_action('rematch', id) |
| 171 | check(b.game(id)['opponentRematch'], 'Rematch notification') |
| 172 | b.game_action('rematch', id) |
| 173 | check(a.game(id)['color'] != old_color and a.game(id)['state']['ply']==0, 'Rematch swaps sides, same tile') |
| 174 | turn = a if a.game(id)['yourTurn'] else b |
| 175 | turn.game_action('offer_draw', id) |
| 176 | move = turn.game(id)['legal'][0]; turn.game_action('move',id,**move) |
| 177 | other = b if turn is a else a |
| 178 | other.game_action('accept_draw', id) |
| 179 | check(a.state()['friends'][0]['ties']==1, 'Draw stats') |
| 180 | check(white.state()['user']['rating']==1215 and black.state()['user']['rating']==1185, 'Rematch draw transfers one point to lower rated player') |
| 181 | check(a.state()['user']['ratedGames']==b.state()['user']['ratedGames']==2, 'Rematch rated separately') |
| 182 | archive=a.get('/api.php?action=history') |
| 183 | check(len(archive)==2 and archive[0]['id']>archive[1]['id'] and archive[0]['outcome']=='draw','Rematches saved separately, newest first') |
| 184 | check(white.get(f'/api.php?action=replay&id={saved_id}')['positions']==saved['positions'] and white.get(f'/api.php?action=replay&id={saved_id}')['color']==saved['color'],'Rematch preserves original replay') |
| 185 | a.game_action('rematch', id); b.game_action('exit', id) |
| 186 | check(not a.game(id)['canRematch'], 'Exit disables opponent rematch') |
| 187 | check(not b.state()['games'], 'Exit hides own tile only') |
| 188 | check(len(b.get('/api.php?action=history'))==2,'Exiting keeps saved games') |
| 189 | a.game_action('rematch', id, expected=409) |
| 190 | a.game_action('exit', id) |
| 191 | match = a.post('create_link',kind='match')['url'].split('=')[1] |
| 192 | check(b.get('/api.php?invite='+match)['invite']['kind']=='match', 'Match link modal') |
| 193 | check(b.get('/api.php?invite='+match)['invite']['kind']=='match', 'Preview/reject leaves link valid') |
| 194 | def race_accept(_): |
| 195 | req=urllib.request.Request(base+'/api.php',data=json.dumps({'action':'accept_link','token':match}).encode(),headers={'Content-Type':'application/json','X-CSRF-Token':b.csrf}) |
| 196 | try: |
| 197 | with b.opener.open(req) as response: |
| 198 | response.read() |
| 199 | return response.status |
| 200 | except urllib.error.HTTPError as error: |
| 201 | return error.code |
| 202 | with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool: |
| 203 | statuses=list(pool.map(race_accept,range(2))) |
| 204 | check(sorted(statuses)==[200,409],'Concurrent link acceptance starts only one game') |
| 205 | con=sqlite3.connect(root/'data/microchess.sqlite') |
| 206 | expired = a.post('create_link',kind='match')['url'].split('=')[1] |
| 207 | con.execute('UPDATE links SET expires=0 WHERE token=?',(expired,));con.commit() |
| 208 | b.post('accept_link',token=expired,expected=409) |
| 209 | # Server-side claim with an intended move: no requirement to play it first. |
| 210 | id=a.state()['games'][0]['id'];g=a.game(id) |
| 211 | raw=json.loads(con.execute('SELECT state FROM games WHERE id=?',(id,)).fetchone()[0]);raw['halfmove']=99 |
| 212 | con.execute('UPDATE games SET state=? WHERE id=?',(json.dumps(raw),id));con.commit() |
| 213 | turn=a if g['yourTurn'] else b |
| 214 | move=next(m for m in turn.game(id)['legal'] if m['from']==18) |
| 215 | turn.game_action('claim_draw',id,**move) |
| 216 | check(a.game(id)['reason']=='50-move rule','Intended move draw claim') |
| 217 | a.post('remove_friend',friend='local:test_user_2') |
| 218 | check(not a.state()['friends'] and not b.state()['friends'],'Mutual removal') |
| 219 | a.post('challenge',friend='local:test_user_2',expected=409) |
| 220 | exported=a.get('/api.php?action=export') |
| 221 | check(len(exported['results'])==3 and len(exported['games'])==2,'Export includes archived games/results') |
| 222 | check('token' not in exported['profile'],'No auth token in export') |
| 223 | check(exported['profile']['avatar']=='blue-knight','Data export includes avatar preference') |
| 224 | for path in ['/data/microchess.sqlite','/lib/bootstrap.php','/tests/chess.php','/data%2fmicrochess.sqlite','/.htaccess']: |
| 225 | try: |
| 226 | urllib.request.urlopen(base+path) |
| 227 | check(False,'Protected '+path) |
| 228 | except urllib.error.HTTPError as e: |
| 229 | check(e.code==403,'Protected '+path) |
| 230 | req=urllib.request.Request(base+'/api.php',headers={'Host':'production.example'}) |
| 231 | try: |
| 232 | urllib.request.urlopen(req) |
| 233 | check(False,'No production autologin') |
| 234 | except urllib.error.HTTPError as e: |
| 235 | check(e.code==401,'No production autologin') |
| 236 | # Inactivity is enforced on the server, with stale confirmation protection. |
| 237 | token=a.post('create_link',kind='match')['url'].split('=')[1] |
| 238 | inactive_id=b.post('accept_link',token=token)['game'] |
| 239 | a.game_action('stop_inactive',inactive_id,expected=409) |
| 240 | con.execute('UPDATE games SET activity_at=? WHERE id=?',(int(time.time())-8*86400,inactive_id));con.commit() |
| 241 | old=a.game(inactive_id) |
| 242 | check(old['inactiveDays']==8,'Eight inactive days shown') |
| 243 | turn=a if old['yourTurn'] else b |
| 244 | turn.game_action('move',inactive_id,**turn.game(inactive_id)['legal'][0]) |
| 245 | a.post('stop_inactive',game=inactive_id,version=old['version'],expected=409) |
| 246 | check(a.game(inactive_id)['inactiveDays']==0,'Move resets inactivity') |
| 247 | con.execute('UPDATE games SET activity_at=? WHERE id=?',(int(time.time())-7*86400,inactive_id));con.commit() |
| 248 | a.game_action('stop_inactive',inactive_id) |
| 249 | check(all(g['id']!=inactive_id for c in [a,b] for g in c.state()['games']),'Stop removes both tiles') |
| 250 | check(len(a.get('/api.php?action=export')['results'])==3,'Stopped game adds no result') |
| 251 | # Opponent statistics work without friendship; requests need recipient consent. |
| 252 | opponent=a.state()['games'][0] |
| 253 | check(opponent['opponentId']=='local:test_user_2' and sum(opponent['rivalry'].values())==3,'Non-friend opponent has historical rivalry statistics') |
| 254 | a.post('send_friend_request',friend='local:test_user_1',expected=409) |
| 255 | a.post('send_friend_request',friend='missing',expected=409) |
| 256 | a.post('send_friend_request',friend=['invalid'],expected=409) |
| 257 | old=a.csrf; a.csrf='wrong';a.post('send_friend_request',friend='local:test_user_2',expected=403);a.csrf=old |
| 258 | with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool: |
| 259 | list(pool.map(lambda _:a.post('send_friend_request',friend='local:test_user_2'),range(2))) |
| 260 | check(len(a.state()['friendRequests'])==len(b.state()['friendRequests'])==1,'Concurrent sends produce one request') |
| 261 | check(not a.state()['friendRequests'][0]['incoming'] and b.state()['friendRequests'][0]['incoming'],'Request direction is correct') |
| 262 | check(not a.state()['friends'] and not b.state()['friends'],'Sending request does not create friendship') |
| 263 | check(len(a.get('/api.php?action=export')['friend_requests'])==1,'Export includes pending requests') |
| 264 | a.post('accept_friend_request',friend='local:test_user_2',expected=409) |
| 265 | a.post('reject_friend_request',friend='local:test_user_2',expected=409) |
| 266 | b.post('send_friend_request',friend='local:test_user_1',expected=409) |
| 267 | b.post('reject_friend_request',friend='local:test_user_1') |
| 268 | check(not a.state()['friendRequests'] and not b.state()['friends'],'Rejection clears request without friendship') |
| 269 | a.post('send_friend_request',friend='local:test_user_2') |
| 270 | b.post('accept_friend_request',friend='local:test_user_1') |
| 271 | check(len(a.state()['friends'])==len(b.state()['friends'])==1,'Acceptance creates bilateral friendship') |
| 272 | check(not a.state()['friendRequests'] and not b.state()['friendRequests'],'Acceptance consumes request for both users') |
| 273 | check({k:a.state()['friends'][0][k] for k in ['wins','losses','ties']}==opponent['rivalry'],'Friend and opponent statistics match') |
| 274 | b.post('accept_friend_request',friend='local:test_user_1',expected=409) |
| 275 | a.post('send_friend_request',friend='local:test_user_2',expected=409) |
| 276 | a.post('remove_friend',friend='local:test_user_2') |
| 277 | a.post('send_friend_request',friend='local:test_user_2') |
| 278 | token=b.post('create_link',kind='friend')['url'].split('=')[1] |
| 279 | a.post('accept_link',token=token) |
| 280 | check(not a.state()['friendRequests'],'Friend link acceptance clears pending request') |
| 281 | a.post('remove_friend',friend='local:test_user_2') |
| 282 | # Queue requests survive sessions and never match the same user to themselves. |
| 283 | before_count=con.execute('SELECT count(*) FROM games').fetchone()[0] |
| 284 | a.post('join_matchmaking'); a.post('join_matchmaking') |
| 285 | check(con.execute('SELECT count(*) FROM matchmaking').fetchone()[0]==1,'One queue entry per user') |
| 286 | check(a.state()['matchmaking']['status']=='searching','Search persists on polling') |
| 287 | check(con.execute('SELECT count(*) FROM games').fetchone()[0]==before_count,'No self match') |
| 288 | a.post('cancel_matchmaking'); a.post('cancel_matchmaking') |
| 289 | check(a.state()['matchmaking'] is None,'Cancel is persistent and idempotent') |
| 290 | # Simultaneous joins produce one bilateral game, even without friendship. |
| 291 | with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool: |
| 292 | list(pool.map(lambda c:c.post('join_matchmaking'),[a,b])) |
| 293 | check(con.execute('SELECT count(*) FROM games').fetchone()[0]==before_count+1,'Concurrent joins create exactly one game') |
| 294 | check(a.state()['matchmaking'] is None and b.state()['matchmaking'] is None,'Both entries consumed') |
| 295 | matched=a.state()['games'][0] |
| 296 | check(matched['id']==b.state()['games'][0]['id'] and matched['status']=='active','Match starts for both players') |
| 297 | check(matched['color']!=b.game(matched['id'])['color'],'Matched players have opposite colors') |
| 298 | # Set ratings and elapsed time directly rather than sleeping for minutes. |
| 299 | con.execute('UPDATE ratings SET rating=1200 WHERE user_id=?',('local:test_user_1',)) |
| 300 | con.execute('UPDATE ratings SET rating=1400 WHERE user_id=?',('local:test_user_2',)); con.commit() |
| 301 | a.post('join_matchmaking'); b.post('join_matchmaking') |
| 302 | check(a.state()['matchmaking'] and b.state()['matchmaking'],'200 point gap initially waits') |
| 303 | con.execute('UPDATE matchmaking SET joined=? WHERE user_id=?',(int(time.time())-120,'local:test_user_1')) |
| 304 | con.execute('UPDATE matchmaking_clock SET checked=0');con.commit() |
| 305 | check(a.state()['matchmaking'] is not None,'Both players must allow rating gap') |
| 306 | con.execute('UPDATE matchmaking SET joined=?',(int(time.time())-120,)) |
| 307 | con.execute('UPDATE matchmaking_clock SET checked=0');con.commit() |
| 308 | check(a.state()['matchmaking'] is None and b.state()['matchmaking'] is None,'Activity matches after two minutes of widening') |
| 309 | a.post('join_matchmaking') |
| 310 | con.execute('UPDATE matchmaking SET joined=?',(int(time.time())-86400,));con.commit() |
| 311 | b.post('join_matchmaking') |
| 312 | check(a.state()['matchmaking']['status']=='expired','Expired registration is excluded') |
| 313 | a.post('join_matchmaking') |
| 314 | check(a.state()['matchmaking']['status']=='searching','Expired search can restart') |
| 315 | con.execute('UPDATE ratings SET rating=1800 WHERE user_id=?',('local:test_user_2',)) |
| 316 | con.execute('UPDATE matchmaking SET joined=?',(int(time.time())-3600,)) |
| 317 | con.execute('UPDATE matchmaking_clock SET checked=0');con.commit() |
| 318 | check(a.state()['matchmaking'] is not None,'Rating tolerance capped at 500') |
| 319 | a.post('cancel_matchmaking');b.post('cancel_matchmaking') |
| 320 | # An unrelated user's page activity processes the entire pool, oldest first, |
| 321 | # choosing the closest eligible opponent and leaving unmatched entries intact. |
| 322 | now=int(time.time()) |
| 323 | for uid,rating,joined in [('queue:oldest',1200,now-300),('queue:far',1290,now-200),('queue:near',1220,now-100)]: |
| 324 | con.execute('INSERT INTO users VALUES(?,?)',(uid,uid)) |
| 325 | con.execute('INSERT INTO ratings VALUES(?,?,0)',(uid,rating)) |
| 326 | con.execute('INSERT INTO matchmaking VALUES(?,?)',(uid,joined)) |
| 327 | con.execute('UPDATE matchmaking_clock SET checked=0');con.commit() |
| 328 | a.state() |
| 329 | pair=con.execute('SELECT a,b FROM games ORDER BY id DESC LIMIT 1').fetchone() |
| 330 | check(set(pair)=={'queue:oldest','queue:near'},'Whole pool prefers closest opponent for oldest entry') |
| 331 | check(con.execute('SELECT user_id FROM matchmaking').fetchall()==[('queue:far',)],'Unmatched entry remains queued') |
| 332 | check(con.execute('SELECT count(*) FROM rating_changes').fetchone()[0]==3,'Every completed result has one rating ledger entry') |
| 333 | con.close() |
| 334 | with sqlite3.connect(root/'data/microchess.sqlite') as archive_db: |
| 335 | private_id=archive_db.execute("INSERT INTO results(game_id,a,b,winner,reason,state,ended) SELECT game_id,'queue:oldest','queue:near',NULL,'Draw','{}',ended FROM results WHERE id=?",(saved_id,)).lastrowid |
| 336 | archive_db.execute("INSERT INTO replays SELECT ?,'queue:oldest',moves FROM replays WHERE result_id=?",(private_id,saved_id)) |
| 337 | outsider_token=os.urandom(24).hex() |
| 338 | archive_db.execute('INSERT INTO replay_links(result_id,token) VALUES(?,?)',(private_id,outsider_token)) |
| 339 | check(all(r['id']!=private_id for r in a.get('/api.php?action=history')),'History excludes other players games') |
| 340 | check(a.get('/api.php?action=shared_replay&token='+outsider_token)['shared'] is True,'Signed-in outsider can view another pair\'s shared replay') |
| 341 | try: |
| 342 | a.get(f'/api.php?action=replay&id={private_id}') |
| 343 | check(False,'Other players replay unavailable') |
| 344 | except urllib.error.HTTPError as e: |
| 345 | check(e.code==404,'Other players replay unavailable') |
| 346 | with sqlite3.connect(root/'data/microchess.sqlite') as archive_db: |
| 347 | check(archive_db.execute('SELECT length(moves) FROM replays WHERE result_id=?',(saved_id,)).fetchone()[0]==23,'One move uses two bytes plus initial position') |
| 348 | archive_db.execute('UPDATE results SET ended=? WHERE id=?',(int(time.time())-10*365*86400,saved_id)) |
| 349 | check(any(r['id']==saved_id for r in a.get('/api.php?action=history')),'Old replay remains in history without a retention limit') |
| 350 | check(a.get(f'/api.php?action=replay&id={saved_id}')['positions']==saved['positions'],'Old replay remains fully playable') |
| 351 | check(any(r['id']==saved_id for r in a.get('/api.php?action=export')['replays']),'Old replay remains available in exports') |
| 352 | print(f'API: {checks} assertions passed.') |
| 353 | finally: |
| 354 | os.killpg(process.pid,signal.SIGTERM);process.wait();log.close() |
projects-timfalken-com microchess