76 lines
4.3 KiB
JavaScript
76 lines
4.3 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import * as THREE from 'three';
|
|
import { PoseHistory } from '../src/embed/pose-history.js';
|
|
import { indexExactGeometry } from '../src/embed/index-geometry.js';
|
|
import { bindMovementWake } from '../src/embed/movement-wake.js';
|
|
import { InputRouter } from '../src/platform/input/InputRouter.js';
|
|
import { OnnxController } from '../src/platform/policy/controllers/OnnxController.js';
|
|
|
|
test('display interpolation fills 50 Hz poses without mutating physics and handles quaternion sign', () => {
|
|
const history = new PoseHistory(20, [1]);
|
|
const source = new Float64Array([0, 1, 0, 0, 0]);
|
|
history.reset(source, 0);
|
|
source.set([2, -Math.SQRT1_2, 0, -Math.SQRT1_2, 0]);
|
|
history.push(source, 20);
|
|
const pose = history.sample(30);
|
|
assert.equal(pose[0], 1);
|
|
assert.ok(Math.abs(pose[1] - Math.cos(Math.PI / 8)) < 1e-12);
|
|
assert.ok(Math.abs(pose[3] - Math.sin(Math.PI / 8)) < 1e-12);
|
|
assert.deepEqual([...source], [2, -Math.SQRT1_2, 0, -Math.SQRT1_2, 0]);
|
|
assert.equal(history.sample(100)[0], 2); // no extrapolation/overshoot
|
|
history.reset([10, 1, 0, 0, 0], 100);
|
|
assert.equal(history.sample(100)[0], 10); // resets never slide from old position
|
|
history.push([20, 1, 0, 0, 0], 1000);
|
|
assert.equal(history.sample(1000)[0], 20); // tab resumed after a gap
|
|
});
|
|
|
|
test('indexing removes duplicate vertices but preserves every triangle, normal and UV seam', () => {
|
|
const g = new THREE.BufferGeometry();
|
|
g.setAttribute('position', new THREE.Float32BufferAttribute([0,0,0, 1,0,0, 0,1,0, 0,0,0, 0,1,0, -1,0,0],3));
|
|
g.setAttribute('normal', new THREE.Float32BufferAttribute(Array(6).fill([0,0,1]).flat(),3));
|
|
// Same position on vertex 4 but a different UV must stay separate.
|
|
g.setAttribute('uv', new THREE.Float32BufferAttribute([0,0, 1,0, 0,1, 0,0, 0.5,1, -1,0],2));
|
|
const optimized = indexExactGeometry(g);
|
|
assert.equal(optimized.attributes.position.count,5);
|
|
assert.equal(optimized.index.count,6);
|
|
for (const [name, original] of Object.entries(g.attributes)) {
|
|
const actual = optimized.attributes[name];
|
|
for (let i=0;i<6;i++) for(let c=0;c<original.itemSize;c++) {
|
|
assert.equal(actual.array[optimized.index.getX(i)*actual.itemSize+c],original.array[i*original.itemSize+c]);
|
|
}
|
|
}
|
|
assert.equal(g.attributes.position.count,6); // original GLB geometry untouched
|
|
});
|
|
|
|
test('first movement wakes before command handling; orbit/focus/typing/repeats do not start simulation', () => {
|
|
const target = new EventTarget();
|
|
let running = false, wakes = 0;
|
|
const unbind = bindMovementWake(target, {codes:['KeyW'],canWake:()=>!running,wake:()=>{running=true;wakes++;}});
|
|
const input = new InputRouter({bindings:[{action:'move_forward',codes:['KeyW']}],getVelocityLimits:()=>[0.2,-0.1,0.5],isEnabled:()=>running});
|
|
input.init(target);
|
|
target.dispatchEvent(new Event('focus'));
|
|
target.dispatchEvent(new Event('pointerdown'));
|
|
assert.equal(running,false);
|
|
const key = (code, repeat=false) => { const e=new Event('keydown'); Object.assign(e,{code,repeat}); return e; };
|
|
const editing=key('KeyW'); Object.defineProperty(editing,'target',{value:{closest:()=>({})}});
|
|
target.dispatchEvent(editing); assert.equal(wakes,0);
|
|
target.dispatchEvent(key('KeyW',true)); assert.equal(wakes,0);
|
|
target.dispatchEvent(key('KeyW')); assert.equal(wakes,1); assert.ok(input.command[0]>0);
|
|
target.dispatchEvent(new Event('blur')); assert.equal(input.command[0],0);
|
|
input.dispose(); unbind(); running=false;
|
|
target.dispatchEvent(key('KeyW')); assert.equal(running,false);
|
|
});
|
|
|
|
test('generic ONNX proxy does not detach observation pipeline buffers', async () => {
|
|
const values = new Float32Array([1,2]);
|
|
class Tensor { constructor(type,data,dims){Object.assign(this,{type,data,dims});} }
|
|
const seen=[];
|
|
const session={inputNames:['obs'], outputNames:['act'], inputMetadata:[{name:'obs',isTensor:true,type:'float32',shape:[1,2]}],
|
|
outputMetadata:[{isTensor:true,type:'float32',shape:[1,1]}],
|
|
async run(feeds){seen.push([...feeds.obs.data]);structuredClone(feeds.obs.data,{transfer:[feeds.obs.data.buffer]});return {act:{data:new Float32Array([3])}};}};
|
|
const controller = new OnnxController({env:{wasm:{proxy:true}},Tensor},session,{inputs:{obs:values}},1);
|
|
await controller.run(); values[0]=4; await controller.run();
|
|
assert.deepEqual(seen,[[1,2],[4,2]]); assert.equal(values.byteLength,8);
|
|
});
|