Skip to content

gitflarePublicPartly agent-ready40

sketch-rush

Multiplayer draw-and-guess game on Cloudflare Workers, Durable Objects and D1.

main
Add the Apache-2.0 licencedbe1e99
JavaScript70 lines3.2 KB
import test from "node:test";import assert from "node:assert/strict";import { encodeOps, encodeStrokeMsg, encodeReplay, applyStrokeMsg, newBoard, encodeSnapshot, applySnapshot } from "../public/shared/protocol.js";import { takeToken, newBucket } from "../src/room-logic.ts"; // minimal fake of the rules agent's stroke logfunction mk() { return { strokes: [], strokeOps: [], opCount: 0, boardEpoch: 0 }; }const add = (st, id) => { st.strokes.push({ id, owner: "p1", color: 1, size: 1, pts: [] }); st.strokeOps.push(["add", st.strokes.length - 1]); st.opCount++; };const pts = (st, i, p) => { const from = st.strokes[i].pts.length; st.strokes[i].pts.push(...p); st.strokeOps.push(["pts", i, from]); st.opCount++; }; // mirrors Room.broadcast for one tickfunction flush(st, sent, board) {  if (st.opCount === sent.n) return;  const ops = encodeOps(st, sent.n, st.opCount);  const m = ops ? [...encodeStrokeMsg(st.boardEpoch, sent.n, ops), st.opCount] : encodeReplay(st, st.opCount);  sent.n = st.opCount;  applyStrokeMsg(board, JSON.parse(JSON.stringify(m)));} test("incremental ops equal live state; late join replay equals live", () => {  const st = mk(), sent = { n: 0 }, b = newBoard();  add(st, 1); pts(st, 0, [1, 2, 3, 4]); flush(st, sent, b);  pts(st, 0, [5, 6]); add(st, 2); pts(st, 1, [9, 9]); flush(st, sent, b);  assert.deepEqual(b.strokes, st.strokes);  const late = newBoard();  applyStrokeMsg(late, JSON.parse(JSON.stringify(encodeReplay(st))));  assert.deepEqual(late.strokes, st.strokes);}); test("undo, clear and epoch change", () => {  const st = mk(), sent = { n: 0 }, b = newBoard();  add(st, 1); pts(st, 0, [1, 1]); add(st, 2); pts(st, 1, [2, 2]); flush(st, sent, b);  st.strokes.pop(); st.strokeOps.push(["undo", 1]); st.opCount++; flush(st, sent, b);  assert.equal(b.strokes.length, 1);  assert.deepEqual(b.strokes, st.strokes);  st.strokes = []; st.strokeOps = []; st.opCount = 0; st.boardEpoch = 1; sent.n = 1;  add(st, 3); pts(st, 0, [7, 7]);  const m = encodeReplay(st); sent.n = st.opCount;  const v = b.version;  applyStrokeMsg(b, m);  assert.deepEqual(b.strokes, st.strokes);  assert.ok(b.version > v);}); test("token bucket: honest traffic never strikes, flood does", () => {  const b = newBucket(0);  let t = 0, bad = 0;  for (let ms = 0; ms < 10000; ms += 10) {    t = ms;    if (ms % 50 === 0) bad += takeToken(b, t, 100).ok ? 0 : 1;    if (ms % 100 === 0) bad += takeToken(b, t, 20).ok ? 0 : 1;    if (ms % 1000 === 0) bad += takeToken(b, t, 20).ok ? 0 : 1;  }  assert.equal(bad, 0);  assert.equal(b.strikes, 0);  const f = newBucket(0);  let closed = false;  for (let i = 0; i < 200; i++) closed = takeToken(f, 0, 10).close || closed;  assert.ok(closed);  assert.equal(takeToken(newBucket(0), 0, 3000).ok, false);}); test("delta against baseline 5 ticks old reproduces next", () => {  const snap = (i) => ({ ph: "drawing", tm: 80 - i, dr: "p1", mk: "_ _", rd: 1, wd: "", ep: 0, rs: 3, tt: 80, ch: [], ps: { p1: ["A", i, 0, 1] } });  const ring = [];  for (let i = 0; i < 10; i++) ring.push(snap(i));  const base = ring[4], next = ring[9];  assert.deepEqual(applySnapshot(base, encodeSnapshot(base, next)), { ...next, sk: [] });});