"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 }); 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."); } finally { srv.kill(); } })().catch(e => { console.error(e); process.exit(1); });