import { test } from 'node:test'; import assert from 'node:assert/strict'; import { Box3, BoxGeometry, Group, Mesh, MeshStandardMaterial, Vector3 } from '../vendor/three/build/three.module.min.js'; import { createExplodedLayout } from '../js/assembly-explode.js'; const near = (a, b) => assert.ok(a.distanceTo(b) < 1e-9, `${a.toArray()} ≠ ${b.toArray()}`); function fixture(coincident = false) { const root = new Group(), instances = new Map(); root.rotation.set(0.4, 0.8, -0.2); root.scale.set(0.8, 1.2, 0.6); root.position.set(2, 3, -1); const parts = [ { id: 'body', parentId: null, position: [0, 0, 0], installOffset: [0, 1, 0] }, { id: 'nested', parentId: 'body', position: coincident ? [0, 0, 0] : [2, -1, 0.5], installOffset: [0, 0, 1] }, { id: 'shell', parentId: null, position: coincident ? [0, 0, 0] : [-1, 3, -2], installOffset: [-1, 0, 0] }, ]; for (const part of parts) { const group = new Group(); group.add(new Mesh(new BoxGeometry(0.4, 0.6, 0.8), new MeshStandardMaterial({color: 0xfeaa33, metalness: 0.7}))); group.position.fromArray(part.position); if (!coincident) group.rotation.set(0.2, -0.3, 0.1); instances.set(part.id, group); (part.parentId ? instances.get(part.parentId) : root).add(group); } root.updateMatrixWorld(true); return { root, instances, config: { parts } }; } test('nested parts move by their own world displacement under rotated and scaled parents', () => { const { root, config, instances } = fixture(); const layout = createExplodedLayout(config, instances); for (const amount of [0, 0.3, 1, 0.65, 0]) { layout.apply(amount); root.updateMatrixWorld(true); const actualBounds = new Box3().setFromObject(root); for (const record of layout.records) { const center = new Box3().setFromObject(record.instance.children[0]).getCenter(new Vector3()); near(center, record.center.clone().addScaledVector(record.worldOffset, amount)); } near(actualBounds.min, layout.boundsAt(amount).min); near(actualBounds.max, layout.boundsAt(amount).max); assert.ok(actualBounds.min.y >= layout.originalBounds.min.y - 1e-9); } }); test('repeated adjustments restore exact transforms and never change GLB materials or geometry', () => { const { config, instances } = fixture(); const original = [...instances.values()].map(instance => ({ instance, position: instance.position.toArray(), quaternion: instance.quaternion.toArray(), scale: instance.scale.toArray(), material: instance.children[0].material, geometry: instance.children[0].geometry, })); const json = JSON.stringify(config), layout = createExplodedLayout(config, instances); for (let i = 0; i < 100; i++) layout.apply((i % 11) / 10); layout.apply(0); for (const saved of original) { assert.deepEqual(saved.instance.position.toArray(), saved.position); assert.deepEqual(saved.instance.quaternion.toArray(), saved.quaternion); assert.deepEqual(saved.instance.scale.toArray(), saved.scale); assert.equal(saved.instance.children[0].material, saved.material); assert.equal(saved.instance.children[0].geometry, saved.geometry); } assert.equal(JSON.stringify(config), json); }); test('coincident parts separate deterministically and invalid/out-of-range amounts stay bounded', () => { const { config, instances } = fixture(true); const layout = createExplodedLayout(config, instances); assert.notDeepEqual(layout.records[0].worldOffset.toArray(), layout.records[1].worldOffset.toArray()); const again = createExplodedLayout(config, instances); layout.records.forEach((record, i) => near(record.offset, again.records[i].offset)); for (const value of [-1, NaN, Infinity]) { layout.apply(value); config.parts.forEach(part => assert.deepEqual(instances.get(part.id).position.toArray(), part.position)); } near(layout.boundsAt(100).max, layout.boundsAt(1).max); });