Files

36 lines
3.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {build} from 'esbuild';
const load=async path=>{const r=await build({entryPoints:[new URL(path,import.meta.url).pathname],bundle:true,write:false,platform:'node',format:'esm'});return import('data:text/javascript;base64,'+Buffer.from(r.outputFiles[0].contents).toString('base64'));};
const {SceneHistory,historyCommand}=await load('../src/core/roverScene/sceneHistory.ts');
const {defaultChargeGradient,chargePair,parseChargeGradient}=await load('../src/core/roverScene/chargeGradient.ts');
const {defaultBatteryScene}=await load('../src/core/batteryScene/profile.ts');
const {parseScene,changeField}=await load('../src/core/roverScene/sceneDocument.ts');
test('one slider gesture is one undo step; redo, branch replacement and more than a thousand steps',()=>{
const h=new SceneHistory(0);h.begin();for(let i=1;i<=80;i++)h.update(i);h.end();h.undo();assert.equal(h.present,0);h.redo();assert.equal(h.present,80);
for(let i=81;i<=1200;i++)h.update(i);
for(let i=1199;i>=80;i--){assert.equal(h.undo(),true);assert.equal(h.present,i);}
h.undo();assert.equal(h.present,0);assert.equal(h.undo(),false);h.redo();assert.equal(h.present,80);h.update(5);assert.equal(h.canRedo,false);
});
test('history restores environment, assignments, visibility and gradient while sharing untouched textures',()=>{
const initial=defaultBatteryScene(),h=new SceneHistory(initial);
h.update(changeField(h.present,'postFx.lighting.exposure',1.9));
const lighting=h.present;h.update({...lighting,objectVisibility:{'object/Model/cell':false}});
const visibility=h.present;h.update({...visibility,objectAssignments:{'object/Model/cell':'bms/glass'}});
h.undo();assert.equal(h.present,visibility);h.undo();assert.equal(h.present,lighting);h.undo();assert.equal(h.present,initial);assert.equal(lighting.materials,initial.materials);
});
test('undo and redo accept physical keys or English/Russian key labels on Ctrl and Cmd',()=>{
for(const modifier of ['ctrlKey','metaKey'])for(const [key,code] of [['z','KeyZ'],['я','KeyZ'],['я',''],['z','']]){
const e={key,code,ctrlKey:false,metaKey:false,shiftKey:false,altKey:false,[modifier]:true};assert.equal(historyCommand(e),'undo');assert.equal(historyCommand({...e,shiftKey:true}),'redo');assert.equal(historyCommand({...e,altKey:true}),null);
}
assert.equal(historyCommand({key:'н',code:'KeyY',ctrlKey:true,metaKey:false,shiftKey:false,altKey:false}),'redo');
});
test('charge gradient interpolates both colour pairs, clamps SOC and validates JSON',()=>{
const g={...defaultChargeGradient(),lowA:'#000000',lowB:'#ff0000',highA:'#ffffff',highB:'#00ff00'};
assert.deepEqual(chargePair(g,0),[[0,0,0],[1,0,0]]);assert.deepEqual(chargePair(g,100),[[1,1,1],[0,1,0]]);assert.deepEqual(chargePair(g,50),[[.5,.5,.5],[.5,.5,0]]);assert.deepEqual(chargePair(g,200),chargePair(g,100));
const doc=defaultBatteryScene();doc.materials['bms/cells'].chargeGradient=g;
assert.deepEqual(parseScene(JSON.parse(JSON.stringify(doc)),defaultBatteryScene).materials['bms/cells'].chargeGradient,g);
assert.equal(parseChargeGradient({...g,speed:Infinity,scale:500,intensity:-10}).scale,50);assert.equal(parseChargeGradient({...g,intensity:-10}).intensity,0);
const old=defaultBatteryScene();delete old.materials['bms/cells'].chargeGradient;assert.deepEqual(parseScene(old,defaultBatteryScene).materials['bms/cells'].chargeGradient,defaultChargeGradient());
});