feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
|
'use strict';
|
|
|
|
|
|
const { test, before, after } = require('node:test');
|
|
|
|
|
|
const assert = require('node:assert/strict');
|
|
|
|
|
|
const { startServer } = require('./helpers');
|
|
|
|
|
|
const RC = globalThis.RC;
|
|
|
|
|
|
|
|
|
|
|
|
let S;
|
|
|
|
|
|
before(async () => { S = await startServer(); });
|
|
|
|
|
|
after(() => S.close());
|
|
|
|
|
|
|
|
|
|
|
|
const part = slot => Object.values(RC.PARTS).find(p => p.slot === slot && !Object.values(RC.STARTER).includes(p.id));
|
|
|
|
|
|
|
|
|
|
|
|
test('online save needs a login', async () => {
|
|
|
|
|
|
assert.equal((await S.json('GET', '/api/online')).status, 401);
|
|
|
|
|
|
assert.equal((await S.json('POST', '/api/online/action', { type: 'buy', part: 'x' })).status, 401);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('shop actions run on the server and persist', async () => {
|
|
|
|
|
|
const a = await S.account();
|
|
|
|
|
|
const p = part('armour');
|
|
|
|
|
|
const r = await S.json('POST', '/api/online/action', { type: 'buy', part: p.id }, a.idToken);
|
|
|
|
|
|
assert.equal(r.status, 200);
|
|
|
|
|
|
assert.equal(r.body.ok, true);
|
|
|
|
|
|
assert.equal(r.body.save.money, 800 - p.cost);
|
|
|
|
|
|
const again = await S.json('GET', '/api/online', undefined, a.idToken);
|
|
|
|
|
|
assert.equal(again.body.money, 800 - p.cost, 'stored');
|
|
|
|
|
|
assert.equal(again.body.build.armour, p.id);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('refused actions return the authoritative save and change nothing', async () => {
|
|
|
|
|
|
const a = await S.account();
|
|
|
|
|
|
const expensive = Object.values(RC.PARTS).filter(p => p.slot !== 'weapon').sort((x, y) => y.cost - x.cost)[0];
|
|
|
|
|
|
const r = await S.json('POST', '/api/online/action', { type: 'buy', part: expensive.id }, a.idToken);
|
|
|
|
|
|
assert.equal(r.body.ok, false);
|
|
|
|
|
|
assert.equal(r.body.error, 'NO_MONEY');
|
|
|
|
|
|
assert.equal(r.body.save.money, 800);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('dev coins only for admins', async () => {
|
|
|
|
|
|
const a = await S.account();
|
|
|
|
|
|
const r = await S.json('POST', '/api/online/action', { type: 'devCoins', money: 99999 }, a.idToken);
|
|
|
|
|
|
assert.equal(r.body.error, 'DENIED');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('cosmetics merge but money in the upload is ignored', async () => {
|
|
|
|
|
|
const a = await S.account();
|
|
|
|
|
|
const r = await S.json('PUT', '/api/online/save', { money: 1e9, fame: 1e9, flag: 'AT', robots: [{ name: 'Rusty', paint: { body: 0x00ff00, accent: 0, decal: null } }] }, a.idToken);
|
|
|
|
|
|
assert.equal(r.status, 200);
|
|
|
|
|
|
assert.equal(r.body.money, 800);
|
|
|
|
|
|
assert.equal(r.body.fame, 0);
|
|
|
|
|
|
assert.equal(r.body.flag, 'AT');
|
|
|
|
|
|
assert.equal(r.body.robotName, 'Rusty');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('ranking is public and only lists players with fame', async () => {
|
|
|
|
|
|
const r = await S.json('GET', '/api/ranks');
|
|
|
|
|
|
assert.equal(r.status, 200);
|
|
|
|
|
|
assert.ok(Array.isArray(r.body));
|
|
|
|
|
|
assert.ok(r.body.every(x => x.fame > 0));
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('live settings: public read, admin-only write', async () => {
|
|
|
|
|
|
const a = await S.account();
|
|
|
|
|
|
assert.equal((await S.json('PUT', '/api/admin/live', { motd: { en: 'hi' } }, a.idToken)).status, 403);
|
|
|
|
|
|
assert.equal((await S.json('GET', '/api/live')).status, 200);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('campaign cloud save round trip', async () => {
|
|
|
|
|
|
const a = await S.account();
|
|
|
|
|
|
await S.json('PUT', '/api/campaign', { money: 5, ts: 1 }, a.idToken);
|
|
|
|
|
|
const r = await S.json('GET', '/api/campaign', undefined, a.idToken);
|
|
|
|
|
|
assert.deepEqual(r.body, { money: 5, ts: 1 });
|
|
|
|
|
|
});
|
2026-11-19 02:11:13 +01:00
|
|
|
|
|
|
|
|
|
|
test('admin event presets: admin only; admin page is served', async () => {
|
|
|
|
|
|
const a = await S.account();
|
|
|
|
|
|
assert.equal((await S.json('GET', '/api/admin/events', undefined, a.idToken)).status, 403);
|
|
|
|
|
|
assert.equal((await S.json('POST', '/api/admin/events', { name: 'x' }, a.idToken)).status, 403);
|
|
|
|
|
|
const page = await fetch(S.base + '/admin/');
|
|
|
|
|
|
assert.equal(page.status, 200);
|
|
|
|
|
|
assert.match(await page.text(), /Robot Clash – Admin/);
|
|
|
|
|
|
});
|