'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, GameCore } = require('./helpers.js'); function flyingGrey (game, col, y) { const cluster = { id: game.nextClusterId++, jumpHeight: 3, vy: 4, decel: 2, phase: 'up' }; game.clusters[cluster.id] = cluster; const b = game.createBlock(col, y, GameCore.COLOR_GRAY, 'flying'); b.clusterId = cluster.id; b.vy = cluster.vy; b.flame = true; return { cluster, block: b }; } function landedGroup (game, col, specs) { // specs: [{ color, y }] const cluster = { id: game.nextClusterId++, jumpHeight: 3, vy: 0, decel: 0, phase: 'landed' }; game.clusters[cluster.id] = cluster; const members = []; for (const s of specs) { const state = 'settling'; const b = game.createBlock(col, s.y, s.color, state); b.clusterId = cluster.id; b.vy = 0; b.flame = false; b.settleTime = s.settleTime || 0; members.push(b); } return { cluster, members }; } describe('Blocks under a flying cluster are draggable (t1107)', () => { it('a still stack under a flyer in the same column can be grabbed and swapped', () => { const g = makeGame({ noSpawn: true }); const bottom = g.place(3, 0, 0); const mid = g.place(3, 1, 1); const { cluster, block: flyer } = flyingGrey(g, 3, 7); assert.equal(g.isColumnStill(3), false, 'isColumnStill stays strict'); assert.equal(g.columnAllowsDrag(3), true); assert.ok(g.isDraggableBlock(bottom)); assert.equal(g.isDraggableBlock(flyer), false, 'the flyer itself is not draggable'); assert.ok(g.beginDrag(3, 0.5)); assert.equal(g.drag.blockId, bottom.id); g.moveDrag(1.5); assert.equal(bottom.y, 1); assert.equal(mid.y, 0); g.endDrag(); assert.equal(flyer.state, 'flying'); assert.equal(flyer.clusterId, cluster.id); assert.equal(flyer.y, 7, 'flyer was left alone'); }); it('pressing on a flying cluster does not grab it or a block below', () => { const g = makeGame({ noSpawn: true }); g.place(1, 0, 2); const { block: flyer } = flyingGrey(g, 1, 5); assert.equal(g.beginDrag(1, 5.5), false); assert.equal(g.drag, null); assert.equal(flyer.state, 'flying'); }); }); describe('Dragging within, into and out of a landed cluster (t1107)', () => { it('dragging within a landed cluster swaps members and keeps them in the group', () => { const g = makeGame({ noSpawn: true }); const { cluster, members } = landedGroup(g, 4, [ { color: GameCore.COLOR_GRAY, y: 0 }, { color: 1, y: 1 }, { color: 2, y: 2 } ]); const [grey, cargo1, cargo2] = members; assert.ok(g.isDraggableBlock(grey)); assert.ok(g.isDraggableBlock(cargo1)); assert.ok(g.beginDrag(4, 0.4)); assert.equal(g.drag.blockId, grey.id); g.moveDrag(2.5); g.endDrag(); assert.equal(grey.y, 2); assert.equal(cargo1.y, 0); assert.equal(cargo2.y, 1); assert.equal(grey.clusterId, cluster.id); assert.equal(cargo1.clusterId, cluster.id); assert.equal(cargo2.clusterId, cluster.id); assert.equal(g.clusters[cluster.id].phase, 'landed'); }); it('swapping a still block into a landed cluster makes it a member', () => { const g = makeGame({ noSpawn: true }); const still = g.place(5, 0, 3); const { cluster, members } = landedGroup(g, 5, [ { color: GameCore.COLOR_GRAY, y: 1 }, { color: 4, y: 2 } ]); assert.ok(g.beginDrag(5, 0.5)); assert.equal(g.drag.blockId, still.id); // Stop on the top cell, short of LAUNCH_PULL, so endDrag does not throw. g.moveDrag(2.2); g.endDrag(); assert.equal(still.y, 2); assert.equal(still.clusterId, cluster.id); assert.equal(still.state, 'settling'); assert.equal(still.color, 3); // The grey was swapped into the outside slot and left the cluster. assert.equal(members[0].y, 0); assert.equal(members[0].clusterId, null); assert.equal(members[0].state, 'settling'); assert.equal(members[0].color, GameCore.COLOR_GRAY); // Cargo only moved to another cluster slot, so it stays a member. assert.equal(members[1].y, 1); assert.equal(members[1].clusterId, cluster.id); assert.equal(members[1].color, 4); }); it('swapping a landed member out leaves the cluster; a grey keeps its settle timer', () => { const g = makeGame({ noSpawn: true }); g.place(6, 0, 0); const { cluster, members } = landedGroup(g, 6, [ { color: GameCore.COLOR_GRAY, y: 1, settleTime: 1.25 }, { color: 2, y: 2 } ]); const grey = members[0]; assert.ok(g.beginDrag(6, 1.4)); assert.equal(g.drag.blockId, grey.id); g.moveDrag(0.4); g.endDrag(); assert.equal(grey.y, 0); assert.equal(grey.clusterId, null); assert.equal(grey.state, 'settling', 'grey keeps settling on its own timer'); assert.equal(grey.settleTime, 1.25); assert.equal(grey.color, GameCore.COLOR_GRAY); // The still block that moved in joined the cluster. const joined = g.blocks.find((b) => b.color === 0); assert.equal(joined.clusterId, cluster.id); assert.equal(joined.state, 'settling'); assert.equal(joined.y, 1); }); it('a gap in the column stops the swap; hanging members are part of the stack', () => { const g = makeGame({ noSpawn: true }); const bottom = g.place(2, 0, 1); const { members } = landedGroup(g, 2, [ { color: 3, y: 2 } ]); // Gap at y=1: swapToward must stop, the hanging member stays at 2. assert.ok(g.beginDrag(2, 0.5)); g.moveDrag(2.5); g.endDrag(); assert.equal(bottom.y, 0); assert.equal(members[0].y, 2); }); });