'use strict'; // t1189 / t1191: opening spawn burst (0.1 s for 3 s), lerp to 1 s by 6 s, // then the 0.3% per-spawn speed-up (only spawns after 6 s count). const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, almost, placeRow } = require('./helpers.js'); const { GameCore } = require('../src/gameCore.js'); function fresh (seed) { const g = makeGame({ seed: seed || 1 }); return g; } describe('Spawn timeline (t1189/t1191)', () => { it('constants', () => { assert.equal(GameCore.SPAWN_INTRO_END, 3); assert.equal(GameCore.SPAWN_INTRO_INTERVAL, 0.1); assert.equal(GameCore.SPAWN_RAMP_END, 6); }); it('interval: 0.1 s until 3 s, linear to 1 s at 6 s, then the per-spawn rate', () => { const g = fresh(); const at = (t) => { g.playTime = t; return g.currentSpawnInterval(); }; assert.equal(at(0), 0.1); assert.equal(at(2.99), 0.1); assert.ok(almost(at(3), 0.1)); assert.ok(almost(at(4.5), 0.55)); assert.ok(almost(at(5.999), 0.1 + 0.9 * (2.999 / 3), 1e-9)); assert.equal(at(6), 1); g.spawnInterval = 0.8; assert.equal(at(7), 0.8); }); it('no speed-up from blocks spawned before 6 s; after 6 s each spawn ×0.997', () => { const g = fresh(2); g.playTime = 1; assert.ok(g.trySpawn()); g.playTime = 5; assert.ok(g.trySpawn()); assert.equal(g.spawnInterval, 1); g.playTime = 6.2; assert.ok(g.trySpawn()); assert.ok(almost(g.spawnInterval, 0.997, 1e-12)); }); it('the real tick follows the timeline; play time starts at the first tick', () => { const g = fresh(3); assert.equal(g.playTime, 0); const ticks = []; const orig = g.trySpawn; g.trySpawn = function () { ticks.push(+this.playTime.toFixed(4)); const b = orig.call(this); if (b) { this.removeBlock(b); } // keep every column open: count spawn ticks return b; }; for (let i = 0; i < 60 * 10; i++) g.tick(1 / 60); const inBurst = ticks.filter((t) => t < 3).length; const inRamp = ticks.filter((t) => t >= 3 && t < 6).length; const after = ticks.filter((t) => t >= 6 && t < 10).length; assert.ok(inBurst >= 29 && inBurst <= 31, 'burst ticks ' + inBurst); // ∫ dt / (0.1 + 0.3 (t-3)) over 3..6 = ln(10)/0.3 ≈ 7.7 assert.ok(inRamp >= 7 && inRamp <= 9, 'ramp ticks ' + inRamp); assert.ok(after >= 4 && after <= 5, 'after ticks ' + after); assert.ok(ticks.length >= 36 && ticks.length <= 40, 'spawn ticks in first 6 s+ ' + ticks.length); assert.ok(almost(g.playTime, 10, 1e-6)); }); it('first 6 s of a normal game: how many blocks really spawn', () => { const g = fresh(4); let spawned = 0; const orig = g.trySpawn; g.trySpawn = function () { const b = orig.call(this); if (b && this.playTime < 6) spawned++; return b; }; for (let i = 0; i < 60 * 6; i++) g.tick(1 / 60); // One falling block per column at a time limits the burst. assert.ok(spawned >= 12 && spawned <= 38, 'spawned ' + spawned); assert.ok(g.state === 'playing'); }); it('burst column choice is weighted towards emptier columns', () => { const g = fresh(5); g.spawnInterval = 1; // normal for (let r = 0; r < 10; r++) g.place(0, r, r % 5); // column 0 almost full const counts = new Array(10).fill(0); for (let i = 0; i < 4000; i++) { counts[g.pickWeightedSpawnColumn([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])]++; } // weight col 0 = 3, others 13 → ~2.4% vs ~10.8% assert.ok(counts[0] < 160, 'full-ish column picked ' + counts[0]); for (let c = 1; c < 10; c++) assert.ok(counts[c] > 330, 'col ' + c + ' ' + counts[c]); }); it('the departing-rocket rule still applies during the burst', () => { const g = fresh(6); g.spawnInterval = 1e9; // drive manually const row = placeRow(g, 0, [3, 4, 5], 1); const cluster = g.launchCluster(row, 30); for (let i = 0; i < 400 && Math.max(...row.map((b) => b.y)) < 10; i++) g.step(1 / 120, 1 / 120); g.playTime = 1; for (let i = 0; i < 200; i++) { const b = g.trySpawn(); assert.ok(b && ![3, 4, 5].includes(b.col), 'col ' + (b && b.col)); g.removeBlock(b); } assert.ok(cluster); }); it('spawning switched off by tools/tests (interval above 1 s) stays off in the burst', () => { const g = makeGame({ noSpawn: true }); for (let i = 0; i < 60 * 4; i++) g.tick(1 / 60); assert.equal(g.blocks.length, 0); }); it('opening: a column takes the next block once the previous one cleared a cell; later one at a time', () => { const g = fresh(7); g.playTime = 1; for (let c = 1; c < 10; c++) g.place(c, 0, 0, 'locked'); const a = g.trySpawn(); assert.equal(a.col, 0); assert.equal(g.trySpawn(), null, 'previous block still in the spawn cell'); a.y = 12; const b = g.trySpawn(); assert.ok(b && b.col === 0 && b.y - a.y >= 1 - 1e-9, 'second block above, no overlap'); g.playTime = 6; a.y = 5; b.y = 9; assert.equal(g.trySpawn(), null, 'after 6 s: one falling block per column'); }); it('a full game through the burst keeps the physics invariants (strict mode)', () => { for (const seed of [1, 2, 3, 4, 5]) { const g = fresh(seed); let spawned = 0; const orig = g.trySpawn; g.trySpawn = function () { const b = orig.call(this); if (b && this.playTime < 6) spawned++; return b; }; for (let i = 0; i < 60 * 20 && g.state === 'playing'; i++) g.tick(1 / 60); assert.ok(spawned >= 25, 'seed ' + seed + ' spawned in first 6 s: ' + spawned); // No two blocks overlap in a column. for (let c = 0; c < g.width; c++) { const col = g.blocksInColumn(c); for (let k = 1; k < col.length; k++) { assert.ok(col[k].y - col[k - 1].y >= 1 - 1e-6, 'overlap col ' + c + ' ' + col[k - 1].y + '/' + col[k].y); } } } }); });