BlueGit

projects-timfalken-com meteors

Files

meteors / test / swap.test.js

swap.test.js

swap.test.js 6.1 KiB Download Raw
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, fillColumn, GameCore } = require('./helpers.js');
6
7describe('Vertical swapping', () =>
8{
9 it('dragging a block through a column swaps it with the blocks it passes', () =>
10 {
11 const g = makeGame({ noSpawn: true });
12 const a = g.place(0, 0, 0);
13 const b = g.place(0, 1, 1);
14 const c = g.place(0, 2, 2);
15 assert.ok(g.beginDrag(0, 0));
16 g.moveDrag(2);
17 g.endDrag();
18 assert.equal(a.y, 2);
19 assert.equal(b.y, 0);
20 assert.equal(c.y, 1);
21 assert.equal(g.blockAt(0, 2).color, 0);
22 assert.equal(g.blockAt(0, 0).color, 1);
23 });
24
25 it('swapTo moves a block stepwise and exchanges neighbors', () =>
26 {
27 const g = makeGame({ noSpawn: true });
28 g.place(1, 0, 4);
29 g.place(1, 1, 3);
30 g.place(1, 2, 2);
31 g.place(1, 3, 1);
32 const moved = g.swapTo(1, 0, 3);
33 assert.equal(moved.color, 4);
34 assert.equal(moved.y, 3);
35 assert.equal(g.blockAt(1, 0).color, 3);
36 assert.equal(g.blockAt(1, 1).color, 2);
37 assert.equal(g.blockAt(1, 2).color, 1);
38 });
39
40 it('refuses to drag in a locked or flying column', () =>
41 {
42 const g = makeGame({ noSpawn: true });
43 fillColumn(g, 0, 13, [0]);
44 g.checkLocks();
45 assert.equal(g.isColumnLocked(0), true);
46 assert.equal(g.beginDrag(0, 0), false);
47
48 const g2 = makeGame({ noSpawn: true });
49 g2.place(2, 0, 1);
50 g2.place(3, 0, 1);
51 g2.place(4, 0, 1);
52 g2.place(2, 1, 2);
53 g2.resolveMatches();
54 assert.equal(g2.isColumnStill(2), false);
55 assert.equal(g2.beginDrag(2, 1), false);
56 // A quiet neighboring column is still draggable.
57 g2.place(7, 0, 3);
58 g2.place(7, 1, 4);
59 assert.equal(g2.isColumnStill(7), true);
60 assert.ok(g2.beginDrag(7, 0));
61 g2.endDrag();
62 });
63
64 it('pulling a top block further up starts a manual launch that cannot leave the field', () =>
65 {
66 const g = makeGame({ noSpawn: true });
67 g.place(0, 0, 1);
68 g.place(0, 1, 2);
69 g.place(0, 2, 3);
70 assert.ok(g.beginDrag(0, 2));
71 g.moveDrag(2 + 1.5);
72 assert.equal(g.drag.launchIntent, true);
73 g.endDrag();
74 const block = g.blocks.find((b) => b.color === 3);
75 assert.equal(block.state, 'manual');
76 assert.equal(block.manualPhase, 'up');
77 assert.ok(block.vy > 0);
78 g.step(g.planet.riseTime, 1 / 240);
79 assert.ok(Math.abs(block.y - (g.height - 1)) < 0.05, 'apex should be just under the top, y=' + block.y);
80 assert.ok(block.y <= g.height - 1 + 1e-9, 'y=' + block.y);
81 assert.ok(block.y < g.height, 'must not leave the field');
82 assert.ok(block.manualPhase === 'down' || block.state === 'still' || Math.abs(block.y - (g.height - 1)) < 0.05);
83 g.step(0.5, 1 / 120);
84 assert.ok(block.y < g.height - 1 - 0.01 || block.state === 'still', 'must come back down, y=' + block.y + ' state=' + block.state);
85 assert.ok(block.y < g.height);
86 });
87
88 it('a manual jump from mid-stack still caps at the top cell', () =>
89 {
90 const g = makeGame({ noSpawn: true });
91 g.place(5, 0, 0);
92 g.place(5, 1, 1);
93 const top = g.place(5, 2, 2);
94 g.startManualLaunch(top);
95 g.step(4, 1 / 120);
96 assert.ok(top.y <= g.height - 1 + 1e-9);
97 assert.ok(top.y < g.height);
98 // After coming back down it lands on the remaining stack (y=1, since it left).
99 assert.ok(top.state === 'still' || top.state === 'manual');
100 if (top.state === 'still')
101 { assert.equal(top.y, 2); }
102 });
103
104 it('a click in the lower half of a cell grabs that cell, not the one above', () =>
105 {
106 const g = makeGame({ noSpawn: true });
107 g.place(0, 0, 0);
108 g.place(0, 1, 1);
109 assert.ok(g.beginDrag(0, 0.4));
110 assert.equal(g.blockById(g.drag.blockId).color, 0);
111 g.endDrag();
112 assert.ok(g.beginDrag(0, 0.9));
113 assert.equal(g.blockById(g.drag.blockId).color, 0);
114 g.endDrag();
115 assert.ok(g.beginDrag(0, 1.1));
116 assert.equal(g.blockById(g.drag.blockId).color, 1);
117 g.endDrag();
118 });
119});
120
121describe('Manual launch of the already-topmost block', () =>
122{
123 it('dragging the top block upward past its own position starts a manual jump', () =>
124 {
125 const g = makeGame({ noSpawn: true });
126 // A single block is already the topmost (and only) block of its column.
127 const block = g.place(0, 0, 2);
128 // Grab at the visual centre (y=0.5), then pull just past its position —
129 // well short of the old "empty space above the cell" threshold (1.25).
130 assert.ok(g.beginDrag(0, 0.5));
131 assert.equal(g.drag.startedAsTop, true);
132 g.moveDrag(0.5 + 0.4);
133 assert.ok(0.9 < 0 + 1 + 0.25, 'this pull would have failed the old threshold');
134 assert.equal(g.drag.launchIntent, true, 'pull of 0.4 tiles past the press point must launch');
135 g.endDrag();
136 assert.equal(block.state, 'manual');
137 assert.equal(block.manualPhase, 'up');
138 assert.ok(block.vy > 0);
139 g.step(g.planet.riseTime, 1 / 240);
140 assert.ok(Math.abs(block.y - (g.height - 1)) < 0.08, 'jumps to just under the top, y=' + block.y);
141 assert.ok(block.y <= g.height - 1 + 1e-9);
142 });
143
144 it('a short upward twitch below LAUNCH_PULL does not launch', () =>
145 {
146 const g = makeGame({ noSpawn: true });
147 const block = g.place(3, 0, 1);
148 assert.ok(g.beginDrag(3, 0.5));
149 g.moveDrag(0.5 + 0.2);
150 assert.ok(0.2 < g.constructor.LAUNCH_PULL || 0.2 < 0.35);
151 assert.equal(g.drag.launchIntent, false);
152 g.endDrag();
153 assert.equal(block.state, 'still');
154 assert.equal(block.y, 0);
155 });
156
157 it('dragging a mid-stack block up to the top still swaps instead of launching', () =>
158 {
159 const g = makeGame({ noSpawn: true });
160 const bottom = g.place(1, 0, 0);
161 g.place(1, 1, 1);
162 g.place(1, 2, 2);
163 assert.ok(g.beginDrag(1, 0.3));
164 assert.equal(g.drag.startedAsTop, false);
165 // Pointer is past the top cell centre but not into the empty space.
166 g.moveDrag(2.4);
167 assert.equal(bottom.y, 2, 'swapped to the top');
168 assert.equal(g.drag.launchIntent, false, 'must not launch a mid-stack rearrange');
169 g.endDrag();
170 assert.equal(bottom.state, 'still');
171 assert.equal(bottom.y, 2);
172 });
173
174 it('after a mid-stack block becomes the top, a further pull into empty space still launches', () =>
175 {
176 const g = makeGame({ noSpawn: true });
177 const bottom = g.place(2, 0, 4);
178 g.place(2, 1, 3);
179 assert.ok(g.beginDrag(2, 0.2));
180 assert.equal(g.drag.startedAsTop, false);
181 g.moveDrag(1 + 1 + 0.4);
182 assert.equal(bottom.y, 1);
183 assert.equal(g.drag.launchIntent, true);
184 g.endDrag();
185 assert.equal(bottom.state, 'manual');
186 });
187});