PlotDirector/PlotLine.Tests/Browser/investigation-ux-correction.cjs

23 lines
12 KiB
JavaScript

const{chromium}=require('playwright'),fs=require('fs'),assert=require('node:assert/strict');
(async()=>{const w=JSON.parse(fs.readFileSync(process.env.PD_UX_IDENTITY)),base='https://dev.plotdirector.com',out=process.env.PD_UX_SCREENSHOTS,b=await chromium.launch(),timings=[];try{const c=await b.newContext({viewport:{width:1440,height:1000}});await c.addInitScript(u=>{Object.defineProperty(navigator,'standalone',{value:true});localStorage.setItem('plotdirector.desktop.v1.'+u,JSON.stringify({dismissedAt:Date.now()}));},w.UserID);const p=await c.newPage(),errors=[];p.on('pageerror',e=>errors.push(e.message));await p.exposeFunction('navigationAck',ms=>timings.push({ackMs:ms}));await p.addInitScript(()=>{document.addEventListener('submit',e=>{if(!e.target.matches('[data-guidance-navigation]'))return;const start=performance.now(),observer=new MutationObserver(()=>{if(e.target.textContent.includes('Opening…')){window.navigationAck(performance.now()-start);observer.disconnect();}});observer.observe(e.target,{subtree:true,childList:true,characterData:true});},true);});const shot=async name=>{if(out){fs.mkdirSync(out,{recursive:true});await p.mouse.move(2,995);await p.screenshot({path:out+'/'+name+'.png'});}};
await p.goto(base+'/Account/Login');await p.locator('[name=Email]').fill(w.Email);await p.locator('[name=Password]').fill(w.Password);await Promise.all([p.waitForURL('**/Projects'),p.locator('button[type=submit]').click()]);const explore=`${base}/Writer/Explore?projectId=${w.project}&bookId=${w.book}`;await p.goto(explore);await p.locator('[data-my-investigations] article').first().waitFor();await shot('My-investigations');const root=p.locator('[data-investigation-companion]'),help=p.locator('[data-help-drawer]');
// Starting from saved results does not activate guidance. Continue is explicit.
assert(await root.isHidden());assert(await root.locator('[data-guidance-expand]').isHidden());
const older=p.locator('[data-my-investigations] article').filter({has:p.locator(`input[name=id][value="${w.oldInvestigation}"]`)});await older.getByRole('button',{name:'Continue',exact:true}).click();await p.waitForFunction(()=>document.querySelector('[data-guidance-content]')?.textContent.includes('older investigation'));assert.equal(await root.locator('.pd-guidance-tool').count(),0);await shot('Older-investigation');await p.goto(explore);await p.locator('[data-my-investigations] article').first().waitFor();const card=p.locator('[data-my-investigations] article').filter({has:p.locator(`input[name=id][value="${w.investigation}"]`)});await card.getByRole('button',{name:'Continue',exact:true}).click();await p.locator('[data-conclusion]').waitFor();await root.waitFor();const count=c.pages().length;
for(let n=0;n<3;n++){if(n)await p.goto(`${base}/Writer/Explore/Investigate?bookId=${w.book}&id=${w.investigation}`);const network=[];const capture=r=>{if(r.request().isNavigationRequest()||r.url().includes('/InvestigationCompanion/Summary'))network.push({url:new URL(r.url()).pathname,status:r.status(),timing:r.request().timing(),serverTiming:r.headers()['server-timing']});};p.on('response',capture);const start=Date.now();await p.locator('.investigation form[data-guidance-navigation]').first().getByRole('button').click();await p.waitForURL(/PlotLineWorkspace/);const documentMs=Date.now()-start;await root.locator('#guidance-finding').waitFor();timings.push({documentMs,restoredMs:Date.now()-start,network});p.removeListener('response',capture);}
await p.goto(`${base}/Writer/Explore/Investigate?bookId=${w.book}&id=${w.investigation}`);let submissions=0;const record=r=>{if(r.method()==='POST'&&new URL(r.url()).pathname==='/Writer/Explore/Investigate/Open')submissions++;};p.on('request',record);await p.locator('.investigation form[data-guidance-navigation]').first().evaluate(f=>{f.requestSubmit(f.querySelector('button'));f.requestSubmit(f.querySelector('button'));});await p.waitForURL(/PlotLineWorkspace/);await root.locator('#guidance-finding').waitFor();p.removeListener('request',record);assert.equal(submissions,1);
assert.equal(c.pages().length,count);const panelContent=root.locator('.plotline-help-drawer-panel');assert.equal(await root.getAttribute('data-state'),'open');await p.evaluate(()=>PlotLineHelpDrawer.open('plotLines.workspace'));await help.locator('.help-drawer-article').waitFor();assert(await p.locator('.plot-workspace-controls').evaluate(n=>{const r=n.closest('.plotline-help-main').getBoundingClientRect();return [...n.querySelectorAll('input:not([type=hidden]),select,button')].every(c=>c.getBoundingClientRect().right<=r.right);}));await shot('Both-open');
for(const lp of[false,true])for(const rp of[false,true]){await p.locator('[data-guidance-open]').click();await p.evaluate(()=>PlotLineHelpDrawer.open('plotLines.workspace'));for(const[control,value]of [[root.locator('[data-guidance-pin]'),lp],[help.locator('[data-help-pin]'),rp]])if((await control.getAttribute('aria-pressed')==='true')!==value)await control.click();await p.reload();await p.waitForFunction(()=>!document.querySelector('[data-guidance-open]').hidden);assert.equal(await root.getAttribute('data-state'),lp?'open':'closed');assert.equal(await help.getAttribute('data-state'),rp?'open':'closed');}
for(const left of['open','collapsed','closed'])for(const right of['open','collapsed','closed']){
if(await root.getAttribute('data-state')!=='open')await p.locator('[data-guidance-open]').click();if(left==='collapsed')await root.locator('[data-guidance-collapse]').click();if(left==='closed')await root.locator('[data-guidance-close]').click();
await p.evaluate(()=>PlotLineHelpDrawer.open('plotLines.workspace'));await help.locator('.help-drawer-article').waitFor();if(right==='collapsed')await help.locator('[data-help-collapse]').click();if(right==='closed')await help.locator('[data-help-close]').click();
assert.equal(await root.isVisible(),left!=='closed');assert.equal(await panelContent.isVisible(),left==='open');assert.equal(await help.isVisible(),right!=='closed');assert.equal(await help.locator('.plotline-help-drawer-panel').isVisible(),right==='open');}
await p.locator('[data-guidance-open]').click();await p.evaluate(()=>PlotLineHelpDrawer.open('plotLines.workspace'));await help.locator('.help-drawer-article').waitFor();await root.locator('[data-guidance-close]').click();await shot('Help-only');assert(await help.isVisible());await p.locator('[data-guidance-open]').click();await help.locator('[data-help-close]').click();await shot('Investigation-only');
// A close is presentation only, and a saved selector changes only authorised guidance.
const before=await (await p.request.get(base+`/InvestigationCompanion/Summary?projectId=${w.project}&bookId=${w.book}`)).json();await root.locator('[data-guidance-close]').click();const after=await (await p.request.get(base+`/InvestigationCompanion/Summary?projectId=${w.project}&bookId=${w.book}`)).json();assert.equal(after.summary.version,before.summary.version);await p.reload();assert(await root.isHidden());await p.locator('[data-guidance-open]').click();
const picker=root.getByLabel('Switch saved investigation');await picker.focus();await picker.locator(`option[value="${w.secondInvestigation}"]`).waitFor({state:'attached'});await picker.selectOption(w.secondInvestigation);await p.waitForFunction(()=>document.querySelector('[data-guidance-picker] a')?.href.includes(window.location.origin)&&document.querySelector('[data-guidance-picker] a')?.href.includes('id='));await p.waitForFunction(id=>document.querySelector('[data-guidance-picker] a')?.href.includes(id),w.secondInvestigation);assert((await root.getByRole('link',{name:'Full investigation results'}).getAttribute('href')).includes(w.secondInvestigation));
await root.locator('[data-guidance-pin]').click(); // pin (Close unpinned it)
await p.evaluate(()=>PlotLineHelpDrawer.open('plotLines.workspace'));if(await help.locator('[data-help-pin]').getAttribute('aria-pressed')!=='true')await help.locator('[data-help-pin]').click();const companionNetwork=[];const trace=r=>{if(r.request().isNavigationRequest()||r.url().includes('/InvestigationCompanion/Summary'))companionNetwork.push({url:new URL(r.url()).pathname,status:r.status(),timing:r.request().timing(),serverTiming:r.headers()['server-timing']});};p.on('response',trace);const companionStart=Date.now();await root.locator('.pd-guidance-tool[data-capability=timeline] form button').click();await p.waitForURL(/Timeline/);timings.push({entryPoint:'companion',documentMs:Date.now()-companionStart,network:companionNetwork});p.removeListener('response',trace);await root.locator('#guidance-finding').waitFor();await help.locator('.help-drawer-article').waitFor();assert.equal(await help.getAttribute('data-state'),'open');await shot('Timeline');await p.reload();await root.locator('#guidance-finding').waitFor();assert.equal(await root.getAttribute('data-state'),'open');
await root.locator('[data-guidance-pin]').click();await root.locator('.pd-guidance-tool select').first().selectOption('Done');await p.waitForFunction(()=>document.querySelector('[data-guidance-message]')?.textContent==='');assert.equal(await root.getAttribute('data-state'),'open');await p.reload();assert(await root.isHidden());await p.locator('[data-guidance-open]').click();await p.setViewportSize({width:800,height:1000});await p.evaluate(()=>PlotLineHelpDrawer.open('timeline.overview'));await help.locator('.help-drawer-article').waitFor();assert(await p.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+2));await shot('Narrow-both');await root.locator('[data-guidance-close]').click();assert(await help.isVisible());
await p.setViewportSize({width:1440,height:1000});await p.goto(explore);await p.locator('[data-my-investigations] article').first().waitFor();assert(await root.isHidden());await p.locator('[data-guidance-open]').click();await root.getByRole('button',{name:'Finish this journey'}).click();await p.waitForFunction(()=>document.querySelector('[data-investigation-companion]')?.dataset.state==='closed');assert.equal((await (await p.request.get(base+`/InvestigationCompanion/Summary?bookId=${w.book}`)).json()).active,false);await p.goto(explore);await p.locator('[data-my-investigations] article').first().waitFor();const original=p.locator('[data-my-investigations] article').filter({has:p.locator(`input[name=id][value="${w.investigation}"]`)});await original.getByRole('link',{name:'Original results'}).click();assert.equal((await (await p.request.get(base+`/InvestigationCompanion/Summary?bookId=${w.book}`)).json()).active,false);await p.goto(explore);await p.locator('[data-my-investigations] article').filter({has:p.locator(`input[name=id][value="${w.investigation}"]`)}).getByRole('button',{name:'Continue',exact:true}).click();await root.locator('#guidance-finding').waitFor();if(await root.locator('[data-guidance-pin]').textContent()==='Pin')await root.locator('[data-guidance-pin]').click();await p.goto(`${base}/Writer/Review?projectId=${w.project}&bookId=${w.book}`);await root.locator('#guidance-finding').waitFor();await p.evaluate(()=>PlotLineHelpDrawer.open('writer3.reviewing'));const prior=[await root.getAttribute('data-state'),await help.getAttribute('data-state')];await p.locator('#reader-focus').click();assert(await root.evaluate(n=>n.inert));assert(await help.evaluate(n=>n.inert));await p.locator('#reader-focus').click();assert.deepEqual([await root.getAttribute('data-state'),await help.getAttribute('data-state')],prior);await shot('Writer-review');await p.goto(`${base}/Writer/Explore?projectId=${w.project}&bookId=${w.otherBook}`);await p.waitForFunction(()=>document.querySelector('[data-saved-investigations]')?.textContent.includes('No saved investigations'));assert(await root.isHidden());assert(await p.locator('[data-guidance-open]').isHidden());await p.goto(explore);await root.locator('#guidance-finding').waitFor();assert.equal(await root.getAttribute('data-state'),'open');assert.deepEqual(errors,[]);fs.writeFileSync(process.env.PD_UX_TIMINGS||'/tmp/ux-timings.json',JSON.stringify(timings,null,2));console.log('PASS authenticated UX correction: saved discovery, nine independent lifecycle pairs, close preserves guidance, explicit reopen, switching, pin/unpin navigation/refresh, no duplicate windows, progress, narrow layout');await c.close();}finally{await b.close()}})().catch(e=>{console.error(e.stack);process.exitCode=1});