| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | // php server/tests/run.php |
| 4 | define('METEORS_NO_SESSION', true); |
| 5 | require dirname(__DIR__) . '/lib/bootstrap.php'; |
| 6 | require dirname(__DIR__) . '/lib/Store.php'; |
| 7 | |
| 8 | $failed = 0; |
| 9 | $passed = 0; |
| 10 | |
| 11 | function expect(bool $cond, string $msg): void |
| 12 | { |
| 13 | global $failed, $passed; |
| 14 | if ($cond) { |
| 15 | $passed++; |
| 16 | echo " ok $msg\n"; |
| 17 | } else { |
| 18 | $failed++; |
| 19 | echo " FAIL $msg\n"; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | function almostEqual(float $a, float $b, float $eps = 1e-6): bool |
| 24 | { |
| 25 | return abs($a - $b) <= $eps; |
| 26 | } |
| 27 | |
| 28 | $tmp = sys_get_temp_dir() . '/meteors-test-' . bin2hex(random_bytes(6)); |
| 29 | mkdir($tmp, 0775, true); |
| 30 | putenv('METEORS_DATA_DIR=' . $tmp); |
| 31 | |
| 32 | echo "Store: lobby and game\n"; |
| 33 | $store = Store::open($tmp); |
| 34 | |
| 35 | $a = $store->createLobby('local:test_user_1', 'test_user_1', 0.30); |
| 36 | expect(isset($a['id'], $a['code']), 'create lobby returns id and code'); |
| 37 | expect($a['status'] === 'waiting', 'new lobby is waiting'); |
| 38 | expect(strlen($a['code']) === 4, 'code is 4 characters'); |
| 39 | expect(almostEqual((float) $a['miss_chance'], 0.30), 'default miss chance 0.30'); |
| 40 | |
| 41 | $view = $store->viewFor('local:test_user_1'); |
| 42 | expect($view['lobby']['players'][0]['is_host'] === true, 'creator is host'); |
| 43 | expect($view['lobby']['can_start'] === false, 'cannot start alone'); |
| 44 | |
| 45 | try { |
| 46 | $store->startGame('local:test_user_1'); |
| 47 | expect(false, 'start alone should throw'); |
| 48 | } catch (RuntimeException $e) { |
| 49 | expect(str_contains($e->getMessage(), 'twee'), 'start alone is refused: ' . $e->getMessage()); |
| 50 | } |
| 51 | |
| 52 | $joined = $store->joinLobby('local:test_user_2', 'test_user_2', $a['code']); |
| 53 | expect($joined['id'] === $a['id'], 'second player joins by code'); |
| 54 | $view = $store->viewFor('local:test_user_1'); |
| 55 | expect(count($view['lobby']['players']) === 2, 'two players in lobby'); |
| 56 | expect($view['lobby']['can_start'] === false, 'cannot start until both ready'); |
| 57 | |
| 58 | $store->setReady('local:test_user_1', true); |
| 59 | $store->setReady('local:test_user_2', true); |
| 60 | $view = $store->viewFor('local:test_user_1'); |
| 61 | expect($view['lobby']['all_ready'] === true, 'all ready'); |
| 62 | expect($view['lobby']['can_start'] === true, 'host can start when all ready and 2+'); |
| 63 | |
| 64 | $view2 = $store->viewFor('local:test_user_2'); |
| 65 | expect($view2['lobby']['can_start'] === false, 'non-host cannot start'); |
| 66 | |
| 67 | try { |
| 68 | $store->startGame('local:test_user_2'); |
| 69 | expect(false, 'non-host start should throw'); |
| 70 | } catch (RuntimeException $e) { |
| 71 | expect(str_contains($e->getMessage(), 'host'), 'non-host start refused'); |
| 72 | } |
| 73 | |
| 74 | $started = $store->startGame('local:test_user_1'); |
| 75 | expect($started['status'] === 'playing', 'game starts'); |
| 76 | |
| 77 | // Shots: force miss by using miss_chance 1.0 on a fresh lobby later. |
| 78 | // Here miss is 0.30 — fire many shots and check some hits and some misses, or set chance. |
| 79 | $results = $store->recordShots('local:test_user_1', [ |
| 80 | ['client_id' => 'c1', 'color' => 2], |
| 81 | ['client_id' => 'c2', 'color' => -1], |
| 82 | ]); |
| 83 | expect(count($results) === 2, 'two shot results'); |
| 84 | expect(isset($results[0]['miss'], $results[0]['flight_duration']), 'result has miss and duration'); |
| 85 | expect(almostEqual($results[0]['flight_duration'], METEORS_FLIGHT_DURATION), 'flight duration 1.2'); |
| 86 | |
| 87 | // Incoming is delayed until deliver_at. |
| 88 | $inc = $store->viewFor('local:test_user_2'); |
| 89 | $hits = array_filter($results, fn ($r) => !$r['miss']); |
| 90 | if ($hits) { |
| 91 | expect(count($inc['incoming']) === 0, 'incoming not delivered before flight duration'); |
| 92 | } else { |
| 93 | expect(true, 'all missed — no incoming expected'); |
| 94 | } |
| 95 | |
| 96 | // Fast-forward: rewrite deliver_at in the sqlite file. |
| 97 | $pdo = new PDO('sqlite:' . $tmp . '/meteors.sqlite'); |
| 98 | $pdo->exec('UPDATE shots SET deliver_at = 0 WHERE to_user = "local:test_user_2"'); |
| 99 | $inc = $store->viewFor('local:test_user_2'); |
| 100 | $expectedHits = count($hits); |
| 101 | expect(count($inc['incoming']) === $expectedHits, 'incoming delivered after deliver_at (' . count($inc['incoming']) . " vs $expectedHits)"); |
| 102 | $inc2 = $store->viewFor('local:test_user_2'); |
| 103 | expect(count($inc2['incoming']) === 0, 'incoming is delivered only once'); |
| 104 | |
| 105 | // Elimination and podium. |
| 106 | $store->updateSnapshot('local:test_user_2', ['score' => 3, 'blocks' => []], 3, true); |
| 107 | $view = $store->viewFor('local:test_user_1'); |
| 108 | expect($view['lobby']['status'] === 'finished', 'game finishes when one remains'); |
| 109 | $ranks = $view['lobby']['rankings']; |
| 110 | expect(count($ranks) >= 2, 'rankings present'); |
| 111 | $first = $ranks[0]; |
| 112 | expect($first['place'] === 1 && $first['id'] === 'local:test_user_1', 'survivor is 1st'); |
| 113 | $second = $ranks[1]; |
| 114 | expect($second['place'] === 2 && $second['id'] === 'local:test_user_2', 'eliminated is 2nd'); |
| 115 | |
| 116 | // Podium wait: not yet reset. |
| 117 | $view = $store->viewFor('local:test_user_1'); |
| 118 | expect($view['lobby']['status'] === 'finished', 'still finished during podium'); |
| 119 | |
| 120 | $pdo->exec('UPDATE lobbies SET finished = ' . (time() - 6)); |
| 121 | $view = $store->viewFor('local:test_user_1'); |
| 122 | expect($view['lobby']['status'] === 'waiting', 'back to lobby after podium seconds'); |
| 123 | expect($view['lobby']['players'][0]['ready'] === false, 'ready flags reset'); |
| 124 | |
| 125 | // Host leave transfers host. |
| 126 | $store->leaveLobby('local:test_user_1'); |
| 127 | $view = $store->viewFor('local:test_user_2'); |
| 128 | expect($view['lobby']['host_id'] === 'local:test_user_2', 'host transfers to remaining player'); |
| 129 | |
| 130 | // Mid-game leave counts as elimination. |
| 131 | $store->leaveLobby('local:test_user_2'); |
| 132 | $b = $store->createLobby('local:test_user_1', 'test_user_1', 0.0); |
| 133 | $store->joinLobby('local:test_user_2', 'test_user_2', $b['code']); |
| 134 | $store->setReady('local:test_user_1', true); |
| 135 | $store->setReady('local:test_user_2', true); |
| 136 | $store->startGame('local:test_user_1'); |
| 137 | $store->leaveLobby('local:test_user_2'); |
| 138 | $view = $store->viewFor('local:test_user_1'); |
| 139 | expect($view['lobby']['status'] === 'finished', 'leave mid-game finishes if one remains'); |
| 140 | expect($view['lobby']['rankings'][0]['id'] === 'local:test_user_1', 'stayer wins'); |
| 141 | |
| 142 | // Miss chance 1.0: all miss. Need a fresh game with 2 players. |
| 143 | // Leave leftover memberships. |
| 144 | $store->leaveLobby('local:test_user_1'); |
| 145 | $c = $store->createLobby('local:test_user_1', 'test_user_1', 1.0); |
| 146 | $store->joinLobby('local:test_user_2', 'test_user_2', $c['code']); |
| 147 | $store->setReady('local:test_user_1', true); |
| 148 | $store->setReady('local:test_user_2', true); |
| 149 | $store->startGame('local:test_user_1'); |
| 150 | $allMiss = $store->recordShots('local:test_user_1', [ |
| 151 | ['client_id' => 'm1', 'color' => 0], |
| 152 | ['client_id' => 'm2', 'color' => 1], |
| 153 | ['client_id' => 'm3', 'color' => 2], |
| 154 | ]); |
| 155 | expect(count($allMiss) === 3 && array_reduce($allMiss, fn ($ok, $r) => $ok && $r['miss'], true), 'miss_chance 1.0 → all miss'); |
| 156 | |
| 157 | $d = null; |
| 158 | $store->leaveLobby('local:test_user_1'); |
| 159 | $store->leaveLobby('local:test_user_2'); |
| 160 | $d = $store->createLobby('local:test_user_1', 'test_user_1', 0.0); |
| 161 | $store->joinLobby('local:test_user_2', 'test_user_2', $d['code']); |
| 162 | $store->setReady('local:test_user_1', true); |
| 163 | $store->setReady('local:test_user_2', true); |
| 164 | $store->startGame('local:test_user_1'); |
| 165 | $allHit = $store->recordShots('local:test_user_1', [ |
| 166 | ['client_id' => 'h1', 'color' => 0], |
| 167 | ['client_id' => 'h2', 'color' => 1], |
| 168 | ]); |
| 169 | expect(count($allHit) === 2 && array_reduce($allHit, fn ($ok, $r) => $ok && !$r['miss'] && $r['target_id'] === 'local:test_user_2', true), 'miss_chance 0 → all hit the other player'); |
| 170 | |
| 171 | // Eliminated player is not a target. |
| 172 | $store->updateSnapshot('local:test_user_2', null, 0, true); |
| 173 | $none = $store->recordShots('local:test_user_1', [['client_id' => 'x', 'color' => 0]]); |
| 174 | // Game may already be finished; recordShots on finished returns []. |
| 175 | expect($none === [] || $none[0]['miss'] === true, 'no living target after opponent is out'); |
| 176 | |
| 177 | echo "\nConfig helpers\n"; |
| 178 | $cfg = config(); |
| 179 | expect(isset($cfg['client_id'], $cfg['client_secret']), 'config() creates a file'); |
| 180 | expect(is_file($tmp . '/config.php'), 'config.php written to the data dir'); |
| 181 | expect($cfg['client_secret'] === '', 'secret starts empty (Tim must fill it in)'); |
| 182 | expect(authConfigured() === false, 'auth is unconfigured without a secret'); |
| 183 | putenv('METEORS_CLIENT_SECRET=test-secret-value'); |
| 184 | // config file empty secret falls back to env |
| 185 | $cfg2 = config(); |
| 186 | expect($cfg2['client_secret'] === 'test-secret-value', 'empty file field falls back to env'); |
| 187 | putenv('METEORS_CLIENT_SECRET'); |
| 188 | |
| 189 | echo "\n$passed passed, $failed failed\n"; |
| 190 | |
| 191 | // cleanup |
| 192 | $files = new RecursiveIteratorIterator( |
| 193 | new RecursiveDirectoryIterator($tmp, FilesystemIterator::SKIP_DOTS), |
| 194 | RecursiveIteratorIterator::CHILD_FIRST |
| 195 | ); |
| 196 | foreach ($files as $f) { |
| 197 | $f->isDir() ? @rmdir($f->getPathname()) : @unlink($f->getPathname()); |
| 198 | } |
| 199 | @rmdir($tmp); |
| 200 | |
| 201 | exit($failed === 0 ? 0 : 1); |
projects-timfalken-com meteors