dom-smoke.mjs
Текст · 73 рядків
1import {readFileSync} from 'node:fs';
2import {tmpdir} from 'node:os';
3import {join} from 'node:path';
4import {createRequire} from 'node:module';
5import {pathToFileURL,fileURLToPath} from 'node:url';
6import assert from 'node:assert/strict';
7
8// DOM regression check. Test-only jsdom is installed in a temporary directory.
9const require=createRequire(import.meta.url);
10const {JSDOM,VirtualConsole}=require(join(tmpdir(),'r02-html-qa/node_modules/jsdom'));
11const file=new URL('../../../r-02-presentation/index.html',import.meta.url);
12const errors=[];
13const virtualConsole=new VirtualConsole();
14virtualConsole.on('jsdomError',error=>errors.push(error));
15const dom=new JSDOM(readFileSync(file,'utf8'),{url:pathToFileURL(fileURLToPath(file)).href,runScripts:'dangerously',pretendToBeVisual:true,virtualConsole,
16 beforeParse(window){
17 window.scrollTo=()=>{};window.matchMedia=()=>({matches:false});
18 if(process.argv.includes('--commonjs')){window.module={exports:{}};window.exports=window.module.exports;}
19 if(process.argv.includes('--amd')){window.define=()=>{throw new Error('Host AMD loader should not receive embedded Anime.js');};window.define.amd={};}
20 },
21});
22const {window}=dom,d=window.document;
23const wait=ms=>new Promise(resolve=>setTimeout(resolve,ms));
24try{
25 await wait(50);
26 console.log('Initial:',d.getElementById('counter').textContent,'title:',d.querySelector('h1')?.textContent);
27 if(errors.length)throw errors[0];
28 d.getElementById(process.argv.includes('--autoplay')?'play':'next').click();
29 await wait(50);
30 console.log('After Next:',d.getElementById('counter').textContent,'phase:',d.getElementById('op-position')?.textContent);
31 if(errors.length)throw errors[0];
32 assert.equal(d.getElementById('seek').value,'2');
33 if(process.argv.includes('--autoplay')){
34 assert.equal(d.getElementById('op-play').getAttribute('aria-pressed'),'true','Autoplay should start the first example immediately');
35 d.getElementById('op-play').click();
36 }
37 d.getElementById('op-next').click();
38 await wait(50);
39 console.log('Operation:',d.getElementById('op-position').textContent);
40 if(errors.length)throw errors[0];
41 assert.equal(d.getElementById('op-position').textContent,'2 / 5');
42 const range=d.getElementById('op-seek');
43 range.value='5900';range.dispatchEvent(new window.Event('input',{bubbles:true}));
44 d.getElementById('op-play').click();
45 await wait(400);
46 if(errors.length)throw errors[0];
47 assert.equal(d.getElementById('op-position').textContent,'2 / 5','Playback must cross the first boundary');
48 d.getElementById('op-play').click();
49 let phases=0,stories=0;
50 for(let slide=1;slide<=39;slide++){
51 const mainRange=d.getElementById('seek');mainRange.value=String(slide);mainRange.dispatchEvent(new window.Event('input',{bubbles:true}));
52 assert.equal(mainRange.value,String(slide));
53 if(errors.length)throw errors[0];
54 const key=d.querySelector('[data-story]')?.dataset.story;
55 if(!key)continue;
56 stories++;
57 const frames=window.R02Story.frames(key);
58 for(let phase=0;phase<frames.length;phase++){
59 if(phase)d.getElementById('op-next').click();
60 assert.equal(d.getElementById('op-position').textContent,`${phase+1} / ${frames.length}`);
61 assert(d.getElementById('process-view').textContent.includes(frames[phase].title));
62 if(errors.length)throw errors[0];phases++;
63 }
64 d.getElementById('op-replay').click();
65 assert.equal(d.getElementById('op-position').textContent,`1 / ${frames.length}`);
66 }
67 // No deferred animation callback may fail after the preceding DOM replacements.
68 await wait(100);
69 if(errors.length)throw errors[0];
70 assert.equal(stories,22);assert.equal(phases,137);
71 console.log(`Full DOM + motion enabled: ${stories} stories / ${phases} phases, real button events, playback and replay OK; environment ${process.argv.slice(2).join(' ')||'browser globals'}.`);
72}finally{dom.window.close();}
73