BlueGit

projects-timfalken-com meteors

Files

meteors / test / flight-local.test.js

flight-local.test.js

flight-local.test.js 6.0 KiB Download Raw
1'use strict';
2
3// t1181: flights are drawn locally, decoupled from the poll/server cadence.
4
5const { describe, it, beforeEach } = require('node:test');
6const assert = require('node:assert/strict');
7
8function fakeEl ()
9{
10 return {
11 style: {},
12 className: '',
13 parentNode: null,
14 children: [],
15 appendChild (c) { c.parentNode = this; this.children.push(c); return c; },
16 removeChild (c) { c.parentNode = null; this.children = this.children.filter((x) => x !== c); },
17 querySelector () { return null; },
18 getBoundingClientRect () { return { left: 0, top: 0, width: 0, height: 0, right: 0, bottom: 0 }; }
19 };
20}
21
22const layers = { flights: fakeEl(), 'flights-back': fakeEl() };
23globalThis.document = {
24 getElementById: (id) => layers[id] || null,
25 querySelector: () => null,
26 createElement: () => fakeEl()
27};
28globalThis.window = globalThis.window || { innerWidth: 1280, innerHeight: 800 };
29globalThis.CSS = globalThis.CSS || { escape: (s) => String(s) };
30globalThis.app = { fieldX: 400, fieldY: 100, fieldW: 400, fieldH: 520, tile: 40 };
31
32const Online = require('../src/online.js');
33
34function reset ()
35{
36 Online.clearFlights();
37 Online._pendingShots = [];
38 Online.me = { id: 'me' };
39 layers.flights.children = [];
40 layers['flights-back'].children = [];
41}
42
43function poseOf (f, now)
44{
45 Online.tickFlights(now);
46 const m = /translate\(([-\d.]+)px,([-\d.]+)px\)/.exec(f.el.style.transform || '');
47 return m ? { x: Number(m[1]), y: Number(m[2]) } : null;
48}
49
50describe('Local flights (t1181)', () =>
51{
52 beforeEach(reset);
53
54 it('the sender sees its flight at once, before any server answer', () =>
55 {
56 const ok = Online.startLocalShot({ id: 41, col: 3, y: 13, vy: 6, color: 2 }, { x: 470, y: 100 });
57 assert.equal(ok, true);
58 assert.equal(Online._flights.length, 1);
59 assert.equal(layers.flights.children.length, 1, 'img element is on screen immediately');
60 const f = Online._flights[0];
61 assert.equal(f.preview, true);
62 const p = poseOf(f, f.t0 + 16);
63 assert.ok(p && Math.abs(p.x - 470) < 20 && p.y <= 100, 'starts at the exit cell going up: ' + JSON.stringify(p));
64 });
65
66 it('the poll result retargets the same flight without a jump, and only once', () =>
67 {
68 Online.startLocalShot({ id: 7, col: 5, y: 13, vy: 6, color: 1 }, { x: 510, y: 100 });
69 const f = Online._flights[0];
70 f.t0 = performance.now() - 300; // pretend it has been flying for 300 ms
71 const before = poseOf(f, performance.now());
72 // Server answer for the same client id (own shot, a miss so no mini is needed).
73 Online.spawnFlights([{ id: 900, client_id: '7', from_id: 'me', miss: true, color: 1, flight_duration: 1.2, age: 0 }]);
74 assert.equal(Online._flights.length, 1, 'no second flight for the same shot');
75 assert.equal(Online._flights[0], f);
76 assert.equal(f.preview, false);
77 // Retargeting happened at performance.now(); sample the new curve at its start.
78 const after = poseOf(f, f.t0);
79 assert.ok(Math.hypot(after.x - before.x, after.y - before.y) < 2, 'continues from the current pose');
80 // The same shot later in the lobby-wide shot_flights list is ignored.
81 Online.spawnFlights([{ id: 900, client_id: '7', from_id: 'me', miss: true, color: 1, flight_duration: 1.2, age: 0.4 }]);
82 assert.equal(Online._flights.length, 1);
83 });
84
85 it('the retarget keeps the total close to one flight duration', () =>
86 {
87 assert.equal(Online.retargetDuration(0, 1200), 1200);
88 assert.equal(Online.retargetDuration(300, 1200), 900);
89 assert.equal(Online.retargetDuration(1100, 1200), 600, 'never shorter than half a flight');
90 });
91
92 it('a preview waits at its apex for a slow server instead of vanishing', () =>
93 {
94 Online.startLocalShot({ id: 8, col: 5, y: 13, vy: 6, color: 1 }, { x: 510, y: 100 });
95 const f = Online._flights[0];
96 Online.tickFlights(f.t0 + f.duration + 500);
97 assert.equal(Online._flights.length, 1, 'still there while waiting');
98 Online.tickFlights(f.t0 + f.duration + Online.PREVIEW_HOLD_MS + 50);
99 assert.equal(Online._flights.length, 0, 'gives up after the hold');
100 });
101
102 it('a receiver plays a late-seen flight from the start, a bit faster (no mid-arc pop-in)', () =>
103 {
104 assert.equal(Online.lateFlightDuration(0, 1200), 1200);
105 assert.equal(Online.lateFlightDuration(null, 1200), 1200);
106 assert.equal(Online.lateFlightDuration(0.5, 1200), 700);
107 assert.equal(Online.lateFlightDuration(1.0, 1200), 600);
108 const now = performance.now();
109 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 }],
110 { 77: { startX: 200, startY: 90 } });
111 const f = Online._flights.find((x) => x.id === '77');
112 assert.ok(f, 'flight spawned');
113 assert.ok(f.t0 >= now - 1, 't0 is now, not back-dated');
114 assert.ok(Math.abs(f.duration - 700) < 1e-6);
115 const p = poseOf(f, f.t0);
116 assert.ok(Math.hypot(p.x - 200, p.y - 90) < 2, 'starts at the sender field: ' + JSON.stringify(p));
117 });
118
119 it('interpolation depends only on time, not on how often tickFlights runs', () =>
120 {
121 Online.startLocalShot({ id: 9, col: 5, y: 13, vy: 6, color: 1 }, { x: 510, y: 100 });
122 const f = Online._flights[0];
123 const t = f.t0 + 420;
124 poseOf(f, f.t0 + 100);
125 poseOf(f, f.t0 + 250);
126 const a = poseOf(f, t);
127 const b = poseOf(f, t);
128 assert.deepEqual(a, b);
129 });
130
131 it('flushDecision sends new shots right away unless a poll is in flight or just happened', () =>
132 {
133 assert.equal(Online.flushDecision({ polling: true, pending: 2, inFlight: false, sinceLast: 500 }), 'now');
134 assert.equal(Online.flushDecision({ polling: true, pending: 2, inFlight: true, sinceLast: 500 }), 'later');
135 assert.equal(Online.flushDecision({ polling: true, pending: 2, inFlight: false, sinceLast: 30 }), 'later');
136 assert.equal(Online.flushDecision({ polling: true, pending: 0, inFlight: false, sinceLast: 500 }), 'none');
137 assert.equal(Online.flushDecision({ polling: false, pending: 3, inFlight: false, sinceLast: 500 }), 'none');
138 });
139
140 it('the authoritative payload is unchanged: shots are queued exactly as before', () =>
141 {
142 Online.queueShots([{ id: 5, color: 3, col: 1, y: 13 }]);
143 assert.deepEqual(Online._pendingShots, [{ client_id: '5', color: 3, col: 1, y: 13 }]);
144 });
145});