'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame } = require('./helpers.js'); describe('Swap events (t1121)', () => { it('swapBlocks records visual events and consumeSwapEvents clears them', () => { const g = makeGame({ noSpawn: true }); const a = g.place(0, 0, 1); const b = g.place(0, 1, 2); g.swapBlocks(a, b); assert.equal(a.y, 1); assert.equal(b.y, 0); const ev = g.consumeSwapEvents(); assert.equal(ev.length, 2); assert.equal(ev[0].id, a.id); assert.equal(ev[0].fromY, 0); assert.equal(ev[0].toY, 1); assert.equal(ev[1].id, b.id); assert.equal(ev[1].fromY, 1); assert.equal(ev[1].toY, 0); assert.equal(g.consumeSwapEvents().length, 0); }); it('rapid multi-cell swapToward records a chain of events (logic already at the final cell)', () => { const g = makeGame({ noSpawn: true }); const a = g.place(1, 0, 0); g.place(1, 1, 1); g.place(1, 2, 2); g.swapToward(a, 2); assert.equal(a.y, 2); const ev = g.consumeSwapEvents(); // Two pairwise swaps (0↔1, then 1↔2) → 4 events, final toY of a is 2. assert.ok(ev.length >= 4, 'events=' + ev.length); const aEvents = ev.filter((e) => e.id === a.id); assert.equal(aEvents[aEvents.length - 1].toY, 2); }); it('the invariant checker is unaffected by swap events (grid is already updated)', () => { const g = makeGame({ noSpawn: true }); g.place(2, 0, 0); g.place(2, 1, 1); g.swapToward(g.blocks[0], 1); g.assertInvariants('after-swap'); assert.equal(g.lastInvariantViolations.length, 0); }); });