BlueGit

projects-timfalken-com meteors

Files

meteors / test / departing.test.js

departing.test.js

departing.test.js 4.5 KiB Download Raw
1'use strict';
2
3// t1184/t1185/t1186: no new or received blocks in columns of a rocket
4// stack that is certainly leaving (highest block past 75% of the field).
5
6const { describe, it } = require('node:test');
7const assert = require('node:assert/strict');
8const { makeGame, placeRow } = require('./helpers.js');
9
10function launchedRocket (g, cols, height)
11{
12 const row = placeRow(g, 0, cols, 1);
13 const cluster = g.launchCluster(row, height);
14 return { cluster, row };
15}
16
17function riseUntil (g, row, cluster, minTop)
18{
19 for (let i = 0; i < 2000; i++)
20 {
21 const top = Math.max(...row.filter((b) => g.blocks.includes(b)).map((b) => b.y));
22 if (top >= minTop || cluster.phase !== 'up')
23 { return top; }
24 g.step(1 / 120, 1 / 120);
25 }
26 throw new Error('never rose');
27}
28
29describe('Departing rocket columns (t1184)', () =>
30{
31 it('threshold is 75% of the field height', () =>
32 {
33 const g = makeGame({ noSpawn: true });
34 assert.equal(g.DEPART_LINE(), 13 * 0.75);
35 });
36
37 it('a rising stack below 75% does not block its columns', () =>
38 {
39 const g = makeGame({ noSpawn: true });
40 const { cluster, row } = launchedRocket(g, [2, 3, 4], 30);
41 riseUntil(g, row, cluster, 5);
42 assert.deepEqual(Object.keys(g.departingColumns()), []);
43 });
44
45 it('past 75% and certain to leave: all its columns are skipped by new blocks', () =>
46 {
47 const g = makeGame({ noSpawn: true, seed: 11 });
48 const { cluster, row } = launchedRocket(g, [2, 3, 4], 30);
49 const top = riseUntil(g, row, cluster, 10);
50 assert.ok(top >= 9.75 && top < 13, 'top=' + top);
51 assert.deepEqual(Object.keys(g.departingColumns()).map(Number).sort(), [2, 3, 4]);
52 const seen = new Set();
53 for (let i = 0; i < 300; i++)
54 {
55 const b = g.trySpawn();
56 assert.ok(b, 'spawned');
57 seen.add(b.col);
58 g.removeBlock(b);
59 }
60 for (const c of [2, 3, 4])
61 { assert.ok(!seen.has(c), 'spawned in departing column ' + c); }
62 assert.ok(seen.size >= 6, 'other columns still used: ' + [...seen]);
63 });
64
65 it('received garbage aimed at a departing column drops in another column', () =>
66 {
67 const g = makeGame({ noSpawn: true, seed: 5 });
68 const { cluster, row } = launchedRocket(g, [5, 6], 30);
69 riseUntil(g, row, cluster, 10);
70 for (let i = 0; i < 50; i++)
71 {
72 const want = 5 + (i % 2);
73 const b = g.ingestGarbage(want);
74 assert.ok(b);
75 assert.ok(b.col !== 5 && b.col !== 6, 'garbage col ' + b.col);
76 g.removeBlock(b);
77 }
78 // Open preferred columns are still honoured.
79 const ok = g.ingestGarbage(1);
80 assert.equal(ok.col, 1);
81 // pickGarbageColumn (used by bots / previews) also avoids them.
82 for (let i = 0; i < 50; i++)
83 {
84 const c = g.pickGarbageColumn();
85 assert.ok(c !== 5 && c !== 6);
86 }
87 });
88
89 it('a stack whose apex stays below the top is not departing', () =>
90 {
91 const g = makeGame({ noSpawn: true });
92 const { cluster, row } = launchedRocket(g, [3], 10.5);
93 const top = riseUntil(g, row, cluster, 9.8);
94 assert.ok(top >= 9.75, 'top=' + top);
95 assert.deepEqual(Object.keys(g.departingColumns()), []);
96 });
97
98 it('a stack that is coming back down is not departing', () =>
99 {
100 const g = makeGame({ noSpawn: true });
101 const { cluster, row } = launchedRocket(g, [3], 30);
102 riseUntil(g, row, cluster, 10);
103 cluster.phase = 'down';
104 cluster.vy = -1;
105 assert.deepEqual(Object.keys(g.departingColumns()), []);
106 });
107
108 it('if every open column is departing, garbage waits and arrives later (never lost)', () =>
109 {
110 const g = makeGame({ noSpawn: true });
111 const all = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
112 const { cluster, row } = launchedRocket(g, all, 30);
113 riseUntil(g, row, cluster, 10);
114 assert.equal(Object.keys(g.departingColumns()).length, 10);
115 assert.equal(g.trySpawn(), null, 'no new block while every column is departing');
116 // t1192: it waits in the queue (not dropped into the rocket, not lost).
117 assert.equal(g.ingestGarbage(4), null);
118 assert.equal(g.pendingGarbage(), 1);
119 for (let i = 0; i < 600 && g.pendingGarbage(); i++) g.step(1 / 120, 1 / 120);
120 assert.equal(g.pendingGarbage(), 0, 'dropped once the rocket left');
121 assert.ok(g.blocks.some((x) => x.color === -1 && x.state === 'falling' && x.col === 4), 'in the wanted column');
122 });
123
124 it('the stack really leaves and the columns open up again', () =>
125 {
126 const g = makeGame({ noSpawn: true });
127 const { cluster, row } = launchedRocket(g, [2, 3], 30);
128 riseUntil(g, row, cluster, 10);
129 assert.ok(g.departingColumns()[2]);
130 for (let i = 0; i < 600 && row.some((b) => g.blocks.includes(b)); i++)
131 { g.step(1 / 120, 1 / 120); }
132 assert.ok(!row.some((b) => g.blocks.includes(b)), 'rocket left the field');
133 assert.deepEqual(Object.keys(g.departingColumns()), []);
134 });
135});