BlueGit

projects-timfalken-com meteors

Files

meteors / test / lock.test.js

lock.test.js

lock.test.js 2.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('Column lock and game over', () =>
8{
9 it('a column filled from floor to top becomes dark gray and locked', () =>
10 {
11 const g = makeGame({ noSpawn: true });
12 fillColumn(g, 3, 13, [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2]);
13 g.checkLocks();
14 assert.equal(g.isColumnLocked(3), true);
15 assert.equal(g.isColumnStill(3), false);
16 for (let r = 0; r < 13; r++)
17 {
18 const b = g.blockAt(3, r);
19 assert.ok(b);
20 assert.equal(b.state, 'locked');
21 assert.equal(b.color, GameCore.COLOR_GRAY);
22 }
23 assert.equal(g.beginDrag(3, 5), false);
24 });
25
26 it('a new spawn never lands in a locked column', () =>
27 {
28 const g = makeGame({ seed: 11, noSpawn: false });
29 g.spawnInterval = 1e9;
30 fillColumn(g, 0, 13, [0, 1]);
31 g.checkLocks();
32 assert.equal(g.isColumnLocked(0), true);
33 for (let i = 0; i < 40; i++)
34 {
35 const spawned = g.trySpawn();
36 assert.ok(spawned);
37 assert.notEqual(spawned.col, 0);
38 g.removeBlock(spawned);
39 }
40 });
41
42 it('a nearly full column (12 blocks) is not locked', () =>
43 {
44 const g = makeGame({ noSpawn: true });
45 fillColumn(g, 2, 12, [0, 1, 2]);
46 g.checkLocks();
47 assert.equal(g.isColumnLocked(2), false);
48 assert.equal(g.isColumnStill(2), true);
49 assert.ok(g.beginDrag(2, 0));
50 g.endDrag();
51 });
52
53 it('when every column is locked the player loses', () =>
54 {
55 const g = makeGame({ noSpawn: true });
56 for (let c = 0; c < 10; c++)
57 { fillColumn(g, c, 13, [c % 5, (c + 1) % 5, (c + 2) % 5]); }
58 g.tick(0.01);
59 assert.equal(g.state, 'gameover');
60 assert.equal(g.lockedColumnCount(), 10);
61 assert.equal(g.trySpawn(), null);
62 assert.equal(g.beginDrag(0, 0), false);
63 });
64
65 it('game over does not fire while at least one column is open', () =>
66 {
67 const g = makeGame({ noSpawn: true });
68 for (let c = 0; c < 9; c++)
69 { fillColumn(g, c, 13, [0, 1, 2]); }
70 fillColumn(g, 9, 4, [3, 4]);
71 g.checkLocks();
72 g.checkGameOver();
73 assert.equal(g.state, 'playing');
74 assert.equal(g.lockedColumnCount(), 9);
75 });
76});