gitflarePublicPartly agent-ready40
sketch-rush
Multiplayer draw-and-guess game on Cloudflare Workers, Durable Objects and D1.
- LicenseApache-2.0
- LanguageJavaScript
- Last landed
dbe1e99Oct 23, 2026
Add the Apache-2.0 licence
dbe1e99import test from "node:test";import assert from "node:assert/strict";import { checkMessage, newRate, decideJoin, expiredSlots, isValidCode, randomCode, randomHex, RECONNECT_MS } from "../src/room-logic.ts"; test("rate limiter: 60 msgs/s ok, 61st is a strike, 3 strikes close", () => { const r = newRate(0); for (let i = 0; i < 60; i++) assert.equal(checkMessage(r, 10, 10).ok, true); assert.equal(checkMessage(r, 20, 10).ok, false); assert.equal(r.strikes, 1); assert.equal(checkMessage(r, 1500, 10).ok, true); assert.equal(checkMessage(r, 1500, 3000).ok, false); assert.equal(checkMessage(r, 1500, 3000).close, true); assert.equal(r.strikes, 3);}); test("oversize payload is dropped", () => { const r = newRate(0); assert.equal(checkMessage(r, 0, 2048).ok, true); assert.equal(checkMessage(r, 0, 2049).ok, false);}); const slot = (id, token, connected, at = 0) => ({ id, token, connected, disconnectedAt: at }); test("reconnect within 15s keeps the slot, after 15s frees it", () => { const slots = new Map([["p1", slot("p1", "tok", false, 1000)]]); const d = decideJoin(slots, "tok", 1000 + RECONNECT_MS, "drawing", 12); assert.equal(d.kind, "reconnect"); assert.equal(d.slot.id, "p1"); assert.equal(decideJoin(slots, "tok", 1000 + RECONNECT_MS + 1, "drawing", 12).kind, "player"); assert.deepEqual(expiredSlots(slots, 1000 + RECONNECT_MS + 1), ["p1"]); assert.deepEqual(expiredSlots(slots, 1000 + RECONNECT_MS), []);}); test("wrong token does not steal a slot", () => { const slots = new Map([["p1", slot("p1", "tok", false, 0)]]); assert.equal(decideJoin(slots, "nope", 100, "waiting", 12).kind, "player");}); test("full or finished rooms admit spectators", () => { const slots = new Map(); for (let i = 0; i < 12; i++) slots.set("p" + i, slot("p" + i, "t" + i, true)); assert.equal(decideJoin(slots, null, 0, "waiting", 12).kind, "spectator"); assert.equal(decideJoin(new Map(), null, 0, "results", 12).kind, "spectator"); assert.equal(decideJoin(new Map(), null, 0, "waiting", 12).kind, "player");}); test("room codes and tokens", () => { assert.ok(isValidCode(randomCode(Math.random))); assert.equal(isValidCode("abcdef"), false); assert.equal(isValidCode("ABCDE0"), false); assert.equal(randomHex(16, (a) => a.fill(255)), "ff".repeat(16));});