'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'); }); }); describe('Instant grab, snap and 1:1 pointer tracking (t1102/t1103/t1104)', () => { it('grabs instantly on beginDrag with no hold delay or elapsed time', () => { const g = makeGame({ noSpawn: true }); const block = g.place(2, 1, 1); g.place(2, 0, 0); // No tick between "pointerdown" and the grab — there is no long press. assert.ok(g.beginDrag(2, 1.4)); assert.ok(g.drag); assert.equal(g.drag.blockId, block.id); assert.equal(g.drag.accum, 0); assert.equal(block.state, 'still'); }); it('snaps to the nearest still block when the press is slightly outside it', () => { const g = makeGame({ noSpawn: true }); const bottom = g.place(1, 0, 0); g.place(1, 1, 1); const top = g.place(1, 2, 2); // Just below the bottom block (occupies [0, 1)). assert.ok(g.beginDrag(1, -0.2)); assert.equal(g.blockById(g.drag.blockId).id, bottom.id); g.endDrag(); // Just above the top block (occupies [2, 3)). assert.ok(g.beginDrag(1, 3.2)); assert.equal(g.blockById(g.drag.blockId).id, top.id); g.endDrag(); // A miss further than SNAP_Y does not grab. assert.equal(g.beginDrag(1, 3.2 + GameCore.SNAP_Y + 0.1), false); }); it('horizontal slop grabs a neighbouring column when the press is near the edge', () => { const g = makeGame({ noSpawn: true }); const block = g.place(2, 1, 3); g.place(2, 0, 0); // Pointer is in column 3, 0.2 tiles from the left edge — close to col 2. assert.ok(g.beginDrag(3.2, 1.5)); assert.equal(g.blockById(g.drag.blockId).id, block.id); assert.equal(g.blockById(g.drag.blockId).col, 2); g.endDrag(); // An integer column 3 (no slop — used by existing tests) does not jump left. assert.equal(g.beginDrag(3, 1.5), false); }); it('a slightly diagonal drag stays in the starting column', () => { const g = makeGame({ noSpawn: true }); const a = g.place(4, 0, 0); g.place(4, 1, 1); g.place(4, 2, 2); g.place(5, 0, 3); g.place(5, 1, 4); g.place(5, 2, 0); assert.ok(g.beginDrag(4.85, 0.4)); assert.equal(g.blockById(g.drag.blockId).col, 4); // moveDrag only takes y — column is locked for the whole press. g.moveDrag(2.2); g.endDrag(); assert.equal(a.col, 4); assert.equal(a.y, 2); assert.equal(g.blockAt(5, 0).color, 3, 'neighbour column is untouched'); }); it('a short move that stays inside the same cell does not swap', () => { const g = makeGame({ noSpawn: true }); const a = g.place(0, 0, 0); g.place(0, 1, 1); g.place(0, 2, 2); assert.ok(g.beginDrag(0, 0.5)); // Previously SWAP_DISTANCE (0.25) triggered a full-tile jump. g.moveDrag(0.5 + GameCore.SWAP_DISTANCE); assert.equal(a.y, 0, 'still in the starting cell, y=' + a.y); g.endDrag(); }); it('a 1-tile downward drag swaps exactly one step and stops when the pointer stops', () => { 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, 1.5)); g.moveDrag(0.5); assert.equal(b.y, 0, 'grabbed block followed the pointer one tile down'); assert.equal(a.y, 1); assert.equal(c.y, 2); g.moveDrag(0.5); assert.equal(b.y, 0, 'a stopped pointer moves nothing else'); assert.equal(a.y, 1); assert.equal(c.y, 2); g.endDrag(); }); it('a 1-tile upward drag swaps exactly one step and stops when the pointer stops', () => { 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.5)); g.moveDrag(1.5); assert.equal(a.y, 1, 'grabbed block followed the pointer one tile up'); assert.equal(b.y, 0); assert.equal(c.y, 2); g.moveDrag(1.5); assert.equal(a.y, 1, 'a stopped pointer moves nothing else'); assert.equal(b.y, 0); assert.equal(c.y, 2); g.endDrag(); }); it('a 2-tile drag then stop leaves the block on the row under the pointer', () => { 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); const d = g.place(0, 3, 3); assert.ok(g.beginDrag(0, 0.5)); g.moveDrag(2.5); assert.equal(a.y, 2); assert.equal(b.y, 0); assert.equal(c.y, 1); assert.equal(d.y, 3); g.moveDrag(2.5); assert.equal(a.y, 2, 'stopped pointer, no further movement'); assert.equal(b.y, 0); assert.equal(c.y, 1); assert.equal(d.y, 3); // A twitch that stays in the same cell also leaves the stack alone. g.moveDrag(2.7); assert.equal(a.y, 2); g.endDrag(); }); it('a fast swipe of several tiles swaps through every block and does not skip', () => { const g = makeGame({ noSpawn: true }); const a = g.place(6, 0, 0); const b = g.place(6, 1, 1); const c = g.place(6, 2, 2); const d = g.place(6, 3, 3); const e = g.place(6, 4, 4); assert.ok(g.beginDrag(6, 0.5)); // One large move event, as a fast swipe that only reports the end point. g.moveDrag(0.5 + 4); g.endDrag(); assert.equal(a.y, 4, 'bottom block walked all four steps, y=' + a.y); assert.equal(b.y, 0, 'each neighbour was swapped, not skipped: b at ' + b.y); assert.equal(c.y, 1); assert.equal(d.y, 2); assert.equal(e.y, 3); assert.equal(g.blockAt(6, 4).id, a.id); }); });