BlueGit

projects-timfalken-com meteors

Files

meteors / test / swap.test.js

swap.test.js

swap.test.js 3.7 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});