Files
politickekarty/tests/rooms.js
T
BRNSystemsandClaude Opus 5.5 605be8abd1 Fotky, Čabelka, Poliačik s ATK, Gašparovič do strán, viac použití, posledné udalosti, chat
- Nové fotky (Wikimedia: Kňažko, Lorenc, Clementis, Kaliňák, Svätopluk, Šefčovič,
  Fígeľ, Beneš; Google: Horváth, Čabelka, Čalfa, Belousovová); štítok „kandidát“ len s ?debug
- Čepelka premenovaný na Jozef Čabelka (kľúč cabelka)
- Poliačik v Balanced 400/20, liečenie 85 HP; Balanced štatistiky preladené simuláciou
- Gašparovič môže 2× odísť zo strany alebo vstúpiť do inej (bez ťahu)
- Štúr, Čabelka, Danko, Truban, Kolíková, Nicholsonová: 2 použitia
- Nad stolom posledná (väčšie) a predposledná udalosť
- Minimálny chat v online miestnosti (api/chat, limit 200 znakov, 50 správ)
- crop.py nájde Haar kaskády aj pri systémovom OpenCV

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 19:04:14 +01:00

73 lines
4.5 KiB
JavaScript

"use strict";
// Životný cyklus miestností: odchod hráča, prenos zakladateľa, zánik prázdnej miestnosti.
const assert = require("node:assert/strict");
const { spawn } = require("node:child_process");
const http = require("node:http");
const path = require("node:path");
const PORT = 3900 + Math.floor(Math.random() * 90);
const base = `http://127.0.0.1:${PORT}`;
const sleep = ms => new Promise(r => setTimeout(r, ms));
const post = async (p, b) => (await fetch(base + p, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(b) })).json();
const rooms = async () => (await fetch(base + "/api/rooms")).json();
// SSE pripojenie; vráti posledný prijatý stav a funkciu na zatvorenie
function listen(room, token) {
const st = { last: null, ended: false };
const req = http.get(`${base}/api/events?room=${room}&token=${token}`, res => {
let buf = "";
res.on("data", d => { buf += d; let i; while ((i = buf.indexOf("\n\n")) >= 0) { const m = buf.slice(0, i); buf = buf.slice(i + 2); if (m.startsWith("data: ")) st.last = JSON.parse(m.slice(6)); } });
res.on("end", () => { st.ended = true; });
});
req.on("error", () => {});
st.close = () => req.destroy();
return st;
}
(async () => {
const srv = spawn(process.execPath, [path.join(__dirname, "..", "server.js")], { env: { ...process.env, PORT, ROOM_GRACE_MS: "600" }, stdio: "ignore" });
try {
for (let i = 0; i < 50; i++) { try { await fetch(base + "/api/ping"); break; } catch (e) { await sleep(100); } }
// 1) odchod jediného hráča zruší miestnosť hneď
const a = await post("/api/create", { name: "A", maxPlayers: 4 });
const ea = listen(a.room, a.token); await sleep(150);
assert.ok((await rooms()).some(r => r.id === a.room));
await post("/api/leave", { room: a.room, token: a.token }); await sleep(150);
assert.ok(!(await rooms()).some(r => r.id === a.room), "room should vanish after its only player leaves");
assert.ok(ea.ended, "open streams should be closed when the room is removed");
// 2) zakladateľ odíde pred hrou -> miesto sa uvoľní a zakladateľom je ďalší hráč
const b = await post("/api/create", { name: "B", maxPlayers: 4 });
const c = await post("/api/join", { room: b.room, name: "C" });
const eb = listen(b.room, b.token), ec = listen(b.room, c.token); await sleep(150);
await post("/api/leave", { room: b.room, token: b.token }); eb.close(); await sleep(150);
assert.equal(ec.last.players.filter(Boolean).length, 1);
assert.equal(ec.last.host, ec.last.you, "remaining player becomes host");
assert.deepEqual((await rooms()).find(r => r.id === b.room).players, ["C"]);
// 3) všetci sa odpoja bez odchodu -> miestnosť zmizne zo zoznamu hneď a po limite zanikne
ec.close(); await sleep(150);
assert.ok(!(await rooms()).some(r => r.id === b.room), "room without online players is hidden");
await sleep(1200);
assert.equal((await post("/api/join", { room: b.room, name: "D" })).error, "Miestnosť neexistuje.");
// 4) odchod počas hry = vzdanie sa, druhý hráč vyhráva
const d = await post("/api/create", { name: "D", maxPlayers: 2 });
const e = await post("/api/join", { room: d.room, name: "E" });
const ed = listen(d.room, d.token), ee = listen(d.room, e.token); await sleep(150);
await post("/api/act", { room: d.room, token: d.token, action: { type: "team", team: ["babis", "sutaj", "meciar", "lenart"] } });
await post("/api/act", { room: d.room, token: e.token, action: { type: "team", team: ["mihal", "kazimir", "kamenicky", "horvath"] } });
assert.deepEqual(await post("/api/act", { room: d.room, token: d.token, action: { type: "start" } }), { ok: true });
// 5) chat: hráč píše, cudzí token nie, príliš rýchle správy sa odmietnu
assert.deepEqual(await post("/api/chat", { room: d.room, token: d.token, text: " ahoj <b>\u0007 " }), { ok: true });
assert.equal((await post("/api/chat", { room: d.room, token: d.token, text: "znova" })).error, "Píšeš príliš rýchlo.");
assert.equal((await post("/api/chat", { room: d.room, token: "x", text: "hack" })).error, "Nie si v miestnosti.");
await sleep(150);
assert.deepEqual(ee.last.chat, [{ n: "D", t: "ahoj <b>", s: 0 }]);
await post("/api/leave", { room: d.room, token: d.token }); ed.close(); await sleep(150);
assert.equal(ee.last.G.over, "Vyhráva E");
ee.close();
console.log("Rooms test OK: leave, host handover, cleanup of empty rooms, leave during game, chat.");
} finally { srv.kill(); }
})().catch(e => { console.error(e); process.exit(1); });