import test from "node:test"; import assert from "node:assert/strict"; import * as THREE from "three"; import { generateInteractiveProps, createInteractivePropVisuals, resetProp, } from "../src/game/interactive-props.js"; import { setArenaLineColor } from "../src/game/worlds/arena/line-color.js"; import { makeInfiniteGrid, makeArenaWalls, } from "../src/game/worlds/arena/render.js"; import { VARIANT_SWATCH_HEX } from "../src/game/variants.js"; function rng(seed) { return () => (seed = (1664525 * seed + 1013904223) >>> 0) / 4294967296; } test("random layouts have 5–7 varied, separated, lightweight toys inside walls and clear of spawn", () => { const counts = new Set(); for (let seed = 0; seed < 1000; seed++) { for (const halfExtent of [1.5, 2.25]) { const specs = generateInteractiveProps({ halfExtent, spawn: [-0.6, 0, 0.12], random: rng(seed * 7919), }); counts.add(specs.length); assert.ok(specs.length >= 5 && specs.length <= 7); assert.equal(new Set(specs.map((p) => p.type)).size, 4); assert.equal(new Set(specs.map((p) => p.name)).size, specs.length); assert.ok(specs[0].pos[0] > -0.3 && specs[0].pos[0] < 0.25); for (const [i, spec] of specs.entries()) { assert.ok(spec.mass >= 0.04 && spec.mass <= 0.08); assert.ok(Math.abs(spec.pos[0]) < halfExtent - 0.1); assert.ok(Math.abs(spec.pos[1]) < halfExtent - 0.1); assert.ok(Math.hypot(spec.pos[0] + 0.6, spec.pos[1]) > 0.34); for (const other of specs.slice(i + 1)) assert.ok( Math.hypot(spec.pos[0] - other.pos[0], spec.pos[1] - other.pos[1]) > 0.3, ); } } } assert.deepEqual([...counts].sort(), [5, 6, 7]); assert.notDeepEqual( generateInteractiveProps({ halfExtent: 2.25, spawn: [-0.6, 0], random: rng(1), }), generateInteractiveProps({ halfExtent: 2.25, spawn: [-0.6, 0], random: rng(2), }), ); }); test("visual geometry matches MJCF dimensions, poses sync through Z-up root, reset clears only prop DOFs", () => { const specs = generateInteractiveProps({ halfExtent: 2.25, spawn: [-0.6, 0], random: rng(1), }); const view = createInteractivePropVisuals(specs); const addresses = specs.map((_, i) => ({ qAdr: 7 + i * 7, dofAdr: 6 + i * 6, })); const data = { qpos: new Float64Array(7 + specs.length * 7).fill(123), qvel: new Float64Array(6 + specs.length * 6).fill(42), }; specs.forEach((spec, i) => resetProp(data, addresses[i], spec)); view.sync(data, addresses); view.root.updateMatrixWorld(true); view.meshes.forEach((mesh, i) => { assert.equal(mesh.name, specs[i].name); assert.equal(mesh.userData.interactiveIndex, i); assert.deepEqual( mesh .getWorldPosition(new THREE.Vector3()) .toArray() .map((v) => +v.toFixed(6)), [specs[i].pos[0], specs[i].pos[2], -specs[i].pos[1]].map( (v) => +v.toFixed(6), ), ); mesh.geometry.computeBoundingBox(); const size = mesh.geometry.boundingBox.getSize(new THREE.Vector3()); const spec = specs[i]; const height = 2 * (spec.type === "sphere" ? spec.size[0] : spec.type === "capsule" ? spec.size[0] + spec.size[1] : spec.size.at(-1)); assert.ok(Math.abs(size.z - height) < 1e-7); }); assert.deepEqual(Array.from(data.qpos.slice(0, 7)), Array(7).fill(123)); assert.deepEqual(Array.from(data.qvel.slice(0, 6)), Array(6).fill(42)); assert.ok(Array.from(data.qvel.slice(6)).every((v) => v === 0)); resetProp(data, addresses[0], specs[0], true, 0); view.sync(data, addresses, false); assert.equal(view.root.visible, false); assert.equal(data.qpos[addresses[0].qAdr], 50); for (const mesh of view.meshes) { mesh.geometry.dispose(); mesh.material.dispose(); } }); test("the original four swatches tint floor AND all walls without altering fades or reveal", () => { const grid = makeInfiniteGrid({ subdivisions: 1 }); const { wallMats, wallMeshes } = makeArenaWalls(); const materials = [grid.material, ...wallMats]; for (const [name, hex] of Object.entries(VARIANT_SWATCH_HEX)) { assert.equal(setArenaLineColor(materials, name), true); for (const mat of materials) { assert.equal( mat.uniforms.uSectionColor.value.getHexString(), hex.slice(1), ); assert.equal(mat.uniforms.uReveal.value, 0); assert.ok( mat.uniforms.uCellColor.value.equals( mat.uniforms.uSectionColor.value.clone().multiplyScalar(0.55), ), ); } } assert.equal(setArenaLineColor(materials, "unknown"), false); for (const mesh of [grid, ...wallMeshes]) { mesh.geometry.dispose(); mesh.material.dispose(); } });