'use strict'; // t1181: flights are drawn locally, decoupled from the poll/server cadence. const { describe, it, beforeEach } = require('node:test'); const assert = require('node:assert/strict'); function fakeEl () { return { style: {}, className: '', parentNode: null, children: [], appendChild (c) { c.parentNode = this; this.children.push(c); return c; }, removeChild (c) { c.parentNode = null; this.children = this.children.filter((x) => x !== c); }, querySelector () { return null; }, getBoundingClientRect () { return { left: 0, top: 0, width: 0, height: 0, right: 0, bottom: 0 }; } }; } const layers = { flights: fakeEl(), 'flights-back': fakeEl() }; globalThis.document = { getElementById: (id) => layers[id] || null, querySelector: () => null, createElement: () => fakeEl() }; globalThis.window = globalThis.window || { innerWidth: 1280, innerHeight: 800 }; globalThis.CSS = globalThis.CSS || { escape: (s) => String(s) }; globalThis.app = { fieldX: 400, fieldY: 100, fieldW: 400, fieldH: 520, tile: 40 }; const Online = require('../src/online.js'); function reset () { Online.clearFlights(); Online._pendingShots = []; Online.me = { id: 'me' }; layers.flights.children = []; layers['flights-back'].children = []; } function poseOf (f, now) { Online.tickFlights(now); const m = /translate\(([-\d.]+)px,([-\d.]+)px\)/.exec(f.el.style.transform || ''); return m ? { x: Number(m[1]), y: Number(m[2]) } : null; } describe('Local flights (t1181)', () => { beforeEach(reset); it('the sender sees its flight at once, before any server answer', () => { const ok = Online.startLocalShot({ id: 41, col: 3, y: 13, vy: 6, color: 2 }, { x: 470, y: 100 }); assert.equal(ok, true); assert.equal(Online._flights.length, 1); assert.equal(layers.flights.children.length, 1, 'img element is on screen immediately'); const f = Online._flights[0]; assert.equal(f.preview, true); const p = poseOf(f, f.t0 + 16); assert.ok(p && Math.abs(p.x - 470) < 20 && p.y <= 100, 'starts at the exit cell going up: ' + JSON.stringify(p)); }); it('the poll result retargets the same flight without a jump, and only once', () => { Online.startLocalShot({ id: 7, col: 5, y: 13, vy: 6, color: 1 }, { x: 510, y: 100 }); const f = Online._flights[0]; f.t0 = performance.now() - 300; // pretend it has been flying for 300 ms const before = poseOf(f, performance.now()); // Server answer for the same client id (own shot, a miss so no mini is needed). Online.spawnFlights([{ id: 900, client_id: '7', from_id: 'me', miss: true, color: 1, flight_duration: 1.2, age: 0 }]); assert.equal(Online._flights.length, 1, 'no second flight for the same shot'); assert.equal(Online._flights[0], f); assert.equal(f.preview, false); // Retargeting happened at performance.now(); sample the new curve at its start. const after = poseOf(f, f.t0); assert.ok(Math.hypot(after.x - before.x, after.y - before.y) < 2, 'continues from the current pose'); // The same shot later in the lobby-wide shot_flights list is ignored. Online.spawnFlights([{ id: 900, client_id: '7', from_id: 'me', miss: true, color: 1, flight_duration: 1.2, age: 0.4 }]); assert.equal(Online._flights.length, 1); }); it('the retarget keeps the total close to one flight duration', () => { assert.equal(Online.retargetDuration(0, 1200), 1200); assert.equal(Online.retargetDuration(300, 1200), 900); assert.equal(Online.retargetDuration(1100, 1200), 600, 'never shorter than half a flight'); }); it('a preview waits at its apex for a slow server instead of vanishing', () => { Online.startLocalShot({ id: 8, col: 5, y: 13, vy: 6, color: 1 }, { x: 510, y: 100 }); const f = Online._flights[0]; Online.tickFlights(f.t0 + f.duration + 500); assert.equal(Online._flights.length, 1, 'still there while waiting'); Online.tickFlights(f.t0 + f.duration + Online.PREVIEW_HOLD_MS + 50); assert.equal(Online._flights.length, 0, 'gives up after the hold'); }); it('a receiver plays a late-seen flight from the start, a bit faster (no mid-arc pop-in)', () => { assert.equal(Online.lateFlightDuration(0, 1200), 1200); assert.equal(Online.lateFlightDuration(null, 1200), 1200); assert.equal(Online.lateFlightDuration(0.5, 1200), 700); assert.equal(Online.lateFlightDuration(1.0, 1200), 600); const now = performance.now(); Online.spawnFlights([{ id: 77, client_id: '3', from_id: 'other', miss: true, color: 0, flight_duration: 1.2, age: 0.5, col: 2, y: 13 }], { 77: { startX: 200, startY: 90 } }); const f = Online._flights.find((x) => x.id === '77'); assert.ok(f, 'flight spawned'); assert.ok(f.t0 >= now - 1, 't0 is now, not back-dated'); assert.ok(Math.abs(f.duration - 700) < 1e-6); const p = poseOf(f, f.t0); assert.ok(Math.hypot(p.x - 200, p.y - 90) < 2, 'starts at the sender field: ' + JSON.stringify(p)); }); it('interpolation depends only on time, not on how often tickFlights runs', () => { Online.startLocalShot({ id: 9, col: 5, y: 13, vy: 6, color: 1 }, { x: 510, y: 100 }); const f = Online._flights[0]; const t = f.t0 + 420; poseOf(f, f.t0 + 100); poseOf(f, f.t0 + 250); const a = poseOf(f, t); const b = poseOf(f, t); assert.deepEqual(a, b); }); it('flushDecision sends new shots right away unless a poll is in flight or just happened', () => { assert.equal(Online.flushDecision({ polling: true, pending: 2, inFlight: false, sinceLast: 500 }), 'now'); assert.equal(Online.flushDecision({ polling: true, pending: 2, inFlight: true, sinceLast: 500 }), 'later'); assert.equal(Online.flushDecision({ polling: true, pending: 2, inFlight: false, sinceLast: 30 }), 'later'); assert.equal(Online.flushDecision({ polling: true, pending: 0, inFlight: false, sinceLast: 500 }), 'none'); assert.equal(Online.flushDecision({ polling: false, pending: 3, inFlight: false, sinceLast: 500 }), 'none'); }); it('the authoritative payload is unchanged: shots are queued exactly as before', () => { Online.queueShots([{ id: 5, color: 3, col: 1, y: 13 }]); assert.deepEqual(Online._pendingShots, [{ client_id: '5', color: 3, col: 1, y: 13 }]); }); });