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
JavaScript51 lines2.6 KB
import test from 'node:test';import assert from 'node:assert/strict';import { joinRoom, promoteQueued } from '../src/room-logic.ts'; test('joiner during results is queued, promoted at next countdown in join order', () => {  const slots = new Map();  let n = 1;  const mk = (fin) => joinRoom(slots, null, 0, 16, fin, () => 't' + n, () => n++);  const a = mk(true), b = mk(true);  assert.equal(a.kind, 'spectator');  assert.equal(a.slot.queued, true);  assert.deepEqual(promoteQueued(slots, 16), [a.slot.id, b.slot.id]);  assert.equal(a.slot.spectator, false);  assert.equal(a.slot.queued, false);  assert.deepEqual(promoteQueued(slots, 16), []);}); test('promotion respects the seat limit', () => {  const slots = new Map();  let n = 1;  joinRoom(slots, null, 0, 1, false, () => 'x', () => n++);  const q = joinRoom(slots, null, 0, 1, false, () => 'y', () => n++);  assert.equal(q.slot.queued, true);  assert.deepEqual(promoteQueued(slots, 1), []);  assert.deepEqual(promoteQueued(slots, 2), [q.slot.id]);}); test('net.js sets queued from welcome element 7', async () => {  const { registerHooks } = await import('node:module');  const { pathToFileURL } = await import('node:url');  const { resolve: pres } = await import('node:path');  registerHooks({ resolve(spec, ctx, next) { if (spec.startsWith('/shared/')) return next(pathToFileURL(pres(import.meta.dirname, '../public' + spec)).href, ctx); return next(spec, ctx); } });  class WS { constructor() { this.readyState = 1; WS.last = this; setTimeout(() => this.onopen && this.onopen(), 0); } close() {} send() {} }  globalThis.WebSocket = WS;  globalThis.location = { protocol: 'http:', host: 'x', search: '' };  const st = {}; globalThis.sessionStorage = { getItem: (k) => st[k] ?? null, setItem: (k, v) => { st[k] = v; } };  const { createNet } = await import('../public/net.js');  const { createState, addPlayer, step } = await import('../public/shared/sim.js');  const { encodeSnapshot, makeSnapshot } = await import('../public/shared/protocol.js');  const net = createNet('ABC', 'x', {});  await new Promise((r) => setTimeout(r, 10));  const s = createState(1, {}); addPlayer(s, 1, 'A'); step(s, new Map(), 1 / 30);  const full = encodeSnapshot(null, makeSnapshot(s));  WS.last.onmessage({ data: JSON.stringify(['w', 4, 'tok', 'ABC', 'spectator', s.tick, full, 1]) });  assert.equal(net.queued, true);  WS.last.onmessage({ data: JSON.stringify(['w', 4, 'tok', 'ABC', 'player', s.tick, full]) });  assert.equal(net.queued, false);  net.close();  setTimeout(() => process.exit(process.exitCode ?? 0), 50).unref();});