BlueGit

projects-timfalken-com meteors

Files

meteors / test / mini-layout.test.js

mini-layout.test.js

mini-layout.test.js 6.6 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const MiniLayout = require('../src/miniLayout.js');
6const Online = require('../src/online.js');
7
8function hudOf (vh)
9{
10 return Math.max(56, Math.round(vh * 0.09));
11}
12function marginOf (vw, vh)
13{
14 return Math.max(6, Math.round(Math.min(vw, vh) * 0.015));
15}
16function plan (vw, vh, n)
17{
18 return MiniLayout.plan(vw, vh, n, hudOf(vh), marginOf(vw, vh));
19}
20
21describe('MiniLayout (t1125 / t1127 / t1131 / t1133)', () =>
22{
23 it('uses sides on a wide desktop and top on a narrow or portrait one', () =>
24 {
25 const wide = plan(1280, 900, 2);
26 assert.equal(wide.mode, 'sides', '1280×900 with 2 opponents is sides');
27 assert.ok(wide.tile >= MiniLayout.MIN_TILE_FOR_SIDES, 'tiles stay large enough');
28 assert.equal(wide.miniH, 0, 'sides mode does not reserve a top band');
29 assert.equal(wide.positions.length, 2);
30
31 const shortWide = plan(1024, 532, 3);
32 assert.equal(shortWide.mode, 'sides', '1024×532 (Tim\'s window) is sides, not a top row');
33 assert.equal(shortWide.positions.length, 3);
34
35 const narrow = plan(720, 800, 2);
36 assert.equal(narrow.mode, 'top', '720-tall-ish keeps the top row');
37 assert.ok(narrow.miniH > 0, 'top mode reserves height above the field');
38 assert.equal(narrow.positions.length, 2, 'top mode also places its minis (t1203)');
39
40 const mobile = plan(393, 851, 1);
41 assert.equal(mobile.mode, 'top', 'phone stays on the top layout');
42 });
43
44 it('anchors minis to the outer edges and fills inward, bottom-aligned (t1133)', () =>
45 {
46 const p = plan(1024, 532, 3);
47 assert.equal(p.mode, 'sides');
48 assert.equal(p.positions.length, 3);
49 assert.deepEqual(p.positions.map(x => x.side), ['left', 'right', 'left']);
50 assert.deepEqual(p.positions.map(x => x.row), [0, 0, 0], '3 bots fit in one row');
51 assert.deepEqual(p.positions.map(x => x.fromOuter), [0, 0, 1]);
52
53 const [a, b, c] = p.positions;
54 assert.ok(Math.abs(a.x - MiniLayout.EDGE) <= 1, 'bot 1 sits at the left screen edge, x=' + a.x);
55 assert.ok(Math.abs((b.x + b.w) - (1024 - MiniLayout.EDGE)) <= 1,
56 'bot 2 sits at the right screen edge');
57 assert.ok(c.x > a.x, 'bot 3 is inward of bot 1, toward the field');
58 assert.ok(c.x + c.w + MiniLayout.GAP <= p.fieldX + 0.5,
59 'bot 3 does not overlap the field');
60 const bottom = p.fieldY + p.fieldH;
61 for (const m of p.positions)
62 {
63 assert.ok(Math.abs((m.y + m.h) - bottom) <= 2,
64 'mini ' + m.index + ' is bottom-aligned with the field');
65 }
66 });
67
68 it('fits 8 bots in a single row at 1400 and 1920, and wraps on a narrower wide screen (t1127)', () =>
69 {
70 const wide = plan(1400, 900, 8);
71 assert.equal(wide.mode, 'sides');
72 assert.equal(wide.positions.length, 8);
73 assert.ok(wide.positions.every(x => x.row === 0), '1400×900: all 8 in one row');
74 assert.ok(wide.colsPerSide >= 4, 'at least 4 columns per side, got ' + wide.colsPerSide);
75 const lefts = wide.positions.filter(x => x.side === 'left');
76 const rights = wide.positions.filter(x => x.side === 'right');
77 assert.equal(lefts.length, 4);
78 assert.equal(rights.length, 4);
79 // Outer to inward: x increases on the left, decreases on the right.
80 for (let i = 1; i < lefts.length; i++)
81 { assert.ok(lefts[i].x > lefts[i - 1].x, 'left fills inward'); }
82 for (let i = 1; i < rights.length; i++)
83 { assert.ok(rights[i].x < rights[i - 1].x, 'right fills inward'); }
84 assert.ok(Math.abs(lefts[0].x - MiniLayout.EDGE) <= 1, 'first left is at the outer edge');
85 assert.ok(Math.abs((rights[0].x + rights[0].w) - (1400 - MiniLayout.EDGE)) <= 1,
86 'first right is at the outer edge');
87
88 const fullhd = plan(1920, 1080, 8);
89 assert.equal(fullhd.mode, 'sides');
90 assert.ok(fullhd.positions.every(x => x.row === 0), '1920×1080: all 8 in one row');
91 assert.ok(fullhd.colsPerSide >= 4);
92
93 const narrower = plan(1280, 900, 8);
94 assert.equal(narrower.mode, 'sides');
95 const rowsUsed = Math.max(...narrower.positions.map(x => x.row));
96 assert.ok(rowsUsed >= 1, '1280×900 wraps to a second row for 8 bots');
97 const row0 = narrower.positions.filter(x => x.row === 0);
98 const row1 = narrower.positions.filter(x => x.row === 1);
99 assert.ok(row0.length >= 2 && row1.length >= 2);
100 // New row starts again at the outer edges.
101 const r1L = row1.find(x => x.side === 'left');
102 const r0L = row0.find(x => x.side === 'left' && x.fromOuter === 0);
103 assert.ok(r1L && r0L);
104 assert.equal(r1L.x, r0L.x, 'wrapped left mini is again at the outer edge');
105 assert.ok(r1L.y < r0L.y, 'wrapped row sits above the first');
106 assert.ok(r1L.y >= hudOf(900) + 3, 'wrapped row stays under the HUD');
107 });
108
109 it('shrinks minis so stacked rows stay under the HUD', () =>
110 {
111 const p = plan(1100, 700, 16);
112 assert.equal(p.mode, 'sides');
113 for (const m of p.positions)
114 {
115 assert.ok(m.y >= hudOf(700) + 3, 'mini top stays below the HUD, y=' + m.y);
116 assert.ok(m.canvasH >= 40, 'minis do not shrink below a readable size');
117 }
118 });
119
120 it('solo (n=0) never reserves minis', () =>
121 {
122 const p = plan(1280, 900, 0);
123 assert.equal(p.mode, 'top');
124 assert.equal(p.miniH, 0);
125 assert.equal(p.positions.length, 0);
126 });
127});
128
129describe('Online flight visuals (t1125)', () =>
130{
131 it('classifies player, enemy, and miss kinds', () =>
132 {
133 const orig = Online.playerId;
134 Online.playerId = function () { return 'player'; };
135 try
136 {
137 assert.equal(Online.flightKind('player', 'bot-1', false), 'player-to-enemy');
138 assert.equal(Online.flightKind('bot-1', 'player', false), 'enemy-to-player');
139 assert.equal(Online.flightKind('bot-1', 'bot-2', false), 'enemy-to-enemy');
140 assert.equal(Online.flightKind('player', null, true), 'player-miss');
141 assert.equal(Online.flightKind('bot-1', null, true), 'enemy-miss');
142 assert.equal(Online.flightKind('bot-3', 'player', false), 'enemy-to-player');
143 }
144 finally
145 { Online.playerId = orig; }
146 });
147
148 it('arc is a parabola that peaks at mid-flight above the chord', () =>
149 {
150 const start = { x: 0, y: 200 };
151 const end = { x: 100, y: 200 };
152 const h = 80;
153 const mid = Online.arcPoint(start, end, 0.5, h);
154 assert.equal(mid.x, 50);
155 assert.equal(mid.y, 120);
156 const a = Online.arcPoint(start, end, 0, h);
157 const b = Online.arcPoint(start, end, 1, h);
158 assert.deepEqual(a, start);
159 assert.deepEqual(b, end);
160 const q1 = Online.arcPoint(start, end, 0.25, h);
161 const q3 = Online.arcPoint(start, end, 0.75, h);
162 assert.equal(q1.y, q3.y);
163 assert.ok(q1.y < 200 && mid.y < q1.y);
164 });
165
166 it('arc height scales with distance, clamped', () =>
167 {
168 const short = Online.arcHeight({ x: 0, y: 0 }, { x: 10, y: 0 });
169 const long = Online.arcHeight({ x: 0, y: 0 }, { x: 2000, y: 0 });
170 assert.equal(short, 48);
171 assert.equal(long, 220);
172 const mid = Online.arcHeight({ x: 0, y: 0 }, { x: 400, y: 0 });
173 assert.ok(Math.abs(mid - 400 * 0.28) < 1e-9);
174 });
175});