Files
vibemake/tests/assembly-visibility.test.js

57 lines
2.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'node:test';
import assert from 'node:assert/strict';
import { Group } from 'three';
import { validatePackage, visibleAssemblyParts } from '../js/experience-packages.js';
import { AssemblyViewer } from '../js/assembly-viewer.js';
const ids = (start, end) => Array.from({ length:end - start + 1 }, (_, i) => `p${start+i}`);
const fixture = () => ({ schemaVersion:1, kind:'assembly', unit:'meter',
files:[{path:'part.glb',bytes:1,sha256:'a'.repeat(64)}],
parts:ids(1,10).map(id=>({id,file:'part.glb',installOffset:[0,.05,0]})),
steps:ids(1,10).map((id,i)=>({id:`s${i+1}`,parts:[id],
...(i>=2&&i<=7?{visibleParts:ids(3,i+1)}:{})})),
});
const validate = raw => validatePackage(raw,'assembly','https://example.com/assembly.json');
test('plus subassembly hides 1–2, cumulatively installs 3–8, and step 9 restores earlier parts',()=>{
const pkg=validate(fixture());
for(const [index,expected] of [[0,ids(1,1)],[1,ids(1,2)],[2,ids(3,3)],
[3,ids(3,4)],[7,ids(3,8)],[8,ids(1,9)],[9,ids(1,10)],
[2,ids(3,3)],[null,ids(1,10)],[7,ids(3,8)]]) {
assert.deepEqual([...visibleAssemblyParts(pkg,index)],expected);
}
});
test('rejects unknown/future parts, hidden active parts, duplicates and invisible parents',()=>{
for(const visibleParts of [null,[],['p3','unknown'],['p3','p10'],['p1'],['p3','p3']]) {
const raw=fixture();raw.steps[2].visibleParts=visibleParts;
assert.throws(()=>validate(raw));
}
const raw=fixture();raw.parts[2].parentId='p1';
assert.throws(()=>validate(raw),/父零件/);
});
test('real player restores hidden assemblies without replaying them; only the new part moves',()=>{
const original=globalThis.matchMedia;
globalThis.matchMedia=()=>({matches:false});
try {
const v=Object.create(AssemblyViewer.prototype);
v.config=validate(fixture());
v.parts=new Map(v.config.parts.map(p=>[p.id,new Group()]));
v.transitions=[];v.stepIndex=null;
v.renderer={shadowMap:{},render(){}};v.controls={update(){return false;}};
v.loop={invalidate(){}};v.resetView=()=>{};v.notifyMotion=()=>{};v.refreshCloseup=()=>{};
const visible=()=>[...v.parts].filter(([,p])=>p.visible).map(([id])=>id);
v.setStep(7,true);assert.deepEqual(visible(),ids(3,8));
assert.equal(v.transitions.length,1);assert.equal(v.parts.get('p8').position.y,.05);
v.setStep(8,true);assert.deepEqual(visible(),ids(1,9));
assert.equal(v.transitions.length,1);
for(const id of ids(1,8)) assert.deepEqual(v.parts.get(id).position.toArray(),[0,0,0]);
assert.equal(v.parts.get('p9').position.y,.05);
v.render(3);assert.equal(v.parts.get('p9').position.y,0);
v.setStep(2,false);assert.deepEqual(visible(),['p3']);
assert.equal(v.transitions.length,0);
v.setStep(null,false);assert.deepEqual(visible(),ids(1,10));
} finally {globalThis.matchMedia=original;}
});