PlotDirector/PlotLine.Tests/Browser/writing-solver-polish.cjs

10 lines
6.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const{chromium}=require('playwright'),assert=require('node:assert/strict'),fs=require('node:fs');
const base=process.env.SOLVER_BASE_URL||'http://127.0.0.1:5238';
const graph=JSON.parse(fs.readFileSync('PlotLine/Content/WritingSolver/Candidates/curated-v2-stage1-repair/writing-solver-curated-v2-stage1-repair.json'));
const catalog=JSON.parse(fs.readFileSync('PlotLine/Content/WritingSolver/Presentation/payoff-v1.json'));
(async()=>{const b=await chromium.launch();try{const measurements=[];for(const width of [390,768,1440,1920]){const c=await b.newContext({viewport:{width,height:1000}});await c.addCookies([{name:'solver-anonymous',value:'true',url:base}]);const p=await c.newPage();await p.addInitScript(()=>{window.testCLS=0;new PerformanceObserver(l=>{for(const e of l.getEntries())if(!e.hadRecentInput)window.testCLS+=e.value;}).observe({type:'layout-shift',buffered:true});});
for(const family of [...new Set(catalog.results.map(r=>r.family))]){const map=catalog.results.find(r=>r.family===family),s=graph.solutions.find(s=>s.id===map.solution);const images=[];p.on('request',r=>{if(r.url().includes('/images/writing-solver/'))images.push(r.url())});await p.goto(base+'/problem-solver/'+s.slug);await p.waitForLoadState('networkidle');const main=await p.locator('.solver-content').boundingBox(),action=await p.locator('.solver-author-action').boundingBox(),copy=await p.locator('.solver-permanent-copy').boundingBox(),related=await p.locator('.solver-related').boundingBox();assert(Math.abs(main.x-action.x)<1&&Math.abs(main.width-action.width)<1);assert(Math.abs(copy.x-main.x)<1);assert(Math.abs(related.width-copy.width)<1);assert(copy.width<=896.5);if(width>=1440)assert(copy.width>850);assert.equal(await p.evaluate(()=>document.documentElement.scrollWidth>innerWidth),false);
const trigger=p.locator('.solver-start [data-solver-image]');await trigger.scrollIntoViewIfNeeded();await p.locator('.solver-start .solver-tool-visual img').evaluate(e=>e.decode());const full=await trigger.getAttribute('href');const initialResources=await p.evaluate(()=>performance.getEntriesByType('resource').map(r=>({url:r.name,bytes:r.transferSize})));assert(!initialResources.some(r=>r.url===base+full&&r.bytes>0),'Full inspection WebP transferred before opening '+JSON.stringify({width,family,initialResources}));await trigger.focus();const before=await trigger.boundingBox(),scrollBefore=await p.evaluate(()=>scrollY);await p.keyboard.press('Enter');const dialog=p.getByRole('dialog',{name:'Application screenshot'});await dialog.waitFor({state:'visible'});const large=dialog.locator('img');await large.evaluate(e=>e.decode());assert.equal(await large.getAttribute('alt'),await trigger.locator('img').getAttribute('alt'));assert.equal(await p.evaluate(()=>document.documentElement.style.overflow),'hidden');assert.equal(c.pages().length,1);const box=await dialog.boundingBox(),image=await large.boundingBox();assert(box.width<=(width<=700?width-23:width*.9+1),JSON.stringify({width,family,box}));assert(box.height<=(width<=700?976:901));assert(image.x>=box.x&&image.x+image.width<=box.x+box.width+1);const ratio=await large.evaluate(e=>e.naturalWidth/e.naturalHeight);assert(Math.abs(image.width/image.height-ratio)<.01,'Image was distorted/cropped');assert(await dialog.getByRole('button',{name:'Close screenshot'}).evaluate(e=>e===document.activeElement));await p.keyboard.press('Tab');await p.keyboard.press('Shift+Tab');assert(await p.evaluate(()=>document.activeElement.closest('dialog')!==null),'Tab escaped dialog');await p.keyboard.press('Escape');await dialog.waitFor({state:'hidden'});await p.waitForFunction(()=>document.documentElement.style.overflow!=='hidden');assert(await trigger.evaluate(e=>e===document.activeElement));assert.equal(await p.evaluate(()=>document.documentElement.style.overflow),'');assert.equal(await p.evaluate(()=>scrollY),scrollBefore);const after=await trigger.boundingBox();assert(Math.abs(before.x-after.x)<1&&Math.abs(before.width-after.width)<1,'Scrollbar lock shifted layout');await trigger.click();await dialog.getByRole('button',{name:'Close screenshot'}).click();await dialog.waitFor({state:'hidden'});await trigger.click();await p.mouse.click(2,2);await dialog.waitFor({state:'hidden'});assert.equal(await p.evaluate(()=>document.documentElement.scrollWidth>innerWidth),false);assert((await p.evaluate(()=>window.testCLS))<.01);measurements.push({width,family,main:main.width,prose:copy.width,cls:await p.evaluate(()=>window.testCLS),inspectionBytes:(await p.evaluate(()=>performance.getEntriesByType('resource').filter(r=>r.name.endsWith('.webp')).map(r=>({url:r.name,bytes:r.transferSize})))).filter(r=>r.url===base+full)});p.removeAllListeners('request');}
await p.goto(base+'/problem-solver');for(const choice of ['story-not-working','too-many','lost','deliberate']){await p.locator(`input[name=choices][value="${choice}"]`).check();await p.locator('.solver-form button[type=submit]').click();}assert.equal(await p.locator('.solver-reassurance').count(),1);assert.equal(await p.locator('.solver-recap').count(),1);assert.equal(await p.locator('.solver-recap a').innerText(),'Restart');assert.equal(await p.locator('.solver-recap').getAttribute('aria-label'),'Areas discussed');assert.equal(await p.evaluate(()=>document.documentElement.scrollWidth>innerWidth),false);await c.close();}
const nojs=await b.newContext({javaScriptEnabled:false});const p=await nojs.newPage();await p.goto(base+'/problem-solver/'+graph.solutions[0].slug);assert.equal(await p.locator('[data-solver-image]').first().getAttribute('target'),null);assert((await p.locator('[data-solver-image]').first().getAttribute('href')).endsWith('.webp'));await nojs.close();fs.writeFileSync(process.env.POLISH_METRICS||'/tmp/polish-metrics.json',JSON.stringify(measurements,null,2));console.log('PASS seven result families × four widths: aligned main/action, shared wider prose/related area; modal fit/aspect, deferred image, keyboard focus/Tab/Escape, close/backdrop, scroll lock/restoration, no new tab or overflow; reassurance recap; no-JS image fallback.');}finally{await b.close();}})().catch(e=>{console.error(e);process.exit(1)});