BlueGit

projects-timfalken-com meteors

Files

meteors / test / garbage-queue.test.js

garbage-queue.test.js

garbage-queue.test.js 5.5 KiB Download Raw
1'use strict';
2
3// t1192: received blocks queue and are never lost.
4
5const { describe, it } = require('node:test');
6const assert = require('node:assert/strict');
7const { makeGame } = require('./helpers.js');
8const { GameCore } = require('../src/gameCore.js');
9
10const GRAY = GameCore.COLOR_GRAY;
11
12function lockedExcept (g, open)
13{
14 for (let c = 0; c < g.width; c++)
15 {
16 if (open.includes(c)) continue;
17 for (let r = 0; r < g.height; r++) g.place(c, r, (r + c) % 5, 'locked');
18 }
19}
20
21function run (g, seconds, onShot)
22{
23 let shots = 0;
24 for (let i = 0; i < Math.round(seconds * 120); i++)
25 {
26 g.step(1 / 120, 1 / 120);
27 shots += g.consumeOutgoingShots().length;
28 }
29 return shots;
30}
31
32function grayCount (g, cols)
33{
34 return g.blocks.filter((b) => cols.includes(b.col) && (b.color === GRAY || b.garbage)).length;
35}
36
37describe('Received garbage queue (t1192)', () =>
38{
39 it('exact scenario: 5 blocks at once, 2 open columns, all aimed at one → all 5 land', () =>
40 {
41 for (const seed of [1, 2, 3, 4, 5, 6])
42 {
43 const g = makeGame({ noSpawn: true, seed });
44 lockedExcept(g, [3, 7]);
45 const before = g.blocks.length;
46 for (let i = 0; i < 5; i++) g.ingestGarbage(3);
47 const shots = run(g, 8);
48 assert.equal(g.pendingGarbage(), 0, 'queue drained (seed ' + seed + ')');
49 assert.equal(g.blocks.length - before, 5, 'all 5 on the field (seed ' + seed + ')');
50 assert.equal(shots, 0, 'none culled off the top / sent back');
51 assert.equal(g.score, 0, 'receiver gains no points from them');
52 // Stacked without overlap in columns 3 and 7.
53 for (const c of [3, 7])
54 {
55 const ys = g.blocksInColumn(c).map((b) => b.y);
56 ys.forEach((y, k) => assert.equal(y, k, 'col ' + c + ' row ' + k));
57 }
58 }
59 });
60
61 it('random aims with 2 open columns: still all 5 land', () =>
62 {
63 const g = makeGame({ noSpawn: true, seed: 9 });
64 lockedExcept(g, [0, 9]);
65 const before = g.blocks.length;
66 for (const c of [0, 9, 0, 4, 9]) g.ingestGarbage(c);
67 run(g, 8);
68 assert.equal(g.blocks.length - before, 5);
69 assert.equal(g.pendingGarbage(), 0);
70 });
71
72 it('blocks in one column never overlap and drop only after a full cell of clearance', () =>
73 {
74 const g = makeGame({ noSpawn: true, seed: 2 });
75 lockedExcept(g, [5]);
76 for (let i = 0; i < 3; i++) g.ingestGarbage(5);
77 assert.equal(g.pendingGarbage(), 2, 'one drops now, two wait');
78 let minGap = Infinity;
79 for (let i = 0; i < 120 * 6; i++)
80 {
81 g.step(1 / 120, 1 / 120);
82 const col = g.blocksInColumn(5);
83 for (let k = 1; k < col.length; k++) minGap = Math.min(minGap, col[k].y - col[k - 1].y);
84 }
85 assert.ok(minGap >= 1 - 1e-6, 'min gap ' + minGap);
86 assert.equal(g.blocksInColumn(5).length, 3);
87 });
88
89 it('no open column: garbage waits (not dropped, not lost) and lands when room appears', () =>
90 {
91 const g = makeGame({ noSpawn: true, seed: 3 });
92 lockedExcept(g, [2]);
93 // The only open column holds a rocket that is leaving (t1184).
94 const r = g.place(2, 0, 1);
95 const cluster = g.launchCluster([r], 30);
96 for (let i = 0; i < 400 && r.y < 10; i++) g.step(1 / 120, 1 / 120);
97 assert.ok(g.departingColumns()[2]);
98 assert.equal(g.ingestGarbage(2), null);
99 assert.equal(g.ingestGarbage(7), null);
100 assert.equal(g.pendingGarbage(), 2, 'both wait');
101 run(g, 5);
102 assert.equal(g.state, 'playing');
103 assert.equal(g.pendingGarbage(), 0, 'dropped after the rocket left');
104 assert.equal(g.blocksInColumn(2).filter((b) => b.color === GRAY).length, 2);
105 assert.ok(cluster);
106 });
107
108 it('a full column (counting blocks still falling) is skipped for another open one', () =>
109 {
110 const g = makeGame({ noSpawn: true, seed: 4 });
111 lockedExcept(g, [1, 6]);
112 for (let r = 0; r < 12; r++) g.place(1, r, r % 5, 'still'); // one cell left in col 1
113 const before = g.blocks.length;
114 for (let i = 0; i < 4; i++) g.ingestGarbage(1);
115 run(g, 10);
116 assert.equal(g.blocks.length - before, 4);
117 assert.equal(g.blocksInColumn(1).length, 13, 'col 1 filled to the top, not beyond');
118 assert.equal(g.blocksInColumn(6).length, 3, 'rest went to col 6');
119 assert.equal(g.pendingGarbage(), 0);
120 });
121
122 it('open wanted column wins; a free field takes all at once in distinct columns', () =>
123 {
124 const g = makeGame({ noSpawn: true, seed: 5 });
125 const a = g.ingestGarbage(4);
126 assert.equal(a.col, 4);
127 const b = g.ingestGarbage(4);
128 assert.ok(b && b.col !== 4, 'second picks another column instead of overlapping');
129 assert.equal(g.pendingGarbage(), 0);
130 });
131
132 it('garbage is not accepted after game over (as before)', () =>
133 {
134 const g = makeGame({ noSpawn: true });
135 g.state = 'gameover';
136 assert.equal(g.ingestGarbage(1), null);
137 assert.equal(g.pendingGarbage(), 0);
138 });
139});
140
141describe('Received garbage queue — offline bots (t1192)', () =>
142{
143 it('5 shots at once to a bot with 2 open columns: all 5 land on the bot', () =>
144 {
145 const { Planet, Rng } = require('./helpers.js');
146 const Offline = require('../src/offline.js');
147 const battle = Offline.createBattle({ playerPlanet: Planet.defaultPlanet(), rng: Rng.create(7), round: 1 });
148 const bot = battle.bots[0];
149 const core = bot.core;
150 core.spawnInterval = 1e9;
151 lockedExcept(core, [2, 8]);
152 const before = core.blocks.length;
153 for (let i = 0; i < 5; i++)
154 { battle.pending.push({ at: battle.clock, targetId: bot.id, color: 1, fromId: 'me', landCol: 2 }); }
155 const player = makeGame({ noSpawn: true });
156 for (let i = 0; i < 120 * 8; i++)
157 {
158 battle.clock += 1 / 120;
159 battle._deliver(player);
160 core.step(1 / 120, 1 / 120);
161 core.consumeOutgoingShots();
162 }
163 assert.equal(core.pendingGarbage(), 0);
164 assert.equal(core.blocks.length - before, 5, 'all 5 on the bot field');
165 });
166});