After a database reset an old (still signed) token made economy.load insert a save for a missing user; the FOREIGN KEY error escaped the WebSocket handler and took the process down in a restart loop.
164 lines
7.2 KiB
JavaScript
164 lines
7.2 KiB
JavaScript
'use strict';
|
|
const { test, before, after } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { startServer, fastForward } = require('./helpers');
|
|
const RC = globalThis.RC;
|
|
|
|
let S;
|
|
before(async () => { S = await startServer(); });
|
|
after(() => S.close());
|
|
|
|
test('socket must authenticate with hello first', async () => {
|
|
const c = await S.client(null, { noHello: true });
|
|
c.send({ t: 'room', room: 'DE' });
|
|
assert.equal((await c.closed).code, 4001);
|
|
const bad = await S.client('not-a-token', { expectFail: true });
|
|
assert.equal((await bad.next('error')).code, 'UNAUTH');
|
|
assert.equal((await bad.closed).code, 4003);
|
|
});
|
|
|
|
test('a token of a deleted account is refused - and the server keeps running', async () => {
|
|
const a = await S.account();
|
|
await S.json('DELETE', '/api/me', undefined, a.idToken);
|
|
const c = await S.client(a.idToken, { expectFail: true });
|
|
assert.equal((await c.next('error')).code, 'UNAUTH');
|
|
assert.equal((await S.json('GET', '/api/online', undefined, a.idToken)).status, 401);
|
|
assert.equal((await S.json('GET', '/healthz')).status, 200);
|
|
});
|
|
|
|
test('a failing message handler answers with an error instead of crashing', async () => {
|
|
const a = await S.account();
|
|
const c = await S.client(a.idToken);
|
|
const orig = S.lobby.handle;
|
|
S.lobby.handle = () => { throw new Error('boom'); };
|
|
try {
|
|
c.send({ t: 'room', room: 'DE' });
|
|
assert.equal((await c.next('error')).code, 'SERVER_ERROR');
|
|
} finally { S.lobby.handle = orig; }
|
|
assert.equal((await S.json('GET', '/healthz')).status, 200);
|
|
c.close();
|
|
});
|
|
|
|
test('old protocol version is told to reload', async () => {
|
|
const a = await S.account();
|
|
const c = await S.client(a.idToken, { v: 0, expectFail: true });
|
|
assert.equal((await c.next('error')).code, 'OUTDATED');
|
|
assert.equal((await c.closed).code, 4002);
|
|
});
|
|
|
|
test('invalid messages get an error, the socket stays open', async () => {
|
|
const a = await S.account();
|
|
const c = await S.client(a.idToken);
|
|
c.send({ t: 'input', seq: 1, throttle: 'fast' });
|
|
assert.match((await c.next('error')).code, /BAD_FIELD/);
|
|
c.ws.send('{not json');
|
|
assert.equal((await c.next('error')).code, 'BAD_JSON');
|
|
c.close();
|
|
});
|
|
|
|
test('presence: join and leave are pushed to the others', async () => {
|
|
const a = await S.account('Anna'), b = await S.account('Bert');
|
|
const ca = await S.client(a.idToken);
|
|
await ca.next('lobby');
|
|
const cb = await S.client(b.idToken);
|
|
const list = await cb.next('lobby');
|
|
assert.ok(list.players.some(p => p.uid === a.uid && p.state === 'idle'));
|
|
assert.equal((await ca.next('lobby.join', m => m.player.uid === b.uid)).player.name, 'Bert');
|
|
cb.close();
|
|
assert.equal((await ca.next('lobby.leave', m => m.uid === b.uid)).uid, b.uid);
|
|
ca.close();
|
|
});
|
|
|
|
test('challenge -> decline / cancel', async () => {
|
|
const a = await S.account(), b = await S.account();
|
|
const ca = await S.client(a.idToken), cb = await S.client(b.idToken);
|
|
ca.send({ t: 'challenge', to: b.uid });
|
|
assert.equal((await cb.next('challenge.in')).from, a.uid);
|
|
cb.send({ t: 'challenge.decline', from: a.uid });
|
|
assert.equal((await ca.next('challenge.gone')).reason, 'declined');
|
|
ca.send({ t: 'challenge', to: b.uid });
|
|
await cb.next('challenge.in');
|
|
ca.send({ t: 'challenge.cancel' });
|
|
assert.equal((await cb.next('challenge.gone')).reason, 'cancelled');
|
|
ca.send({ t: 'challenge', to: a.uid });
|
|
assert.equal((await ca.next('error')).code, 'NOT_AVAILABLE', 'cannot challenge yourself');
|
|
ca.close(); cb.close();
|
|
});
|
|
|
|
test('full live match over WebSocket: both get start, snapshots, and mirrored results with rewards', async () => {
|
|
const a = await S.account(), b = await S.account();
|
|
const ca = await S.client(a.idToken), cb = await S.client(b.idToken);
|
|
ca.send({ t: 'challenge', to: b.uid });
|
|
await cb.next('challenge.in');
|
|
cb.send({ t: 'challenge.accept', from: a.uid });
|
|
const [sa, sb] = await Promise.all([ca.next('match.start'), cb.next('match.start')]);
|
|
assert.equal(sa.matchId, sb.matchId);
|
|
assert.deepEqual([sa.seat, sb.seat].sort(), [0, 1]);
|
|
assert.equal(sa.seed, sb.seed);
|
|
assert.equal(sa.arena, sb.arena);
|
|
assert.equal(sa.robots[sa.seat].uid, a.uid, 'my seat holds my robot');
|
|
|
|
// inputs flow in, then the server plays the rest at full speed
|
|
ca.send({ t: 'input', seq: 1, throttle: 1, turn: 0, fire: true, fire2: false, fire3: false });
|
|
await new Promise(r => setTimeout(r, 50));
|
|
fastForward(S.runner);
|
|
const [ea, eb] = await Promise.all([ca.next('match.end'), cb.next('match.end')]);
|
|
assert.deepEqual(ea.result, eb.result, 'same seat-based result for both');
|
|
const la = RC.Proto.localResult(ea.seat, ea.result).winner, lb = RC.Proto.localResult(eb.seat, eb.result).winner;
|
|
if (ea.result.winner === -1) assert.ok(la === -1 && lb === -1);
|
|
else assert.ok((la === 0) !== (lb === 0), 'exactly one player won');
|
|
assert.equal(ea.save.games, 1);
|
|
assert.ok(Array.isArray(ea.reward.ach), 'achievements decided on the server');
|
|
const winner = la === 0 ? ea : lb === 0 ? eb : null;
|
|
if (winner) assert.ok(winner.reward.ach.some(x => x.id === 'first_win'), 'first win pays out');
|
|
assert.equal(eb.save.games, 1);
|
|
assert.ok(ca.inbox.some(m => m.t === 'snap'), 'snapshots arrived before the end');
|
|
|
|
// rewards were booked on the server
|
|
const sA = await S.json('GET', '/api/online', undefined, a.idToken);
|
|
assert.equal(sA.body.money, ea.save.money);
|
|
ca.close(); cb.close();
|
|
});
|
|
|
|
test('forfeit ends the match at once; disconnect + reconnect gets the running match back', async () => {
|
|
const a = await S.account(), b = await S.account();
|
|
const ca = await S.client(a.idToken), cb = await S.client(b.idToken);
|
|
ca.send({ t: 'challenge', to: b.uid });
|
|
await cb.next('challenge.in');
|
|
cb.send({ t: 'challenge.accept', from: a.uid });
|
|
const st = await ca.next('match.start');
|
|
await cb.next('match.start');
|
|
S.runner.stop();
|
|
|
|
// b drops and comes back: gets match.start again for the same match and seat
|
|
cb.close(); await cb.closed;
|
|
await ca.next('lobby.leave', m => m.uid === b.uid);
|
|
const room = S.runner.get(st.matchId);
|
|
assert.ok(room && !room.connected(1 - st.seat), 'room marks the seat as away');
|
|
const cb2 = await S.client(b.idToken);
|
|
const again = await cb2.next('match.start');
|
|
assert.equal(again.matchId, st.matchId);
|
|
assert.equal(again.seat, 1 - st.seat);
|
|
assert.ok(room.connected(1 - st.seat));
|
|
|
|
ca.send({ t: 'match.leave' });
|
|
await new Promise(r => setTimeout(r, 30));
|
|
fastForward(S.runner);
|
|
const ea = await ca.next('match.end'), eb = await cb2.next('match.end');
|
|
assert.equal(RC.Proto.localResult(ea.seat, ea.result).winner, 1, 'the one who left lost');
|
|
assert.equal(RC.Proto.localResult(eb.seat, eb.result).winner, 0);
|
|
ca.close(); cb2.close();
|
|
});
|
|
|
|
test('an invalid robot cannot challenge', async () => {
|
|
const a = await S.account(), b = await S.account();
|
|
// strip the battery via the economy: equip refuses required slots, so break the save directly
|
|
const econ = require('../economy');
|
|
const s = econ.load(a.uid); s.robots[0].build.battery = null; require('../db').OnlineSaves.put(a.uid, s);
|
|
const ca = await S.client(a.idToken), cb = await S.client(b.idToken);
|
|
ca.send({ t: 'challenge', to: b.uid });
|
|
const e = await ca.next('error');
|
|
assert.equal(e.code, 'INVALID_ROBOT');
|
|
ca.close(); cb.close();
|
|
});
|