import test from 'node:test'; import assert from 'node:assert/strict'; import vm from 'node:vm'; import fs from 'node:fs'; const source=fs.readFileSync(process.env.CAROUSEL_SOURCE || new URL('../assets/custom/js/carousel.js',import.meta.url),'utf8'); function fixture({width=390,size=330,length=5}={}) { const timers=new Map();let timer=0, resize; const target=()=>({listeners:{},dataset:{},classList:{toggle(){},add(){},remove(){}},addEventListener(n,f){(this.listeners[n]??=[]).push(f);},emit(n,e={}){for(const f of this.listeners[n]||[])f(e);}}); const images=Array.from({length},()=>({...target(),tagName:'IMG'})); const root={...target(),clientWidth:width}; const items=images.map((image,i)=>({...target(),get offsetWidth(){return size;},get offsetLeft(){return (root.clientWidth-size)/2+i*(size+24);},querySelectorAll(){return [];}})); const calls=[]; const track={...target(),children:items,scrollLeft:0,get clientWidth(){return root.clientWidth;},querySelectorAll(){return images;},scrollTo(o){calls.push(o);if(o.behavior!=='smooth')this.scrollLeft=o.left;}}; const prev=target(),next=target(),count={},controls={}; root.querySelector=s=>({'[data-carousel-track]':track,'[data-carousel-prev]':prev,'[data-carousel-next]':next,'[data-carousel-count]':count,'[data-carousel-controls]':controls}[s]); root.querySelectorAll=()=>[]; vm.runInNewContext(source,{matchMedia:()=>({matches:false}),getComputedStyle:()=>({columnGap:'24'}),document:{readyState:'complete',querySelectorAll:()=>[root]},window:target(),setTimeout:f=>{timers.set(++timer,f);return timer;},clearTimeout:id=>timers.delete(id),ResizeObserver:class{constructor(f){resize=f;}observe(){}},IntersectionObserver:class{observe(){}}}); return {root,track,images,prev,next,count,calls,resize:()=>resize(),settle(){const pending=[...timers.values()];timers.clear();pending.forEach(f=>f());},swipe(left){track.scrollLeft=left;track.emit('scroll');}}; } test('late image loads do not cancel smooth desktop navigation',()=>{ const f=fixture({width:1663,size:1280});f.next.emit('click');const n=f.calls.length; f.images.forEach(i=>i.emit('load')); assert.equal(f.calls.length,n,'decoding media must not issue instant scrollTo');assert.equal(f.count.textContent,'2 / 5'); }); test('late images do not reset an unfinished native swipe',()=>{ const f=fixture();f.swipe(3*354);f.images[3].emit('load'); assert.equal(f.track.scrollLeft,1062);f.settle();assert.equal(f.count.textContent,'4 / 5'); }); test('height-only resize preserves native momentum and position',()=>{ const f=fixture();f.swipe(2*354);const n=f.calls.length;f.resize(); assert.equal(f.track.scrollLeft,708);assert.equal(f.calls.length,n);f.settle();assert.equal(f.count.textContent,'3 / 5'); }); test('real width changes recenter the selected item, fitting groups disable navigation',()=>{ const f=fixture();f.swipe(3*354);f.settle();f.root.clientWidth=844;f.resize(); assert.equal(f.track.scrollLeft,1062);assert.equal(f.count.textContent,'4 / 5'); const pair=fixture({width:1920,size:560,length:2});assert.equal(pair.track.dataset.fits,'true');assert.equal(pair.track.scrollLeft,0);assert.equal(pair.prev.disabled,true);assert.equal(pair.next.disabled,true); });