Files
Robot-Clash/tests/netfight.test.js

50 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

'use strict';
// Entity-interpolation math for the live fight (the jitter buffer that stops the robot from jumping).
const { test } = require('node:test');
const assert = require('node:assert');
const path = require('path');
require(path.join(__dirname, '..', 'game', 'protocol.js'));
require(path.join(__dirname, '..', 'game', 'netfight.js'));
const RC = globalThis.RC;
const { pickFrames, lerpRobot } = RC.NetFight;
test('pickFrames: brackets the render time and gives the fraction between the two snapshots', () => {
const buf = [{ at: 0, tick: 1 }, { at: 100, tick: 2 }];
const fr = pickFrames(buf, 50);
assert.strictEqual(fr.a.tick, 1);
assert.strictEqual(fr.b.tick, 2);
assert.ok(Math.abs(fr.frac - 0.5) < 1e-9);
});
test('pickFrames: past the newest snapshot holds it (no extrapolation jump)', () => {
const buf = [{ at: 0, tick: 1 }, { at: 100, tick: 2 }];
const fr = pickFrames(buf, 500);
assert.strictEqual(fr.a.tick, 2);
assert.strictEqual(fr.b.tick, 2);
assert.strictEqual(fr.frac, 1);
});
test('pickFrames: empty buffer -> null', () => {
assert.strictEqual(pickFrames([], 0), null);
});
test('lerpRobot: positions interpolate linearly', () => {
const r = lerpRobot({ x: 0, z: 0, y: 0, hp: 100 }, { x: 10, z: -4, y: 2, hp: 50 }, 0.5);
assert.ok(Math.abs(r.x - 5) < 1e-9);
assert.ok(Math.abs(r.z + 2) < 1e-9);
assert.ok(Math.abs(r.y - 1) < 1e-9);
});
test('lerpRobot: angles take the shortest path across the +/-PI seam', () => {
const r = lerpRobot({ a: 3.0 }, { a: -3.0 }, 0.5);
// short way is +0.28 rad across PI, not -6 rad the long way
assert.ok(r.a > 3.0 || r.a < -3.0, 'wrapped past PI, not back toward 0');
assert.ok(Math.abs(Math.abs(r.a) - Math.PI) < 0.2, 'lands near the seam');
});
test('lerpRobot: discrete fields come from the reached (older) snapshot, not the newer one', () => {
const r = lerpRobot({ x: 0, hp: 100, dead: false }, { x: 10, hp: 0, dead: true }, 0.5);
assert.strictEqual(r.hp, 100);
assert.strictEqual(r.dead, false);
});