149 lines
8.6 KiB
JavaScript
149 lines
8.6 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { Group, Vector3, Quaternion, Mesh, BoxGeometry } from '../vendor/three/build/three.module.min.js';
|
|
import { ScrewTransition, screwDuration, inferToolTipAxis } from '../js/assembly-motion.js';
|
|
import { validatePackage } from '../js/experience-packages.js';
|
|
|
|
const raw = () => ({ schemaVersion: 1, kind: 'assembly', unit: 'meter',
|
|
files: ['part.glb', 'tool.glb'].map(path => ({ path, bytes: 100, sha256: 'a'.repeat(64) })),
|
|
tools: [{ id: 'hex', file: 'tool.glb', tipPosition: [0.00525, 0, 0.03277] }],
|
|
parts: [{ id: 'screw', file: 'part.glb', position: [0.3, 0.2, 0.1],
|
|
quaternion: new Quaternion().setFromAxisAngle(new Vector3(1, 1, 0).normalize(), 0.7).toArray(),
|
|
installOffset: [0.05, 0.02, 0.03], installation: {
|
|
type: 'screw', toolId: 'hex', axis: [0, 0, -1], socketPosition: [0.01, -0.03, -0.0009],
|
|
toolQuaternion: [0, 0, Math.sin(Math.PI / 12), Math.cos(Math.PI / 12)],
|
|
direction: 'clockwise', turns: 3, travel: 0.0012,
|
|
} }], steps: [{ id: 'step', parts: ['screw'] }],
|
|
});
|
|
const validate = p => validatePackage(p, 'assembly', 'https://example.com/manifest.json');
|
|
function fixture(delay = 0, toolScale) {
|
|
const source = raw();
|
|
if (toolScale !== undefined) source.parts[0].installation.toolScale = toolScale;
|
|
const pkg = validate(source), part = pkg.parts[0], toolConfig = pkg.tools[0];
|
|
const parent = new Group(), instance = new Group(), tool = new Group();
|
|
parent.position.set(-2, 3, -4);
|
|
parent.quaternion.setFromAxisAngle(new Vector3(0, 1, 0), 1.1);
|
|
parent.scale.setScalar(2);
|
|
parent.add(instance);
|
|
const transition = new ScrewTransition(instance, part, tool, toolConfig, delay);
|
|
return { parent, instance, tool, part, toolConfig, transition, m: part.installation };
|
|
}
|
|
const near = (a, b, eps = 1e-10) => assert.ok(a.distanceTo(b) < eps, `${a.toArray()} ≠ ${b.toArray()}`);
|
|
|
|
test('new instructions are retained; old packages remain tools-free and slide-only', () => {
|
|
const pkg = validate(raw());
|
|
assert.equal(pkg.parts[0].installation.toolId, 'hex');
|
|
assert.deepEqual(pkg.tools[0].tipPosition, [0.00525, 0, 0.03277]);
|
|
const old = raw(); delete old.tools; delete old.parts[0].installation;
|
|
assert.deepEqual(validate(old).tools, []);
|
|
assert.equal(validate(old).parts[0].installation, undefined);
|
|
});
|
|
|
|
test('invalid tool references, axes, timing and distorted parent scales fail before playback', () => {
|
|
for (const mutate of [
|
|
p => p.tools.push(p.tools[0]), p => p.tools[0].file = 'missing.glb',
|
|
p => p.tools[0].tipPosition = [NaN, 0, 0], p => p.tools[0].tipAxis = [0, 0, 2],
|
|
p => p.tools[0].tipAxis = [0, 0, 0], p => p.parts[0].installation.toolId = 'missing',
|
|
p => p.parts[0].installation.axis = [0, 0, 0], p => p.parts[0].installation.axis = [0, 0, 2],
|
|
p => p.parts[0].installation.turns = 2.5, p => p.parts[0].installation.turns = 0,
|
|
p => p.parts[0].installation.direction = 'left', p => p.parts[0].installation.travel = -1,
|
|
p => p.parts[0].installation.toolScale = -1,
|
|
p => p.parts[0].installation.tightenDuration = Infinity, p => p.parts[0].installation.socketPosition = [],
|
|
p => p.parts[0].scale = [1, 2, 1],
|
|
p => { p.parts.push({id: 'parent', file: 'part.glb', scale: [1, 2, 1]}); p.parts[0].parentId = 'parent'; },
|
|
]) { const p = raw(); mutate(p); assert.throws(() => validate(p)); }
|
|
});
|
|
|
|
test('legacy tool direction is measured at the tip end cap including GLB node transforms', () => {
|
|
const template = new Group(), shaft = new Mesh(new BoxGeometry(0.002, 0.002, 0.04));
|
|
shaft.position.set(0.00525, 0, 0.05277);
|
|
template.add(shaft);
|
|
near(new Vector3(...inferToolTipAxis(template, [0.00525, 0, 0.03277])), new Vector3(0, 0, -1));
|
|
shaft.geometry.dispose();
|
|
});
|
|
|
|
test('GLB float32 scale rounding preserves final poses but real anisotropy is rejected', () => {
|
|
const source = raw();
|
|
source.parts[0].scale = [1.0000001343588858, 1.0000001343588858, 1.0000002687177536];
|
|
assert.deepEqual(validate(source).parts[0].scale, source.parts[0].scale);
|
|
for (const scale of [[1, 1.0001, 1], [.001, .00101, .001]]) {
|
|
source.parts[0].scale = scale;
|
|
assert.throws(() => validate(source), /等比缩放/);
|
|
}
|
|
});
|
|
|
|
test('reversed legacy tool faces the screw head, with its handle outside throughout tightening', () => {
|
|
for (const axis of [[0, 0, -1], [0, 0, 1], new Vector3(0.03193, -0.02235, -0.99924).normalize().toArray()]) {
|
|
const rawPkg = raw();
|
|
rawPkg.tools[0].tipAxis = [0, 0, -1];
|
|
rawPkg.parts[0].installation.axis = axis;
|
|
// The faulty export aligned +Z instead of the actual -Z working end.
|
|
rawPkg.parts[0].installation.toolQuaternion = new Quaternion().setFromUnitVectors(new Vector3(0, 0, 1), new Vector3(...axis)).toArray();
|
|
const pkg = validate(rawPkg), part = pkg.parts[0], config = pkg.tools[0];
|
|
const instance = new Group(), tool = new Group();
|
|
const t = new ScrewTransition(instance, part, tool, config), m = part.installation;
|
|
for (const progress of [0, 0.25, 0.5, 0.75, 1]) {
|
|
t.sample(m.approachDuration + m.engageDuration + progress * m.tightenDuration);
|
|
instance.updateMatrixWorld(true);
|
|
const tip = tool.localToWorld(new Vector3(...config.tipPosition));
|
|
const handle = tool.localToWorld(new Vector3(...config.tipPosition).add(new Vector3(0, 0, 0.04)));
|
|
const inward = new Vector3(...axis).applyQuaternion(instance.quaternion);
|
|
near(tip, instance.localToWorld(new Vector3(...m.socketPosition)));
|
|
assert.ok(handle.sub(tip).dot(inward) < -0.03999, 'handle must remain outside the screw head');
|
|
}
|
|
}
|
|
});
|
|
|
|
test('tool tip stays exactly in the rotating socket through tightening under parent transforms', () => {
|
|
const { parent, instance, tool, part, toolConfig, transition, m } = fixture(0, 1.65);
|
|
assert.equal(tool.scale.x, 1.65);
|
|
for (let i = 0; i <= 100; i++) {
|
|
const progress = i / 100, eased = progress ** 3 * (progress * (progress * 6 - 15) + 10);
|
|
transition.sample(m.approachDuration + m.engageDuration + progress * m.tightenDuration);
|
|
parent.updateMatrixWorld(true);
|
|
const tip = tool.localToWorld(new Vector3(...toolConfig.tipPosition));
|
|
const socket = instance.localToWorld(new Vector3(...m.socketPosition));
|
|
near(tip, socket);
|
|
// The pivot lies on one straight insertion axis, even for an off-center GLB origin.
|
|
const expected = new Vector3(...m.socketPosition).addScaledVector(new Vector3(...m.axis), -m.travel * (1 - eased))
|
|
.applyQuaternion(new Quaternion(...part.quaternion)).add(new Vector3(...part.position));
|
|
near(socket, parent.localToWorld(expected));
|
|
}
|
|
});
|
|
|
|
test('clockwise rotates toward -Y from +X when looking from +Z into a -Z hole', () => {
|
|
const p = raw(); p.parts[0].quaternion = [0, 0, 0, 1];
|
|
const pkg = validate(p), part = pkg.parts[0], instance = new Group();
|
|
const t = new ScrewTransition(instance, part, new Group(), pkg.tools[0]);
|
|
t.sample(part.installation.approachDuration + part.installation.engageDuration + 0.1);
|
|
assert.ok(new Vector3(1, 0, 0).applyQuaternion(instance.quaternion).y < 0);
|
|
part.installation.direction = 'counterclockwise';
|
|
t.sample(part.installation.approachDuration + part.installation.engageDuration + 0.1);
|
|
assert.ok(new Vector3(1, 0, 0).applyQuaternion(instance.quaternion).y > 0);
|
|
});
|
|
|
|
test('tool approaches and retracts along the configured insertion axis; ends at exact original pose', () => {
|
|
const { instance, tool, part, toolConfig, transition, m } = fixture();
|
|
const localTip = () => new Vector3(...toolConfig.tipPosition).applyQuaternion(tool.quaternion).add(tool.position);
|
|
transition.sample(m.approachDuration);
|
|
near(localTip(), new Vector3(...m.socketPosition).addScaledVector(new Vector3(...m.axis), -m.toolClearance));
|
|
transition.sample(m.approachDuration + m.engageDuration);
|
|
near(localTip(), new Vector3(...m.socketPosition));
|
|
transition.sample(screwDuration(m) - m.retractDuration / 2);
|
|
near(localTip(), new Vector3(...m.socketPosition).addScaledVector(new Vector3(...m.axis), -m.toolClearance / 2));
|
|
transition.sample(100);
|
|
assert.deepEqual(instance.position.toArray(), part.position);
|
|
assert.deepEqual(instance.quaternion.toArray(), part.quaternion);
|
|
assert.equal(tool.visible, false);
|
|
assert.equal(instance.visible, true);
|
|
});
|
|
|
|
test('delayed fasteners wait their turn; cancelling removes temporary tool without disposing shared assets', () => {
|
|
const { transition, instance, tool } = fixture(3);
|
|
assert.equal(instance.visible, false);
|
|
transition.sample(2.99); assert.equal(tool.visible, false);
|
|
transition.sample(3); assert.equal(instance.visible, true);
|
|
transition.dispose(); assert.equal(tool.parent, null);
|
|
assert.equal(instance.children.length, 0);
|
|
});
|