'use strict'; const { test } = require('node:test'); const assert = require('node:assert/strict'); const path = require('path'); for (const f of ['parts.js', 'robot.js', 'combat.js', 'ai.js', 'protocol.js']) require(path.join(__dirname, '..', 'game', f)); const RC = globalThis.RC; const P = RC.Proto; test('check accepts valid client messages', () => { assert.ok(P.check({ t: 'hello', v: 1, token: 'abc' }).ok); assert.ok(P.check({ t: 'challenge', to: 'u1' }).ok); assert.ok(P.check({ t: 'challenge.cancel' }).ok); assert.ok(P.check({ t: 'room', room: 'DE' }).ok); assert.ok(P.check({ t: 'input', seq: 3, throttle: 1, turn: -0.5, fire: true, fire2: false, fire3: false }).ok); }); test('check rejects bad messages', () => { assert.equal(P.check(null).ok, false); assert.equal(P.check([]).ok, false); assert.equal(P.check({ t: 'nope' }).error, 'UNKNOWN_TYPE'); assert.equal(P.check({ t: 'hello', v: '1', token: 'x' }).error, 'BAD_FIELD:v'); assert.equal(P.check({ t: 'hello', v: 1, token: '' }).error, 'BAD_FIELD:token'); assert.equal(P.check({ t: 'challenge', to: 'x'.repeat(65) }).error, 'BAD_FIELD:to'); assert.equal(P.check({ t: 'room', room: 'ZZ' }).error, 'BAD_FIELD:room'); assert.equal(P.check({ t: 'input', seq: -1, throttle: 0, turn: 0, fire: false, fire2: false, fire3: false }).error, 'BAD_FIELD:seq'); assert.equal(P.check({ t: 'input', seq: 1, throttle: NaN, turn: 0, fire: false, fire2: false, fire3: false }).error, 'BAD_FIELD:throttle'); assert.equal(P.check({ t: 'input', seq: 1, throttle: 0, turn: 0, fire: 1, fire2: false, fire3: false }).error, 'BAD_FIELD:fire'); }); test('input clamps ranges and coerces flags', () => { assert.deepEqual(P.input({ throttle: 5, turn: -9, fire: 1 }), { throttle: 1, turn: -1, fire: true, fire2: false, fire3: false }); assert.deepEqual(P.input(null), { throttle: 0, turn: 0, fire: false, fire2: false, fire3: false }); }); function fakeSnap() { return { tick: 10, ack: [4, 7], time: 1, left: 100, pitOpen: 0, pitAlarm: false, endIn: 0, result: { winner: 0, reason: 'ko' }, press: { st: 'idle', y: 1 }, crane: { st: 'grab', x: 0, z: 0, who: 1 }, rockets: [{ kind: 'rocket', owner: 0, x: 1, z: 2, y: 0.5, a: 0 }], mines: [{ owner: 1, x: 0, z: 0, arm: 1 }], robots: [{ x: 1, hp: 50, grabbedBy: null, dealt: 30 }, { x: 2, hp: 0, grabbedBy: 0, dealt: 5 }], ev: [{ type: 'dmg', who: 1, amount: 3 }, { type: 'end', winner: 0, reason: 'ko' }, { type: 'pit_open' }], }; } test('toLocal for seat 0 keeps order and indices', () => { const l = P.toLocal(0, fakeSnap()); assert.equal(l.robots[0].x, 1); assert.equal(l.result.winner, 0); assert.equal(l.ack, 4); assert.equal(l.crane.who, 1); assert.equal(l.ev[0].who, 1); }); test('toLocal for seat 1 mirrors every robot index', () => { const l = P.toLocal(1, fakeSnap()); assert.equal(l.robots[0].x, 2, 'own robot first'); assert.equal(l.robots[0].dealt, 5); assert.equal(l.robots[0].grabbedBy, 1, 'grabbed by the other robot = local index 1'); assert.equal(l.robots[1].grabbedBy, null); assert.equal(l.result.winner, 1, 'seat 0 won -> for seat 1 the winner is the opponent (local 1)'); assert.equal(l.ack, 7); assert.equal(l.crane.who, 0); assert.equal(l.rockets[0].owner, 1); assert.equal(l.mines[0].owner, 0); assert.equal(l.ev[0].who, 0); assert.equal(l.ev[1].winner, 1); assert.ok(!('who' in l.ev[2])); }); test('draw stays a draw for both seats; mirrored results always disagree on the winner', () => { assert.equal(P.localResult(0, { winner: -1, reason: 'time' }).winner, -1); assert.equal(P.localResult(1, { winner: -1, reason: 'time' }).winner, -1); for (const w of [0, 1]) { const a = P.localResult(0, { winner: w }).winner, b = P.localResult(1, { winner: w }).winner; assert.notEqual(a === 0, b === 0, 'exactly one seat sees itself as the winner'); } }); test('packSnap of a real fight carries what the result screen needs', () => { const s = RC.Combat.create({ robots: [RC.stats(RC.STARTER), RC.stats(RC.STARTER)], seed: 3 }); RC.Combat.step(s, 1 / 60, [P.input({ throttle: 1 }), P.input({})]); const snap = P.packSnap(s, 1, [0, 0]); assert.equal(snap.robots.length, 2); for (const k of ['x', 'z', 'a', 'hp', 'maxHp', 'dealt', 'taken']) assert.equal(typeof snap.robots[0][k], 'number', k); assert.ok(Array.isArray(snap.robots[0].W)); assert.ok(JSON.stringify(snap).length < 6000, 'snapshot stays small'); }); test('a snapshot never carries a field named t (it is the message type on the wire)', () => { const s = RC.Combat.create({ robots: [RC.stats(RC.STARTER), RC.stats(RC.STARTER)], seed: 1 }); const msg = Object.assign({ t: 'snap' }, P.packSnap(s, 1, [0, 0])); assert.equal(msg.t, 'snap'); assert.equal(typeof msg.time, 'number'); });