feat(tools): dumb test bot that accepts challenges and drives a pattern (npm run bot) (P4-1)

This commit is contained in:
Benjamin Oezcan
2026-11-19 14:26:50 +01:00
parent 593d69714b
commit a8d6158384
3 changed files with 178 additions and 1 deletions

40
server/test/bot.test.js Normal file
View File

@@ -0,0 +1,40 @@
'use strict';
const { test, before, after } = require('node:test');
const assert = require('node:assert/strict');
const { startServer, fastForward } = require('./helpers');
const { startBot, PATTERNS } = require('../../tools/bot');
const RC = globalThis.RC;
let S;
before(async () => { S = await startServer(); });
after(() => S.close());
test('every bot pattern gives valid inputs', () => {
for (const [name, f] of Object.entries(PATTERNS)) for (const t of [0, 1.3, 7.9]) {
const i = RC.Proto.input(f(t));
assert.ok(RC.Proto.check(Object.assign({ t: 'input', seq: 1 }, i)).ok, name);
}
});
test('the bot accepts a challenge, drives its pattern, and deletes itself on stop', async () => {
const bot = await startBot({ url: S.base, pattern: 'circle', quiet: true });
const me = await S.account();
const c = await S.client(me.idToken);
await c.next('lobby.join', m => m.player.uid === bot.uid).catch(() => {});
c.send({ t: 'challenge', to: bot.uid });
const st = await c.next('match.start', () => true, 5000);
assert.equal(st.robots[1 - st.seat].uid, bot.uid);
// let the real 60 Hz runner play a bit after the countdown: the bot's robot must move
const room = S.runner.get(st.matchId);
const botSeat = 1 - st.seat, x0 = room.s.robots[botSeat].x, a0 = room.s.robots[botSeat].a;
await new Promise(r => setTimeout(r, (st.countdown + 1.2) * 1000));
const moved = Math.abs(room.s.robots[botSeat].x - x0) + Math.abs(room.s.robots[botSeat].a - a0);
assert.ok(moved > 0.1, 'bot robot moves (circle pattern)');
fastForward(S.runner);
await c.next('match.end', () => true, 5000);
await bot.stop();
assert.ok(!(await S.json('GET', '/api/ranks')).body.some(r => r.uid === bot.uid), 'bot account is gone');
c.close();
});