PlotDirector/PlotLine.Tests/Browser/home-solver-marketing.cjs

6 lines
4.3 KiB
JavaScript

const{chromium}=require('playwright'),assert=require('node:assert/strict'),fs=require('fs');
const base=process.env.PUBLIC_BASE_URL||'http://127.0.0.1:5238',live=base.includes('dev.plotdirector.com'),output=process.env.PUBLIC_CAPTURE_DIR||'/tmp/home-marketing-browser';fs.mkdirSync(output,{recursive:true});
(async()=>{const b=await chromium.launch(),results=[];try{for(const width of [390,768,1440,1920]){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}]);for(const name of ['home','tour']){await p.goto(base+(live?(name==='home'?'/':'/features'):`/public-fixture/${name}?anonymous=true`));await p.waitForLoadState('networkidle');for(const picture of await p.locator('[data-marketing-screenshot]').all()){await picture.scrollIntoViewIfNeeded();const img=picture.locator('img');await img.evaluate(e=>e.decode());assert((await img.evaluate(e=>e.currentSrc)).endsWith('.avif'));assert.equal(await img.evaluate(e=>e.naturalWidth>0),true);if(name==='tour'||!(await picture.evaluate(e=>e.closest('.power-composition'))))assert.equal(await picture.getAttribute('data-screenshot-theme'),'plotdirector');}assert.equal(await p.evaluate(()=>document.documentElement.scrollWidth>innerWidth),false,`${name}/${width}`);if(name==='home'){const section=p.locator('.writer-solver-invitation');assert.equal(await section.count(),1);assert.equal(await section.locator('li').count(),4);assert.equal(await section.locator('button,input,select,form').count(),0);assert.match(await section.innerText(),/No sign-up needed/);const link=section.getByRole('link',{name:'Help me find the problem'});assert.equal(await link.getAttribute('href'),'/problem-solver');await link.focus();assert.equal(await link.evaluate(e=>e===document.activeElement),true);await section.screenshot({path:`${output}/invitation-${width}.png`});assert(await p.locator('.writer-problems').evaluate(e=>!!e.nextElementSibling?.matches('.writer-solver-invitation')));assert(await section.evaluate(e=>!!e.nextElementSibling?.matches('.remembers-section')));}await p.evaluate(()=>scrollTo(0,0));await p.screenshot({path:`${output}/${name}-${width}-full.png`,fullPage:true});if(name==='tour'){const failures=await p.evaluate(()=>{const rgb=s=>(s.match(/[\d.]+/g)||[]).map(Number),lin=v=>(v/=255)<=.04045?v/12.92:((v+.055)/1.055)**2.4,lum=c=>.2126*lin(c[0])+.7152*lin(c[1])+.0722*lin(c[2]);const out=[];const walk=document.createTreeWalker(document.querySelector('.writer-tour'),NodeFilter.SHOW_TEXT);while(walk.nextNode()){const n=walk.currentNode,e=n.parentElement;if(!n.textContent.trim()||e.closest('[aria-hidden=true],script,style')||!e.getBoundingClientRect().width)continue;const st=getComputedStyle(e);if(st.visibility==='hidden'||st.display==='none')continue;let bg=null;for(let a=e;a;a=a.parentElement){const col=rgb(getComputedStyle(a).backgroundColor);if(col.length===3||col[3]===1){bg=col;break;}}bg??=[252,243,229];const fg=rgb(st.color),ratio=(Math.max(lum(fg),lum(bg))+.05)/(Math.min(lum(fg),lum(bg))+.05),large=parseFloat(st.fontSize)>=24||(parseFloat(st.fontSize)>=18.66&&parseInt(st.fontWeight)>=700);if(ratio<(large?3:4.5))out.push({text:n.textContent.trim().slice(0,90),ratio,fg:st.color,bg,tag:e.tagName,cls:e.className});}return out;});results.push({width,contrastFailures:failures});assert.deepEqual(failures,[],`Tour contrast ${width}: ${JSON.stringify(failures)}`);}}
// Ordinary server navigation remains usable without JavaScript and without an account.
await c.close();const nj=await b.newContext({javaScriptEnabled:false,viewport:{width,height:1000}});await nj.addCookies([{name:'solver-anonymous',value:'true',url:base}]);const np=await nj.newPage();await np.goto(base+(live?'/':'/public-fixture/home?anonymous=true'));await np.locator('.writer-solver-invitation a').click();assert.equal(new URL(np.url()).pathname,'/problem-solver');assert.equal(await np.locator('form').count()>0,true);assert(!np.url().includes('Login'));await nj.close();}fs.writeFileSync(output+'/checks.json',JSON.stringify(results,null,2));console.log('PASS Home invitation/anonymous no-JS CTA, canonical screenshot clusters, AVIF loading, all Tour text contrast, full-page captures and overflow at four widths.');}finally{await b.close();}})().catch(e=>{console.error(e);process.exit(1)});