PlotDirector/PlotLine.Tests/Browser/public-marketing-performance.cjs

2 lines
1.7 KiB
JavaScript

const{chromium}=require('playwright'),fs=require('fs'),assert=require('node:assert/strict');const base=process.env.PUBLIC_BASE_URL||'https://dev.plotdirector.com';(async()=>{const b=await chromium.launch(),out=[];try{for(const width of [390,1440])for(const path of ['/','/features']){const c=await b.newContext({viewport:{width,height:1000},reducedMotion:'reduce'}),p=await c.newPage();await c.addCookies([{name:'solver-anonymous',value:'true',url:base}]);await p.addInitScript(()=>{window.marketingCLS=0;new PerformanceObserver(list=>{for(const e of list.getEntries())if(!e.hadRecentInput)window.marketingCLS+=e.value;}).observe({type:'layout-shift',buffered:true});});await p.goto(base+path);await p.waitForLoadState('networkidle');const first=await p.evaluate(()=>({cls:window.marketingCLS,bytes:performance.getEntriesByType('resource').reduce((s,r)=>s+r.transferSize,0)}));for(const img of await p.locator('main img').all()){await img.scrollIntoViewIfNeeded();await img.evaluate(e=>e.decode());}await p.waitForLoadState('networkidle');await p.waitForTimeout(300);const full=await p.evaluate(()=>({cls:window.marketingCLS,bytes:performance.getEntriesByType('resource').reduce((s,r)=>s+r.transferSize,0),images:performance.getEntriesByType('resource').filter(r=>/\.(avif|webp|png|jpe?g)(\?|$)/.test(r.name)).map(r=>({url:r.name,encoded:r.encodedBodySize}))}));out.push({path,width,first,full});assert(full.cls<.02,`Layout shift ${path}/${width}: ${full.cls}`);await c.close();}fs.writeFileSync(process.argv[2],JSON.stringify(out,null,2));console.log('PASS full-page image decoding and CLS <0.02; recorded fresh-context initial/full-scroll transfers.');}finally{await b.close();}})().catch(e=>{console.error(e);process.exit(1)});