smoke-animated.mjs
Текст · 84 рядків
1import assert from 'node:assert/strict';
2import { readFileSync } from 'node:fs';
3import vm from 'node:vm';
4
5const html=readFileSync(new URL('../../../r-02-presentation/index.html',import.meta.url),'utf8');
6assert(!/<script[^>]+src=|<link[^>]+rel="stylesheet"|@import/i.test(html),'Single HTML cannot require runtime downloads');
7const scripts=[...html.matchAll(/<script>([\s\S]*?)<\/script>/g)];
8assert.equal(scripts.length,2,'Embedded Anime.js and presentation');
9const context=vm.createContext({console,setTimeout,clearTimeout,setImmediate,clearImmediate,performance});
10// Use the real Anime.js library in its non-DOM environment, not an animation mock.
11vm.runInContext(scripts[0][1],context);
12vm.runInContext(`
13 const tweenValue={v:0};
14 const probe=anime.createTimeline({autoplay:false}).add(tweenValue,{v:[0,100],duration:100,ease:'linear'});
15 probe.seek(50,true);
16 if(Math.abs(tweenValue.v-50)>.001)throw new Error('Actual Anime.js interpolation failed');
17 probe.pause();
18`,context);
19const elements=new Map(),docEvents={},winEvents={};
20class Element {
21 constructor(id){this.id=id;this.listeners={};this.attrs={};this.value=id==='speed'?'20000':'';this.tagName='BUTTON';}
22 addEventListener(event,fn){(this.listeners[event]??=[]).push(fn);}
23 setAttribute(key,value){this.attrs[key]=value;}
24 removeAttribute(key){delete this.attrs[key];}
25 dispatch(event,target=this){const e={target,preventDefault(){}};this[`on${event}`]?.(e);for(const fn of this.listeners[event]??[])fn(e);}
26 focus(){}
27}
28const element=id=>{if(!elements.has(id))elements.set(id,new Element(id));return elements.get(id);};
29const document={getElementById:element,querySelectorAll:()=>[],querySelector:s=>s.startsWith('[data-schema')?element('schema'):null,addEventListener:(e,fn)=>(docEvents[e]??=[]).push(fn)};
30const window={scrollTo(){},matchMedia:()=>({matches:true}),addEventListener:(e,fn)=>(winEvents[e]??=[]).push(fn)};
31const location={hash:''};
32Object.assign(context,{window,document,location,setInterval,clearInterval});
33vm.runInContext(scripts[1][1],context);
34const seek=n=>{element('seek').value=String(n);element('seek').dispatch('input');};
35const count=Number(element('seek').max),before=JSON.stringify(window.R02_DATA);
36let processes=0,operations=0;
37for(let n=1;n<=count;n++){
38 seek(n);const body=element('slide').innerHTML;
39 assert(body.includes('<h1>'));
40 assert(!/undefined|NaN/.test(body),`Invalid slide ${n}`);
41 const key=body.match(/data-story="([^"]+)"/)?.[1];
42 if(!key)continue;
43 processes++;
44 const list=window.R02Story.frames(key);
45 assert.equal(list[0].title,'Було');assert.equal(list.at(-1).title,'Стало');
46 assert(element('process-view').innerHTML.includes('Було'));
47 const rendered=[];
48 for(let p=0;p<list.length;p++){
49 element('op-seek').value=String(p*6000);element('op-seek').dispatch('input');
50 assert.equal(element('op-position').textContent,`${p+1} / ${list.length}`);
51 assert(!/undefined|NaN/.test(element('process-view').innerHTML));
52 rendered.push(element('process-view').innerHTML);
53 const s=list[p].state;
54 assert.equal(s.items.filter(i=>!i.is_container).reduce((sum,i)=>sum+i.quantity,0),s.stock.total,`${key}, phase ${p}`);
55 assert.equal(s.ledger,s.stock.total,`${key}, phase ${p}, ledger`);
56 operations++;
57 }
58 for(let p=list.length-1;p>=0;p--){
59 element('op-seek').value=String(p*6000);element('op-seek').dispatch('input');
60 assert.equal(element('process-view').innerHTML,rendered[p],`${key}: reverse seek must reconstruct the same records and calculations`);
61 }
62 element('op-replay').dispatch('click');assert.equal(element('op-position').textContent,`1 / ${list.length}`);
63 element('op-play').dispatch('click');assert.equal(element('op-play').attrs['aria-pressed'],'true');
64 element('op-play').dispatch('click');assert.equal(element('op-play').attrs['aria-pressed'],'false');
65 element('op-speed').value='10000';element('op-speed').dispatch('change');
66 element('op-next').dispatch('click');assert.equal(element('op-position').textContent,`2 / ${list.length}`);
67 element('op-back').dispatch('click');assert.equal(element('op-position').textContent,`1 / ${list.length}`);
68 element('next').dispatch('click');assert.equal(element('seek').value,String(n));
69 assert.equal(element('op-position').textContent,`2 / ${list.length}`);
70}
71seek(1);element('prev').dispatch('click');assert.equal(element('seek').value,'1');
72element('next').dispatch('click');assert.equal(element('seek').value,'2');
73element('prev').dispatch('click');assert.equal(element('seek').value,'1');
74location.hash='#step=11';winEvents.hashchange[0]();assert.equal(element('seek').value,'11');
75seek(count);element('next').dispatch('click');assert.equal(element('seek').value,String(count));
76assert.equal(JSON.stringify(window.R02_DATA),before,'Rewinding must not mutate inventory snapshots');
77const one=window.R02Story.frames('one');
78assert.deepEqual(Array.from(one.filter(f=>f.phase==='operation'),f=>f.state.stock.total),[2000,2000,1999]);
79assert.equal(one[4].state.items.find(i=>i.id===7).quantity,10,'OPEN creates ten loose units before ISSUE');
80assert.equal(one[5].state.items.find(i=>i.id===7).quantity,9,'ISSUE then subtracts one');
81assert.equal(window.R02_DATA.snapshots.simple10.stock.total,10);
82assert.equal(window.R02_DATA.snapshots.simpleIssue.stock.total,9);
83console.log(`Actual Anime.js tween + ${count} slides, ${processes} processes, ${operations} phases: seek, replay, speed, operation navigation, journal/stock conservation OK.`);
84