'use strict'; // t1182: prediction / reconcile of falling blocks on opponents' minis. const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const MP = require('../src/miniPredict.js'); const FS = 13 / 3; // fallTime 3 → 4.33 cells/s function snap (t, blocks) { return { t, blocks }; } function run (st, from, to, fs, stepMs) { let out = null; for (let now = from; now <= to + 1e-9; now += stepMs || 16) { out = MP.step(st, now, fs); } return out; } describe('MiniPredict (t1182)', () => { it('fall speed comes from the opponent planet (13 / fallTime)', () => { assert.equal(MP.fallSpeedFor({ fallTime: 2 }), 6.5); assert.equal(MP.fallSpeedFor({ fallTime: 5 }), 13 / 5); assert.equal(MP.fallSpeedFor(null), 13 / 3); }); it('extrapolates a falling block downwards between snapshots, sub-cell', () => { const st = MP.create(); MP.update(st, snap(1000, [{ id: 1, col: 2, y: 10, state: 'falling' }]), 5000); const a = MP.step(st, 5000, FS)[1]; const b = MP.step(st, 5100, FS)[1]; const c = MP.step(st, 5300, FS)[1]; assert.equal(a, 10); assert.ok(Math.abs(b - (10 - FS * 0.1)) < 1e-9, 'b=' + b); assert.ok(Math.abs(c - (10 - FS * 0.3)) < 1e-9, 'c=' + c); assert.ok(c % 1 !== 0, 'sub-cell position'); }); it('never predicts a landing below the stack, and leaves non-falling blocks alone', () => { const st = MP.create(); MP.update(st, snap(0, [ { id: 1, col: 4, y: 0, state: 'still' }, { id: 2, col: 4, y: 1, state: 'locked' }, { id: 3, col: 4, y: 4, state: 'falling' }, { id: 4, col: 4, y: 5.5, state: 'falling' } ]), 0); const out = run(st, 0, 1500, FS); assert.equal(out[3], 2, 'stops on top of the stack (y=2), no lock shown'); assert.equal(out[4], 3, 'the block above it stops one cell higher'); assert.equal(out[1], undefined, 'still blocks are drawn at their own y'); assert.equal(out[2], undefined); }); it('caps extrapolation when snapshots stop coming', () => { const st = MP.create(); MP.update(st, snap(0, [{ id: 1, col: 0, y: 12, state: 'falling' }]), 0); const out = run(st, 0, 5000, FS, 50); assert.ok(Math.abs(out[1] - (12 - FS * MP.MAX_EXTRAP_S)) < 1e-6, 'y=' + out[1]); }); it('a repeated snapshot (opponent has not polled) does not restart the prediction', () => { const st = MP.create(); const s1 = snap(1000, [{ id: 1, col: 0, y: 12, state: 'falling' }]); MP.update(st, s1, 0); run(st, 0, 400, FS); const again = snap(1000, [{ id: 1, col: 0, y: 12, state: 'falling' }]); assert.equal(MP.update(st, again, 400), false); const out = MP.step(st, 416, FS); assert.ok(out[1] < 12 - FS * 0.4 + 0.01, 'kept falling: ' + out[1]); // Same for snapshots without a sender time (content signature). const st2 = MP.create(); MP.update(st2, { blocks: [{ id: 1, col: 0, y: 12, state: 'falling' }] }, 0); assert.equal(MP.update(st2, { blocks: [{ id: 1, col: 0, y: 12, state: 'falling' }] }, 400), false); }); it('a late snapshot is placed at the time it was taken (sender clock offset)', () => { const st = MP.create(); MP.update(st, snap(10000, [{ id: 1, col: 0, y: 12, state: 'falling' }]), 50); // offset 50 ms // Next snapshot taken 750 ms later but arrives 300 ms late. MP.update(st, snap(10750, [{ id: 1, col: 0, y: 12 - FS * 0.75, state: 'falling' }]), 50 + 750 + 300); assert.equal(st.at, 10750 + (50 - 10000)); const t = MP.predict(st.snap, (1100 - st.at) / 1000, FS)[1]; assert.ok(Math.abs(t - (12 - FS * 1.05)) < 1e-6, 'continuous with true motion: ' + t); }); it('reconciles smoothly: a lagging block catches up, a leading one never moves up', () => { // Lagging: shown 0.8 cell above the new truth. const st = MP.create(); MP.update(st, snap(0, [{ id: 1, col: 0, y: 10, state: 'falling' }]), 0); MP.step(st, 0, FS); st.shown[1] = 10.8; let prev = 10.8; for (let now = 16; now <= 400; now += 16) { const y = MP.step(st, now, FS)[1]; assert.ok(y <= prev + 1e-9, 'monotonic down'); prev = y; } const truth = 10 - FS * 0.4; assert.ok(Math.abs(prev - truth) < 0.02, 'caught up: ' + prev + ' vs ' + truth); // Leading: shown 0.6 below the truth → keeps falling (slower), never jumps up. const st2 = MP.create(); MP.update(st2, snap(0, [{ id: 1, col: 0, y: 10, state: 'falling' }]), 0); MP.step(st2, 0, FS); st2.shown[1] = 9.4; prev = 9.4; for (let now = 16; now <= 800; now += 16) { const y = MP.step(st2, now, FS)[1]; assert.ok(y <= prev + 1e-9, 'never up: ' + y + ' > ' + prev); prev = y; } assert.ok(Math.abs(prev - (10 - FS * 0.8)) < 0.05, 'converged: ' + prev); }); it('snaps when far off (e.g. block jumped / was dragged) and drops blocks that landed', () => { const st = MP.create(); MP.update(st, snap(0, [{ id: 1, col: 0, y: 10, state: 'falling' }]), 0); run(st, 0, 200, FS); // Authoritative: block now 4 cells higher (far) → snap. MP.update(st, snap(250, [{ id: 1, col: 0, y: 13.5, state: 'falling' }]), 250); assert.equal(MP.step(st, 250, FS)[1], 13.5); // Then it landed: not falling any more → no predicted pose. MP.update(st, snap(900, [{ id: 1, col: 0, y: 0, state: 'still' }]), 900); assert.equal(MP.step(st, 916, FS)[1], undefined); }); it('steady 750 ms snapshots of a constant fall render as one smooth descent', () => { const st = MP.create(); let maxStep = 0; let prev = null; const y0 = 12.5; for (let now = 0; now <= 2200; now += 16) { const k = Math.floor(now / 750); const taken = k * 750; if (now % 750 < 16) { MP.update(st, snap(taken, [{ id: 9, col: 1, y: y0 - FS * taken / 1000, state: 'falling' }]), now + 40); } const y = MP.step(st, now + 40, FS)[9]; if (prev != null) { maxStep = Math.max(maxStep, Math.abs(prev - y)); } prev = y; } const perFrame = FS * 0.016; assert.ok(maxStep < perFrame * 1.6, 'max per-frame move ' + maxStep.toFixed(3) + ' vs fall ' + perFrame.toFixed(3)); }); });