#!/usr/bin/env node /* Smoke test against a running server (after every deploy): node tools/smoke.js https://roboclash.michael-media.de node tools/smoke.js http://localhost:8080 Two throw-away guest accounts: health -> login -> WebSocket -> lobby -> challenge -> accept -> snapshots -> one gives up -> both get the result + rewards. Exit code 0 = everything worked. */ 'use strict'; const path = require('path'); const WebSocket = require(path.join(__dirname, '..', 'server', 'node_modules', 'ws')); require(path.join(__dirname, '..', 'game', 'protocol.js')); const RC = globalThis.RC; const base = (process.argv[2] || 'http://localhost:8080').replace(/\/+$/, ''); const wsBase = base.replace(/^http/, 'ws'); let failed = 0; const ok = (c, msg) => { console.log((c ? 'ok ' : 'FAIL ') + msg); if (!c) failed++; }; async function json(method, p, body, token) { const r = await fetch(base + p, { method, headers: Object.assign({ 'Content-Type': 'application/json' }, token ? { Authorization: 'Bearer ' + token } : {}), body: body && JSON.stringify(body) }); return { status: r.status, body: await r.json().catch(() => null) }; } function client(token) { return new Promise((resolve, reject) => { const ws = new WebSocket(wsBase + '/ws'), inbox = [], waiters = []; const c = { send: m => ws.send(JSON.stringify(m)), close: () => ws.close(), inbox, next(t, pred = () => true, ms = 8000) { const i = inbox.findIndex(m => m.t === t && pred(m)); if (i >= 0) return Promise.resolve(inbox.splice(i, 1)[0]); return new Promise((res, rej) => waiters.push({ t, pred, res, timer: setTimeout(() => rej(new Error('timeout: ' + t)), ms) })); }, }; ws.on('message', d => { const m = JSON.parse(d); const i = waiters.findIndex(w => w.t === m.t && w.pred(m)); if (i >= 0) { const w = waiters.splice(i, 1)[0]; clearTimeout(w.timer); w.res(m); } else inbox.push(m); }); ws.on('open', () => { c.send({ t: 'hello', v: RC.Proto.VERSION, token }); c.next('welcome').then(() => resolve(c), reject); }); ws.on('error', reject); }); } (async () => { const h = await json('GET', '/healthz'); ok(h.status === 200 && h.body.ok, `health ${base} (version ${h.body && h.body.version}, commit ${h.body && h.body.commit})`); const [a, b] = await Promise.all([json('POST', '/api/auth/guest', { name: 'SmokeA' }), json('POST', '/api/auth/guest', { name: 'SmokeB' })]); ok(a.status === 200 && b.status === 200, 'two guest logins'); const ca = await client(a.body.idToken), cb = await client(b.body.idToken); ok(true, 'WebSocket connected + authenticated (both)'); await cb.next('lobby'); ca.send({ t: 'challenge', to: b.body.uid }); await cb.next('challenge.in'); cb.send({ t: 'challenge.accept', from: a.body.uid }); const [sa, sb] = await Promise.all([ca.next('match.start'), cb.next('match.start')]); ok(sa.matchId === sb.matchId && sa.seat !== sb.seat, `match ${sa.matchId} in arena ${sa.arena}`); const t0 = Date.now(); await ca.next('snap', () => true, 10000); ok(true, `first snapshot after ${Date.now() - t0} ms (countdown ${sa.countdown}s)`); let n = 0; const t1 = Date.now(); while (Date.now() - t1 < 2000) { await ca.next('snap', () => true, 3000); n++; } ok(n >= 25, `${n} snapshots in 2 s (expected ~40)`); ca.send({ t: 'match.leave' }); const [ea, eb] = await Promise.all([ca.next('match.end'), cb.next('match.end')]); const la = RC.Proto.localResult(ea.seat, ea.result).winner, lb = RC.Proto.localResult(eb.seat, eb.result).winner; ok(la === 1 && lb === 0, `result mirrored: leaver lost, other won (+${eb.reward.money} credits, +${eb.reward.fame} fame)`); ca.close(); cb.close(); // clean up: the throw-away accounts must not stay in the ranking const del = await Promise.all([json('DELETE', '/api/me', undefined, a.body.idToken), json('DELETE', '/api/me', undefined, b.body.idToken)]); ok(del.every(d => d.body && d.body.ok), 'test accounts deleted again'); console.log(failed ? `\n${failed} check(s) failed` : '\nsmoke test passed'); process.exit(failed ? 1 : 0); })().catch(e => { console.error('FAIL', e.message); process.exit(1); });