Skip to content

gitflarePublicPartly agent-ready40

neon-drift

Real-time multiplayer arcade racer on Cloudflare Workers and Durable Objects.

main
Add the Apache-2.0 licence3efe3da
JavaScript91 lines3.6 KB
import { test } from 'node:test';import assert from 'node:assert/strict';import { createState, addPlayer, step } from '../public/shared/sim.js';import { makeSnapshot, encodeSnapshot, applySnapshot } from '../public/shared/protocol.js';import { createLimiter, admit, joinRoom, leaveRoom, expireSlots } from '../src/room-logic.ts'; const wire = (x) => JSON.parse(JSON.stringify(x)); test('trail deltas with 5 ticks of ack latency reproduce server trails', () => {  const st = createState(11);  for (let i = 1; i <= 6; i++) addPlayer(st, i, 'p' + i);  const ring = new Map();  let client = null;  const inflight = [];  let acked = -1;  for (let t = 0; t < 900; t++) {    const inputs = new Map();    for (let i = 1; i <= 6; i++) inputs.set(i, { turn: ((t / 7 + i) | 0) % 3 - 1, boost: t % 50 < 8 ? 1 : 0 });    step(st, inputs);    const snap = makeSnapshot(st);    ring.set(snap.tick, snap);    ring.delete(snap.tick - 64);    const base = ring.get(acked);    const useFull = !base || snap.tick % 60 === 0;    const delta = wire(encodeSnapshot(useFull ? null : base, snap));    inflight.push({ at: t + 5, tick: snap.tick, baseTick: useFull ? -1 : acked, delta });    while (inflight.length && inflight[0].at <= t) {      const m = inflight.shift();      client = applySnapshot(m.baseTick === -1 ? null : client, m.delta, m.tick);      acked = m.tick;    }    if (inflight.length === 0 && client) assert.deepEqual(client.trails, ring.get(client.tick).trails);  }  assert.deepEqual(client.trails, ring.get(client.tick).trails);  for (const p of st.players.values()) assert.ok(client.trails[p.id].every((v) => Number.isInteger(v) && !Object.is(v, -0)));}); test('snapshot extras; delta-coded full snapshot < 4 KB at 16 players with 30-point trails', () => {  const st = createState(5);  for (let i = 1; i <= 16; i++) {    const p = addPlayer(st, i, 'player' + i);    p.trail = [];    for (let k = 0; k < 30; k++) p.trail.push(Math.cos(k / 20 + i) * (100 + k), Math.sin(k / 20 + i) * (100 + k));  }  const s = makeSnapshot(st);  assert.equal(s.cycles[1].length, 10);  assert.ok(Array.isArray(s.arena) && s.arena.length === 3);  const full = wire(encodeSnapshot(null, s));  assert.ok(JSON.stringify(['s', s.tick, -1, 0, full]).length < 4096);  assert.deepEqual(applySnapshot(null, full, s.tick), s);}); test('reconnect within 15 s keeps the cycle', () => {  const slots = new Map();  let n = 1;  const a = joinRoom(slots, null, 0, 16, false, () => 'tok', () => n++);  leaveRoom(slots, a.slot.id, 1000);  const r = joinRoom(slots, 'tok', 10000, 16, false, () => 'x', () => n++);  assert.equal(r.kind, 'reconnect');  assert.equal(r.slot.id, a.slot.id);  leaveRoom(slots, a.slot.id, 11000);  assert.deepEqual(expireSlots(slots, 11000 + 15001), [a.slot.id]);}); test('honest client never strikes', () => {  const l = createLimiter(0);  let closed = false;  for (let ms = 0; ms < 20000; ms += 1) {    if (ms % 33 === 0 && !admit(l, ms, 40).ok) closed = true;    if (ms % 100 === 0 && !admit(l, ms, 12).ok) closed = true; // acks    if (ms % 1000 === 0 && !admit(l, ms, 20).ok) closed = true;  }  assert.equal(l.strikes, 0);  assert.equal(closed, false);}); test('flood strikes and closes, strikes spaced by 250 ms', () => {  const l = createLimiter(0);  let res, closedAt = -1;  for (let ms = 0; ms < 4000; ms += 5) {    res = admit(l, ms, 20);    if (res.close) { closedAt = ms; break; }  }  assert.ok(closedAt > 0, 'closed');  assert.ok(l.strikes >= 3);  assert.ok(closedAt >= 500, 'strikes >= 250 ms apart');  const big = createLimiter(0);  admit(big, 0, 5000); admit(big, 0, 5000);  assert.equal(admit(big, 0, 5000).close, true);});