'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.equal(pts.length, 2, 'no extra waypoints when both ends are above'); 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); }); });