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
JavaScript141 lines5.0 KB
import test from "node:test";import assert from "node:assert/strict";import { createState, addPlayer, removePlayer, step, sanitizeInput, hashState, sanitizeName, normalizeText, editDistance, snapshot, TURN_SEC, DT } from "../public/shared/sim.js";import { createRng } from "../public/shared/rng.js"; const mk = (n = 3) => {  const s = createState(42);  for (let i = 0; i < n; i++) addPlayer(s, "p" + i, "P" + i);  return s;};const toDrawing = (s) => {  step(s, new Map());  const d = s.drawer;  step(s, new Map([[d, [{ t: "w", seq: 1, idx: 0 }]]]));  assert.equal(s.phase, "drawing");  return d;}; function run() {  const s = mk();  const rng = createRng(7);  for (let i = 0; i < 300; i++) {    const id = "p" + rng.int(3);    const a = sanitizeInput(["d", i, 1, rng.int(8), rng.int(3), rng.int(1001), rng.int(751)]);    const g = sanitizeInput(["g", i, "word" + rng.int(5)]);    const w = sanitizeInput(["w", i, rng.int(3)]);    step(s, new Map([[id, [a, g, w].filter(Boolean)]]), DT);  }  return hashState(s);} test("deterministic replay", () => assert.equal(run(), run())); test("rng state is serialisable", () => {  const a = createRng(5);  a.next();  const b = createRng(a.state);  assert.equal(a.next(), b.next());}); test("game starts with 3 players and stays waiting with 2", () => {  const s = mk(2);  step(s, new Map());  assert.equal(s.phase, "waiting");  addPlayer(s, "p2", "x");  step(s, new Map());  assert.equal(s.phase, "choosing");}); test("correct guess scores guesser and drawer; word hidden from guessers", () => {  const s = mk();  const d = toDrawing(s);  const g = ["p0", "p1", "p2"].find((x) => x !== d);  assert.equal(snapshot(s, g).wd, "");  assert.equal(snapshot(s, d).wd, s.word);  const ev = step(s, new Map([[g, [{ t: "g", seq: 1, text: "  " + s.word.toUpperCase() + " " }]]]));  assert.ok(ev.some((e) => e[1] === "correct"));  assert.ok(s.players.get(g).score >= 500);  assert.equal(s.players.get(d).score, 50);}); test("everyone guessed ends the turn early; close guess is private event", () => {  const s = mk();  const d = toDrawing(s);  const [a, b] = ["p0", "p1", "p2"].filter((x) => x !== d);  const ev = step(s, new Map([[a, [{ t: "g", seq: 1, text: s.word + "x" }]]]));  assert.ok(ev.some((e) => e[1] === "close" && e[2] === a));  step(s, new Map([[a, [{ t: "g", seq: 2, text: s.word }]], [b, [{ t: "g", seq: 1, text: s.word }]]]));  assert.equal(s.phase, "turnend");}); test("only drawer may draw while drawing; undo and clear", () => {  const s = mk();  const d = toDrawing(s);  const other = ["p0", "p1", "p2"].find((x) => x !== d);  const stroke = (seq) => sanitizeInput(["d", seq, seq, 0, 0, 1, 1, 2, 2]);  step(s, new Map([[other, [stroke(1)]]]));  assert.equal(s.strokes.length, 0);  step(s, new Map([[d, [stroke(2), stroke(3)]]]));  assert.equal(s.strokes.length, 2);  step(s, new Map([[d, [{ t: "u", seq: 4 }]]]));  assert.equal(s.strokes.length, 1);  step(s, new Map([[d, [{ t: "c", seq: 5 }]]]));  assert.equal(s.strokes.length, 0);}); test("hints reveal a letter at 50%", () => {  const s = mk();  toDrawing(s);  const start = s.revealed.length;  while (s.timer > TURN_SEC * 0.5 - 0.1) step(s, new Map());  assert.ok(s.revealed.length > start || s.word.length < 3);}); test("removing the drawer ends the turn", () => {  const s = mk(4);  const d = toDrawing(s);  removePlayer(s, d);  assert.equal(s.phase, "turnend");}); test("full game reaches results then waiting", () => {  const s = mk();  for (let i = 0; i < 30 * 60 * 30 && !(s.phase === "results" && s.timer <= DT); i++) step(s, new Map());  assert.equal(s.phase, "results");  step(s, new Map());  assert.equal(s.phase, "waiting");}); test("input sanitisation", () => {  assert.equal(sanitizeInput(null), null);  assert.equal(sanitizeInput("x"), null);  assert.equal(sanitizeInput(["d", NaN, 1, 0, 0, 1, 1]), null);  assert.equal(sanitizeInput(["d", 1, 1, 9, 0, 1, 1]), null);  assert.equal(sanitizeInput(["d", 1, 1, 0, 0, 1e9, 1]), null);  assert.equal(sanitizeInput(["d", 1, 1, 0, 0, "1", 1]), null);  assert.equal(sanitizeInput(["d", 1, 1, 0, 0, 1]), null);  assert.equal(sanitizeInput(["u", 1, "extra"]), null);  assert.equal(sanitizeInput(["g", 1, 5]), null);  assert.equal(sanitizeInput(["g", 1, "   "]), null);  assert.equal(sanitizeInput(["zz", 1]), null);  assert.equal(sanitizeInput(["g", 1, "a".repeat(200)]).text.length, 64);  const many = ["d", 1, 1, 0, 0];  for (let i = 0; i < 130; i++) many.push(1);  assert.equal(sanitizeInput(many), null);  assert.deepEqual(sanitizeInput(["d", 1, 1, 0, 0, 5, 6]).pts, [5, 6]);}); test("name sanitisation", () => {  assert.equal(sanitizeName("  a   b\u0000\n c "), "a b c");  assert.equal(sanitizeName("x".repeat(40)).length, 16);  assert.equal(sanitizeName(42), "Player");  assert.equal(sanitizeName("   "), "Player");}); test("guess normalisation and edit distance", () => {  assert.equal(normalizeText("  CAFÉ   au  Lait "), "cafe au lait");  assert.equal(editDistance("cat", "cart"), 1);  assert.equal(editDistance("cat", "dog"), 3);});