'use strict'; const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const MiniLayout = require('../src/miniLayout.js'); const Online = require('../src/online.js'); function hudOf (vh) { return Math.max(56, Math.round(vh * 0.09)); } function marginOf (vw, vh) { return Math.max(6, Math.round(Math.min(vw, vh) * 0.015)); } function plan (vw, vh, n) { return MiniLayout.plan(vw, vh, n, hudOf(vh), marginOf(vw, vh)); } describe('MiniLayout (t1125 / t1127 / t1131 / t1133)', () => { it('uses sides on a wide desktop and top on a narrow or portrait one', () => { const wide = plan(1280, 900, 2); assert.equal(wide.mode, 'sides', '1280×900 with 2 opponents is sides'); assert.ok(wide.tile >= MiniLayout.MIN_TILE_FOR_SIDES, 'tiles stay large enough'); assert.equal(wide.miniH, 0, 'sides mode does not reserve a top band'); assert.equal(wide.positions.length, 2); const shortWide = plan(1024, 532, 3); assert.equal(shortWide.mode, 'sides', '1024×532 (Tim\'s window) is sides, not a top row'); assert.equal(shortWide.positions.length, 3); const narrow = plan(720, 800, 2); assert.equal(narrow.mode, 'top', '720-tall-ish keeps the top row'); assert.ok(narrow.miniH > 0, 'top mode reserves height above the field'); assert.equal(narrow.positions.length, 2, 'top mode also places its minis (t1203)'); const mobile = plan(393, 851, 1); assert.equal(mobile.mode, 'top', 'phone stays on the top layout'); }); it('anchors minis to the outer edges and fills inward, bottom-aligned (t1133)', () => { const p = plan(1024, 532, 3); assert.equal(p.mode, 'sides'); assert.equal(p.positions.length, 3); assert.deepEqual(p.positions.map(x => x.side), ['left', 'right', 'left']); assert.deepEqual(p.positions.map(x => x.row), [0, 0, 0], '3 bots fit in one row'); assert.deepEqual(p.positions.map(x => x.fromOuter), [0, 0, 1]); const [a, b, c] = p.positions; assert.ok(Math.abs(a.x - MiniLayout.EDGE) <= 1, 'bot 1 sits at the left screen edge, x=' + a.x); assert.ok(Math.abs((b.x + b.w) - (1024 - MiniLayout.EDGE)) <= 1, 'bot 2 sits at the right screen edge'); assert.ok(c.x > a.x, 'bot 3 is inward of bot 1, toward the field'); assert.ok(c.x + c.w + MiniLayout.GAP <= p.fieldX + 0.5, 'bot 3 does not overlap the field'); const bottom = p.fieldY + p.fieldH; for (const m of p.positions) { assert.ok(Math.abs((m.y + m.h) - bottom) <= 2, 'mini ' + m.index + ' is bottom-aligned with the field'); } }); it('fits 8 bots in a single row at 1400 and 1920, and wraps on a narrower wide screen (t1127)', () => { const wide = plan(1400, 900, 8); assert.equal(wide.mode, 'sides'); assert.equal(wide.positions.length, 8); assert.ok(wide.positions.every(x => x.row === 0), '1400×900: all 8 in one row'); assert.ok(wide.colsPerSide >= 4, 'at least 4 columns per side, got ' + wide.colsPerSide); const lefts = wide.positions.filter(x => x.side === 'left'); const rights = wide.positions.filter(x => x.side === 'right'); assert.equal(lefts.length, 4); assert.equal(rights.length, 4); // Outer to inward: x increases on the left, decreases on the right. for (let i = 1; i < lefts.length; i++) { assert.ok(lefts[i].x > lefts[i - 1].x, 'left fills inward'); } for (let i = 1; i < rights.length; i++) { assert.ok(rights[i].x < rights[i - 1].x, 'right fills inward'); } assert.ok(Math.abs(lefts[0].x - MiniLayout.EDGE) <= 1, 'first left is at the outer edge'); assert.ok(Math.abs((rights[0].x + rights[0].w) - (1400 - MiniLayout.EDGE)) <= 1, 'first right is at the outer edge'); const fullhd = plan(1920, 1080, 8); assert.equal(fullhd.mode, 'sides'); assert.ok(fullhd.positions.every(x => x.row === 0), '1920×1080: all 8 in one row'); assert.ok(fullhd.colsPerSide >= 4); const narrower = plan(1280, 900, 8); assert.equal(narrower.mode, 'sides'); const rowsUsed = Math.max(...narrower.positions.map(x => x.row)); assert.ok(rowsUsed >= 1, '1280×900 wraps to a second row for 8 bots'); const row0 = narrower.positions.filter(x => x.row === 0); const row1 = narrower.positions.filter(x => x.row === 1); assert.ok(row0.length >= 2 && row1.length >= 2); // New row starts again at the outer edges. const r1L = row1.find(x => x.side === 'left'); const r0L = row0.find(x => x.side === 'left' && x.fromOuter === 0); assert.ok(r1L && r0L); assert.equal(r1L.x, r0L.x, 'wrapped left mini is again at the outer edge'); assert.ok(r1L.y < r0L.y, 'wrapped row sits above the first'); assert.ok(r1L.y >= hudOf(900) + 3, 'wrapped row stays under the HUD'); }); it('shrinks minis so stacked rows stay under the HUD', () => { const p = plan(1100, 700, 16); assert.equal(p.mode, 'sides'); for (const m of p.positions) { assert.ok(m.y >= hudOf(700) + 3, 'mini top stays below the HUD, y=' + m.y); assert.ok(m.canvasH >= 40, 'minis do not shrink below a readable size'); } }); it('solo (n=0) never reserves minis', () => { const p = plan(1280, 900, 0); assert.equal(p.mode, 'top'); assert.equal(p.miniH, 0); assert.equal(p.positions.length, 0); }); }); describe('Online flight visuals (t1125)', () => { it('classifies player, enemy, and miss kinds', () => { const orig = Online.playerId; Online.playerId = function () { return 'player'; }; try { assert.equal(Online.flightKind('player', 'bot-1', false), 'player-to-enemy'); assert.equal(Online.flightKind('bot-1', 'player', false), 'enemy-to-player'); assert.equal(Online.flightKind('bot-1', 'bot-2', false), 'enemy-to-enemy'); assert.equal(Online.flightKind('player', null, true), 'player-miss'); assert.equal(Online.flightKind('bot-1', null, true), 'enemy-miss'); assert.equal(Online.flightKind('bot-3', 'player', false), 'enemy-to-player'); } finally { Online.playerId = orig; } }); it('arc is a parabola that peaks at mid-flight above the chord', () => { const start = { x: 0, y: 200 }; const end = { x: 100, y: 200 }; const h = 80; const mid = Online.arcPoint(start, end, 0.5, h); assert.equal(mid.x, 50); assert.equal(mid.y, 120); const a = Online.arcPoint(start, end, 0, h); const b = Online.arcPoint(start, end, 1, h); assert.deepEqual(a, start); assert.deepEqual(b, end); const q1 = Online.arcPoint(start, end, 0.25, h); const q3 = Online.arcPoint(start, end, 0.75, h); assert.equal(q1.y, q3.y); assert.ok(q1.y < 200 && mid.y < q1.y); }); it('arc height scales with distance, clamped', () => { const short = Online.arcHeight({ x: 0, y: 0 }, { x: 10, y: 0 }); const long = Online.arcHeight({ x: 0, y: 0 }, { x: 2000, y: 0 }); assert.equal(short, 48); assert.equal(long, 220); const mid = Online.arcHeight({ x: 0, y: 0 }, { x: 400, y: 0 }); assert.ok(Math.abs(mid - 400 * 0.28) < 1e-9); }); });