BlueGit

projects-timfalken-com allianties

Files

allianties / server / tests / run.php

run.php

run.php 8.7 KiB Download Raw
1<?php
2declare(strict_types=1);
3// php server/tests/run.php
4define('ALLIANTIES_NO_SESSION', true);
5define('ALLIANTIES_TEST_SUITE', true);
6require dirname(__DIR__) . '/lib/bootstrap.php';
7require dirname(__DIR__) . '/lib/Store.php';
8
9require __DIR__ . '/engine_test.php';
10
11$failed = (int) ($GLOBALS['engine_failed'] ?? 0);
12$passed = (int) ($GLOBALS['engine_passed'] ?? 0);
13
14function expect2(bool $cond, string $msg): void
15{
16 global $failed, $passed;
17 if ($cond) {
18 $passed++;
19 echo " ok $msg\n";
20 } else {
21 $failed++;
22 echo " FAIL $msg\n";
23 }
24}
25
26$tmp = sys_get_temp_dir() . '/allianties-test-' . bin2hex(random_bytes(6));
27mkdir($tmp, 0775, true);
28putenv('ALLIANTIES_DATA_DIR=' . $tmp);
29// localDev() looks at HTTP_HOST — not set in CLI. debug_advance checks localDev.
30$_SERVER['HTTP_HOST'] = '127.0.0.1';
31$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
32
33echo "Store: lobby, join, start, actions, sync\n";
34$store = Store::open($tmp);
35
36$a = $store->createLobby('local:test_user_1', 'Alice', 'images/avatars/test_user_1.svg', []);
37expect2(isset($a['id'], $a['code']), 'create lobby');
38expect2($a['status'] === 'lobby', 'status lobby');
39expect2(strlen($a['code']) === 6, '6-char code: ' . $a['code']);
40expect2($a['rules']['jury'] === true, 'jury on');
41expect2($a['rules']['shrinking_arena'] === false, 'shrink off');
42expect2($a['rules']['ceasefire'] === false, 'ceasefire off');
43
44try {
45 $store->startGame('local:test_user_1', $a['id']);
46 expect2(false, 'start alone');
47} catch (RuntimeException $e) {
48 expect2($e->getMessage() === 'min_players', 'start alone refused');
49}
50
51$b = $store->joinByCode('local:test_user_2', 'Bob', 'images/avatars/test_user_2.svg', $a['code']);
52expect2(count($b['players']) === 2, 'Bob joined');
53expect2($b['id'] === $a['id'], 'same lobby');
54
55// Case-insensitive join of already-present player (re-open).
56$again = $store->joinByCode('local:test_user_2', 'Bob', null, strtolower($a['code']));
57expect2(count($again['players']) === 2, 're-join is idempotent');
58
59try {
60 $store->startGame('local:test_user_2', $a['id']);
61 expect2(false, 'non-host start');
62} catch (RuntimeException $e) {
63 expect2($e->getMessage() === 'not_host', 'non-host refused');
64}
65
66// Only the host may change the rules (server-side, 403).
67$before = $store->load($a['id'])['rules'];
68try {
69 $store->setRules('local:test_user_2', $a['id'], ['jury' => false, 'ap_loot' => true]);
70 expect2(false, 'non-host setRules must throw');
71} catch (RuntimeException $e) {
72 expect2($e->getMessage() === 'not_host', 'non-host setRules refused: not_host');
73 expect2(apiHttpStatus($e->getMessage()) === 403, 'not_host maps to HTTP 403');
74}
75expect2($store->load($a['id'])['rules'] === $before, 'rules unchanged after refused non-host update');
76expect2(apiHttpStatus('login') === 401 && apiHttpStatus('csrf') === 403 && apiHttpStatus('no_ap') === 400, 'other status codes unchanged');
77
78$g = $store->setRules('local:test_user_1', $a['id'], [
79 'jury' => true,
80 'shrinking_arena' => true,
81 'shrink_interval_days' => 3,
82 'ceasefire' => true,
83 'ceasefire_ranges' => [['start' => '22:00', 'end' => '06:00']],
84]);
85expect2($g['rules']['shrinking_arena'] === true, 'host set shrink on');
86expect2($g['rules']['shrink_interval_days'] === 3, 'interval 3');
87expect2($g['rules']['ceasefire'] === true, 'ceasefire on');
88expect2(count($g['rules']['ceasefire_ranges']) === 1, 'one range stored');
89
90$g = $store->startGame('local:test_user_1', $a['id']);
91expect2($g['status'] === 'running', 'started');
92expect2($g['players'][0]['ap'] === 1, '1 AP at start');
93
94$view1 = $store->view($g, 'local:test_user_1');
95expect2($view1['status'] === 'running', 'view running');
96expect2(isset($view1['legal']['move']), 'legal moves in view');
97expect2($view1['version'] >= 1, 'has version');
98expect2(isset($view1['server_now']), 'has server_now');
99expect2($view1['timezone'] === 'Europe/Amsterdam', 'timezone');
100
101// Sync: same version → no game payload
102$sync = $store->sync('local:test_user_1', $a['id'], $view1['version']);
103expect2($sync['changed'] === false, 'unchanged sync');
104expect2(!isset($sync['game']), 'no full state when unchanged');
105expect2(isset($sync['next_distribution_at'], $sync['server_now']), 'lightweight fields present');
106
107// Alice moves
108$legal = Engine::legal($g, 'local:test_user_1', $store->now());
109expect2(!empty($legal['move']), 'Alice can move');
110$t = $legal['move'][0];
111$g = $store->act('local:test_user_1', $a['id'], ['type' => 'move', 'x' => $t['x'], 'y' => $t['y']]);
112expect2($g['players'][0]['ap'] === 0, 'AP spent');
113$view2 = $store->view($g, 'local:test_user_1');
114$sync = $store->sync('local:test_user_2', $a['id'], $view1['version']);
115expect2($sync['changed'] === true, 'Bob sees a change');
116expect2(isset($sync['game']['events']), 'full state on change');
117expect2($sync['game']['players'][0]['x'] === $t['x'], 'Bob sees Alice’s new position');
118
119// Notes privacy through present
120$g = $store->setNote('local:test_user_1', $a['id'], 'local:test_user_2', 'suspicious');
121$va = $store->view($g, 'local:test_user_1');
122$vb = $store->view($g, 'local:test_user_2');
123$noteA = '';
124$noteB = '';
125foreach ($va['players'] as $p) {
126 if ($p['id'] === 'local:test_user_2') {
127 $noteA = $p['note'];
128 }
129}
130foreach ($vb['players'] as $p) {
131 if ($p['id'] === 'local:test_user_2') {
132 $noteB = $p['note'];
133 }
134}
135expect2($noteA === 'suspicious', 'Alice sees her note');
136expect2($noteB === '', 'Bob does not');
137
138$g = $store->addMessage('local:test_user_1', $a['id'], 'local:test_user_2', 'hi bob');
139$va = $store->view($g, 'local:test_user_1');
140$vb = $store->view($g, 'local:test_user_2');
141expect2(count($va['messages']) === 1, 'Alice sees message');
142expect2(count($vb['messages']) === 1 && $vb['messages'][0]['text'] === 'hi bob', 'Bob sees message');
143
144// Third player cannot join after start
145try {
146 $store->joinByCode('local:test_user_3', 'Cara', null, $a['code']);
147 expect2(false, 'join after start');
148} catch (RuntimeException $e) {
149 expect2($e->getMessage() === 'game_started', 'join after start refused');
150}
151
152// Locale
153$loc = $store->setLocale('local:test_user_1', 'nl');
154expect2($loc === 'nl', 'locale stored');
155expect2($store->locale('local:test_user_1') === 'nl', 'locale read');
156$loc = $store->setLocale('local:test_user_1', 'zh-Hans');
157expect2($loc === 'zh', 'zh-Hans normalised to zh');
158
159// List
160$list = $store->listFor('local:test_user_1');
161expect2(count($list) === 1 && $list[0]['status'] === 'running', 'listed running game');
162
163// Concurrent-ish: two IMMEDIATE transactions in sequence (same process)
164$g = $store->createLobby('local:test_user_3', 'Cara', null, ['jury' => false]);
165$store->joinByCode('local:test_user_4', 'Dan', null, $g['code']);
166$g = $store->startGame('local:test_user_3', $g['id']);
167$store->act('local:test_user_3', $g['id'], ['type' => 'surrender']);
168$g = $store->present('local:test_user_4', $g['id']);
169expect2($g['status'] === 'finished' && $g['winner_id'] === 'local:test_user_4', 'Dan wins after Cara surrenders');
170
171// Unknown code
172try {
173 $store->joinByCode('local:test_user_1', 'Alice', null, 'XXXXXX');
174 expect2(false, 'unknown code');
175} catch (RuntimeException $e) {
176 expect2($e->getMessage() === 'unknown_code', 'unknown_code');
177}
178
179// Anti-race: nested mutate is fine (txDepth); invalid action rolls back AP
180$lobby = $store->createLobby('local:test_user_5', 'Eve', null, []);
181$store->joinByCode('local:test_user_6', 'Fay', null, $lobby['code']);
182$g = $store->startGame('local:test_user_5', $lobby['id']);
183$apBefore = $g['players'][0]['ap'];
184try {
185 $store->act('local:test_user_5', $lobby['id'], ['type' => 'shoot', 'target' => 'nobody']);
186 expect2(false, 'bad shoot');
187} catch (RuntimeException $e) {
188 expect2($e->getMessage() === 'out_of_range', 'bad shoot refused: ' . $e->getMessage());
189}
190$g = $store->load($lobby['id']);
191expect2((int) $g['players'][0]['ap'] === $apBefore, 'AP unchanged after refused action (rollback)');
192
193
194echo "safeNext\n";
195expect2(safeNext(null) === null, 'null next');
196expect2(safeNext('') === null, 'empty next');
197expect2(safeNext('index.html') === 'index.html', 'plain path');
198expect2(safeNext('index.html?join=AQBYH9') === 'index.html?join=AQBYH9', 'join query with =');
199expect2(safeNext('index.html?join=AQBYH9&from=invite') === 'index.html?join=AQBYH9&from=invite', 'ampersand');
200expect2(safeNext('index.html?join=ABC#frag') === 'index.html?join=ABC#frag', 'hash fragment');
201expect2(safeNext('https://evil.example') === null, 'rejects scheme');
202expect2(safeNext('//evil.example') === null, 'rejects protocol-relative');
203expect2(safeNext('/etc/passwd') === null, 'rejects absolute path');
204expect2(safeNext('index.html\\x') === null, 'rejects backslash');
205
206echo "\nTotal passed: $passed Failed: $failed\n";
207
208// Cleanup
209$files = glob($tmp . '/*');
210if (is_array($files)) {
211 foreach ($files as $f) {
212 @unlink($f);
213 }
214}
215@rmdir($tmp);
216
217exit($failed > 0 ? 1 : 0);