'use strict'; // t1184/t1185/t1186: no new or received blocks in columns of a rocket // stack that is certainly leaving (highest block past 75% of the field). const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, placeRow } = require('./helpers.js'); function launchedRocket (g, cols, height) { const row = placeRow(g, 0, cols, 1); const cluster = g.launchCluster(row, height); return { cluster, row }; } function riseUntil (g, row, cluster, minTop) { for (let i = 0; i < 2000; i++) { const top = Math.max(...row.filter((b) => g.blocks.includes(b)).map((b) => b.y)); if (top >= minTop || cluster.phase !== 'up') { return top; } g.step(1 / 120, 1 / 120); } throw new Error('never rose'); } describe('Departing rocket columns (t1184)', () => { it('threshold is 75% of the field height', () => { const g = makeGame({ noSpawn: true }); assert.equal(g.DEPART_LINE(), 13 * 0.75); }); it('a rising stack below 75% does not block its columns', () => { const g = makeGame({ noSpawn: true }); const { cluster, row } = launchedRocket(g, [2, 3, 4], 30); riseUntil(g, row, cluster, 5); assert.deepEqual(Object.keys(g.departingColumns()), []); }); it('past 75% and certain to leave: all its columns are skipped by new blocks', () => { const g = makeGame({ noSpawn: true, seed: 11 }); const { cluster, row } = launchedRocket(g, [2, 3, 4], 30); const top = riseUntil(g, row, cluster, 10); assert.ok(top >= 9.75 && top < 13, 'top=' + top); assert.deepEqual(Object.keys(g.departingColumns()).map(Number).sort(), [2, 3, 4]); const seen = new Set(); for (let i = 0; i < 300; i++) { const b = g.trySpawn(); assert.ok(b, 'spawned'); seen.add(b.col); g.removeBlock(b); } for (const c of [2, 3, 4]) { assert.ok(!seen.has(c), 'spawned in departing column ' + c); } assert.ok(seen.size >= 6, 'other columns still used: ' + [...seen]); }); it('received garbage aimed at a departing column drops in another column', () => { const g = makeGame({ noSpawn: true, seed: 5 }); const { cluster, row } = launchedRocket(g, [5, 6], 30); riseUntil(g, row, cluster, 10); for (let i = 0; i < 50; i++) { const want = 5 + (i % 2); const b = g.ingestGarbage(want); assert.ok(b); assert.ok(b.col !== 5 && b.col !== 6, 'garbage col ' + b.col); g.removeBlock(b); } // Open preferred columns are still honoured. const ok = g.ingestGarbage(1); assert.equal(ok.col, 1); // pickGarbageColumn (used by bots / previews) also avoids them. for (let i = 0; i < 50; i++) { const c = g.pickGarbageColumn(); assert.ok(c !== 5 && c !== 6); } }); it('a stack whose apex stays below the top is not departing', () => { const g = makeGame({ noSpawn: true }); const { cluster, row } = launchedRocket(g, [3], 10.5); const top = riseUntil(g, row, cluster, 9.8); assert.ok(top >= 9.75, 'top=' + top); assert.deepEqual(Object.keys(g.departingColumns()), []); }); it('a stack that is coming back down is not departing', () => { const g = makeGame({ noSpawn: true }); const { cluster, row } = launchedRocket(g, [3], 30); riseUntil(g, row, cluster, 10); cluster.phase = 'down'; cluster.vy = -1; assert.deepEqual(Object.keys(g.departingColumns()), []); }); it('if every open column is departing, garbage waits and arrives later (never lost)', () => { const g = makeGame({ noSpawn: true }); const all = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const { cluster, row } = launchedRocket(g, all, 30); riseUntil(g, row, cluster, 10); assert.equal(Object.keys(g.departingColumns()).length, 10); assert.equal(g.trySpawn(), null, 'no new block while every column is departing'); // t1192: it waits in the queue (not dropped into the rocket, not lost). assert.equal(g.ingestGarbage(4), null); assert.equal(g.pendingGarbage(), 1); for (let i = 0; i < 600 && g.pendingGarbage(); i++) g.step(1 / 120, 1 / 120); assert.equal(g.pendingGarbage(), 0, 'dropped once the rocket left'); assert.ok(g.blocks.some((x) => x.color === -1 && x.state === 'falling' && x.col === 4), 'in the wanted column'); }); it('the stack really leaves and the columns open up again', () => { const g = makeGame({ noSpawn: true }); const { cluster, row } = launchedRocket(g, [2, 3], 30); riseUntil(g, row, cluster, 10); assert.ok(g.departingColumns()[2]); for (let i = 0; i < 600 && row.some((b) => g.blocks.includes(b)); i++) { g.step(1 / 120, 1 / 120); } assert.ok(!row.some((b) => g.blocks.includes(b)), 'rocket left the field'); assert.deepEqual(Object.keys(g.departingColumns()), []); }); });