Calio/tests/physics.test.js
Elijah ddea7fc512
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 28s
Extensive UI changes
2026-10-20 19:46:08 -07:00

135 lines
4.7 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {
bubbleCount,
createBubbleWorld,
screenGravity,
MAX_BUBBLES,
BUBBLE_RADIUS,
DRAW_BUBBLE_RADIUS,
VESSEL_RADIUS,
} from "../shared/bubble-physics.js";
test("bubble count tracks calories, remains visible at small amounts, and caps over goal", () => {
assert.equal(bubbleCount(0, 2000, true), 0);
assert.equal(bubbleCount(1, 2000, true), 1);
assert.equal(bubbleCount(1000, 2000, true), Math.round(MAX_BUBBLES / 2));
assert.equal(bubbleCount(2000, 2000, true), MAX_BUBBLES);
assert.equal(bubbleCount(20000, 2000, true), MAX_BUBBLES);
});
test("an 800 calorie meal drops in with 50 percent more bubbles per second", () => {
const world = createBubbleWorld(0, () => 0.5);
const target = bubbleCount(800, 2000, true);
world.setTarget(target);
for (let i = 0; i < 60; i++) world.step();
assert.equal(world.balls.length, 30);
for (let i = 0; i < target * 2 - 60; i++) world.step();
assert.equal(world.balls.length, target);
world.dispose();
});
test("new bubbles enter at the top, fall, collide and stay inside the calorie vessel", () => {
const world = createBubbleWorld(0, () => 0.5);
world.setTarget(8);
world.step(50);
assert.equal(world.balls.length, 1);
assert.ok(world.balls[0].position.y < 50);
for (let i = 0; i < 400; i++) world.step();
assert.equal(world.balls.length, 8);
assert.ok(world.balls.every((ball) => ball.position.y > 220));
for (const ball of world.balls)
assert.ok(
Math.hypot(ball.position.x - 160, ball.position.y - 160) <=
VESSEL_RADIUS - DRAW_BUBBLE_RADIUS + 1,
);
for (let i = 0; i < world.balls.length; i++)
for (let j = i + 1; j < world.balls.length; j++)
assert.ok(
Math.hypot(
world.balls[i].position.x - world.balls[j].position.x,
world.balls[i].position.y - world.balls[j].position.y,
) >
BUBBLE_RADIUS * 1.8,
);
world.dispose();
});
test("full and over-goal vessels remain bounded through repeated gravity reversals", () => {
const world = createBubbleWorld(MAX_BUBBLES);
assert.equal(world.balls.length, MAX_BUBBLES);
assert.ok(BUBBLE_RADIUS - DRAW_BUBBLE_RADIUS >= 1);
assert.ok(Math.min(...world.balls.map((ball) => ball.position.y)) < 30);
assert.ok(Math.max(...world.balls.map((ball) => ball.position.y)) > 290);
assert.ok(
world.balls.some(
(ball) => Math.hypot(ball.position.x - 160, ball.position.y - 160) < 2,
),
);
for (const [x, y] of [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
[0.6, 0.8],
]) {
world.gravity(x, y);
for (let i = 0; i < 180; i++) world.step();
for (const ball of world.balls)
assert.ok(
Number.isFinite(ball.position.x) &&
Math.hypot(ball.position.x - 160, ball.position.y - 160) <=
VESSEL_RADIUS - DRAW_BUBBLE_RADIUS + 1,
);
}
world.setTarget(10);
assert.equal(world.balls.length, 10);
world.setTarget(0);
assert.equal(world.balls.length, 0);
world.dispose();
});
test("bubbles follow changing gravity and wake when nudged", () => {
const world = createBubbleWorld(12);
world.gravity(1, 0);
for (let i = 0; i < 400; i++) world.step();
assert.ok(
world.balls.reduce((sum, ball) => sum + ball.position.x, 0) / 12 > 240,
);
world.gravity(-1, 0);
for (let i = 0; i < 400; i++) world.step();
assert.ok(
world.balls.reduce((sum, ball) => sum + ball.position.x, 0) / 12 < 80,
);
world.nudge();
assert.ok(world.awake());
assert.ok(world.balls.some((ball) => ball.speed > 1));
world.dispose();
});
test("a settled 100% vessel fills to the upper rim and retains a visible gap between dots", () => {
const world = createBubbleWorld(MAX_BUBBLES);
for (let i = 0; i < 700; i++) world.step();
assert.ok(
Math.min(...world.balls.map((ball) => ball.position.y)) -
DRAW_BUBBLE_RADIUS <
35,
);
for (let i = 0; i < world.balls.length; i++)
for (let j = i + 1; j < world.balls.length; j++) {
const distance = Math.hypot(
world.balls[i].position.x - world.balls[j].position.x,
world.balls[i].position.y - world.balls[j].position.y,
);
assert.ok(distance - 2 * DRAW_BUBBLE_RADIUS > 0.75);
}
world.dispose();
});
test("orientation projects gravity for portrait, landscape, upside-down and flat phones", () => {
const upright = screenGravity(90, 0, 0);
assert.ok(Math.abs(upright.x) < 1e-10);
assert.ok(upright.y > 0.99);
const inverted = screenGravity(-90, 0, 0);
assert.ok(inverted.y < -0.99);
const right = screenGravity(0, 90, 0);
assert.ok(right.x > 0.99);
const landscape = screenGravity(0, 90, 90);
assert.ok(Math.abs(landscape.x) < 1e-10);
assert.ok(landscape.y < -0.99);
assert.deepEqual(screenGravity(0, 0, 0), { x: 0, y: 0.18 });
});