'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, fillColumn, GameCore } = require('./helpers.js'); describe('Vertical swapping', () => { it('dragging a block through a column swaps it with the blocks it passes', () => { const g = makeGame({ noSpawn: true }); const a = g.place(0, 0, 0); const b = g.place(0, 1, 1); const c = g.place(0, 2, 2); assert.ok(g.beginDrag(0, 0)); g.moveDrag(2); g.endDrag(); assert.equal(a.y, 2); assert.equal(b.y, 0); assert.equal(c.y, 1); assert.equal(g.blockAt(0, 2).color, 0); assert.equal(g.blockAt(0, 0).color, 1); }); it('swapTo moves a block stepwise and exchanges neighbors', () => { const g = makeGame({ noSpawn: true }); g.place(1, 0, 4); g.place(1, 1, 3); g.place(1, 2, 2); g.place(1, 3, 1); const moved = g.swapTo(1, 0, 3); assert.equal(moved.color, 4); assert.equal(moved.y, 3); assert.equal(g.blockAt(1, 0).color, 3); assert.equal(g.blockAt(1, 1).color, 2); assert.equal(g.blockAt(1, 2).color, 1); }); it('refuses to drag in a locked or flying column', () => { const g = makeGame({ noSpawn: true }); fillColumn(g, 0, 13, [0]); g.checkLocks(); assert.equal(g.isColumnLocked(0), true); assert.equal(g.beginDrag(0, 0), false); const g2 = makeGame({ noSpawn: true }); g2.place(2, 0, 1); g2.place(3, 0, 1); g2.place(4, 0, 1); g2.place(2, 1, 2); g2.resolveMatches(); assert.equal(g2.isColumnStill(2), false); assert.equal(g2.beginDrag(2, 1), false); // A quiet neighboring column is still draggable. g2.place(7, 0, 3); g2.place(7, 1, 4); assert.equal(g2.isColumnStill(7), true); assert.ok(g2.beginDrag(7, 0)); g2.endDrag(); }); it('pulling a top block further up starts a manual launch that cannot leave the field', () => { const g = makeGame({ noSpawn: true }); g.place(0, 0, 1); g.place(0, 1, 2); g.place(0, 2, 3); assert.ok(g.beginDrag(0, 2)); g.moveDrag(2 + 1.5); assert.equal(g.drag.launchIntent, true); g.endDrag(); const block = g.blocks.find((b) => b.color === 3); assert.equal(block.state, 'manual'); assert.equal(block.manualPhase, 'up'); assert.ok(block.vy > 0); g.step(g.planet.riseTime, 1 / 240); assert.ok(Math.abs(block.y - (g.height - 1)) < 0.05, 'apex should be just under the top, y=' + block.y); assert.ok(block.y <= g.height - 1 + 1e-9, 'y=' + block.y); assert.ok(block.y < g.height, 'must not leave the field'); assert.ok(block.manualPhase === 'down' || block.state === 'still' || Math.abs(block.y - (g.height - 1)) < 0.05); g.step(0.5, 1 / 120); assert.ok(block.y < g.height - 1 - 0.01 || block.state === 'still', 'must come back down, y=' + block.y + ' state=' + block.state); assert.ok(block.y < g.height); }); it('a manual jump from mid-stack still caps at the top cell', () => { const g = makeGame({ noSpawn: true }); g.place(5, 0, 0); g.place(5, 1, 1); const top = g.place(5, 2, 2); g.startManualLaunch(top); g.step(4, 1 / 120); assert.ok(top.y <= g.height - 1 + 1e-9); assert.ok(top.y < g.height); // After coming back down it lands on the remaining stack (y=1, since it left). assert.ok(top.state === 'still' || top.state === 'manual'); if (top.state === 'still') { assert.equal(top.y, 2); } }); it('a click in the lower half of a cell grabs that cell, not the one above', () => { const g = makeGame({ noSpawn: true }); g.place(0, 0, 0); g.place(0, 1, 1); assert.ok(g.beginDrag(0, 0.4)); assert.equal(g.blockById(g.drag.blockId).color, 0); g.endDrag(); assert.ok(g.beginDrag(0, 0.9)); assert.equal(g.blockById(g.drag.blockId).color, 0); g.endDrag(); assert.ok(g.beginDrag(0, 1.1)); assert.equal(g.blockById(g.drag.blockId).color, 1); g.endDrag(); }); }); describe('Manual launch of the already-topmost block', () => { it('dragging the top block upward past its own position starts a manual jump', () => { const g = makeGame({ noSpawn: true }); // A single block is already the topmost (and only) block of its column. const block = g.place(0, 0, 2); // Grab at the visual centre (y=0.5), then pull just past its position — // well short of the old "empty space above the cell" threshold (1.25). assert.ok(g.beginDrag(0, 0.5)); assert.equal(g.drag.startedAsTop, true); g.moveDrag(0.5 + 0.4); assert.ok(0.9 < 0 + 1 + 0.25, 'this pull would have failed the old threshold'); assert.equal(g.drag.launchIntent, true, 'pull of 0.4 tiles past the press point must launch'); g.endDrag(); assert.equal(block.state, 'manual'); assert.equal(block.manualPhase, 'up'); assert.ok(block.vy > 0); g.step(g.planet.riseTime, 1 / 240); assert.ok(Math.abs(block.y - (g.height - 1)) < 0.08, 'jumps to just under the top, y=' + block.y); assert.ok(block.y <= g.height - 1 + 1e-9); }); it('a short upward twitch below LAUNCH_PULL does not launch', () => { const g = makeGame({ noSpawn: true }); const block = g.place(3, 0, 1); assert.ok(g.beginDrag(3, 0.5)); g.moveDrag(0.5 + 0.2); assert.ok(0.2 < g.constructor.LAUNCH_PULL || 0.2 < 0.35); assert.equal(g.drag.launchIntent, false); g.endDrag(); assert.equal(block.state, 'still'); assert.equal(block.y, 0); }); it('dragging a mid-stack block up to the top still swaps instead of launching', () => { const g = makeGame({ noSpawn: true }); const bottom = g.place(1, 0, 0); g.place(1, 1, 1); g.place(1, 2, 2); assert.ok(g.beginDrag(1, 0.3)); assert.equal(g.drag.startedAsTop, false); // Pointer is past the top cell centre but not into the empty space. g.moveDrag(2.4); assert.equal(bottom.y, 2, 'swapped to the top'); assert.equal(g.drag.launchIntent, false, 'must not launch a mid-stack rearrange'); g.endDrag(); assert.equal(bottom.state, 'still'); assert.equal(bottom.y, 2); }); it('after a mid-stack block becomes the top, a further pull into empty space still launches', () => { const g = makeGame({ noSpawn: true }); const bottom = g.place(2, 0, 4); g.place(2, 1, 3); assert.ok(g.beginDrag(2, 0.2)); assert.equal(g.drag.startedAsTop, false); g.moveDrag(1 + 1 + 0.4); assert.equal(bottom.y, 1); assert.equal(g.drag.launchIntent, true); g.endDrag(); assert.equal(bottom.state, 'manual'); }); });