43 lines
6.0 KiB
JavaScript
43 lines
6.0 KiB
JavaScript
const {chromium}=require('playwright'),assert=require('node:assert/strict'),fs=require('node:fs');
|
|
const base=process.env.PD_NARRATIVE_TEST_URL||'http://127.0.0.1:5238';
|
|
(async()=>{
|
|
const installed=process.argv.includes('--installed'),profile=fs.mkdtempSync('/tmp/pd-narrative-pwa-');
|
|
const c=installed?await chromium.launchPersistentContext(profile,{headless:false,viewport:{width:1666,height:1050}}):await(await chromium.launch()).newContext({viewport:{width:1666,height:1050}}),b=c.browser();
|
|
try{
|
|
await c.route('**/Writer/Work/State?*',r=>r.fulfill({json:{session:null,defaultPauseOnEdit:true}}));
|
|
await c.route('**/native-companion/connection-state',r=>r.fulfill({json:{known:false,state:'Disconnected',wordConnected:false}}));
|
|
await c.route('**/native-companion/status*',r=>r.fulfill({json:{leases:[]}}));
|
|
await c.route('**/signalr.min.js',r=>r.fulfill({contentType:'application/javascript',body:fs.readFileSync(require.resolve('@microsoft/signalr/dist/browser/signalr.min.js'),'utf8')}));
|
|
await c.route('**/hubs/**',r=>r.abort());
|
|
await c.addInitScript(()=>localStorage.setItem('plotdirector.desktop.v1.4',JSON.stringify({dismissedAt:Date.now()})));
|
|
let p;
|
|
if(installed){const tab=c.pages()[0];await tab.goto(base+'/fixture');const d=await b.newBrowserCDPSession(),manifestId=base+'/Projects';await d.send('PWA.install',{manifestId,installUrlOrBundleUrl:base+'/fixture'});await d.send('PWA.changeAppUserSettings',{manifestId,displayMode:'standalone'});const opened=c.waitForEvent('page');await d.send('PWA.launch',{manifestId,url:base+'/fixture'});p=await opened;await p.waitForLoadState();assert(await p.evaluate(()=>matchMedia('(display-mode: standalone)').matches));}else p=await c.newPage();
|
|
|
|
|
|
|
|
await c.route('**/WriterActivities/**',r=>r.fulfill({contentType:'text/html',body:'<div data-planner-view>Fixture planner content</div>'}));
|
|
const toggle=p.locator('[data-ribbon-collapse]'),writer=p.locator('#ribbon-tab-writer');
|
|
let documents=0,preferenceWrites=0;const errors=[];p.on('pageerror',e=>errors.push(e.message));
|
|
p.on('request',r=>{if(r.isNavigationRequest()&&r.resourceType()==='document')documents++;if(r.method()==='POST'&&new URL(r.url()).pathname==='/Appearance/Ribbon')preferenceWrites++;});
|
|
async function collapsed(value){if((await toggle.getAttribute('aria-expanded')==='false')!==value)await toggle.click();await p.waitForFunction(()=>!document.querySelector('[data-ribbon-collapse]').disabled);}
|
|
for(const path of ['/Writer?projectId=7&bookId=12','/Writer/Review?projectId=7&bookId=12','/Writer/Explore?projectId=7&bookId=12','/Writer/Plan?projectId=7&bookId=12','/Writer/Reflect?projectId=7&bookId=12','/Timeline/Index?projectId=7&bookId=12']){
|
|
const response=await p.goto(base+path);assert.equal(response.status(),200,path);await p.waitForFunction(()=>window.PlotDirectorNarrativeFocus);
|
|
await collapsed(true);const url=p.url(),requests=documents,writes=preferenceWrites;
|
|
const focus=await p.evaluate(()=>JSON.stringify(window.PlotDirectorNarrativeFocus.state));await p.locator('main').first().evaluate(n=>window.__writerTabMain=n);
|
|
await writer.click();assert.equal(p.url(),url);assert.equal(documents,requests);assert.equal(preferenceWrites,writes,'selecting a tab does not overwrite the collapsed preference');
|
|
assert.equal(await writer.getAttribute('aria-selected'),'true');assert(await p.locator('#ribbon-panel-writer').isVisible());assert(await p.locator('#ribbon-surface').isVisible());assert.equal(await p.locator('#ribbon-tab-writer').count(),1);
|
|
assert(await p.locator('main').first().evaluate(n=>n===window.__writerTabMain),'current workspace remains mounted');assert.equal(await p.evaluate(()=>JSON.stringify(window.PlotDirectorNarrativeFocus.state)),focus);
|
|
await p.reload();assert.equal(await toggle.getAttribute('aria-expanded'),'false','temporary opening retains saved collapsed preference on refresh');
|
|
await collapsed(false);const expandedURL=p.url(),expandedRequests=documents;
|
|
for(const category of ['book','story','characters','world','tools','writer']){await p.locator('#ribbon-tab-'+category).click();assert.equal(p.url(),expandedURL);assert.equal(documents,expandedRequests);assert(await p.locator('#ribbon-panel-'+category).isVisible());}
|
|
await p.locator('#ribbon-tab-book').focus();await p.keyboard.press('ArrowRight');assert.equal(await writer.getAttribute('aria-selected'),'true');assert(await writer.evaluate(n=>n===document.activeElement));await p.keyboard.press('ArrowRight');assert.equal(await p.locator('#ribbon-tab-story').getAttribute('aria-selected'),'true');assert.equal(p.url(),expandedURL);
|
|
}
|
|
await writer.click();const home=p.locator('#ribbon-panel-writer a[data-command-operation="writer.index"]');const homeURL=await home.getAttribute('href');assert(new URL(homeURL,base).pathname==='/Writer');
|
|
const target=p.waitForURL(url=>url.pathname==='/Writer');await home.click();await target;for(const title of ['Continue Writing','Explore Your Story','Writing Dashboard'])assert(await p.getByRole('heading',{name:title,exact:true}).isVisible());
|
|
assert.equal(await toggle.getAttribute('aria-expanded'),'true','explicit expanded preference survives Home navigation');
|
|
for(const width of [1666,800]){await p.setViewportSize({width,height:1050});await collapsed(true);await writer.click();assert(await p.locator('#ribbon-panel-writer').isVisible());await p.screenshot({path:`/tmp/pd-writer-tab-${installed?'pwa':'browser'}-${width}.png`});}
|
|
await collapsed(true);await collapsed(false); // Restore the isolated host's default for other suites.
|
|
assert.deepEqual(errors,[]);console.log(`PASS ${installed?'installed PWA':'Chromium'} Writer tab: Home/Review/Explore/Plan/Reflect/Timeline remain mounted; collapsed and expanded shared selection; no document navigation or preference writes on selection; refresh persistence; keyboard order; other categories; explicit Home and all three choices; desktop/narrow layouts.`);
|
|
}finally{await c.close();await b?.close();fs.rmSync(profile,{recursive:true,force:true});}
|
|
})().catch(e=>{console.error(e.stack);process.exitCode=1;});
|