BlueGit

projects-timfalken-com meteors

Files

meteors / test / mini-top-scale.test.js

mini-top-scale.test.js

mini-top-scale.test.js 4.1 KiB Download Raw
1'use strict';
2
3// t1203: on narrow / portrait screens the opponent minis shrink so they all
4// fit above the player's field; only if even the minimum size does not fit,
5// defeated opponents are sorted to the back (stable).
6
7const { describe, it } = require('node:test');
8const assert = require('node:assert/strict');
9const L = require('../src/miniLayout.js');
10
11const hudFor = (vh) => Math.max(56, Math.round(vh * 0.09));
12const plan = (vw, vh, n) => L.plan(vw, vh, n, hudFor(vh), Math.max(6, Math.round(Math.min(vw, vh) * 0.015)));
13
14function noOverlap (ps)
15{
16 for (let i = 0; i < ps.length; i++)
17 for (let j = i + 1; j < ps.length; j++)
18 {
19 const a = ps[i], b = ps[j];
20 const sep = a.x + a.w <= b.x || b.x + b.w <= a.x || a.y + a.h <= b.y || b.y + b.h <= a.y;
21 if (!sep) return false;
22 }
23 return true;
24}
25
26describe('Top-row mini scaling (t1203)', () =>
27{
28 it('constants: 80 px max, 32 px min canvas width, 9 px min label font', () =>
29 {
30 assert.equal(L.TOP_MAX_W, 80);
31 assert.equal(L.TOP_MIN_W, 32);
32 assert.equal(L.TOP_FONT_MIN, 9);
33 const m = L.topMiniForWidth(10);
34 assert.equal(m.w, 32);
35 assert.equal(m.h, Math.round(32 * 1.3));
36 assert.ok(m.font >= 9);
37 });
38
39 for (const [vw, vh] of [[412, 915], [360, 800], [460, 900], [390, 844]])
40 {
41 for (const n of [6, 7, 8, 10, 12, 16])
42 {
43 it(`${vw}x${vh}, ${n} opponents: all fit above the field, inside the screen, no overlap`, () =>
44 {
45 const p = plan(vw, vh, n);
46 assert.equal(p.mode, 'top');
47 assert.equal(p.mini.fits, true);
48 assert.equal(p.positions.length, n);
49 for (const q of p.positions)
50 {
51 assert.ok(q.aboveField, `slot ${q.slot} bottom ${q.y + q.h} <= field top ${p.fieldY}`);
52 assert.ok(q.x >= 0 && q.x + q.w <= vw, `slot ${q.slot} inside the screen`);
53 assert.ok(q.y >= p.hudH, 'below the HUD');
54 }
55 assert.ok(noOverlap(p.positions));
56 assert.ok(p.mini.w >= L.TOP_MIN_W && p.mini.w <= L.TOP_MAX_W);
57 });
58 }
59 }
60
61 it('few opponents keep the full size; more opponents shrink monotonically', () =>
62 {
63 assert.equal(plan(412, 915, 3).mini.w, 80);
64 let prev = Infinity;
65 for (const n of [3, 6, 12, 16, 20])
66 {
67 const w = plan(412, 915, n).mini.w;
68 assert.ok(w <= prev, n + ': ' + w);
69 prev = w;
70 }
71 });
72
73 it("Tim's case: 6 bots at ~460 px portrait no longer spill behind the field", () =>
74 {
75 const p = plan(460, 900, 6);
76 assert.ok(p.positions.every((q) => q.aboveField));
77 });
78
79 it('order is unchanged while everything fits, even with defeated opponents', () =>
80 {
81 const p = plan(412, 915, 6);
82 const elim = [true, false, true, false, false, false];
83 const pos = L.topPositions(elim, p);
84 assert.deepEqual(pos.map((q) => q.slot), [0, 1, 2, 3, 4, 5]);
85 });
86
87 it('when they do not fit even at the minimum: living first, defeated last, stable', () =>
88 {
89 assert.deepEqual(L.topSlots([true, false, true, false, false], false), [3, 0, 4, 1, 2]);
90 assert.deepEqual(L.topSlots([false, false, false], false), [0, 1, 2]);
91 assert.deepEqual(L.topSlots([true, true], false), [0, 1]);
92 });
93
94 it('overflow at 360x800 with 31 opponents: min size, defeated ones are the ones behind the field', () =>
95 {
96 const n = 31;
97 const p = plan(360, 800, n);
98 assert.equal(p.mini.fits, false);
99 assert.equal(p.mini.w, L.TOP_MIN_W);
100 const cap = p.mini.capacity;
101 assert.ok(cap > 0 && cap < n);
102 const elim = Array.from({ length: n }, (_, i) => i % 3 === 0); // 11 defeated
103 const alive = elim.filter((e) => !e).length;
104 assert.ok(alive <= cap, 'all living fit');
105 const pos = L.topPositions(elim, p);
106 for (const q of pos)
107 {
108 if (!q.eliminated) assert.ok(q.aboveField, 'living opponent ' + q.index + ' visible');
109 }
110 const hidden = pos.filter((q) => !q.aboveField);
111 assert.ok(hidden.length > 0 && hidden.every((q) => q.eliminated), 'only defeated ones fall behind');
112 // stable within groups
113 const aliveSlots = pos.filter((q) => !q.eliminated).map((q) => q.slot);
114 assert.deepEqual(aliveSlots, [...aliveSlots].sort((a, b) => a - b));
115 const elimSlots = pos.filter((q) => q.eliminated).map((q) => q.slot);
116 assert.deepEqual(elimSlots, [...elimSlots].sort((a, b) => a - b));
117 });
118
119 it('wide screens still use the side layout (unchanged)', () =>
120 {
121 assert.equal(plan(1280, 900, 6).mode, 'sides');
122 });
123});