BlueGit

projects-timfalken-com meteors

test: touch-drag regression, cluster drags, cell corners, landing matches and flames

Adds the poisoned-pointer plus correct-touch regression that failed
before the t1106 fix, unit and Playwright coverage for under-flyer and
landed-cluster drags, thrown-falling merges, cell-corner grabs (mouse
and touch), mixed landing matches, and a 3x flame size check with a
screenshot.

timfa Grok Bot · pushed by timfa

Authored 2026-11-13 · 4 days ago

Hash
7278388601993297559e18fe45993397a28eef3f
Parent
681cc658
5 files changed +887 −2
test/drag-cluster.test.js added +176 −0
@@ -0,0 +1,176 @@
1 'use strict';
2
3 const { describe, it } = require('node:test');
4 const assert = require('node:assert/strict');
5 const { makeGame, GameCore } = require('./helpers.js');
6
7 function flyingGrey (game, col, y)
8 {
9 const cluster = {
10 id: game.nextClusterId++,
11 jumpHeight: 3,
12 vy: 4,
13 decel: 2,
14 phase: 'up'
15 };
16 game.clusters[cluster.id] = cluster;
17 const b = game.createBlock(col, y, GameCore.COLOR_GRAY, 'flying');
18 b.clusterId = cluster.id;
19 b.vy = cluster.vy;
20 b.flame = true;
21 return { cluster, block: b };
22 }
23
24 function landedGroup (game, col, specs)
25 {
26 // specs: [{ color, y }]
27 const cluster = {
28 id: game.nextClusterId++,
29 jumpHeight: 3,
30 vy: 0,
31 decel: 0,
32 phase: 'landed'
33 };
34 game.clusters[cluster.id] = cluster;
35 const members = [];
36 for (const s of specs)
37 {
38 const state = 'settling';
39 const b = game.createBlock(col, s.y, s.color, state);
40 b.clusterId = cluster.id;
41 b.vy = 0;
42 b.flame = false;
43 b.settleTime = s.settleTime || 0;
44 members.push(b);
45 }
46 return { cluster, members };
47 }
48
49 describe('Blocks under a flying cluster are draggable (t1107)', () =>
50 {
51 it('a still stack under a flyer in the same column can be grabbed and swapped', () =>
52 {
53 const g = makeGame({ noSpawn: true });
54 const bottom = g.place(3, 0, 0);
55 const mid = g.place(3, 1, 1);
56 const { cluster, block: flyer } = flyingGrey(g, 3, 7);
57 assert.equal(g.isColumnStill(3), false, 'isColumnStill stays strict');
58 assert.equal(g.columnAllowsDrag(3), true);
59 assert.ok(g.isDraggableBlock(bottom));
60 assert.equal(g.isDraggableBlock(flyer), false, 'the flyer itself is not draggable');
61 assert.ok(g.beginDrag(3, 0.5));
62 assert.equal(g.drag.blockId, bottom.id);
63 g.moveDrag(1.5);
64 assert.equal(bottom.y, 1);
65 assert.equal(mid.y, 0);
66 g.endDrag();
67 assert.equal(flyer.state, 'flying');
68 assert.equal(flyer.clusterId, cluster.id);
69 assert.equal(flyer.y, 7, 'flyer was left alone');
70 });
71
72 it('pressing on a flying cluster does not grab it or a block below', () =>
73 {
74 const g = makeGame({ noSpawn: true });
75 g.place(1, 0, 2);
76 const { block: flyer } = flyingGrey(g, 1, 5);
77 assert.equal(g.beginDrag(1, 5.5), false);
78 assert.equal(g.drag, null);
79 assert.equal(flyer.state, 'flying');
80 });
81 });
82
83 describe('Dragging within, into and out of a landed cluster (t1107)', () =>
84 {
85 it('dragging within a landed cluster swaps members and keeps them in the group', () =>
86 {
87 const g = makeGame({ noSpawn: true });
88 const { cluster, members } = landedGroup(g, 4, [
89 { color: GameCore.COLOR_GRAY, y: 0 },
90 { color: 1, y: 1 },
91 { color: 2, y: 2 }
92 ]);
93 const [grey, cargo1, cargo2] = members;
94 assert.ok(g.isDraggableBlock(grey));
95 assert.ok(g.isDraggableBlock(cargo1));
96 assert.ok(g.beginDrag(4, 0.4));
97 assert.equal(g.drag.blockId, grey.id);
98 g.moveDrag(2.5);
99 g.endDrag();
100 assert.equal(grey.y, 2);
101 assert.equal(cargo1.y, 0);
102 assert.equal(cargo2.y, 1);
103 assert.equal(grey.clusterId, cluster.id);
104 assert.equal(cargo1.clusterId, cluster.id);
105 assert.equal(cargo2.clusterId, cluster.id);
106 assert.equal(g.clusters[cluster.id].phase, 'landed');
107 });
108
109 it('swapping a still block into a landed cluster makes it a member', () =>
110 {
111 const g = makeGame({ noSpawn: true });
112 const still = g.place(5, 0, 3);
113 const { cluster, members } = landedGroup(g, 5, [
114 { color: GameCore.COLOR_GRAY, y: 1 },
115 { color: 4, y: 2 }
116 ]);
117 assert.ok(g.beginDrag(5, 0.5));
118 assert.equal(g.drag.blockId, still.id);
119 // Stop on the top cell, short of LAUNCH_PULL, so endDrag does not throw.
120 g.moveDrag(2.2);
121 g.endDrag();
122 assert.equal(still.y, 2);
123 assert.equal(still.clusterId, cluster.id);
124 assert.equal(still.state, 'settling');
125 assert.equal(still.color, 3);
126 // The grey was swapped into the outside slot and left the cluster.
127 assert.equal(members[0].y, 0);
128 assert.equal(members[0].clusterId, null);
129 assert.equal(members[0].state, 'settling');
130 assert.equal(members[0].color, GameCore.COLOR_GRAY);
131 // Cargo only moved to another cluster slot, so it stays a member.
132 assert.equal(members[1].y, 1);
133 assert.equal(members[1].clusterId, cluster.id);
134 assert.equal(members[1].color, 4);
135 });
136
137 it('swapping a landed member out leaves the cluster; a grey keeps its settle timer', () =>
138 {
139 const g = makeGame({ noSpawn: true });
140 g.place(6, 0, 0);
141 const { cluster, members } = landedGroup(g, 6, [
142 { color: GameCore.COLOR_GRAY, y: 1, settleTime: 1.25 },
143 { color: 2, y: 2 }
144 ]);
145 const grey = members[0];
146 assert.ok(g.beginDrag(6, 1.4));
147 assert.equal(g.drag.blockId, grey.id);
148 g.moveDrag(0.4);
149 g.endDrag();
150 assert.equal(grey.y, 0);
151 assert.equal(grey.clusterId, null);
152 assert.equal(grey.state, 'settling', 'grey keeps settling on its own timer');
153 assert.equal(grey.settleTime, 1.25);
154 assert.equal(grey.color, GameCore.COLOR_GRAY);
155 // The still block that moved in joined the cluster.
156 const joined = g.blocks.find((b) => b.color === 0);
157 assert.equal(joined.clusterId, cluster.id);
158 assert.equal(joined.state, 'settling');
159 assert.equal(joined.y, 1);
160 });
161
162 it('a gap in the column stops the swap; hanging members are part of the stack', () =>
163 {
164 const g = makeGame({ noSpawn: true });
165 const bottom = g.place(2, 0, 1);
166 const { members } = landedGroup(g, 2, [
167 { color: 3, y: 2 }
168 ]);
169 // Gap at y=1: swapToward must stop, the hanging member stays at 2.
170 assert.ok(g.beginDrag(2, 0.5));
171 g.moveDrag(2.5);
172 g.endDrag();
173 assert.equal(bottom.y, 0);
174 assert.equal(members[0].y, 2);
175 });
176 });
@@ -0,0 +1,176 @@
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, GameCore } = require('./helpers.js');
6
7function flyingGrey (game, col, y)
8{
9 const cluster = {
10 id: game.nextClusterId++,
11 jumpHeight: 3,
12 vy: 4,
13 decel: 2,
14 phase: 'up'
15 };
16 game.clusters[cluster.id] = cluster;
17 const b = game.createBlock(col, y, GameCore.COLOR_GRAY, 'flying');
18 b.clusterId = cluster.id;
19 b.vy = cluster.vy;
20 b.flame = true;
21 return { cluster, block: b };
22}
23
24function landedGroup (game, col, specs)
25{
26 // specs: [{ color, y }]
27 const cluster = {
28 id: game.nextClusterId++,
29 jumpHeight: 3,
30 vy: 0,
31 decel: 0,
32 phase: 'landed'
33 };
34 game.clusters[cluster.id] = cluster;
35 const members = [];
36 for (const s of specs)
37 {
38 const state = 'settling';
39 const b = game.createBlock(col, s.y, s.color, state);
40 b.clusterId = cluster.id;
41 b.vy = 0;
42 b.flame = false;
43 b.settleTime = s.settleTime || 0;
44 members.push(b);
45 }
46 return { cluster, members };
47}
48
49describe('Blocks under a flying cluster are draggable (t1107)', () =>
50{
51 it('a still stack under a flyer in the same column can be grabbed and swapped', () =>
52 {
53 const g = makeGame({ noSpawn: true });
54 const bottom = g.place(3, 0, 0);
55 const mid = g.place(3, 1, 1);
56 const { cluster, block: flyer } = flyingGrey(g, 3, 7);
57 assert.equal(g.isColumnStill(3), false, 'isColumnStill stays strict');
58 assert.equal(g.columnAllowsDrag(3), true);
59 assert.ok(g.isDraggableBlock(bottom));
60 assert.equal(g.isDraggableBlock(flyer), false, 'the flyer itself is not draggable');
61 assert.ok(g.beginDrag(3, 0.5));
62 assert.equal(g.drag.blockId, bottom.id);
63 g.moveDrag(1.5);
64 assert.equal(bottom.y, 1);
65 assert.equal(mid.y, 0);
66 g.endDrag();
67 assert.equal(flyer.state, 'flying');
68 assert.equal(flyer.clusterId, cluster.id);
69 assert.equal(flyer.y, 7, 'flyer was left alone');
70 });
71
72 it('pressing on a flying cluster does not grab it or a block below', () =>
73 {
74 const g = makeGame({ noSpawn: true });
75 g.place(1, 0, 2);
76 const { block: flyer } = flyingGrey(g, 1, 5);
77 assert.equal(g.beginDrag(1, 5.5), false);
78 assert.equal(g.drag, null);
79 assert.equal(flyer.state, 'flying');
80 });
81});
82
83describe('Dragging within, into and out of a landed cluster (t1107)', () =>
84{
85 it('dragging within a landed cluster swaps members and keeps them in the group', () =>
86 {
87 const g = makeGame({ noSpawn: true });
88 const { cluster, members } = landedGroup(g, 4, [
89 { color: GameCore.COLOR_GRAY, y: 0 },
90 { color: 1, y: 1 },
91 { color: 2, y: 2 }
92 ]);
93 const [grey, cargo1, cargo2] = members;
94 assert.ok(g.isDraggableBlock(grey));
95 assert.ok(g.isDraggableBlock(cargo1));
96 assert.ok(g.beginDrag(4, 0.4));
97 assert.equal(g.drag.blockId, grey.id);
98 g.moveDrag(2.5);
99 g.endDrag();
100 assert.equal(grey.y, 2);
101 assert.equal(cargo1.y, 0);
102 assert.equal(cargo2.y, 1);
103 assert.equal(grey.clusterId, cluster.id);
104 assert.equal(cargo1.clusterId, cluster.id);
105 assert.equal(cargo2.clusterId, cluster.id);
106 assert.equal(g.clusters[cluster.id].phase, 'landed');
107 });
108
109 it('swapping a still block into a landed cluster makes it a member', () =>
110 {
111 const g = makeGame({ noSpawn: true });
112 const still = g.place(5, 0, 3);
113 const { cluster, members } = landedGroup(g, 5, [
114 { color: GameCore.COLOR_GRAY, y: 1 },
115 { color: 4, y: 2 }
116 ]);
117 assert.ok(g.beginDrag(5, 0.5));
118 assert.equal(g.drag.blockId, still.id);
119 // Stop on the top cell, short of LAUNCH_PULL, so endDrag does not throw.
120 g.moveDrag(2.2);
121 g.endDrag();
122 assert.equal(still.y, 2);
123 assert.equal(still.clusterId, cluster.id);
124 assert.equal(still.state, 'settling');
125 assert.equal(still.color, 3);
126 // The grey was swapped into the outside slot and left the cluster.
127 assert.equal(members[0].y, 0);
128 assert.equal(members[0].clusterId, null);
129 assert.equal(members[0].state, 'settling');
130 assert.equal(members[0].color, GameCore.COLOR_GRAY);
131 // Cargo only moved to another cluster slot, so it stays a member.
132 assert.equal(members[1].y, 1);
133 assert.equal(members[1].clusterId, cluster.id);
134 assert.equal(members[1].color, 4);
135 });
136
137 it('swapping a landed member out leaves the cluster; a grey keeps its settle timer', () =>
138 {
139 const g = makeGame({ noSpawn: true });
140 g.place(6, 0, 0);
141 const { cluster, members } = landedGroup(g, 6, [
142 { color: GameCore.COLOR_GRAY, y: 1, settleTime: 1.25 },
143 { color: 2, y: 2 }
144 ]);
145 const grey = members[0];
146 assert.ok(g.beginDrag(6, 1.4));
147 assert.equal(g.drag.blockId, grey.id);
148 g.moveDrag(0.4);
149 g.endDrag();
150 assert.equal(grey.y, 0);
151 assert.equal(grey.clusterId, null);
152 assert.equal(grey.state, 'settling', 'grey keeps settling on its own timer');
153 assert.equal(grey.settleTime, 1.25);
154 assert.equal(grey.color, GameCore.COLOR_GRAY);
155 // The still block that moved in joined the cluster.
156 const joined = g.blocks.find((b) => b.color === 0);
157 assert.equal(joined.clusterId, cluster.id);
158 assert.equal(joined.state, 'settling');
159 assert.equal(joined.y, 1);
160 });
161
162 it('a gap in the column stops the swap; hanging members are part of the stack', () =>
163 {
164 const g = makeGame({ noSpawn: true });
165 const bottom = g.place(2, 0, 1);
166 const { members } = landedGroup(g, 2, [
167 { color: 3, y: 2 }
168 ]);
169 // Gap at y=1: swapToward must stop, the hanging member stays at 2.
170 assert.ok(g.beginDrag(2, 0.5));
171 g.moveDrag(2.5);
172 g.endDrag();
173 assert.equal(bottom.y, 0);
174 assert.equal(members[0].y, 2);
175 });
176});
test/drag-playwright.py +288 −2
@@ -1,5 +1,6 @@
1 1 #!/usr/bin/env python3
2 """1:1 drag tracking (t1104): mouse and real touch, including a high-DPR mobile viewport."""
2 """Drag tests (t1104/t1106/t1107/t1109/t1110): 1:1 tracking, poisoned-pointer
3 regression, cell-corner grabs, under-flyer / landed-cluster drags, flame size."""
3 4 from __future__ import annotations
4 5
5 6 import socket
@@ -299,11 +300,295 @@ def run_device(browser, device):
299 300 check(end == mid, f"after release: still at the pointer row, mid={mid} end={end}", name)
300 301 shot(page, f"t1104-drag-{name}.png")
301 302
303 # --- t1109: press at cell corners grabs the occupant (mouse and touch)
304 run_cell_corner_grabs(page, cdp, kind, name)
305
306 # --- t1107: still stack under a flying cluster is draggable
307 run_under_flyer_drag(page, cdp, kind, name)
308
309 # --- t1107: drag within a landed cluster
310 run_landed_cluster_drag(page, cdp, kind, name)
311
312 # --- t1110: flame is 3x, with a visual screenshot (once per device)
313 run_flame_size(page, name)
314
302 315 if bag:
303 316 print(f"console [{name}]: {bag[:8]}")
304 317 context.close()
305 318
306 319
320 def field_point(page, fx, fy):
321 """Map exact field coordinates (not cell-centres) to client pixels."""
322 return page.evaluate(
323 """([fx, fy]) => {
324 const c = app.scene.canvas;
325 const r = c.getBoundingClientRect();
326 return {
327 x: r.left + (fx / 10) * r.width,
328 y: r.top + (1 - fy / 13) * r.height
329 };
330 }""",
331 [fx, fy],
332 )
333
334
335 def grab_id(page):
336 return page.evaluate(
337 """() => {
338 const g = app.core;
339 if (!g.drag) return null;
340 const b = g.blockById(g.drag.blockId);
341 return b ? { id: b.id, col: b.col, y: b.y, color: b.color } : null;
342 }"""
343 )
344
345
346 def run_cell_corner_grabs(page, cdp, kind, name):
347 reset_board(page, {"2": [0, 1, 2]})
348 page.wait_for_timeout(50)
349 target = page.evaluate(
350 """() => {
351 const b = app.core.blocks.find((x) => x.col === 2 && Math.abs(x.y - 1) < 0.01);
352 return b ? b.id : null;
353 }"""
354 )
355 corners = [(2.05, 1.05, "bottom-left"), (2.05, 1.95, "top-left"),
356 (2.95, 1.05, "bottom-right"), (2.95, 1.95, "top-right")]
357 for fx, fy, label in corners:
358 pt = field_point(page, fx, fy)
359 pointer_down(page, cdp, kind, pt["x"], pt["y"])
360 page.wait_for_timeout(30)
361 grabbed = grab_id(page)
362 check(
363 grabbed is not None and grabbed["id"] == target,
364 f"t1109 corner {label} ({kind}) grabs cell occupant, grabbed={grabbed} want id={target}",
365 name,
366 )
367 pointer_up(page, cdp, kind)
368 page.wait_for_timeout(40)
369 shot(page, f"t1109-corners-{name}.png")
370
371
372 def run_under_flyer_drag(page, cdp, kind, name):
373 reset_board(page, {"3": [0, 1, 2]})
374 page.evaluate(
375 """() => {
376 const g = app.core;
377 const cluster = { id: g.nextClusterId++, jumpHeight: 3, vy: 0, decel: 0, phase: 'up' };
378 g.clusters[cluster.id] = cluster;
379 const b = g.createBlock(3, 8, g.constructor.COLOR_GRAY, 'flying');
380 b.clusterId = cluster.id;
381 b.vy = 0;
382 b.flame = true;
383 }"""
384 )
385 page.wait_for_timeout(40)
386 p0 = field_to_client(page, 3, 0.5)
387 p1 = field_to_client(page, 3, 1.5)
388 drag_to(page, cdp, kind, p0["x"], p0["y"], p1["x"], p1["y"], steps=10, end=True)
389 page.wait_for_timeout(60)
390 info = page.evaluate(
391 """() => {
392 const g = app.core;
393 const still = g.blocks.filter((b) => b.state === 'still' && b.col === 3)
394 .map((b) => ({ color: b.color, y: b.y }));
395 const flyer = g.blocks.find((b) => b.state === 'flying' && b.col === 3);
396 return { still, flyerY: flyer ? flyer.y : null, flyerState: flyer ? flyer.state : null };
397 }"""
398 )
399 ys = {b["color"]: b["y"] for b in info["still"]}
400 check(nearly(ys.get(0, -99), 1), f"t1107 under-flyer: color 0 moved to y=1, info={info}", name)
401 check(nearly(ys.get(1, -99), 0), f"t1107 under-flyer: color 1 swapped to y=0, info={info}", name)
402 check(info["flyerState"] == "flying" and info["flyerY"] is not None and info["flyerY"] > 6,
403 f"t1107 under-flyer: flyer left alone, info={info}", name)
404
405
406 def run_landed_cluster_drag(page, cdp, kind, name):
407 page.evaluate(
408 """() => {
409 const g = app.core;
410 g.spawnInterval = 1e9;
411 g.spawnTimer = 0;
412 g.blocks.length = 0;
413 g.clusters = Object.create(null);
414 g.drag = null;
415 g.state = 'playing';
416 g.nextBlockId = 1;
417 g.nextClusterId = 1;
418 const cluster = { id: g.nextClusterId++, jumpHeight: 3, vy: 0, decel: 0, phase: 'landed' };
419 g.clusters[cluster.id] = cluster;
420 const specs = [[0, -1], [1, 1], [2, 2]];
421 for (const [y, color] of specs) {
422 const b = g.createBlock(6, y, color, 'settling');
423 b.clusterId = cluster.id;
424 b.vy = 0;
425 b.settleTime = 0;
426 }
427 }"""
428 )
429 page.wait_for_timeout(50)
430 p0 = field_to_client(page, 6, 0.5)
431 p1 = field_to_client(page, 6, 2.2)
432 drag_to(page, cdp, kind, p0["x"], p0["y"], p1["x"], p1["y"], steps=12, end=True)
433 page.wait_for_timeout(60)
434 info = page.evaluate(
435 """() => app.core.blocks.map((b) => ({
436 color: b.color, y: b.y, state: b.state, clusterId: b.clusterId
437 }))"""
438 )
439 by_y = {round(b["y"]): b for b in info}
440 check(
441 by_y.get(2) is not None and by_y[2]["color"] == -1 and by_y[2]["clusterId"] is not None,
442 f"t1107 landed: grey dragged within cluster to y=2, info={info}",
443 name,
444 )
445
446
447 def run_flame_size(page, name):
448 page.evaluate(
449 """() => {
450 const g = app.core;
451 g.spawnInterval = 1e9;
452 g.spawnTimer = 0;
453 g.blocks.length = 0;
454 g.clusters = Object.create(null);
455 g.drag = null;
456 g.state = 'playing';
457 g.nextBlockId = 1;
458 g.nextClusterId = 1;
459 g.place(1, 0, 2);
460 g.place(2, 0, 2);
461 g.place(3, 0, 2);
462 g.resolveMatches();
463 }"""
464 )
465 # Let the view sync and the rockets rise a little so the flames are visible.
466 page.wait_for_timeout(200)
467 info = page.evaluate(
468 """() => {
469 const t = app.tile;
470 const flames = Object.keys(app.flames).map((id) => {
471 const f = app.flames[id];
472 return {
473 visible: !!(f.renderer && f.renderer.visible),
474 sx: f.transform.size.x,
475 sy: f.transform.size.y
476 };
477 }).filter((f) => f.visible);
478 return {
479 t,
480 expected: t * 0.85 * 3,
481 flames,
482 flying: app.core.blocks.filter((b) => b.flame).length
483 };
484 }"""
485 )
486 ok = (
487 info
488 and info["flying"] >= 3
489 and info["flames"]
490 and all(abs(f["sx"] - info["expected"]) < 0.51 and abs(f["sy"] - info["expected"]) < 0.51
491 for f in info["flames"])
492 )
493 check(ok, f"t1110 flame is 3x (w and h), info={info}", name)
494 shot(page, f"t1110-flame-{name}.png")
495
496
497 def run_poisoned_pointer_regression(browser):
498 """t1106: pointermoves poisoned to fieldY 9.5, touchmoves correct to 1.5.
499 Before the fix the block teleported to y=9; after, it stops at y=1.
500 This is the regression that failed on the old pointer-only path.
501 """
502 name = "poisoned-pointer"
503 print(f"\n======== {name} ========")
504 kw = dict(
505 viewport={"width": 412, "height": 915},
506 device_scale_factor=2.625,
507 is_mobile=True,
508 has_touch=True,
509 user_agent=(
510 "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 "
511 "(KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36"
512 ),
513 )
514 context = browser.new_context(**kw)
515 page = context.new_page()
516 start_play(page, "touch")
517 reset_board(page, {"2": [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]})
518 page.wait_for_timeout(50)
519 result = page.evaluate(
520 """() => {
521 const c = app.scene.canvas;
522 const r = c.getBoundingClientRect();
523 const toClient = (fy) => ({
524 x: r.left + (2.5 / 10) * r.width,
525 y: r.top + (1 - fy / 13) * r.height
526 });
527 const p0 = toClient(0.5);
528 const pGood = toClient(1.5);
529 const pBad = toClient(9.5);
530 const fireP = (type, x, y) => {
531 const ev = new PointerEvent(type, {
532 bubbles: true, cancelable: true, composed: true,
533 pointerId: 42, pointerType: 'touch', isPrimary: true,
534 clientX: x, clientY: y, pageX: x, pageY: y,
535 buttons: type === 'pointerup' ? 0 : 1, width: 28, height: 28,
536 pressure: type === 'pointerup' ? 0 : 0.5
537 });
538 (type === 'pointerdown' ? c : window).dispatchEvent(ev);
539 };
540 const fireT = (type, x, y) => {
541 const t = new Touch({
542 identifier: 1, target: c, clientX: x, clientY: y,
543 pageX: x, pageY: y, screenX: x, screenY: y,
544 radiusX: 14, radiusY: 14, force: 0.5
545 });
546 const ev = new TouchEvent(type, {
547 bubbles: true, cancelable: true, composed: true,
548 touches: type.includes('end') || type.includes('cancel') ? [] : [t],
549 targetTouches: type.includes('end') || type.includes('cancel') ? [] : [t],
550 changedTouches: [t]
551 });
552 c.dispatchEvent(ev);
553 document.dispatchEvent(ev);
554 };
555 fireP('pointerdown', p0.x, p0.y);
556 fireT('touchstart', p0.x, p0.y);
557 const midYs = [];
558 const steps = 8;
559 for (let i = 1; i <= steps; i++) {
560 const t = i / steps;
561 fireP('pointermove', p0.x, p0.y + (pBad.y - p0.y) * t);
562 fireT('touchmove', p0.x, p0.y + (pGood.y - p0.y) * t);
563 const b = app.core.blockById(app.core.drag && app.core.drag.blockId);
564 if (b) midYs.push(Math.round(b.y * 100) / 100);
565 }
566 fireT('touchend', pGood.x, pGood.y);
567 fireP('pointerup', pGood.x, pGood.y);
568 const colors = app.core.blocks
569 .filter((b) => b.col === 2)
570 .sort((a, b) => a.y - b.y)
571 .map((b) => ({ color: b.color, y: Math.round(b.y * 100) / 100, state: b.state }));
572 const moved = colors.find((b) => b.color === 0);
573 return { moved, colors, midYs, drag: app.core.drag };
574 }"""
575 )
576 moved = result.get("moved") or {}
577 # Must end at y=1 (one tile up), never at the top (y=9).
578 ok = moved.get("y") is not None and abs(moved["y"] - 1) <= 0.15
579 check(
580 ok,
581 f"t1106 poisoned pointer + correct touch leaves color 0 at y=1, result={result}",
582 name,
583 )
584 check(
585 moved.get("y") is None or moved["y"] < 4,
586 f"t1106 must not teleport to the top, y={moved.get('y')}",
587 name,
588 )
589 context.close()
590
591
307 592 def main():
308 593 global URL
309 594 port = free_port()
@@ -324,6 +609,7 @@ def main():
324 609 )
325 610 for device in (DESKTOP, ANDROID, ANDROID_DPR3, IOS):
326 611 run_device(browser, device)
612 run_poisoned_pointer_regression(browser)
327 613 browser.close()
328 614 finally:
329 615 httpd.terminate()
@@ -338,7 +624,7 @@ def main():
338 624 for f in FAILURES:
339 625 print(" -", f)
340 626 return 1
341 print("all 1:1 drag tests passed")
627 print("all drag tests passed")
342 628 return 0
343 629
344 630
@@ -1,5 +1,6 @@
1#!/usr/bin/env python3
1#!/usr/bin/env python3
2"""1:1 drag tracking (t1104): mouse and real touch, including a high-DPR mobile viewport."""
2"""Drag tests (t1104/t1106/t1107/t1109/t1110): 1:1 tracking, poisoned-pointer
3regression, cell-corner grabs, under-flyer / landed-cluster drags, flame size."""
3from __future__ import annotations
4from __future__ import annotations
4
5
5import socket
6import socket
@@ -299,11 +300,295 @@ def run_device(browser, device):
299 check(end == mid, f"after release: still at the pointer row, mid={mid} end={end}", name)
300 check(end == mid, f"after release: still at the pointer row, mid={mid} end={end}", name)
300 shot(page, f"t1104-drag-{name}.png")
301 shot(page, f"t1104-drag-{name}.png")
301
302
303 # --- t1109: press at cell corners grabs the occupant (mouse and touch)
304 run_cell_corner_grabs(page, cdp, kind, name)
305
306 # --- t1107: still stack under a flying cluster is draggable
307 run_under_flyer_drag(page, cdp, kind, name)
308
309 # --- t1107: drag within a landed cluster
310 run_landed_cluster_drag(page, cdp, kind, name)
311
312 # --- t1110: flame is 3x, with a visual screenshot (once per device)
313 run_flame_size(page, name)
314
302 if bag:
315 if bag:
303 print(f"console [{name}]: {bag[:8]}")
316 print(f"console [{name}]: {bag[:8]}")
304 context.close()
317 context.close()
305
318
306
319
320def field_point(page, fx, fy):
321 """Map exact field coordinates (not cell-centres) to client pixels."""
322 return page.evaluate(
323 """([fx, fy]) => {
324 const c = app.scene.canvas;
325 const r = c.getBoundingClientRect();
326 return {
327 x: r.left + (fx / 10) * r.width,
328 y: r.top + (1 - fy / 13) * r.height
329 };
330 }""",
331 [fx, fy],
332 )
333
334
335def grab_id(page):
336 return page.evaluate(
337 """() => {
338 const g = app.core;
339 if (!g.drag) return null;
340 const b = g.blockById(g.drag.blockId);
341 return b ? { id: b.id, col: b.col, y: b.y, color: b.color } : null;
342 }"""
343 )
344
345
346def run_cell_corner_grabs(page, cdp, kind, name):
347 reset_board(page, {"2": [0, 1, 2]})
348 page.wait_for_timeout(50)
349 target = page.evaluate(
350 """() => {
351 const b = app.core.blocks.find((x) => x.col === 2 && Math.abs(x.y - 1) < 0.01);
352 return b ? b.id : null;
353 }"""
354 )
355 corners = [(2.05, 1.05, "bottom-left"), (2.05, 1.95, "top-left"),
356 (2.95, 1.05, "bottom-right"), (2.95, 1.95, "top-right")]
357 for fx, fy, label in corners:
358 pt = field_point(page, fx, fy)
359 pointer_down(page, cdp, kind, pt["x"], pt["y"])
360 page.wait_for_timeout(30)
361 grabbed = grab_id(page)
362 check(
363 grabbed is not None and grabbed["id"] == target,
364 f"t1109 corner {label} ({kind}) grabs cell occupant, grabbed={grabbed} want id={target}",
365 name,
366 )
367 pointer_up(page, cdp, kind)
368 page.wait_for_timeout(40)
369 shot(page, f"t1109-corners-{name}.png")
370
371
372def run_under_flyer_drag(page, cdp, kind, name):
373 reset_board(page, {"3": [0, 1, 2]})
374 page.evaluate(
375 """() => {
376 const g = app.core;
377 const cluster = { id: g.nextClusterId++, jumpHeight: 3, vy: 0, decel: 0, phase: 'up' };
378 g.clusters[cluster.id] = cluster;
379 const b = g.createBlock(3, 8, g.constructor.COLOR_GRAY, 'flying');
380 b.clusterId = cluster.id;
381 b.vy = 0;
382 b.flame = true;
383 }"""
384 )
385 page.wait_for_timeout(40)
386 p0 = field_to_client(page, 3, 0.5)
387 p1 = field_to_client(page, 3, 1.5)
388 drag_to(page, cdp, kind, p0["x"], p0["y"], p1["x"], p1["y"], steps=10, end=True)
389 page.wait_for_timeout(60)
390 info = page.evaluate(
391 """() => {
392 const g = app.core;
393 const still = g.blocks.filter((b) => b.state === 'still' && b.col === 3)
394 .map((b) => ({ color: b.color, y: b.y }));
395 const flyer = g.blocks.find((b) => b.state === 'flying' && b.col === 3);
396 return { still, flyerY: flyer ? flyer.y : null, flyerState: flyer ? flyer.state : null };
397 }"""
398 )
399 ys = {b["color"]: b["y"] for b in info["still"]}
400 check(nearly(ys.get(0, -99), 1), f"t1107 under-flyer: color 0 moved to y=1, info={info}", name)
401 check(nearly(ys.get(1, -99), 0), f"t1107 under-flyer: color 1 swapped to y=0, info={info}", name)
402 check(info["flyerState"] == "flying" and info["flyerY"] is not None and info["flyerY"] > 6,
403 f"t1107 under-flyer: flyer left alone, info={info}", name)
404
405
406def run_landed_cluster_drag(page, cdp, kind, name):
407 page.evaluate(
408 """() => {
409 const g = app.core;
410 g.spawnInterval = 1e9;
411 g.spawnTimer = 0;
412 g.blocks.length = 0;
413 g.clusters = Object.create(null);
414 g.drag = null;
415 g.state = 'playing';
416 g.nextBlockId = 1;
417 g.nextClusterId = 1;
418 const cluster = { id: g.nextClusterId++, jumpHeight: 3, vy: 0, decel: 0, phase: 'landed' };
419 g.clusters[cluster.id] = cluster;
420 const specs = [[0, -1], [1, 1], [2, 2]];
421 for (const [y, color] of specs) {
422 const b = g.createBlock(6, y, color, 'settling');
423 b.clusterId = cluster.id;
424 b.vy = 0;
425 b.settleTime = 0;
426 }
427 }"""
428 )
429 page.wait_for_timeout(50)
430 p0 = field_to_client(page, 6, 0.5)
431 p1 = field_to_client(page, 6, 2.2)
432 drag_to(page, cdp, kind, p0["x"], p0["y"], p1["x"], p1["y"], steps=12, end=True)
433 page.wait_for_timeout(60)
434 info = page.evaluate(
435 """() => app.core.blocks.map((b) => ({
436 color: b.color, y: b.y, state: b.state, clusterId: b.clusterId
437 }))"""
438 )
439 by_y = {round(b["y"]): b for b in info}
440 check(
441 by_y.get(2) is not None and by_y[2]["color"] == -1 and by_y[2]["clusterId"] is not None,
442 f"t1107 landed: grey dragged within cluster to y=2, info={info}",
443 name,
444 )
445
446
447def run_flame_size(page, name):
448 page.evaluate(
449 """() => {
450 const g = app.core;
451 g.spawnInterval = 1e9;
452 g.spawnTimer = 0;
453 g.blocks.length = 0;
454 g.clusters = Object.create(null);
455 g.drag = null;
456 g.state = 'playing';
457 g.nextBlockId = 1;
458 g.nextClusterId = 1;
459 g.place(1, 0, 2);
460 g.place(2, 0, 2);
461 g.place(3, 0, 2);
462 g.resolveMatches();
463 }"""
464 )
465 # Let the view sync and the rockets rise a little so the flames are visible.
466 page.wait_for_timeout(200)
467 info = page.evaluate(
468 """() => {
469 const t = app.tile;
470 const flames = Object.keys(app.flames).map((id) => {
471 const f = app.flames[id];
472 return {
473 visible: !!(f.renderer && f.renderer.visible),
474 sx: f.transform.size.x,
475 sy: f.transform.size.y
476 };
477 }).filter((f) => f.visible);
478 return {
479 t,
480 expected: t * 0.85 * 3,
481 flames,
482 flying: app.core.blocks.filter((b) => b.flame).length
483 };
484 }"""
485 )
486 ok = (
487 info
488 and info["flying"] >= 3
489 and info["flames"]
490 and all(abs(f["sx"] - info["expected"]) < 0.51 and abs(f["sy"] - info["expected"]) < 0.51
491 for f in info["flames"])
492 )
493 check(ok, f"t1110 flame is 3x (w and h), info={info}", name)
494 shot(page, f"t1110-flame-{name}.png")
495
496
497def run_poisoned_pointer_regression(browser):
498 """t1106: pointermoves poisoned to fieldY 9.5, touchmoves correct to 1.5.
499 Before the fix the block teleported to y=9; after, it stops at y=1.
500 This is the regression that failed on the old pointer-only path.
501 """
502 name = "poisoned-pointer"
503 print(f"\n======== {name} ========")
504 kw = dict(
505 viewport={"width": 412, "height": 915},
506 device_scale_factor=2.625,
507 is_mobile=True,
508 has_touch=True,
509 user_agent=(
510 "Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 "
511 "(KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36"
512 ),
513 )
514 context = browser.new_context(**kw)
515 page = context.new_page()
516 start_play(page, "touch")
517 reset_board(page, {"2": [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]})
518 page.wait_for_timeout(50)
519 result = page.evaluate(
520 """() => {
521 const c = app.scene.canvas;
522 const r = c.getBoundingClientRect();
523 const toClient = (fy) => ({
524 x: r.left + (2.5 / 10) * r.width,
525 y: r.top + (1 - fy / 13) * r.height
526 });
527 const p0 = toClient(0.5);
528 const pGood = toClient(1.5);
529 const pBad = toClient(9.5);
530 const fireP = (type, x, y) => {
531 const ev = new PointerEvent(type, {
532 bubbles: true, cancelable: true, composed: true,
533 pointerId: 42, pointerType: 'touch', isPrimary: true,
534 clientX: x, clientY: y, pageX: x, pageY: y,
535 buttons: type === 'pointerup' ? 0 : 1, width: 28, height: 28,
536 pressure: type === 'pointerup' ? 0 : 0.5
537 });
538 (type === 'pointerdown' ? c : window).dispatchEvent(ev);
539 };
540 const fireT = (type, x, y) => {
541 const t = new Touch({
542 identifier: 1, target: c, clientX: x, clientY: y,
543 pageX: x, pageY: y, screenX: x, screenY: y,
544 radiusX: 14, radiusY: 14, force: 0.5
545 });
546 const ev = new TouchEvent(type, {
547 bubbles: true, cancelable: true, composed: true,
548 touches: type.includes('end') || type.includes('cancel') ? [] : [t],
549 targetTouches: type.includes('end') || type.includes('cancel') ? [] : [t],
550 changedTouches: [t]
551 });
552 c.dispatchEvent(ev);
553 document.dispatchEvent(ev);
554 };
555 fireP('pointerdown', p0.x, p0.y);
556 fireT('touchstart', p0.x, p0.y);
557 const midYs = [];
558 const steps = 8;
559 for (let i = 1; i <= steps; i++) {
560 const t = i / steps;
561 fireP('pointermove', p0.x, p0.y + (pBad.y - p0.y) * t);
562 fireT('touchmove', p0.x, p0.y + (pGood.y - p0.y) * t);
563 const b = app.core.blockById(app.core.drag && app.core.drag.blockId);
564 if (b) midYs.push(Math.round(b.y * 100) / 100);
565 }
566 fireT('touchend', pGood.x, pGood.y);
567 fireP('pointerup', pGood.x, pGood.y);
568 const colors = app.core.blocks
569 .filter((b) => b.col === 2)
570 .sort((a, b) => a.y - b.y)
571 .map((b) => ({ color: b.color, y: Math.round(b.y * 100) / 100, state: b.state }));
572 const moved = colors.find((b) => b.color === 0);
573 return { moved, colors, midYs, drag: app.core.drag };
574 }"""
575 )
576 moved = result.get("moved") or {}
577 # Must end at y=1 (one tile up), never at the top (y=9).
578 ok = moved.get("y") is not None and abs(moved["y"] - 1) <= 0.15
579 check(
580 ok,
581 f"t1106 poisoned pointer + correct touch leaves color 0 at y=1, result={result}",
582 name,
583 )
584 check(
585 moved.get("y") is None or moved["y"] < 4,
586 f"t1106 must not teleport to the top, y={moved.get('y')}",
587 name,
588 )
589 context.close()
590
591
307def main():
592def main():
308 global URL
593 global URL
309 port = free_port()
594 port = free_port()
@@ -324,6 +609,7 @@ def main():
324 )
609 )
325 for device in (DESKTOP, ANDROID, ANDROID_DPR3, IOS):
610 for device in (DESKTOP, ANDROID, ANDROID_DPR3, IOS):
326 run_device(browser, device)
611 run_device(browser, device)
612 run_poisoned_pointer_regression(browser)
327 browser.close()
613 browser.close()
328 finally:
614 finally:
329 httpd.terminate()
615 httpd.terminate()
@@ -338,7 +624,7 @@ def main():
338 for f in FAILURES:
624 for f in FAILURES:
339 print(" -", f)
625 print(" -", f)
340 return 1
626 return 1
341 print("all 1:1 drag tests passed")
627 print("all drag tests passed")
342 return 0
628 return 0
343
629
344
630
test/landed-match.test.js added +136 −0
@@ -0,0 +1,136 @@
1 'use strict';
2
3 const { describe, it } = require('node:test');
4 const assert = require('node:assert/strict');
5 const { makeGame, GameCore } = require('./helpers.js');
6
7 function landClusterAt (game, members)
8 {
9 const cluster = {
10 id: game.nextClusterId++,
11 jumpHeight: 3,
12 vy: 0,
13 decel: 0,
14 phase: 'down'
15 };
16 game.clusters[cluster.id] = cluster;
17 const blocks = [];
18 for (const m of members)
19 {
20 const b = game.createBlock(m.col, m.y, m.color, 'flying');
21 b.clusterId = cluster.id;
22 b.vy = 0;
23 b.flame = m.color === GameCore.COLOR_GRAY;
24 blocks.push(b);
25 }
26 game.landCluster(cluster, blocks);
27 return { cluster, blocks };
28 }
29
30 describe('Landed clusters form mixed matches with resting blocks (t1110)', () =>
31 {
32 it('coloured cargo that lands next to two resting blocks of the same colour launches immediately', () =>
33 {
34 const g = makeGame({ noSpawn: true });
35 g.place(0, 0, 2);
36 g.place(1, 0, 2);
37 const { blocks } = landClusterAt(g, [
38 { col: 2, y: 0, color: 2 },
39 { col: 2, y: 1, color: GameCore.COLOR_GRAY }
40 ]);
41 const cargo = blocks[0];
42 assert.equal(cargo.state, 'settling');
43 assert.equal(cargo.color, 2);
44 const launched = g.resolveMatches();
45 assert.ok(launched.length >= 1, 'mixed horizontal row must launch, launched=' + launched.length);
46 assert.equal(cargo.state, 'flying');
47 assert.equal(cargo.color, GameCore.COLOR_GRAY, 'matched cargo is greyed');
48 const flying = g.blocks.filter((b) => b.state === 'flying');
49 assert.ok(flying.length >= 3, 'the two resting blocks and the cargo fly, got ' + flying.length);
50 const cluster = g.clusters[cargo.clusterId];
51 assert.ok(cluster && cluster.phase === 'up' && cluster.vy > 0);
52 });
53
54 it('a vertical mixed line of resting blocks and landed cargo also launches', () =>
55 {
56 const g = makeGame({ noSpawn: true });
57 g.place(4, 0, 1);
58 g.place(4, 1, 1);
59 const { blocks } = landClusterAt(g, [
60 { col: 4, y: 2, color: 1 }
61 ]);
62 const launched = g.resolveMatches();
63 assert.ok(launched.length >= 1, 'vertical mixed match must launch');
64 assert.equal(blocks[0].state, 'flying');
65 assert.equal(g.blocks.filter((b) => b.state === 'flying').length, 3);
66 });
67
68 it('grey cluster blocks have no colour and do not match until they recolor', () =>
69 {
70 const g = makeGame({ noSpawn: true });
71 g.place(1, 0, 0);
72 g.place(2, 0, 0);
73 g.place(0, 0, 3);
74 const { blocks } = landClusterAt(g, [
75 { col: 0, y: 1, color: GameCore.COLOR_GRAY }
76 ]);
77 assert.equal(blocks[0].color, GameCore.COLOR_GRAY);
78 assert.equal(g.resolveMatches().length, 0, 'greys cannot match');
79 assert.equal(blocks[0].state, 'settling');
80 });
81
82 it('after greys recolor on landing, a mixed line with resting blocks launches', () =>
83 {
84 const g = makeGame({ noSpawn: true, seed: 1 });
85 g.place(0, 0, 4);
86 g.place(1, 0, 3);
87 g.place(2, 0, 2);
88 g.place(1, 1, 0);
89 g.place(2, 1, 0);
90 landClusterAt(g, [
91 { col: 0, y: 1, color: GameCore.COLOR_GRAY }
92 ]);
93 g.randomColor = () => 0;
94 g.step(g.settleTime + 0.05, 1 / 120);
95 const flying = g.blocks.filter((b) => b.state === 'flying');
96 assert.ok(flying.length >= 3,
97 'recolored grey + two resting must launch, flying=' + flying.length
98 + '\n' + g.snapshot().picture
99 + '\n' + JSON.stringify(g.blocks.map((b) => ({
100 col: b.col, y: b.y, color: b.color, state: b.state
101 }))));
102 const cluster = Object.values(g.clusters).find((c) => c.phase === 'up');
103 assert.ok(cluster && cluster.vy > 0);
104 });
105
106 it('a descending cluster that lands onto a mixed row launches through the tick', () =>
107 {
108 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
109 g.place(5, 0, 3);
110 g.place(6, 0, 3);
111 const cluster = {
112 id: g.nextClusterId++,
113 jumpHeight: 3,
114 vy: -g.planet.fallSpeed,
115 decel: 0,
116 phase: 'down'
117 };
118 g.clusters[cluster.id] = cluster;
119 const cargo = g.createBlock(7, 4, 3, 'flying');
120 cargo.clusterId = cluster.id;
121 cargo.vy = cluster.vy;
122 const grey = g.createBlock(7, 5, GameCore.COLOR_GRAY, 'flying');
123 grey.clusterId = cluster.id;
124 grey.vy = cluster.vy;
125 grey.flame = true;
126 g.step(2.0, 1 / 120);
127 const up = Object.values(g.clusters).find((c) => c.phase === 'up');
128 assert.ok(up, 'landing then matching must launch a new rocket, clusters='
129 + JSON.stringify(Object.values(g.clusters).map((c) => ({ id: c.id, phase: c.phase, vy: c.vy })))
130 + ' blocks=' + JSON.stringify(g.blocks.map((b) => ({
131 col: b.col, y: Math.round(b.y * 100) / 100, color: b.color, state: b.state
132 }))));
133 assert.ok(up.vy > 0);
134 assert.ok(g.blocks.filter((b) => b.state === 'flying' && b.color === GameCore.COLOR_GRAY).length >= 3);
135 });
136 });
@@ -0,0 +1,136 @@
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, GameCore } = require('./helpers.js');
6
7function landClusterAt (game, members)
8{
9 const cluster = {
10 id: game.nextClusterId++,
11 jumpHeight: 3,
12 vy: 0,
13 decel: 0,
14 phase: 'down'
15 };
16 game.clusters[cluster.id] = cluster;
17 const blocks = [];
18 for (const m of members)
19 {
20 const b = game.createBlock(m.col, m.y, m.color, 'flying');
21 b.clusterId = cluster.id;
22 b.vy = 0;
23 b.flame = m.color === GameCore.COLOR_GRAY;
24 blocks.push(b);
25 }
26 game.landCluster(cluster, blocks);
27 return { cluster, blocks };
28}
29
30describe('Landed clusters form mixed matches with resting blocks (t1110)', () =>
31{
32 it('coloured cargo that lands next to two resting blocks of the same colour launches immediately', () =>
33 {
34 const g = makeGame({ noSpawn: true });
35 g.place(0, 0, 2);
36 g.place(1, 0, 2);
37 const { blocks } = landClusterAt(g, [
38 { col: 2, y: 0, color: 2 },
39 { col: 2, y: 1, color: GameCore.COLOR_GRAY }
40 ]);
41 const cargo = blocks[0];
42 assert.equal(cargo.state, 'settling');
43 assert.equal(cargo.color, 2);
44 const launched = g.resolveMatches();
45 assert.ok(launched.length >= 1, 'mixed horizontal row must launch, launched=' + launched.length);
46 assert.equal(cargo.state, 'flying');
47 assert.equal(cargo.color, GameCore.COLOR_GRAY, 'matched cargo is greyed');
48 const flying = g.blocks.filter((b) => b.state === 'flying');
49 assert.ok(flying.length >= 3, 'the two resting blocks and the cargo fly, got ' + flying.length);
50 const cluster = g.clusters[cargo.clusterId];
51 assert.ok(cluster && cluster.phase === 'up' && cluster.vy > 0);
52 });
53
54 it('a vertical mixed line of resting blocks and landed cargo also launches', () =>
55 {
56 const g = makeGame({ noSpawn: true });
57 g.place(4, 0, 1);
58 g.place(4, 1, 1);
59 const { blocks } = landClusterAt(g, [
60 { col: 4, y: 2, color: 1 }
61 ]);
62 const launched = g.resolveMatches();
63 assert.ok(launched.length >= 1, 'vertical mixed match must launch');
64 assert.equal(blocks[0].state, 'flying');
65 assert.equal(g.blocks.filter((b) => b.state === 'flying').length, 3);
66 });
67
68 it('grey cluster blocks have no colour and do not match until they recolor', () =>
69 {
70 const g = makeGame({ noSpawn: true });
71 g.place(1, 0, 0);
72 g.place(2, 0, 0);
73 g.place(0, 0, 3);
74 const { blocks } = landClusterAt(g, [
75 { col: 0, y: 1, color: GameCore.COLOR_GRAY }
76 ]);
77 assert.equal(blocks[0].color, GameCore.COLOR_GRAY);
78 assert.equal(g.resolveMatches().length, 0, 'greys cannot match');
79 assert.equal(blocks[0].state, 'settling');
80 });
81
82 it('after greys recolor on landing, a mixed line with resting blocks launches', () =>
83 {
84 const g = makeGame({ noSpawn: true, seed: 1 });
85 g.place(0, 0, 4);
86 g.place(1, 0, 3);
87 g.place(2, 0, 2);
88 g.place(1, 1, 0);
89 g.place(2, 1, 0);
90 landClusterAt(g, [
91 { col: 0, y: 1, color: GameCore.COLOR_GRAY }
92 ]);
93 g.randomColor = () => 0;
94 g.step(g.settleTime + 0.05, 1 / 120);
95 const flying = g.blocks.filter((b) => b.state === 'flying');
96 assert.ok(flying.length >= 3,
97 'recolored grey + two resting must launch, flying=' + flying.length
98 + '\n' + g.snapshot().picture
99 + '\n' + JSON.stringify(g.blocks.map((b) => ({
100 col: b.col, y: b.y, color: b.color, state: b.state
101 }))));
102 const cluster = Object.values(g.clusters).find((c) => c.phase === 'up');
103 assert.ok(cluster && cluster.vy > 0);
104 });
105
106 it('a descending cluster that lands onto a mixed row launches through the tick', () =>
107 {
108 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
109 g.place(5, 0, 3);
110 g.place(6, 0, 3);
111 const cluster = {
112 id: g.nextClusterId++,
113 jumpHeight: 3,
114 vy: -g.planet.fallSpeed,
115 decel: 0,
116 phase: 'down'
117 };
118 g.clusters[cluster.id] = cluster;
119 const cargo = g.createBlock(7, 4, 3, 'flying');
120 cargo.clusterId = cluster.id;
121 cargo.vy = cluster.vy;
122 const grey = g.createBlock(7, 5, GameCore.COLOR_GRAY, 'flying');
123 grey.clusterId = cluster.id;
124 grey.vy = cluster.vy;
125 grey.flame = true;
126 g.step(2.0, 1 / 120);
127 const up = Object.values(g.clusters).find((c) => c.phase === 'up');
128 assert.ok(up, 'landing then matching must launch a new rocket, clusters='
129 + JSON.stringify(Object.values(g.clusters).map((c) => ({ id: c.id, phase: c.phase, vy: c.vy })))
130 + ' blocks=' + JSON.stringify(g.blocks.map((b) => ({
131 col: b.col, y: Math.round(b.y * 100) / 100, color: b.color, state: b.state
132 }))));
133 assert.ok(up.vy > 0);
134 assert.ok(g.blocks.filter((b) => b.state === 'flying' && b.color === GameCore.COLOR_GRAY).length >= 3);
135 });
136});
test/manual-falling.test.js added +208 −0
@@ -0,0 +1,208 @@
1 'use strict';
2
3 const { describe, it } = require('node:test');
4 const assert = require('node:assert/strict');
5 const { makeGame, almost, GameCore } = require('./helpers.js');
6
7 describe('Thrown blocks collide with falling blocks (t1108)', () =>
8 {
9 it('mergeManualWithFalling averages velocity, decel and jumpHeight by block count', () =>
10 {
11 const g = makeGame({ noSpawn: true });
12 const thrown = g.place(4, 4, 1);
13 g.startManualLaunch(thrown);
14 thrown.vy = 10;
15 thrown.manualDecel = 6;
16 thrown.manualJumpHeight = 9;
17 const a = g.createBlock(4, 5, 2, 'falling');
18 a.vy = -4;
19 const b = g.createBlock(4, 6, 3, 'falling');
20 b.vy = -2;
21 const cluster = g.mergeManualWithFalling(thrown, [a, b]);
22 assert.ok(cluster);
23 assert.equal(cluster.fromManual, true);
24 assert.ok(almost(cluster.vy, (10 + -4 + -2) / 3, 1e-12), 'vy=' + cluster.vy);
25 assert.ok(almost(cluster.decel, 6 / 3, 1e-12), 'decel=' + cluster.decel);
26 assert.ok(almost(cluster.jumpHeight, 9 / 3, 1e-12), 'jumpHeight=' + cluster.jumpHeight);
27 assert.equal(thrown.state, 'flying');
28 assert.equal(a.state, 'flying');
29 assert.equal(b.state, 'flying');
30 assert.equal(a.clusterId, cluster.id);
31 assert.equal(b.clusterId, cluster.id);
32 assert.ok(almost(thrown.vy, cluster.vy, 1e-12));
33 assert.ok(almost(a.vy, cluster.vy, 1e-12));
34 });
35
36 it('a thrown block merges with a falling block and they never pass through', () =>
37 {
38 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
39 const thrown = g.place(4, 1, 1);
40 g.startManualLaunch(thrown);
41 assert.equal(thrown.state, 'manual');
42 assert.ok(thrown.vy > 0);
43 const falling = g.createBlock(4, 8, 2, 'falling');
44 falling.vy = -g.planet.fallSpeed;
45 let merged = false;
46 for (let i = 0; i < 400; i++)
47 {
48 g.tick(1 / 120);
49 if (thrown.state === 'flying' && falling.state === 'flying')
50 {
51 assert.equal(thrown.clusterId, falling.clusterId);
52 const cluster = g.clusters[thrown.clusterId];
53 assert.ok(cluster, 'a flying cluster was created');
54 assert.equal(cluster.fromManual, true);
55 assert.ok(Math.abs(Math.abs(falling.y - thrown.y) - 1) < 0.05,
56 'members packed one tile apart, thrown=' + thrown.y + ' falling=' + falling.y);
57 merged = true;
58 break;
59 }
60 assert.notEqual(falling.state, 'still',
61 'falling must not land through the thrown block, y=' + falling.y + ' thrown=' + thrown.y);
62 }
63 assert.ok(merged, 'they must have merged, thrown=' + thrown.state + '@' + thrown.y
64 + ' falling=' + falling.state + '@' + falling.y);
65 });
66
67 it('a stack of two falling blocks merges with the thrown block as one cluster of three', () =>
68 {
69 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
70 const thrown = g.place(3, 0, 0);
71 g.startManualLaunch(thrown);
72 const a = g.createBlock(3, 7, 1, 'falling');
73 const b = g.createBlock(3, 8, 2, 'falling');
74 a.vy = -g.planet.fallSpeed;
75 b.vy = -g.planet.fallSpeed;
76 let merged = false;
77 for (let i = 0; i < 400; i++)
78 {
79 g.tick(1 / 120);
80 if (thrown.state === 'flying' && a.state === 'flying' && b.state === 'flying')
81 {
82 assert.equal(a.clusterId, thrown.clusterId);
83 assert.equal(b.clusterId, thrown.clusterId);
84 const cluster = g.clusters[thrown.clusterId];
85 assert.equal(cluster.fromManual, true);
86 assert.equal(g.clusterMembers(cluster.id).length, 3);
87 merged = true;
88 break;
89 }
90 }
91 assert.ok(merged, 'all three must merge, states='
92 + [thrown, a, b].map((x) => x.state + '@' + x.y).join(', '));
93 });
94
95 it('later falling cargo on a fromManual cluster also averages and does not pass through', () =>
96 {
97 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
98 const thrown = g.place(2, 2, 3);
99 g.startManualLaunch(thrown);
100 const first = g.createBlock(2, 6, 1, 'falling');
101 first.vy = -g.planet.fallSpeed;
102 for (let i = 0; i < 400 && thrown.state !== 'flying'; i++)
103 { g.tick(1 / 120); }
104 assert.equal(thrown.state, 'flying');
105 assert.equal(first.state, 'flying');
106 const cluster = g.clusters[thrown.clusterId];
107 assert.equal(cluster.fromManual, true);
108 const roof = Math.max(thrown.y, first.y);
109 const second = g.createBlock(2, roof + 2.5, 4, 'falling');
110 second.vy = -g.planet.fallSpeed;
111 let attached = false;
112 for (let i = 0; i < 400; i++)
113 {
114 const n0 = g.clusterMembers(cluster.id).length;
115 const v0 = cluster.vy;
116 const vFall = second.vy;
117 g.tick(1 / 120);
118 if (second.state === 'flying' && second.clusterId === cluster.id)
119 {
120 // Attach averages, then this same tick's later substeps / cluster
121 // integrate may ease vy a little. The new vy must be between the
122 // two inputs (or just after a small decel from that average).
123 const expected = (v0 * n0 + vFall) / (n0 + 1);
124 assert.ok(Math.abs(cluster.vy - expected) < 0.5
125 || (cluster.vy - expected) * Math.sign(cluster.decel || 1) <= 0,
126 'second cargo averages, vy=' + cluster.vy + ' expected ~' + expected
127 + ' from v0=' + v0 + ' n0=' + n0 + ' vFall=' + vFall);
128 assert.ok(second.y + 1e-6 >= Math.min(thrown.y, first.y) - 0.05,
129 'second cargo did not pass through, y=' + second.y);
130 assert.equal(g.clusterMembers(cluster.id).length, n0 + 1);
131 attached = true;
132 break;
133 }
134 assert.ok(second.y + 1e-6 >= Math.min(thrown.y, first.y) - 0.05,
135 'must not pass through before attach, second=' + second.y
136 + ' cluster min=' + Math.min(thrown.y, first.y));
137 }
138 assert.ok(attached, 'second falling block must attach, state=' + second.state + ' y=' + second.y);
139
140 // Direct attach average, isolated from integrate.
141 const g2 = makeGame({ noSpawn: true });
142 const cl = {
143 id: g2.nextClusterId++,
144 jumpHeight: 6,
145 vy: 8,
146 decel: 4,
147 phase: 'up',
148 fromManual: true
149 };
150 g2.clusters[cl.id] = cl;
151 const r = g2.createBlock(1, 4, GameCore.COLOR_GRAY, 'flying');
152 r.clusterId = cl.id;
153 r.vy = 8;
154 const c1 = g2.createBlock(1, 5, 1, 'flying');
155 c1.clusterId = cl.id;
156 c1.vy = 8;
157 const extra = g2.createBlock(1, 6.05, 2, 'falling');
158 extra.vy = -4;
159 assert.ok(g2.attachToFlyingCluster(extra, c1));
160 assert.ok(almost(cl.vy, (8 * 2 + -4) / 3, 1e-12), 'isolated average, vy=' + cl.vy);
161 assert.ok(almost(extra.vy, cl.vy, 1e-12));
162 assert.ok(almost(r.vy, cl.vy, 1e-12));
163 assert.ok(almost(cl.decel, 4 * 2 / 3, 1e-12));
164 assert.ok(almost(cl.jumpHeight, 6 * 2 / 3, 1e-12));
165 });
166
167 it('a fast substep that would cross still merges; they never pass through', () =>
168 {
169 const g = makeGame({ noSpawn: true });
170 const thrown = g.place(5, 5, 1);
171 g.startManualLaunch(thrown);
172 thrown.vy = 80;
173 thrown.manualDecel = 0;
174 const falling = g.createBlock(5, 6.2, 2, 'falling');
175 falling.vy = -80;
176 g.tick(1 / 120);
177 assert.equal(thrown.state, 'flying', 'merged, thrown=' + thrown.state);
178 assert.equal(falling.state, 'flying');
179 assert.equal(thrown.clusterId, falling.clusterId);
180 assert.ok(Math.abs(falling.y - thrown.y) >= 1 - 0.05,
181 'packed without overlapping, thrown=' + thrown.y + ' falling=' + falling.y);
182 });
183
184 it('a normal match-rocket does not average falling cargo (existing attach rule)', () =>
185 {
186 const g = makeGame({ noSpawn: true });
187 const cluster = {
188 id: g.nextClusterId++,
189 jumpHeight: 4,
190 vy: 6,
191 decel: 3,
192 phase: 'hold'
193 };
194 g.clusters[cluster.id] = cluster;
195 const rocket = g.createBlock(1, 4, GameCore.COLOR_GRAY, 'flying');
196 rocket.clusterId = cluster.id;
197 rocket.vy = 6;
198 rocket.flame = true;
199 const cargo = g.createBlock(1, 5.05, 3, 'falling');
200 cargo.vy = -g.planet.fallSpeed;
201 assert.ok(g.attachToFlyingCluster(cargo, rocket));
202 assert.equal(cargo.state, 'flying');
203 assert.equal(cargo.clusterId, cluster.id);
204 assert.ok(almost(cluster.vy, 6, 1e-12), 'rocket vy unchanged, vy=' + cluster.vy);
205 assert.ok(almost(cargo.vy, 6, 1e-12));
206 assert.equal(cluster.fromManual, undefined);
207 });
208 });
@@ -0,0 +1,208 @@
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, almost, GameCore } = require('./helpers.js');
6
7describe('Thrown blocks collide with falling blocks (t1108)', () =>
8{
9 it('mergeManualWithFalling averages velocity, decel and jumpHeight by block count', () =>
10 {
11 const g = makeGame({ noSpawn: true });
12 const thrown = g.place(4, 4, 1);
13 g.startManualLaunch(thrown);
14 thrown.vy = 10;
15 thrown.manualDecel = 6;
16 thrown.manualJumpHeight = 9;
17 const a = g.createBlock(4, 5, 2, 'falling');
18 a.vy = -4;
19 const b = g.createBlock(4, 6, 3, 'falling');
20 b.vy = -2;
21 const cluster = g.mergeManualWithFalling(thrown, [a, b]);
22 assert.ok(cluster);
23 assert.equal(cluster.fromManual, true);
24 assert.ok(almost(cluster.vy, (10 + -4 + -2) / 3, 1e-12), 'vy=' + cluster.vy);
25 assert.ok(almost(cluster.decel, 6 / 3, 1e-12), 'decel=' + cluster.decel);
26 assert.ok(almost(cluster.jumpHeight, 9 / 3, 1e-12), 'jumpHeight=' + cluster.jumpHeight);
27 assert.equal(thrown.state, 'flying');
28 assert.equal(a.state, 'flying');
29 assert.equal(b.state, 'flying');
30 assert.equal(a.clusterId, cluster.id);
31 assert.equal(b.clusterId, cluster.id);
32 assert.ok(almost(thrown.vy, cluster.vy, 1e-12));
33 assert.ok(almost(a.vy, cluster.vy, 1e-12));
34 });
35
36 it('a thrown block merges with a falling block and they never pass through', () =>
37 {
38 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
39 const thrown = g.place(4, 1, 1);
40 g.startManualLaunch(thrown);
41 assert.equal(thrown.state, 'manual');
42 assert.ok(thrown.vy > 0);
43 const falling = g.createBlock(4, 8, 2, 'falling');
44 falling.vy = -g.planet.fallSpeed;
45 let merged = false;
46 for (let i = 0; i < 400; i++)
47 {
48 g.tick(1 / 120);
49 if (thrown.state === 'flying' && falling.state === 'flying')
50 {
51 assert.equal(thrown.clusterId, falling.clusterId);
52 const cluster = g.clusters[thrown.clusterId];
53 assert.ok(cluster, 'a flying cluster was created');
54 assert.equal(cluster.fromManual, true);
55 assert.ok(Math.abs(Math.abs(falling.y - thrown.y) - 1) < 0.05,
56 'members packed one tile apart, thrown=' + thrown.y + ' falling=' + falling.y);
57 merged = true;
58 break;
59 }
60 assert.notEqual(falling.state, 'still',
61 'falling must not land through the thrown block, y=' + falling.y + ' thrown=' + thrown.y);
62 }
63 assert.ok(merged, 'they must have merged, thrown=' + thrown.state + '@' + thrown.y
64 + ' falling=' + falling.state + '@' + falling.y);
65 });
66
67 it('a stack of two falling blocks merges with the thrown block as one cluster of three', () =>
68 {
69 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
70 const thrown = g.place(3, 0, 0);
71 g.startManualLaunch(thrown);
72 const a = g.createBlock(3, 7, 1, 'falling');
73 const b = g.createBlock(3, 8, 2, 'falling');
74 a.vy = -g.planet.fallSpeed;
75 b.vy = -g.planet.fallSpeed;
76 let merged = false;
77 for (let i = 0; i < 400; i++)
78 {
79 g.tick(1 / 120);
80 if (thrown.state === 'flying' && a.state === 'flying' && b.state === 'flying')
81 {
82 assert.equal(a.clusterId, thrown.clusterId);
83 assert.equal(b.clusterId, thrown.clusterId);
84 const cluster = g.clusters[thrown.clusterId];
85 assert.equal(cluster.fromManual, true);
86 assert.equal(g.clusterMembers(cluster.id).length, 3);
87 merged = true;
88 break;
89 }
90 }
91 assert.ok(merged, 'all three must merge, states='
92 + [thrown, a, b].map((x) => x.state + '@' + x.y).join(', '));
93 });
94
95 it('later falling cargo on a fromManual cluster also averages and does not pass through', () =>
96 {
97 const g = makeGame({ noSpawn: true, planetOpts: { jumpsToClear: 2, fallTime: 3, riseTime: 1.5 } });
98 const thrown = g.place(2, 2, 3);
99 g.startManualLaunch(thrown);
100 const first = g.createBlock(2, 6, 1, 'falling');
101 first.vy = -g.planet.fallSpeed;
102 for (let i = 0; i < 400 && thrown.state !== 'flying'; i++)
103 { g.tick(1 / 120); }
104 assert.equal(thrown.state, 'flying');
105 assert.equal(first.state, 'flying');
106 const cluster = g.clusters[thrown.clusterId];
107 assert.equal(cluster.fromManual, true);
108 const roof = Math.max(thrown.y, first.y);
109 const second = g.createBlock(2, roof + 2.5, 4, 'falling');
110 second.vy = -g.planet.fallSpeed;
111 let attached = false;
112 for (let i = 0; i < 400; i++)
113 {
114 const n0 = g.clusterMembers(cluster.id).length;
115 const v0 = cluster.vy;
116 const vFall = second.vy;
117 g.tick(1 / 120);
118 if (second.state === 'flying' && second.clusterId === cluster.id)
119 {
120 // Attach averages, then this same tick's later substeps / cluster
121 // integrate may ease vy a little. The new vy must be between the
122 // two inputs (or just after a small decel from that average).
123 const expected = (v0 * n0 + vFall) / (n0 + 1);
124 assert.ok(Math.abs(cluster.vy - expected) < 0.5
125 || (cluster.vy - expected) * Math.sign(cluster.decel || 1) <= 0,
126 'second cargo averages, vy=' + cluster.vy + ' expected ~' + expected
127 + ' from v0=' + v0 + ' n0=' + n0 + ' vFall=' + vFall);
128 assert.ok(second.y + 1e-6 >= Math.min(thrown.y, first.y) - 0.05,
129 'second cargo did not pass through, y=' + second.y);
130 assert.equal(g.clusterMembers(cluster.id).length, n0 + 1);
131 attached = true;
132 break;
133 }
134 assert.ok(second.y + 1e-6 >= Math.min(thrown.y, first.y) - 0.05,
135 'must not pass through before attach, second=' + second.y
136 + ' cluster min=' + Math.min(thrown.y, first.y));
137 }
138 assert.ok(attached, 'second falling block must attach, state=' + second.state + ' y=' + second.y);
139
140 // Direct attach average, isolated from integrate.
141 const g2 = makeGame({ noSpawn: true });
142 const cl = {
143 id: g2.nextClusterId++,
144 jumpHeight: 6,
145 vy: 8,
146 decel: 4,
147 phase: 'up',
148 fromManual: true
149 };
150 g2.clusters[cl.id] = cl;
151 const r = g2.createBlock(1, 4, GameCore.COLOR_GRAY, 'flying');
152 r.clusterId = cl.id;
153 r.vy = 8;
154 const c1 = g2.createBlock(1, 5, 1, 'flying');
155 c1.clusterId = cl.id;
156 c1.vy = 8;
157 const extra = g2.createBlock(1, 6.05, 2, 'falling');
158 extra.vy = -4;
159 assert.ok(g2.attachToFlyingCluster(extra, c1));
160 assert.ok(almost(cl.vy, (8 * 2 + -4) / 3, 1e-12), 'isolated average, vy=' + cl.vy);
161 assert.ok(almost(extra.vy, cl.vy, 1e-12));
162 assert.ok(almost(r.vy, cl.vy, 1e-12));
163 assert.ok(almost(cl.decel, 4 * 2 / 3, 1e-12));
164 assert.ok(almost(cl.jumpHeight, 6 * 2 / 3, 1e-12));
165 });
166
167 it('a fast substep that would cross still merges; they never pass through', () =>
168 {
169 const g = makeGame({ noSpawn: true });
170 const thrown = g.place(5, 5, 1);
171 g.startManualLaunch(thrown);
172 thrown.vy = 80;
173 thrown.manualDecel = 0;
174 const falling = g.createBlock(5, 6.2, 2, 'falling');
175 falling.vy = -80;
176 g.tick(1 / 120);
177 assert.equal(thrown.state, 'flying', 'merged, thrown=' + thrown.state);
178 assert.equal(falling.state, 'flying');
179 assert.equal(thrown.clusterId, falling.clusterId);
180 assert.ok(Math.abs(falling.y - thrown.y) >= 1 - 0.05,
181 'packed without overlapping, thrown=' + thrown.y + ' falling=' + falling.y);
182 });
183
184 it('a normal match-rocket does not average falling cargo (existing attach rule)', () =>
185 {
186 const g = makeGame({ noSpawn: true });
187 const cluster = {
188 id: g.nextClusterId++,
189 jumpHeight: 4,
190 vy: 6,
191 decel: 3,
192 phase: 'hold'
193 };
194 g.clusters[cluster.id] = cluster;
195 const rocket = g.createBlock(1, 4, GameCore.COLOR_GRAY, 'flying');
196 rocket.clusterId = cluster.id;
197 rocket.vy = 6;
198 rocket.flame = true;
199 const cargo = g.createBlock(1, 5.05, 3, 'falling');
200 cargo.vy = -g.planet.fallSpeed;
201 assert.ok(g.attachToFlyingCluster(cargo, rocket));
202 assert.equal(cargo.state, 'flying');
203 assert.equal(cargo.clusterId, cluster.id);
204 assert.ok(almost(cluster.vy, 6, 1e-12), 'rocket vy unchanged, vy=' + cluster.vy);
205 assert.ok(almost(cargo.vy, 6, 1e-12));
206 assert.equal(cluster.fromManual, undefined);
207 });
208});
test/snap-cell.test.js added +79 −0
@@ -0,0 +1,79 @@
1 'use strict';
2
3 const { describe, it } = require('node:test');
4 const assert = require('node:assert/strict');
5 const { makeGame, GameCore } = require('./helpers.js');
6
7 describe('Cell-based grab picking (t1109)', () =>
8 {
9 it('a press anywhere inside the cell, including each corner, grabs that block', () =>
10 {
11 const g = makeGame({ noSpawn: true });
12 const block = g.place(2, 3, 1);
13 g.place(2, 2, 0);
14 g.place(2, 4, 2);
15 const corners = [
16 [2.01, 3.01],
17 [2.01, 3.99],
18 [2.99, 3.01],
19 [2.99, 3.99],
20 [2.5, 3.5],
21 [2.15, 3.15],
22 [2.85, 3.85]
23 ];
24 for (const [col, y] of corners)
25 {
26 assert.ok(g.beginDrag(col, y), 'should grab at (' + col + ', ' + y + ')');
27 assert.equal(g.drag.blockId, block.id, 'wrong block at (' + col + ', ' + y + ')');
28 g.endDrag();
29 }
30 });
31
32 it('a press in the padding / gap of a cell still grabs the occupant (not the sprite box)', () =>
33 {
34 const g = makeGame({ noSpawn: true });
35 const block = g.place(4, 1, 3);
36 g.place(4, 0, 0);
37 // Near the edges — "gaps between sprites" if sprites are inset.
38 assert.ok(g.beginDrag(4.02, 1.02));
39 assert.equal(g.drag.blockId, block.id);
40 g.endDrag();
41 assert.ok(g.beginDrag(4.98, 1.98));
42 assert.equal(g.drag.blockId, block.id);
43 g.endDrag();
44 });
45
46 it('when a block is mid-animation (non-integer y), the nearest in the column within half a tile is grabbed', () =>
47 {
48 const g = makeGame({ noSpawn: true });
49 const block = g.place(1, 2, 2);
50 g.place(1, 0, 0);
51 // Simulate a bounce / settle offset: the block sits at 2.35.
52 block.y = 2.35;
53 // Press in cell 2 (overlaps the footprint [2.35, 3.35)).
54 assert.ok(g.beginDrag(1.4, 2.5));
55 assert.equal(g.drag.blockId, block.id);
56 g.endDrag();
57 // Press in cell 3, just above the footprint — fallback within 0.5.
58 // Footprint ends at 3.35; press at 3.55 is 0.20 away.
59 assert.ok(g.beginDrag(1.4, 3.55));
60 assert.equal(g.drag.blockId, block.id);
61 g.endDrag();
62 // Further than SNAP_Y (0.5) above 3.35: at 3.35+0.5+0.15=4.0, no grab.
63 assert.equal(g.beginDrag(1.4, 3.35 + GameCore.SNAP_Y + 0.15), false);
64 });
65
66 it('floor of the pointer chooses the cell; the cell above is a different block', () =>
67 {
68 const g = makeGame({ noSpawn: true });
69 const lower = g.place(0, 5, 1);
70 const upper = g.place(0, 6, 2);
71 g.place(0, 4, 0);
72 assert.ok(g.beginDrag(0.99, 5.99));
73 assert.equal(g.drag.blockId, lower.id);
74 g.endDrag();
75 assert.ok(g.beginDrag(0.01, 6.01));
76 assert.equal(g.drag.blockId, upper.id);
77 g.endDrag();
78 });
79 });
@@ -0,0 +1,79 @@
1'use strict';
2
3const { describe, it } = require('node:test');
4const assert = require('node:assert/strict');
5const { makeGame, GameCore } = require('./helpers.js');
6
7describe('Cell-based grab picking (t1109)', () =>
8{
9 it('a press anywhere inside the cell, including each corner, grabs that block', () =>
10 {
11 const g = makeGame({ noSpawn: true });
12 const block = g.place(2, 3, 1);
13 g.place(2, 2, 0);
14 g.place(2, 4, 2);
15 const corners = [
16 [2.01, 3.01],
17 [2.01, 3.99],
18 [2.99, 3.01],
19 [2.99, 3.99],
20 [2.5, 3.5],
21 [2.15, 3.15],
22 [2.85, 3.85]
23 ];
24 for (const [col, y] of corners)
25 {
26 assert.ok(g.beginDrag(col, y), 'should grab at (' + col + ', ' + y + ')');
27 assert.equal(g.drag.blockId, block.id, 'wrong block at (' + col + ', ' + y + ')');
28 g.endDrag();
29 }
30 });
31
32 it('a press in the padding / gap of a cell still grabs the occupant (not the sprite box)', () =>
33 {
34 const g = makeGame({ noSpawn: true });
35 const block = g.place(4, 1, 3);
36 g.place(4, 0, 0);
37 // Near the edges — "gaps between sprites" if sprites are inset.
38 assert.ok(g.beginDrag(4.02, 1.02));
39 assert.equal(g.drag.blockId, block.id);
40 g.endDrag();
41 assert.ok(g.beginDrag(4.98, 1.98));
42 assert.equal(g.drag.blockId, block.id);
43 g.endDrag();
44 });
45
46 it('when a block is mid-animation (non-integer y), the nearest in the column within half a tile is grabbed', () =>
47 {
48 const g = makeGame({ noSpawn: true });
49 const block = g.place(1, 2, 2);
50 g.place(1, 0, 0);
51 // Simulate a bounce / settle offset: the block sits at 2.35.
52 block.y = 2.35;
53 // Press in cell 2 (overlaps the footprint [2.35, 3.35)).
54 assert.ok(g.beginDrag(1.4, 2.5));
55 assert.equal(g.drag.blockId, block.id);
56 g.endDrag();
57 // Press in cell 3, just above the footprint — fallback within 0.5.
58 // Footprint ends at 3.35; press at 3.55 is 0.20 away.
59 assert.ok(g.beginDrag(1.4, 3.55));
60 assert.equal(g.drag.blockId, block.id);
61 g.endDrag();
62 // Further than SNAP_Y (0.5) above 3.35: at 3.35+0.5+0.15=4.0, no grab.
63 assert.equal(g.beginDrag(1.4, 3.35 + GameCore.SNAP_Y + 0.15), false);
64 });
65
66 it('floor of the pointer chooses the cell; the cell above is a different block', () =>
67 {
68 const g = makeGame({ noSpawn: true });
69 const lower = g.place(0, 5, 1);
70 const upper = g.place(0, 6, 2);
71 g.place(0, 4, 0);
72 assert.ok(g.beginDrag(0.99, 5.99));
73 assert.equal(g.drag.blockId, lower.id);
74 g.endDrag();
75 assert.ok(g.beginDrag(0.01, 6.01));
76 assert.equal(g.drag.blockId, upper.id);
77 g.endDrag();
78 });
79});