Files
VibemakerSandbox/app/test/default-world.test.js
T

183 lines
6.6 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import * as THREE from "three";
import {
DEFAULT_WORLD,
resolveWorldId,
fitWorldToRobot,
arenaWalls,
} from "../src/platform/worlds/arena.js";
import {
validatePackage,
validateWorld,
} from "../src/platform/loader/SchemaValidator.js";
import { createArenaFloorPresentation } from "../src/game/worlds/arena/presentation.js";
const world = JSON.parse(
await fs.readFile(
new URL("../public/worlds/arena/world.json", import.meta.url),
),
);
test("all bundled and future Packages share the arena template and declare nominal height; flat diagnostics stay explicit", async () => {
assert.equal(DEFAULT_WORLD, "arena");
validateWorld(world);
assert.equal(world.type, "flat");
assert.deepEqual(world.boundary, {
half_extent: 2.25,
wall_height: 0.25,
wall_thickness: 0.05,
});
assert.equal(world.boundary.half_extent / 1.5, 1.5);
for (const id of ["microduck-legs", "microduck-rollers", "microban", "rp1"]) {
const config = JSON.parse(
await fs.readFile(
new URL(`../public/robots/${id}/robot.json`, import.meta.url),
),
);
validatePackage(config);
assert.equal(resolveWorldId(config), "arena");
const fitted = fitWorldToRobot(world, config);
assert.equal(fitted.scale, config.dimensions.height / 0.27);
assert.equal(fitted.boundary.half_extent, 2.25 * fitted.scale);
assert.equal(fitted.boundary.wall_height, 0.25 * fitted.scale);
assert.equal(fitted.boundary.wall_thickness, 0.05 * fitted.scale);
assert.equal(world.boundary.half_extent, 2.25); // Never mutate the template.
if (id === "microban") {
config.id = "future-bot";
config.family = "future-bot";
delete config.presentation.world;
validatePackage(config);
assert.equal(resolveWorldId(config), "arena");
config.presentation.world = "flat";
assert.equal(resolveWorldId(config), "flat");
}
}
});
test("fitting is idempotent, missing height stays at reference scale, and fixed diagnostic Worlds do not scale", () => {
const config = { dimensions: { height: 1.32 } };
const fitted = fitWorldToRobot(world, config);
assert.ok(Math.abs(fitted.boundary.half_extent * 2 - 22) < 1e-10);
assert.deepEqual(fitWorldToRobot(fitted, config), fitted);
assert.deepEqual(fitWorldToRobot(world, {}).boundary, world.boundary);
const fixed = structuredClone(world);
delete fixed.scale_to_robot;
assert.equal(fitWorldToRobot(fixed, config), fixed);
const gridded = structuredClone(world);
gridded.floor.grid = { size: 3, divisions: 10, color: "#ffffff" };
assert.equal(
fitWorldToRobot(gridded, config).floor.grid.size,
3 * fitted.scale,
);
assert.equal(gridded.floor.grid.size, 3);
for (const height of [0, -1, Infinity, NaN])
assert.throws(
() => fitWorldToRobot(world, { dimensions: { height } }),
/scale/,
);
});
test("physical wall inner faces match 4.5m bounds and corners overlap", () => {
const walls = arenaWalls(world.boundary);
assert.equal(walls.length, 4);
assert.deepEqual(walls[0], {
name: "wall_px",
pos: [2.275, 0, 0.125],
size: [0.025, 2.3, 0.125],
});
assert.deepEqual(walls[2], {
name: "wall_py",
pos: [0, 2.275, 0.125],
size: [2.3, 0.025, 0.125],
});
for (const w of walls) {
assert.equal(w.size[2] * 2, 0.25);
assert.ok(w.size[0] === 0.025 || w.size[1] === 0.025);
}
});
test("visual walls stay at physical inner faces while the floor focus follows; all resources release once", () => {
const defs = [
"platform_floor",
...arenaWalls(world.boundary).map((w) => w.name),
],
meshes = defs.map(() => new THREE.Mesh());
const sim = { model: { geom: (name) => ({ id: defs.indexOf(name) }) } },
rig = { items: meshes.map((mesh, id) => ({ mesh, id })) };
const view = createArenaFloorPresentation({ sim, rig, world });
assert.equal(view.walls.length, 4);
assert.ok(meshes.every((m) => !m.visible));
const positions = view.walls.map((m) => m.position.toArray());
assert.deepEqual(positions[0], [2.25, 0.125, 0]);
assert.deepEqual(positions[2], [0, 0.125, 2.25]);
view.update(new THREE.Vector3(1, 0.2, -1));
assert.deepEqual(
view.walls.map((m) => m.position.toArray()),
positions,
);
let releases = 0;
for (const mesh of [view.grid, ...view.walls]) {
mesh.geometry.addEventListener("dispose", () => releases++);
mesh.material.addEventListener("dispose", () => releases++);
}
view.dispose();
view.dispose();
assert.equal(releases, 10);
assert.ok(meshes.every((m) => m.visible));
});
test("RP1 visual walls, grid spacing and fade distances scale with the same collision dimensions", () => {
const fitted = fitWorldToRobot(world, { dimensions: { height: 1.32 } });
const names = [
"platform_floor",
...arenaWalls(fitted.boundary).map((w) => w.name),
];
const sim = { model: { geom: (name) => ({ id: names.indexOf(name) }) } };
const rig = { items: names.map((_, id) => ({ id, mesh: new THREE.Mesh() })) };
const view = createArenaFloorPresentation({ sim, rig, world: fitted });
assert.equal(view.grid.material.uniforms.uCell.value, 0.1 * fitted.scale);
assert.equal(view.grid.material.uniforms.uSection.value, 0.6 * fitted.scale);
assert.equal(view.grid.material.uniforms.uFadeDist.value, 3 * fitted.scale);
assert.equal(view.grid.geometry.parameters.width, 30 * fitted.scale);
for (const mesh of view.walls) {
assert.equal(mesh.geometry.parameters.height, fitted.boundary.wall_height);
assert.equal(
mesh.geometry.parameters.width,
2 * (fitted.boundary.half_extent + fitted.boundary.wall_thickness),
);
assert.equal(
mesh.material.uniforms.uWallH.value,
fitted.boundary.wall_height,
);
assert.equal(mesh.material.uniforms.uFadeDist.value, 5 * fitted.scale);
assert.equal(mesh.material.uniforms.uCell.value, 0.1 * fitted.scale);
}
assert.deepEqual(view.walls[0].position.toArray(), [
fitted.boundary.half_extent,
fitted.boundary.wall_height / 2,
0,
]);
view.dispose();
});
test("invalid physical boundaries and scaling declarations are rejected", () => {
for (const scale_to_robot of [
{ reference_height: 0 },
{ reference_height: -1 },
{},
])
assert.throws(
() => validateWorld({ ...world, scale_to_robot }),
/scale_to_robot/,
);
assert.throws(
() => validateWorld({ ...world, boundary: undefined }),
/boundary/,
);
for (const bad of [
{ half_extent: 0 },
{ wall_height: -1 },
{ wall_thickness: 0 },
])
assert.throws(
() =>
validateWorld({ ...world, boundary: { ...world.boundary, ...bad } }),
/boundary/,
);
});