'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const { makeGame, GameCore, Planet, Rng } = require('./helpers.js'); const Offline = require('../src/offline.js'); const Online = require('../src/online.js'); const MiniLayout = require('../src/miniLayout.js'); describe('Garbage landing column (t1126)', () => { it('pickGarbageColumn keeps a preferred open column', () => { const g = makeGame({ noSpawn: true, seed: 3 }); assert.equal(g.pickGarbageColumn(4), 4); assert.equal(g.ingestGarbage(4).col, 4); }); it('pickGarbageColumn skips a full preferred column and still returns an open one', () => { const g = makeGame({ noSpawn: true, seed: 3 }); for (let r = 0; r < 13; r++) { g.place(4, r, r % 5); } const col = g.pickGarbageColumn(4); assert.notEqual(col, 4); assert.ok(col >= 0 && col < 10); const block = g.ingestGarbage(4); assert.notEqual(block.col, 4, 'ingest also skips the full preferred column'); assert.ok(block.col >= 0 && block.col < 10); }); it('offline dispatch picks land_col and ingest uses the same column', () => { const easy = Planet.createPlanet({ jumpsToClear: 1, fallTime: 5, riseTime: 3, id: 'easy' }); const hitRng = () => 0.99; const battle = Offline.createBattle({ playerPlanet: easy, rng: hitRng, round: 1 }); const player = makeGame({ noSpawn: true }); // Bot shoots at the player: from_id is the bot. const bot = battle.bots[0]; bot.core.outgoingShots.push({ id: 11, color: 2, col: 3, y: 13 }); battle.tick(0.01, player); const flight = battle.flightResults.find((f) => f.from_id === bot.id && !f.miss); assert.ok(flight, 'bot hit the player'); assert.equal(typeof flight.land_col, 'number'); assert.ok(flight.land_col >= 0 && flight.land_col < 10); assert.equal(battle.pending[0].landCol, flight.land_col); const before = player.blocks.length; battle.tick(Offline.FLIGHT_DURATION + 0.05, player); assert.equal(battle.pending.length, 0); const greys = player.blocks.filter((b) => b.color === GameCore.COLOR_GRAY); assert.ok(greys.length >= 1, 'player received grey garbage'); assert.equal(greys[greys.length - 1].col, flight.land_col, 'ingested column matches the flight land_col'); assert.ok(player.blocks.length > before); }); }); describe('Around-field flight path (t1128)', () => { const field = { left: 335, top: 173, right: 335 + 550, bottom: 173 + 715 }; it('routes a left-mini shot around the field into a given column', () => { const start = { x: 250, y: 700 }; const end = { x: field.left + (3 + 0.5) / 10 * (field.right - field.left), y: field.top + 4 }; const pts = Online.waypointsAroundField(start, end, field, 72); assert.ok(pts.length >= 2); for (let i = 0; i <= 20; i++) { const u = i / 20; const p = Online.aroundFieldPoint(start, end, u, field, 72); assert.equal(Online.pointInFieldInterior(p.x, p.y, field), false, 'u=' + u + ' is inside the field at ' + p.x + ',' + p.y); } const landed = Online.aroundFieldPoint(start, end, 1, field, 72); assert.ok(Math.abs(landed.x - end.x) < 0.5, 'ends at the landing column x'); assert.ok(Math.abs(landed.y - end.y) < 1, 'ends at the field top'); }); it('routes a player shot around the field to a right-hand mini', () => { const start = { x: field.left + (6 + 0.5) / 10 * (field.right - field.left), y: field.top + 4 }; const end = { x: field.right + 80, y: 700 }; for (let i = 0; i <= 20; i++) { const p = Online.aroundFieldPoint(start, end, i / 20, field, 72); assert.equal(Online.pointInFieldInterior(p.x, p.y, field), false, 'player→enemy u=' + (i / 20) + ' crosses the field'); } }); it('a path already above the field stays as a simple arc above it', () => { const start = { x: 400, y: 80 }; const end = { x: 600, y: field.top + 4 }; const pts = Online.waypointsAroundField(start, end, field, 72); assert.ok(pts.length >= 2); for (let i = 0; i <= 16; i++) { const p = Online.aroundFieldPoint(start, end, i / 16, field, 72); assert.equal(Online.pointInFieldInterior(p.x, p.y, field), false); assert.ok(p.y <= field.top + 6, 'stays at or above the top edge, y=' + p.y); } }); }); describe('Wide-layout top padding (t1129)', () => { it('leaves room above the field on a wide screen, not on narrow or mobile', () => { const wide = MiniLayout.plan(1280, 900, 2, 70, 8); assert.equal(wide.mode, 'sides'); assert.ok(wide.fieldY >= 70 + MiniLayout.ARC_TOP_PAD - 1, 'fieldY=' + wide.fieldY + ' should be at least hud+pad=' + (70 + MiniLayout.ARC_TOP_PAD)); assert.ok(wide.fieldY - 70 >= MiniLayout.ARC_TOP_PAD * 0.9); const narrow = MiniLayout.plan(720, 800, 2, 70, 8); assert.equal(narrow.mode, 'top'); // Unchanged: top band is hud + miniH, no arc pad. assert.ok(narrow.miniH > 0); assert.ok(narrow.fieldY >= 70 + narrow.miniH - 1); const mobile = MiniLayout.plan(393, 851, 1, 60, 6); assert.equal(mobile.mode, 'top'); }); }); describe('takeLandCol consistency (t1126)', () => { it('prefers the server land_col and falls back to a remembered id', () => { Online._pendingLandCol = Object.create(null); assert.equal(Online.takeLandCol({ id: 9, land_col: 6 }), 6); Online.rememberLandCol('abc', 2); assert.equal(Online.takeLandCol({ client_id: 'abc' }), 2); // Server value wins over the remembered one. assert.equal(Online.takeLandCol({ client_id: 'abc', land_col: 8 }), 8); assert.equal(Online.takeLandCol({ id: 1 }), null); }); }); describe('Smooth C1 arcs (t1134)', () => { const field = { left: 335, top: 173, right: 335 + 550, bottom: 173 + 715, width: 550, height: 715 }; function entry (col) { return Online.cellViewport(col, 13, field); } function cell (col, y) { return Online.cellViewport(col, y, field); } it('is a C1 curve: tangents and speeds match at joints, no corners', () => { const start = { x: 250, y: 700 }; const end = entry(3); const curve = Online.makeAroundCurve(start, end, field, { clearance: 80 }); assert.ok(curve.segments && curve.segments.length >= 2, 'multi-segment around-field path'); const n = curve.segments.length; for (let i = 1; i < n; i++) { const u = i / n; const left = Online.curveDeriv(curve, u - 1e-6); const right = Online.curveDeriv(curve, u + 1e-6); const aL = Math.atan2(left.y, left.x); const aR = Math.atan2(right.y, right.x); let da = Math.abs(aR - aL); if (da > Math.PI) { da = 2 * Math.PI - da; } assert.ok(da < 0.02, 'joint ' + i + ' direction jump ' + da); const sL = Math.hypot(left.x, left.y); const sR = Math.hypot(right.x, right.y); assert.ok(Math.abs(sL - sR) / Math.max(sL, 1) < 0.05, 'joint ' + i + ' speed jump ' + sL + ' vs ' + sR); } // Densely sampled tangent never snaps by a large angle (no corner). let prev = null; let maxDa = 0; for (let i = 0; i <= 80; i++) { const d = Online.curveDeriv(curve, i / 80); const a = Math.atan2(d.y, d.x); if (prev != null) { let da = Math.abs(a - prev); if (da > Math.PI) { da = 2 * Math.PI - da; } if (da > maxDa) { maxDa = da; } } prev = a; } assert.ok(maxDa < 0.35, 'max step angle ' + maxDa + ' looks like a corner'); }); it('leaves a mid-field cell going up and stays in launch/land corridors', () => { const start = cell(6, 12); const end = { x: field.right + 80, y: 700 }; assert.ok(start.y > field.top, 'start is inside the field (below the top edge)'); assert.ok(start.y < field.top + field.height * 0.2, 'start is near the top row'); const d0 = Online.curveDeriv(Online.makeAroundCurve(start, end, field, { clearance: 80 }), 0); assert.ok(d0.y < 0, 'initial tangent is upward, vy=' + d0.y); for (let i = 0; i <= 32; i++) { const p = Online.aroundFieldPoint(start, end, i / 32, field, 80); assert.equal(Online.pointAllowedOnArc(p, field, start, end), true, 'u=' + (i / 32) + ' at ' + p.x + ',' + p.y + ' is not allowed'); } }); it('dives into the landing column at the spawn pose with downward fallSpeed (t1135)', () => { const start = { x: 250, y: 700 }; const end = entry(3); // Spawn pose is half a tile above the canvas top (y = 13). assert.ok(Math.abs(end.y - (field.top - 0.5 / 13 * field.height)) < 0.5); assert.ok(Math.abs(end.x - (field.left + 3.5 / 10 * field.width)) < 0.5); const duration = 1.2; const fallPx = (13 / 3) * (field.height / 13); const endSpeedPx = (fallPx * duration) / 3; const curve = Online.makeAroundCurve(start, end, field, { clearance: 80, endSpeedPx: endSpeedPx, duration: duration }); const landed = Online.curvePoint(curve, 1); assert.ok(Math.abs(landed.x - end.x) < 0.5); assert.ok(Math.abs(landed.y - end.y) < 0.5); const d1 = Online.curveDeriv(curve, 1); assert.ok(d1.y > 0, 'end tangent points down into the column'); assert.ok(Math.abs(d1.x) < 20, 'end tangent is vertical, dx=' + d1.x); const v1 = 0.85; const pxPerSec = d1.y * v1 / duration; assert.ok(Math.abs(pxPerSec - fallPx) / fallPx < 0.08, 'arrival ' + pxPerSec + ' px/s vs fallSpeed ' + fallPx); }); it('smoothParam has non-zero endpoint derivatives (no visual stop)', () => { const v0 = 0.85; const v1 = 0.85; const mid = Online.smoothParam(0.5, v0, v1); assert.ok(mid > 0.4 && mid < 0.6); const d0 = (Online.smoothParam(0.001, v0, v1) - Online.smoothParam(0, v0, v1)) / 0.001; const d1 = (Online.smoothParam(1, v0, v1) - Online.smoothParam(0.999, v0, v1)) / 0.001; assert.ok(d0 > 0.5, 'start rate ' + d0); assert.ok(d1 > 0.5, 'end rate ' + d1); assert.equal(Online.smoothParam(0, 1, 1), 0); assert.equal(Online.smoothParam(1, 1, 1), 1); assert.ok(Math.abs(Online.smoothParam(0.5, 1, 1) - 0.5) < 1e-9); }); it('cellViewport matches placeTile centres (y=0 at the bottom)', () => { const rect = { left: 100, top: 50, width: 390, height: 507, right: 490, bottom: 557 }; const bottom = Online.cellViewport(0, 0, rect); assert.ok(Math.abs(bottom.x - (100 + 0.5 / 10 * 390)) < 0.01); assert.ok(Math.abs(bottom.y - (50 + (1 - 0.5 / 13) * 507)) < 0.01); const spawn = Online.cellViewport(4, 13, rect); assert.ok(spawn.y < rect.top, 'spawn of a falling block is just above the canvas'); assert.ok(rect.top - spawn.y - 0.5 / 13 * 507 < 0.01); }); });