// Live test against the real Firebase project (needs network): node --experimental-eventsource tests/online.test.js // Creates two throw-away accounts, runs lobby -> challenge -> match -> state/input sync, then deletes everything. 'use strict'; const path = require('path'); const mem = {}; global.window = globalThis; global.Kit = { store: () => ({ get: (k, d) => (k in mem ? mem[k] : d), set: (k, v) => { mem[k] = v; } }), cleanName: s => String(s || '').slice(0, 20) }; for (const f of ['parts.js', 'robot.js', 'combat.js', 'ai.js', 'net.js', 'lobby.js', 'netfight.js']) require(path.join(__dirname, '..', 'game', f)); const RC = globalThis.RC; const CFG = { apiKey: 'AIzaSyCE29VQnubM_9ok46nnosg07MpvhZH3O5w', dbUrl: 'https://robot-clash-aa37b-default-rtdb.firebaseio.com' }; const sleep = ms => new Promise(r => setTimeout(r, ms)); let fails = 0; const ok = (c, msg) => { console.log((c ? 'ok ' : 'FAIL ') + msg); if (!c) fails++; }; (async () => { const stamp = Date.now(); const A = RC.Net(CFG), B = RC.Net(CFG); ok(A.enabled, 'config accepted'); const ra = await A.auth.register(`rc-test-a-${stamp}@example.com`, 'test-pass-123', 'TestA'); const rb = await B.auth.register(`rc-test-b-${stamp}@example.com`, 'test-pass-123', 'TestB'); ok(ra.ok && rb.ok, `register two accounts ${ra.error || ''} ${rb.error || ''}`); if (!ra.ok || !rb.ok) process.exit(1); const again = RC.Net(CFG); const rl = await again.auth.login(`RC-TEST-A-${stamp}@example.com `, 'test-pass-123'); ok(rl.ok && again.auth.current.name === 'TestA', `login again shows the real name (${again.auth.current && again.auth.current.name}), e-mail case/space tolerant`); const G = RC.Net(CFG); const rg = await G.auth.guest('GastTest'); ok(rg.ok && G.auth.current.guest, 'anonymous guest sign-in ' + (rg.error || '')); const LG = RC.Lobby({ net: G, getProfile: () => ({ name: 'GastTest', flag: 'DE', build: RC.STARTER, paint: { body: 1, accent: 2 }, fame: 0 }) }); await LG.enter(); const wrong = await RC.Net(CFG).auth.login(`rc-test-a-${stamp}@example.com`, 'nope-nope'); ok(!wrong.ok, 'wrong password rejected: ' + wrong.error); const prof = n => () => ({ name: n, flag: 'DE', build: RC.STARTER, paint: { body: 1, accent: 2 }, fame: 5 }); const LA = RC.Lobby({ net: A, getProfile: prof('TestA') }), LB = RC.Lobby({ net: B, getProfile: prof('TestB') }); let listA = [], incomingB = null, matchA = null, matchB = null; LA.onList(r => { listA = r; }); LB.onIncoming(c => { incomingB = c; }); LA.onMatch(m => { matchA = m; }); LB.onMatch(m => { matchB = m; }); await LA.enter(); await LB.enter(); await sleep(2500); ok(listA.some(r => r.uid === B.uid && r.name === 'TestB' && r.flag === 'DE'), 'A sees B in the lobby (push stream)'); ok(listA.some(r => r.uid === G.uid && r.name === 'GastTest'), 'A sees the anonymous guest in the lobby'); LG.leave(); LA.challenge(B.uid); await sleep(2500); ok(incomingB && incomingB.from === A.uid, 'B receives the challenge'); const t0 = Date.now(); if (incomingB) await LB.accept(A.uid); while (!(matchA && matchB) && Date.now() - t0 < 12000) await sleep(100); console.log(` match start took ${Date.now() - t0} ms (A ${!!matchA}, B ${!!matchB})`); ok(matchA && matchB && matchA.id === matchB.id, 'both sides get the match'); ok(matchA && matchB && matchA.host !== matchB.host, 'exactly one host'); ok(matchA && matchA.opp.name === 'TestB' && matchB.opp.name === 'TestA', 'opponent data arrives'); if (matchA && matchB) { const host = matchA.host ? A : B, guest = matchA.host ? B : A, id = matchA.id; const mk = () => RC.Combat.create({ robots: [RC.stats(RC.STARTER), RC.stats(RC.STARTER)], seed: matchA.seed }); const sH = mk(), sG = mk(); const nfH = RC.NetFight({ net: host, id, host: true }), nfG = RC.NetFight({ net: guest, id, host: false }); nfH.start(); nfG.start(); for (let i = 0; i < 35; i++) { nfH.idle(0.1); nfG.idle(0.1); await sleep(100); } // 3.5 s countdown like the game for (let i = 0; i < 180; i++) { // 3 s real time nfH.hostStep(sH, { throttle: 1, turn: 0 }, 1 / 60); sH.events.length = 0; nfG.guestFrame(sG, { throttle: 1, turn: 0.5, fire: false, fire2: false }, 1 / 60); await sleep(16); } await sleep(800); nfG.guestFrame(sG, { throttle: 1, turn: 0.5, fire: false, fire2: false }, 1 / 60); ok(sG.t > 1, `guest receives host snapshots (t=${sG.t.toFixed(2)})`); ok(Math.abs(sG.robots[1].x - sH.robots[0].x) < 1.5, `guest sees host robot near its real place (${sG.robots[1].x.toFixed(2)} vs ${sH.robots[0].x.toFixed(2)})`); ok(Math.abs(sH.robots[1].a - Math.PI) > 0.2, `host applies guest input (guest robot turned, a=${sH.robots[1].a.toFixed(2)})`); ok(nfH.peerAlive && nfG.peerAlive, 'both peers alive'); // rules: guest must not be able to write the state, a stranger must not read the match ok((await guest.db.put(`matches/${id}/state`, { seq: 1 })) === false, 'rule: guest cannot write host state'); nfH.stop(); nfG.stop(); } // cleanup: lobby, match, challenge, accounts LA.endMatch(); LB.endMatch(); await sleep(500); LA.leave(); LB.leave(); await sleep(800); for (const n of [A, B, G]) { const t = await n.token(); await fetch(`https://identitytoolkit.googleapis.com/v1/accounts:delete?key=${CFG.apiKey}`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ idToken: t }) }); } console.log(fails ? `${fails} failure(s)` : 'all ok'); process.exit(fails ? 1 : 0); })();