| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const { makeGame } = require('./helpers.js'); |
| 6 | |
| 7 | describe('Swap events (t1121)', () => |
| 8 | { |
| 9 | it('swapBlocks records visual events and consumeSwapEvents clears them', () => |
| 10 | { |
| 11 | const g = makeGame({ noSpawn: true }); |
| 12 | const a = g.place(0, 0, 1); |
| 13 | const b = g.place(0, 1, 2); |
| 14 | g.swapBlocks(a, b); |
| 15 | assert.equal(a.y, 1); |
| 16 | assert.equal(b.y, 0); |
| 17 | const ev = g.consumeSwapEvents(); |
| 18 | assert.equal(ev.length, 2); |
| 19 | assert.equal(ev[0].id, a.id); |
| 20 | assert.equal(ev[0].fromY, 0); |
| 21 | assert.equal(ev[0].toY, 1); |
| 22 | assert.equal(ev[1].id, b.id); |
| 23 | assert.equal(ev[1].fromY, 1); |
| 24 | assert.equal(ev[1].toY, 0); |
| 25 | assert.equal(g.consumeSwapEvents().length, 0); |
| 26 | }); |
| 27 | |
| 28 | it('rapid multi-cell swapToward records a chain of events (logic already at the final cell)', () => |
| 29 | { |
| 30 | const g = makeGame({ noSpawn: true }); |
| 31 | const a = g.place(1, 0, 0); |
| 32 | g.place(1, 1, 1); |
| 33 | g.place(1, 2, 2); |
| 34 | g.swapToward(a, 2); |
| 35 | assert.equal(a.y, 2); |
| 36 | const ev = g.consumeSwapEvents(); |
| 37 | // Two pairwise swaps (0↔1, then 1↔2) → 4 events, final toY of a is 2. |
| 38 | assert.ok(ev.length >= 4, 'events=' + ev.length); |
| 39 | const aEvents = ev.filter((e) => e.id === a.id); |
| 40 | assert.equal(aEvents[aEvents.length - 1].toY, 2); |
| 41 | }); |
| 42 | |
| 43 | it('the invariant checker is unaffected by swap events (grid is already updated)', () => |
| 44 | { |
| 45 | const g = makeGame({ noSpawn: true }); |
| 46 | g.place(2, 0, 0); |
| 47 | g.place(2, 1, 1); |
| 48 | g.swapToward(g.blocks[0], 1); |
| 49 | g.assertInvariants('after-swap'); |
| 50 | assert.equal(g.lastInvariantViolations.length, 0); |
| 51 | }); |
| 52 | }); |
projects-timfalken-com meteors