| 1 | 'use strict'; |
| 2 | |
| 3 | const { describe, it } = require('node:test'); |
| 4 | const assert = require('node:assert/strict'); |
| 5 | const MiniLayout = require('../src/miniLayout.js'); |
| 6 | const Online = require('../src/online.js'); |
| 7 | |
| 8 | describe('MiniLayout (t1125)', () => |
| 9 | { |
| 10 | it('uses sides on a wide desktop and top on a narrow one', () => |
| 11 | { |
| 12 | const wide = MiniLayout.plan(1280, 900, 2, 70, 8); |
| 13 | assert.equal(wide.mode, 'sides', '1280×900 with 2 opponents is sides'); |
| 14 | assert.ok(wide.tile >= MiniLayout.MIN_TILE_FOR_SIDES, 'tiles stay large enough'); |
| 15 | assert.equal(wide.miniH, 0, 'sides mode does not reserve a top band'); |
| 16 | assert.equal(wide.positions.length, 2); |
| 17 | |
| 18 | const justUnder = MiniLayout.plan(MiniLayout.MIN_VIEWPORT_FOR_SIDES - 1, 900, 2, 70, 8); |
| 19 | assert.equal(justUnder.mode, 'top', 'just under 960 px stays on top'); |
| 20 | const narrow = MiniLayout.plan(720, 800, 2, 70, 8); |
| 21 | assert.equal(narrow.mode, 'top', '720-wide keeps the top row'); |
| 22 | assert.ok(narrow.miniH > 0, 'top mode reserves height above the field'); |
| 23 | assert.equal(narrow.positions.length, 0); |
| 24 | |
| 25 | const mobile = MiniLayout.plan(393, 851, 1, 60, 6); |
| 26 | assert.equal(mobile.mode, 'top', 'phone stays on the top layout'); |
| 27 | }); |
| 28 | |
| 29 | it('alternates first left, second right, third left, and stacks upward', () => |
| 30 | { |
| 31 | const plan = MiniLayout.plan(1400, 900, 6, 70, 8); |
| 32 | assert.equal(plan.mode, 'sides'); |
| 33 | assert.equal(plan.positions.length, 6); |
| 34 | const sides = plan.positions.map(p => p.side); |
| 35 | assert.deepEqual(sides, ['left', 'right', 'left', 'right', 'left', 'right']); |
| 36 | const rows = plan.positions.map(p => p.row); |
| 37 | assert.deepEqual(rows, [0, 0, 1, 1, 2, 2]); |
| 38 | |
| 39 | // Lowest row aligns with (or near) the field bottom; higher rows go up. |
| 40 | const lefts = plan.positions.filter(p => p.side === 'left'); |
| 41 | assert.ok(lefts[0].y + lefts[0].h <= plan.fieldY + plan.fieldH + 2, |
| 42 | 'first row sits at the field bottom'); |
| 43 | assert.ok(lefts[1].y < lefts[0].y, 'second row is above the first'); |
| 44 | assert.ok(lefts[2].y < lefts[1].y, 'third row is above the second'); |
| 45 | // Same x for a column |
| 46 | assert.equal(lefts[0].x, lefts[1].x); |
| 47 | assert.ok(lefts[0].x + lefts[0].w <= plan.fieldX - MiniLayout.GAP + 0.5); |
| 48 | const rights = plan.positions.filter(p => p.side === 'right'); |
| 49 | assert.ok(rights[0].x >= plan.fieldX + plan.fieldW + MiniLayout.GAP - 0.5); |
| 50 | }); |
| 51 | |
| 52 | it('shrinks minis so stacked rows stay under the HUD', () => |
| 53 | { |
| 54 | const plan = MiniLayout.plan(1400, 800, 12, 70, 8); |
| 55 | assert.equal(plan.mode, 'sides'); |
| 56 | for (const p of plan.positions) |
| 57 | { |
| 58 | assert.ok(p.y >= 70 + 3, 'mini top stays below the HUD, y=' + p.y); |
| 59 | assert.ok(p.canvasH >= 40, 'minis do not shrink below a readable size'); |
| 60 | } |
| 61 | }); |
| 62 | |
| 63 | it('solo (n=0) never reserves minis', () => |
| 64 | { |
| 65 | const plan = MiniLayout.plan(1280, 900, 0, 70, 8); |
| 66 | assert.equal(plan.mode, 'top'); |
| 67 | assert.equal(plan.miniH, 0); |
| 68 | assert.equal(plan.positions.length, 0); |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | describe('Online flight visuals (t1125)', () => |
| 73 | { |
| 74 | it('classifies player, enemy, and miss kinds', () => |
| 75 | { |
| 76 | const orig = Online.playerId; |
| 77 | Online.playerId = function () { return 'player'; }; |
| 78 | try |
| 79 | { |
| 80 | assert.equal(Online.flightKind('player', 'bot-1', false), 'player-to-enemy'); |
| 81 | assert.equal(Online.flightKind('bot-1', 'player', false), 'enemy-to-player'); |
| 82 | assert.equal(Online.flightKind('bot-1', 'bot-2', false), 'enemy-to-enemy'); |
| 83 | assert.equal(Online.flightKind('player', null, true), 'player-miss'); |
| 84 | assert.equal(Online.flightKind('bot-1', null, true), 'enemy-miss'); |
| 85 | assert.equal(Online.flightKind('bot-3', 'player', false), 'enemy-to-player'); |
| 86 | } |
| 87 | finally |
| 88 | { Online.playerId = orig; } |
| 89 | }); |
| 90 | |
| 91 | it('arc is a parabola that peaks at mid-flight above the chord', () => |
| 92 | { |
| 93 | const start = { x: 0, y: 200 }; |
| 94 | const end = { x: 100, y: 200 }; |
| 95 | const h = 80; |
| 96 | const mid = Online.arcPoint(start, end, 0.5, h); |
| 97 | assert.equal(mid.x, 50); |
| 98 | // 4*h*t*(1-t) at t=0.5 is h, so y = 200 - 80 |
| 99 | assert.equal(mid.y, 120); |
| 100 | const a = Online.arcPoint(start, end, 0, h); |
| 101 | const b = Online.arcPoint(start, end, 1, h); |
| 102 | assert.deepEqual(a, start); |
| 103 | assert.deepEqual(b, end); |
| 104 | // Symmetric and always above (smaller y) the chord |
| 105 | const q1 = Online.arcPoint(start, end, 0.25, h); |
| 106 | const q3 = Online.arcPoint(start, end, 0.75, h); |
| 107 | assert.equal(q1.y, q3.y); |
| 108 | assert.ok(q1.y < 200 && mid.y < q1.y); |
| 109 | }); |
| 110 | |
| 111 | it('arc height scales with distance, clamped', () => |
| 112 | { |
| 113 | const short = Online.arcHeight({ x: 0, y: 0 }, { x: 10, y: 0 }); |
| 114 | const long = Online.arcHeight({ x: 0, y: 0 }, { x: 2000, y: 0 }); |
| 115 | assert.equal(short, 48); |
| 116 | assert.equal(long, 220); |
| 117 | const mid = Online.arcHeight({ x: 0, y: 0 }, { x: 400, y: 0 }); |
| 118 | assert.ok(Math.abs(mid - 400 * 0.28) < 1e-9); |
| 119 | }); |
| 120 | }); |
projects-timfalken-com meteors