const {chromium}=require('playwright'); const assert=require('node:assert/strict'); const base='http://127.0.0.1:5238'; (async()=>{ const browser=await chromium.launch(); try { for(const pwa of [false,true]) for(const width of [1440,1700,1920]) { const context=await browser.newContext({viewport:{width,height:1000}}); if(pwa) await context.addInitScript(()=>Object.defineProperty(navigator,'standalone',{value:true})); const page=await context.newPage();const errors=[];page.on('pageerror',e=>errors.push(e.message)); await page.goto(base+'/fixture?page=relationships',{waitUntil:'domcontentloaded'}); await page.waitForFunction(()=>!!window.PlotDirectorRelationshipWorkspace); if(pwa && width===1920) { await page.getByRole('tab',{name:'Characters',exact:true}).click(); await page.waitForFunction(()=>document.querySelector('[data-pwa-context-controls] .pd-context-group[aria-label="Focus"] details')?.dataset.compact==='false'); const spacing=await page.evaluate(()=>{const group=document.querySelector('[data-pwa-context-controls] .pd-context-group[aria-label="Focus"]');const buttons=[...group.querySelectorAll('button')];return {compact:group.querySelector('details').dataset.compact,rects:buttons.map(b=>b.getBoundingClientRect().toJSON()),clear:buttons[1].textContent.trim(),disabled:buttons[1].disabled,clipped:buttons[1].querySelector('span').scrollWidth>buttons[1].querySelector('span').clientWidth+1}}); assert.equal(spacing.compact,'false');assert.equal(spacing.clear,'Clear focus');assert(spacing.disabled,'Clear focus is disabled without a focus'); assert(spacing.rects.every(r=>r.width>0&&r.height>0),'both actions present without an empty slot'); assert(Math.abs(spacing.rects[0].width-spacing.rects[1].width)<1,'balanced action widths'); assert(spacing.rects[1].x-spacing.rects[0].right<=8,'normal compact ribbon gap');assert(!spacing.clipped,'Clear focus is not truncated'); } const result=await page.evaluate(async()=>{ const frame=()=>new Promise(r=>requestAnimationFrame(r)); await frame();await frame();await frame(); const w=window.PlotDirectorRelationshipWorkspace, cy=w.cy, canvas=document.getElementById('relationshipMapGraph'); const selectors=['.pd-application-header','#rwApp','.rw-top','.rw-navigation','.rw-rail','.rw-centre','#relationshipMapGraph','.rw-timeline','.rw-bottom']; const bounds=()=>selectors.map(s=>{const r=document.querySelector(s).getBoundingClientRect();return [r.x,r.y,r.width,r.height]}); const baseline=bounds(), samples=[]; const controls=document.querySelector('[data-pwa-context-controls]'); const nodes=[...controls.children], focusButton=controls.querySelector('[data-command-identity="handler:rwFocusName"]'); let rebuilds=0;const observer=new MutationObserver(records=>{rebuilds+=records.filter(r=>r.target===controls).length}); observer.observe(controls,{childList:true}); const sourceReplace=controls.replaceChildren;controls.replaceChildren=function(...args){rebuilds++;return sourceReplace.apply(this,args)}; const long='Alexandra Catherine Montgomery With An Exceptionally Long Character Name'; w.cast.find(c=>c.characterID===2).characterName='Jo'; w.cast.find(c=>c.characterID===3).characterName=long; cy.$id('c-2').data('label','Jo');cy.$id('c-3').data('label',long); cy.zoom(.9); const positions=cy.nodes().map(n=>({id:n.id(),p:{...n.position()}})); for(const id of [1,2,3,2,1,3]) { cy.$id(`c-${id}`).emit('tap');samples.push(bounds()); for(let f=0;f<5;f++){await frame();samples.push(bounds())} const clearProxy=controls.querySelector('[data-command-identity="handler:rwClearFocus"]'); if(clearProxy && clearProxy.disabled) throw new Error('Clear action did not enable on focus'); if(document.querySelector('[data-map-detail-title]').textContent!==w.cast.find(c=>c.characterID===id).characterName) throw new Error('Details did not track focus'); if(!cy.$id(`c-${id}`).hasClass('is-focus'))throw new Error('Graph focus did not update'); } for(const id of [1,3,2,3,1,2,1,3,2,3]) {cy.$id(`c-${id}`).emit('tap');samples.push(bounds())} for(let f=0;f<5;f++){await frame();samples.push(bounds())} const caption=focusButton?.textContent.trim(), tooltip=focusButton?.title; document.getElementById('rwClearFocus').click(); for(let f=0;f<5;f++){await frame();samples.push(bounds())} observer.disconnect(); const clearProxy=controls.querySelector('[data-command-identity="handler:rwClearFocus"]'); if(clearProxy && !clearProxy.disabled) throw new Error('Clear action did not disable after clearing'); const sameNodes=nodes.length===controls.children.length&&nodes.every((n,i)=>n===controls.children[i]); return {baseline,samples,rebuilds,sameNodes,caption,tooltip,long,positionsUnchanged:positions.every(({id,p})=>Math.abs(cy.$id(id).position().x-p.x)<.01&&Math.abs(cy.$id(id).position().y-p.y)<.01),sameGraph:w.cy===cy&&document.getElementById('relationshipMapGraph')===canvas,zoom:cy.zoom(),clearLabel:focusButton?.textContent.trim()}; }); result.samples.forEach((sample,index)=>sample.forEach((rect,region)=>rect.forEach((value,axis)=>assert(Math.abs(value-result.baseline[region][axis])<=1,`${pwa?'PWA':'web'} ${width}: shifted region ${region} axis ${axis} sample ${index}: ${value} vs ${result.baseline[region][axis]}`)))); if(pwa) {assert.equal(result.rebuilds,0,'focus must not rebuild contextual ribbon');assert(result.sameNodes,'ribbon nodes retained');} assert(result.sameGraph&&result.positionsUnchanged,'canvas instance and node layout retained');assert.equal(result.zoom,.9); if(pwa){assert.equal(result.caption,'Focus: '+result.long);assert.equal(result.tooltip,result.caption);assert.equal(result.clearLabel,'Full cast'); assert.equal(await page.locator('[data-pwa-context-controls]').getByRole('button',{name:'Maximise workspace',exact:true,includeHidden:true}).count(),1); assert.equal(await page.locator('[data-pwa-context-controls] .pd-context-group:empty').count(),0); await page.evaluate(()=>{const source=document.createElement('button');source.id='focusFixtureCommand';source.type='button';source.textContent='Fixture command';source.dataset.ribbonIcon='list';source.onclick=()=>window.__fixtureInvoked=true;document.querySelector('.rw-view-switch').append(source)}); const fixture=page.locator('[data-pwa-context-controls]').getByRole('button',{name:'Fixture command',exact:true,includeHidden:true}); await fixture.waitFor({state:'attached'});await fixture.evaluate(button=>button.click());assert(await page.evaluate(()=>window.__fixtureInvoked),'new structural contribution still binds its source'); await page.locator('#focusFixtureCommand').evaluate(button=>button.remove());await fixture.waitFor({state:'detached'}); } else {assert(await page.locator('.rw-toolbar').isVisible());assert(await page.locator('#rwMaximise').isVisible());} assert.deepEqual(errors,[]);console.log(`PASS focus stability: ${pwa?'PWA':'web'} ${width}px, ${result.samples.length} frame/bounds samples${pwa ? ', no ribbon rebuilds' : ''}`); await context.close(); } }finally{await browser.close()} })().catch(e=>{console.error(e);process.exitCode=1});