Files
skribbl-gartic-color/test/socket-gartic-min-players.js

63 lines
2.5 KiB
JavaScript

// Verify Gartic mode requires at least 3 players to start.
const { io } = require("socket.io-client");
const URL = "http://localhost:3000";
const TIMEOUT_MS = 15000;
const results = [];
let exitCode = 0;
const pass = (n) => { results.push({ name: n, status: "pass" }); console.log("PASS:", n); };
const fail = (n, e) => { results.push({ name: n, status: "fail", error: String(e) }); console.log("FAIL:", n, "-", e); exitCode = 1; };
function emitAck(s, event, payload) {
return new Promise((resolve, reject) => {
const t = setTimeout(() => reject(new Error(`ack timeout for ${event}`)), 5000);
s.emit(event, payload, (resp) => { clearTimeout(t); resolve(resp); });
});
}
const killer = setTimeout(() => { console.log("FAIL: Global timeout"); process.exit(1); }, TIMEOUT_MS);
killer.unref();
(async () => {
let a, b;
try {
a = io(URL, { transports: ["websocket"], forceNew: true });
b = io(URL, { transports: ["websocket"], forceNew: true });
await Promise.all([
new Promise((r) => a.on("connect", r)),
new Promise((r) => b.on("connect", r)),
]);
pass("min-players: two clients connected");
const create = await emitAck(a, "room:create", {
nickname: "Alice", avatar: "🐱", mode: "gartic",
settings: {},
});
if (!create || !create.ok) throw new Error("create failed: " + JSON.stringify(create));
const code = create.code;
pass("min-players: room created, code=" + code);
const j = await emitAck(b, "room:join", { code, nickname: "Bob", avatar: "🐶" });
if (!j || !j.ok) throw new Error("B join failed: " + JSON.stringify(j));
pass("min-players: B joined (2 players total)");
// game:start should fail with min-players error
const start = await emitAck(a, "game:start", {});
if (!start) throw new Error("no ack from game:start");
if (start.ok) throw new Error("expected game:start to fail with 2 players, got ok=true");
if (!start.error || !/3 players|at least 3/i.test(start.error)) {
throw new Error("expected error mentioning '3 players', got: " + JSON.stringify(start));
}
pass("min-players: game:start blocked with friendly error: " + start.error);
} catch (e) {
fail("min-players flow", e && e.message ? e.message : String(e));
} finally {
try { a && a.disconnect(); } catch (_) {}
try { b && b.disconnect(); } catch (_) {}
clearTimeout(killer);
console.log("__RESULTS__ " + JSON.stringify(results));
setTimeout(() => process.exit(exitCode), 200);
}
})();