1const assert = require('assert');
2const core = require('./game.js');
3
4function test(name, fn) {
5 try { fn(); console.log('✓', name); }
6 catch (err) { console.error('✗', name); throw err; }
7}
8
9test('clamp bounds values', () => {
10 assert.strictEqual(core.clamp(5, 0, 10), 5);
11 assert.strictEqual(core.clamp(-1, 0, 10), 0);
12 assert.strictEqual(core.clamp(11, 0, 10), 10);
13});
14
15test('circle/rect collision detects overlap and misses', () => {
16 const rect = { x: 10, y: 10, w: 40, h: 20 };
17 assert.strictEqual(core.circleRectCollision({ x: 20, y: 20, r: 5 }, rect), true);
18 assert.strictEqual(core.circleRectCollision({ x: 0, y: 0, r: 5 }, rect), false);
19 assert.strictEqual(core.circleRectCollision({ x: 8, y: 20, r: 3 }, rect), true);
20});
21
22test('paddle reflection sends ball upward and changes horizontal velocity', () => {
23 const state = core.createState();
24 const b = state.ball, p = state.paddle;
25 b.vx = 0; b.vy = 300; b.x = p.x + p.w; b.y = p.y - b.r;
26 core.reflectFromPaddle(b, p);
27 assert(b.vy < 0, 'ball should travel upward');
28 assert(b.vx > 0, 'right edge hit should push right');
29});
30
31test('wall collision reverses horizontal velocity', () => {
32 const state = core.createState();
33 state.paused = false; state.ball.stuck = false;
34 state.ball.x = 3; state.ball.y = 300; state.ball.vx = -200; state.ball.vy = 0;
35 core.step(state, 0.016, { move: 0 });
36 assert(state.ball.vx > 0);
37 assert.strictEqual(state.ball.x, state.ball.r);
38});
39
40test('brick collision kills brick, scores, and reflects', () => {
41 const state = core.createState();
42 state.paused = false; state.ball.stuck = false;
43 const brick = state.bricks[0];
44 const aliveBefore = state.bricks.filter(b => b.alive).length;
45 state.ball.x = brick.x + brick.w / 2;
46 state.ball.y = brick.y - state.ball.r + 1;
47 state.ball.vx = 0; state.ball.vy = 200;
48 core.step(state, 0.016, { move: 0 });
49 assert.strictEqual(brick.alive, false);
50 assert(state.score > 0);
51 assert(state.ball.vy < 0);
52 assert.strictEqual(state.bricks.filter(b => b.alive).length, aliveBefore - 1);
53});
54
55test('missing the paddle costs a life and resets stuck ball', () => {
56 const state = core.createState();
57 state.paused = false; state.ball.stuck = false;
58 state.ball.x = 100; state.ball.y = state.height + 20; state.ball.vy = 300;
59 core.step(state, 0.016, { move: 0 });
60 assert.strictEqual(state.lives, 2);
61 assert.strictEqual(state.ball.stuck, true);
62});
63
64test('clearing all bricks advances level and increases speed', () => {
65 const state = core.createState();
66 state.paused = false; state.ball.stuck = false;
67 const oldSpeed = state.ball.speed;
68 for (const br of state.bricks) br.alive = false;
69 core.step(state, 0.016, { move: 0 });
70 assert.strictEqual(state.level, 2);
71 assert(state.ball.speed > oldSpeed);
72 assert.strictEqual(state.paused, true);
73});
74
75console.log('All core physics tests passed.');
76
Discussion
No comments yet. Start the discussion. Recorded by @patrick-toulme.