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
JavaScript100 lines4.0 KB
import test from "node:test";import assert from "node:assert/strict";import { createState, addPlayer, removePlayer, step, snapshot, normalizeText, DT } from "../public/shared/sim.js";import { WORDS, pickChoices } from "../public/shared/words.js";import { createRng } from "../public/shared/rng.js"; const mk = (n = 3) => { const s = createState(7); for (let i = 1; i <= n; i++) addPlayer(s, "p" + i, "P" + i); return s; };const run = (s, inp = {}) => step(s, new Map(Object.entries(inp)), DT);const kinds = (ev, k) => ev.filter((e) => e[1] === k);let q = 0;const g = (text) => [{ t: "g", seq: ++q, text }]; function toDrawing(s) {  run(s);  assert.equal(s.phase, "choosing");  run(s, { [s.drawer]: [{ t: "w", seq: ++q, idx: 0 }] });  assert.equal(s.phase, "drawing");} test("words", () => {  assert.ok(WORDS.length >= 300);  assert.equal(new Set(WORDS).size, WORDS.length);  const c = pickChoices(createRng(1), 3);  assert.equal(new Set(c).size, 3);}); test("snapshot has no sk, has ep/rs/tt and hides word", () => {  const s = mk(); toDrawing(s);  const v = snapshot(s, "zz");  assert.equal("sk" in v, false);  assert.equal(v.rs, 3); assert.equal(v.tt, 80); assert.equal(v.wd, "");  assert.equal(snapshot(s, s.drawer).wd, s.word);}); test("close guess, accents, correct chat visibility, early end", () => {  const s = mk(); toDrawing(s);  const [a, b] = ["p1", "p2", "p3"].filter((x) => x !== s.drawer);  let ev = run(s, { [a]: g(s.word.slice(0, -1)) });  assert.equal(kinds(ev, "close").length, 1);  ev = run(s, { [a]: g("  " + s.word.toUpperCase() + " ") });  assert.equal(kinds(ev, "correct")[0][3], 500 - Math.round(0) + 50 > 0 ? kinds(ev, "correct")[0][3] : 0);  ev = run(s, { [a]: g("hello") });  assert.equal(kinds(ev, "chat")[0][4], 1);  ev = run(s, { [b]: g("nope") });  assert.equal(kinds(ev, "chat")[0][4], 0);  ev = run(s, { [b]: g(s.word) });  assert.equal(s.phase, "turnend");  assert.equal(kinds(ev, "reveal")[0][2], s.word);  assert.equal(normalizeText("Café  É"), "cafe e");}); test("hints at 50% and 75%", () => {  const s = mk(); toDrawing(s);  let hints = 0;  for (let i = 0; i < 80 * 30 && s.phase === "drawing"; i++) hints += kinds(run(s), "hint").length;  assert.equal(hints, 2);}); test("full 3 round game then auto new game", () => {  const s = mk(); const turns = []; let results = 0, newgame = 0;  for (let i = 0; i < 30 * 600 && newgame === 0; i++) {    const inp = {};    if (s.phase === "drawing") {      for (const id of s.players.keys()) if (id !== s.drawer && !s.players.get(id).guessed && s.timer < 70) inp[id] = g(s.word);    }    const ev = run(s, inp);    for (const e of ev) { if (e[1] === "turn") turns.push(e[2]); if (e[1] === "results") results++; if (e[1] === "newgame") newgame++; }  }  assert.equal(turns.length, 9);  assert.equal(results, 1); assert.equal(newgame, 1);  assert.equal(s.phase, "waiting");}); test("drawer leaving ends turn; late joiner", () => {  const s = mk(4); toDrawing(s);  const d = s.drawer;  removePlayer(s, d);  assert.equal(s.phase, "turnend");  addPlayer(s, "late", "Late");  assert.ok(s.order.includes("late"));}); test("stroke ops, undo, clear, cap", () => {  const s = createState(1); addPlayer(s, "a", "A");  run(s, { a: [{ t: "d", seq: 1, strokeId: 1, color: 0, size: 0, pts: [1, 1, 2, 2] }] });  run(s, { a: [{ t: "d", seq: 2, strokeId: 1, color: 0, size: 0, pts: [3, 3] }] });  run(s, { a: [{ t: "d", seq: 3, strokeId: 2, color: 1, size: 1, pts: [5, 5] }] });  assert.deepEqual(s.strokeOps, [["add", 0], ["pts", 0, 2], ["add", 1]]);  assert.equal(s.opCount, 3);  run(s, { a: [{ t: "u", seq: 4 }] });  assert.equal(s.strokes.length, 1);  assert.deepEqual(s.strokeOps.at(-1), ["undo", 1]);  const ep = s.boardEpoch;  run(s, { a: [{ t: "c", seq: 5 }] });  assert.equal(s.boardEpoch, ep + 1);  assert.equal(s.strokes.length, 0);  for (let i = 0; i < 400; i++) run(s, { a: [{ t: "d", seq: 9, strokeId: 50 + i, color: 0, size: 0, pts: new Array(128).fill(5) }] });  assert.equal(s.points, 20000 - (20000 % 64));});