interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / allen-scene / test-controller.cjs

166 lines · 11403 bytes Raw Blame History
1'use strict';
2/* Lifecycle/interaction unit harness with real WASM, not a browser/visual claim. */
3const fs=require('node:fs'),vm=require('node:vm'),assert=require('node:assert/strict');
4class Events {
5 constructor(){this.listeners=new Map();}
6 addEventListener(name,fn){if(!this.listeners.has(name))this.listeners.set(name,new Set());this.listeners.get(name).add(fn);}
7 removeEventListener(name,fn){this.listeners.get(name)?.delete(fn);}
8 dispatchEvent(event){event.target=this;for(const fn of this.listeners.get(event.type)||[])fn(event);return true;}
9}
10class Canvas extends Events {
11 constructor(){super();this.dataset={};this.style={};this.width=0;this.height=0;this.draws=0;this.captured=null;this.box={width:640,height:400};}
12 getContext(){return {putImageData:image=>{assert.equal(image.data.length,this.width*this.height*4);this.draws++;}};}
13 getBoundingClientRect(){if(this.onMeasure)this.onMeasure();return this.box;}
14 setPointerCapture(id){this.captured=id;}
15 hasPointerCapture(id){return this.captured===id;}
16 releasePointerCapture(){this.captured=null;}
17 focus(){}
18}
19(async()=>{
20 const document=new Events();document.hidden=false;
21 const motion=new Events();motion.matches=false;
22 const window=new Events(),raf=new Map(),resizes=[],intersections=[];let serial=0;
23 let drawCost=2,measurementClock=0;
24 const connection={saveData:false};
25 const wasm=fs.readFileSync('public/allen/scene.wasm');
26 const audio={enter:0,pause:0,resume:0,stop:0};
27 window.AllenAudio={enter:()=>{audio.enter++;return Promise.resolve(true);},pause:()=>audio.pause++,resume:()=>{audio.resume++;return Promise.resolve();},stop:()=>audio.stop++};
28 const context={window,document,HTMLCanvasElement:Canvas,WebAssembly,Uint8ClampedArray,Float64Array,Int16Array,
29 ImageData:class{constructor(data,width,height){this.data=data;this.width=width;this.height=height;}},
30 CustomEvent:class{constructor(type,options){this.type=type;Object.assign(this,options);}},
31 matchMedia:()=>motion,devicePixelRatio:1,
32 navigator:{connection},performance:{now:()=>{measurementClock+=drawCost;return measurementClock;}},
33 fetch:async url=>{assert.equal(url,'/allen/scene.wasm');return {ok:true,arrayBuffer:async()=>wasm};},
34 requestAnimationFrame:fn=>{const id=++serial;raf.set(id,fn);return id;},cancelAnimationFrame:id=>raf.delete(id),
35 ResizeObserver:class{constructor(fn){this.fn=fn;resizes.push(this);}observe(){}disconnect(){this.disconnected=true;}},
36 IntersectionObserver:class{constructor(fn){this.fn=fn;intersections.push(this);}observe(){}disconnect(){this.disconnected=true;}}
37 };
38 let ready=0;document.addEventListener('allen-scene-ready',()=>ready++);
39 vm.runInNewContext(fs.readFileSync('public/js/allen-scene.js','utf8'),context);
40 assert.equal(ready,1);
41 const canvas=new Canvas(),scene=window.AllenScene.mount(canvas,{interactive:true});
42 assert.equal(window.AllenScene.mount(canvas),scene,'one instance per canvas');
43 let initialized=0,complete=0;
44 canvas.addEventListener('allen-scene-initialized',()=>initialized++);
45 canvas.addEventListener('allen-scene-complete',()=>complete++);
46 window.AllenAudio.enter('home');scene.enter('birth');await scene.ready;
47 assert.equal(audio.enter,1,'visual enter must not start duplicate audio');
48 assert.equal(initialized,1,'initialized event follows WASM and buffer readiness');
49 assert.equal(canvas.dataset.sceneEngine,'native-wasm');
50 scene.frame(8000);assert.equal(canvas.dataset.scenePhase,'merger');
51 scene.enter('dusk');scene.frame(2000);assert.equal(canvas.dataset.scenePhase,'tube');
52 scene.frame(7216);assert.equal(canvas.dataset.scenePhase,'presence');
53 assert.equal(complete,1,'dusk completion announced once');
54 scene.key('abc');scene.speak('a safe word');scene.mode('thinking');scene.frame(7600);
55 const event={type:'keydown',key:'ArrowRight',preventDefault(){this.prevented=true;}};
56 canvas.dispatchEvent(event);assert.ok(event.prevented,'keyboard exploration handled');
57 scene.enter('birth');document.hidden=true;document.dispatchEvent({type:'visibilitychange'});
58 assert.equal(raf.size,0,'hidden document has no frame loop');assert.ok(audio.pause>0);
59 document.hidden=false;document.dispatchEvent({type:'visibilitychange'});assert.ok(raf.size>0);assert.ok(audio.resume>0);
60 intersections[0].fn([{isIntersecting:false}]);assert.equal(raf.size,0,'offscreen has no frame loop');
61 intersections[0].fn([{isIntersecting:true}]);
62 motion.matches=true;motion.dispatchEvent({type:'change'});
63 const batch=[...raf];raf.clear();for(const [,fn] of batch)fn(1000);
64 assert.equal(raf.size,0,'reduced-motion redraw settles without a frame loop');
65 assert.equal(canvas.dataset.scenePhase,'presence');
66 const frozen=canvas.dataset.sceneTime;scene.frame(999);assert.equal(canvas.dataset.sceneTime,frozen);
67 scene.destroy();assert.equal(raf.size,0);assert.equal(canvas.dataset.sceneEngine,'destroyed');
68 assert.ok(resizes[0].disconnected && intersections[0].disconnected);
69 motion.matches=false;
70 const icon=new Canvas();icon.box={width:160,height:160};
71 const compact=window.AllenScene.mount(icon,{compact:true});await compact.ready;
72 const compactBatch=[...raf];raf.clear();for(const [,fn] of compactBatch)fn(2000);
73 assert.equal(raf.size,0,'compact has static idle, no background loop');
74 assert.equal(icon.dataset.scenePhase,'presence');compact.destroy();
75 const late=new Canvas();late.box={width:0,height:0};
76 let measured,readyLate=false;
77 const measurement=new Promise(resolve=>{measured=resolve;});
78 late.onMeasure=()=>{late.onMeasure=null;measured();};
79 const deferred=window.AllenScene.mount(late,{});
80 deferred.ready.then(()=>{readyLate=true;});
81 await measurement;assert.equal(readyLate,false,'zero-size canvas is not initialized');
82 late.box={width:640,height:400};resizes[2].fn();
83 const lateBatch=[...raf];raf.clear();for(const [,fn] of lateBatch)fn(3000);
84 await deferred.ready;assert.equal(readyLate,true);
85 assert.equal(late.dataset.scenePhase,'tube','zero-size initial mount retains opening choice');
86 deferred.destroy();assert.equal(raf.size,0);
87 for(const opening of ['idle','dusk','birth']) {
88 const target=new Canvas(),instance=window.AllenScene.mount(target,{opening});
89 await instance.ready;
90 assert.equal(target.dataset.scenePhase,opening==='idle'?'presence':opening==='dusk'?'tube':'merger');
91 instance.destroy();
92 }
93 for(const opening of ['other','',null,0,{},['idle']]) {
94 assert.throws(()=>window.AllenScene.mount(new Canvas(),{opening}),/Invalid Allen scene opening/);
95 }
96 assert.equal(raf.size,0,'opening option tests leave no scheduled work');
97 const skippedCanvas=new Canvas(),skipped=window.AllenScene.mount(skippedCanvas,{opening:'idle'});
98 await skipped.ready;
99 skipped.enter('birth');
100 const stopsBeforeSkip=audio.stop;
101 skipped.enter('idle');
102 const skipBatch=[...raf];raf.clear();for(const [,fn] of skipBatch)fn(4000);
103 assert.equal(skippedCanvas.dataset.scenePhase,'presence','idle entry skips the opening without caller timing');
104 assert.ok(audio.stop>stopsBeforeSkip,'idle entry stops owned birth audio');
105 assert.equal(skippedCanvas.dataset.sceneEngine,'native-wasm','skip retains live native scene');
106 skipped.destroy();assert.equal(raf.size,0);
107 let wall=5000;
108 function frames(count) {
109 for(let i=0;i<count;i++) {
110 wall+=1000/60;
111 const batch=[...raf];raf.clear();for(const [,fn] of batch)fn(wall);
112 }
113 }
114 const adaptiveCanvas=new Canvas();adaptiveCanvas.box={width:320,height:200};
115 const adaptive=window.AllenScene.mount(adaptiveCanvas,{opening:'idle',interactive:true});
116 await adaptive.ready;
117 assert.equal(adaptiveCanvas.dataset.sceneQuality,'full-60');
118 const firstDraw=adaptiveCanvas.draws;
119 frames(5);assert.equal(adaptiveCanvas.draws-firstDraw,5,'capable mode draws every animation callback, not every other frame');
120 drawCost=24;
121 frames(7);assert.equal(adaptiveCanvas.dataset.sceneQuality,'full-60','isolated slow samples do not lower quality');
122 frames(2);assert.equal(adaptiveCanvas.dataset.sceneQuality,'balanced-60','sustained measured draw cost lowers resolution first');
123 const lowerWidth=adaptiveCanvas.width;
124 frames(20);assert.equal(adaptiveCanvas.dataset.sceneQuality,'balanced-60','quality hold prevents oscillation');
125 assert.ok(lowerWidth<320);
126 drawCost=2;frames(250);
127 assert.equal(adaptiveCanvas.dataset.sceneQuality,'full-60','sustained cheap frames restore quality');
128 assert.equal(adaptiveCanvas.width,320);
129 drawCost=24;frames(260);
130 assert.equal(adaptiveCanvas.dataset.sceneQuality,'small-30','pacing decreases only after bounded resolution steps');
131 const pacedDraw=adaptiveCanvas.draws;frames(20);
132 assert.ok(adaptiveCanvas.draws-pacedDraw<20,'last quality level reduces draw cadence');
133 assert.ok(adaptiveCanvas.draws-pacedDraw>0);
134 const qualityBeforeFrame=adaptiveCanvas.dataset.sceneQuality;
135 adaptive.frame(25000);
136 assert.equal(adaptiveCanvas.dataset.sceneQuality,qualityBeforeFrame,'explicit frame does not measure or retune quality');
137 assert.equal(raf.size,0,'explicit frame remains a single draw');
138 assert.equal(adaptiveCanvas.style.touchAction,'pinch-zoom');
139 const zoomEvent={type:'wheel',ctrlKey:true,deltaY:100,preventDefault(){this.prevented=true;}};
140 adaptiveCanvas.dispatchEvent(zoomEvent);
141 assert.equal(zoomEvent.prevented,undefined,'browser pinch/Ctrl+wheel is not intercepted');
142 const sceneWheel={type:'wheel',deltaY:100,preventDefault(){this.prevented=true;}};
143 adaptiveCanvas.dispatchEvent(sceneWheel);assert.ok(sceneWheel.prevented);
144 adaptive.zoom('in');adaptive.zoom('out');
145 assert.throws(()=>adaptive.zoom('other'),/Scene zoom/);
146 for(const [pointerId,clientX] of [[1,10],[2,30]]) adaptiveCanvas.dispatchEvent({type:'pointerdown',pointerType:'touch',button:0,pointerId,clientX,clientY:10});
147 adaptiveCanvas.dispatchEvent({type:'pointermove',pointerType:'touch',pointerId:2,clientX:50,clientY:10});
148 for(const pointerId of [1,2]) adaptiveCanvas.dispatchEvent({type:'pointercancel',pointerId});
149 adaptive.calm(true);frames(2);
150 assert.equal(adaptiveCanvas.dataset.sceneQuality,'calm');
151 assert.equal(raf.size,0,'explicit calm has no continuous loop');
152 adaptive.calm(false);assert.ok(raf.size>0,'explicit resume restores animation');
153 adaptive.destroy();assert.equal(adaptiveCanvas.style.touchAction,undefined);
154 connection.saveData=true;
155 const saverCanvas=new Canvas(),saver=window.AllenScene.mount(saverCanvas);
156 await saver.ready;frames(2);
157 assert.equal(saverCanvas.dataset.sceneQuality,'calm','saveData defaults to static presence');
158 assert.equal(saverCanvas.dataset.scenePhase,'presence');assert.equal(raf.size,0);
159 saver.destroy();
160 const overrideCanvas=new Canvas(),override=window.AllenScene.mount(overrideCanvas,{calm:false,opening:'idle'});
161 await override.ready;assert.equal(overrideCanvas.dataset.sceneQuality,'full-60');assert.ok(raf.size>0);
162 override.destroy();connection.saveData=false;drawCost=2;
163 assert.equal(raf.size,0);
164 console.log('Adaptive controller: every-callback initial cadence, measured cost hysteresis, bounded resolution/pacing, explicit-frame isolation, accessible pinch, zoom API, calm/saveData');
165 console.log('Controller lifecycle: ready, aliases, native rendering, keys, speech, hidden/offscreen pause, reduced motion, compact, zero-size mount, bounded opening options, teardown');
166})().catch(error=>{console.error(error);process.exitCode=1;});