53 lines
8.0 KiB
JavaScript
53 lines
8.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.addCookies([{name:'fixture-focus-reader',value:'mapped',url:base}]);
|
|
await c.addInitScript(()=>{window.__focusEvents=[];window.__positions=[];window.addEventListener('plotdirector:narrative-focus-changed',e=>window.__focusEvents.push({chapterID:e.detail.chapterID,sceneID:e.detail.sceneID,version:e.detail.version}));document.addEventListener('reader:focus-positioned',e=>window.__positions.push(e.detail));});
|
|
const active='.pd-ribbon-panel:not([hidden])',command=async kind=>{const menu=p.locator(active+' .pd-ribbon-focus details[data-compact=true]');if(await menu.count()&&!await menu.evaluate(n=>n.open))await menu.locator('summary').click();await p.locator(active+' [data-global-focus='+kind+']').click();},go=()=>command('go'),state=()=>p.evaluate(()=>window.PlotDirectorNarrativeFocus.state),set=t=>p.evaluate(t=>window.PlotDirectorNarrativeFocus.set(t),t);
|
|
const clear=()=>p.evaluate(()=>{window.__positions=[];window.__focusEvents=[];});
|
|
async function positioned(scene){await p.waitForFunction(id=>window.__positions.some(e=>e.sceneID===id),scene);}
|
|
async function position(){return p.evaluate(()=>{const r=document.querySelector('.manuscript-reader'),n=r.querySelector(`[data-paragraph-id="${r.dataset.focusParagraph}"]`);return {id:r.dataset.focusParagraph,top:n?.getBoundingClientRect().top,revision:r.dataset.revision,version:r.dataset.version,chapter:r.dataset.chapterId};});}
|
|
async function choose(value){await clear();await p.locator('[data-focus-toggle]').click();await p.locator('[data-focus-position]').selectOption(value);await positioned(Number(value.split('/')[1]));}
|
|
await p.goto(base+'/Scenes/Edit/31');await p.waitForFunction(()=>window.PlotDirectorNarrativeFocus?.state.sceneID===31);
|
|
const assertScene=async id=>{await p.waitForURL(new RegExp('/Scenes/Edit/'+id+'(?:\\?|$)'));await p.waitForFunction(id=>window.PlotDirectorNarrativeFocus?.state.sceneID===id,id);assert.equal(await p.locator('[data-scene-workspace]').getAttribute('data-scene-id'),String(id));assert.equal(await p.locator('[data-focus-scene]').getAttribute('data-focus-scene'),String(id));assert.match(await p.locator('[data-scene-identity]').textContent(),new RegExp('Fixture scene '+id));assert.equal(await p.locator('[data-scene-summary-part]').count(),6);};
|
|
await command('next');await assertScene(101);await command('previous');await assertScene(31);
|
|
await command('next');await assertScene(101);await command('next');await assertScene(32);
|
|
await p.locator('[data-focus-toggle]').click();await p.locator('[data-focus-position]').selectOption('21/31');await assertScene(31);
|
|
await p.locator('[data-focus-toggle]').click();await p.locator('[data-focus-recent]').selectOption('22/32');await assertScene(32);
|
|
await p.goto(base+'/Scenes/Edit/31?retainNarrativeFocus=true');await assertScene(32);
|
|
// Retain a deliberately stale view until the explicit recovery action.
|
|
await p.goto(base+'/Scenes/Edit/31');await assertScene(31);
|
|
const before=(await state()).version;await go();assert.equal((await state()).version,before,'aligned Go does not write focus or reload');
|
|
// Use the actual scene editor dirty contract and a real form input event.
|
|
await p.evaluate(()=>{const o=document.querySelector('[data-scene-editor-overlay]');o.hidden=false;const t=document.createElement('textarea');t.value='Unsaved draft';o.append(t);t.dispatchEvent(new Event('input',{bubbles:true}));});
|
|
const version=(await state()).version;p.once('dialog',d=>d.dismiss());assert.equal(await set({chapterID:22,sceneID:32}),false);assert.equal((await state()).version,version);assert.equal((await state()).sceneID,31);assert.match(p.url(),/Scenes\/Edit\/31/);assert.equal(await p.locator('[data-scene-editor-overlay] textarea').last().inputValue(),'Unsaved draft');
|
|
// A failed authoritative request retains draft and unlocks the editor.
|
|
p.once('dialog',d=>d.accept());await p.route('**/NarrativeFocus/Set',r=>r.fulfill({status:503,json:{message:'Synthetic focus failure'}}));assert.equal(await set({chapterID:22,sceneID:32}),false);assert.equal((await state()).sceneID,31);assert.equal(await p.locator('[data-scene-workspace]').evaluate(n=>n.inert),false);await p.unroute('**/NarrativeFocus/Set');
|
|
p.once('dialog',d=>d.dismiss());await command('next');assert.equal((await state()).version,version,'ribbon cancellation never persists a focus change');
|
|
// Another window can change authoritative focus; retain this draft and offer explicit recovery.
|
|
await p.evaluate(async()=>{const f=window.PlotDirectorNarrativeFocus.state,r=document.querySelector('[data-narrative-focus]');await fetch('/NarrativeFocus/Set',{method:'POST',body:new URLSearchParams({projectId:f.projectID,bookId:f.bookID,version:f.version,chapterId:22,sceneId:32,__RequestVerificationToken:r.querySelector('[name=__RequestVerificationToken]').value})});window.dispatchEvent(new Event('focus'));});
|
|
await p.waitForFunction(()=>window.PlotDirectorNarrativeFocus.state.sceneID===32);assert.match(p.url(),/Scenes\/Edit\/31/);assert.match(await p.locator('[data-focus-workspace-status]').textContent(),/unsaved scene edits are retained/);
|
|
p.once('dialog',d=>d.accept());await go();await assertScene(32);
|
|
// Chapter-only focus navigates to Chapter Details without inventing a scene.
|
|
await set({chapterID:23});await p.waitForURL(/Chapters\/Details\/23/);await p.waitForFunction(()=>window.PlotDirectorNarrativeFocus?.state.chapterID===23);assert.equal((await state()).sceneID,null);
|
|
await p.goto(base+'/Scenes/Edit/101');await assertScene(101);await p.screenshot({path:'/tmp/pd-scene-focus-'+(installed?'pwa':'browser')+'.png'});
|
|
await p.setViewportSize({width:950,height:1050});await p.evaluate(()=>window.PlotDirectorApplyApplicationTheme('modern-dark'));await p.screenshot({path:'/tmp/pd-scene-focus-'+(installed?'pwa':'browser')+'-narrow-dark.png'});
|
|
if(installed){assert(await p.evaluate(()=>matchMedia('(display-mode: standalone)').matches));assert.equal(c.pages().length,2,'all document navigation stays inside installed application');}
|
|
console.log('PASS '+(installed?'installed PWA':'Chromium')+' Scene Edit: whole-document stable-ID follow, chapter boundaries, header/history, stale recovery, aligned no-op, cancel before POST, draft retained on failure, chapter-only fallback and internal navigation.');
|
|
}finally{await c.close();await b?.close();fs.rmSync(profile,{recursive:true,force:true});}
|
|
})().catch(e=>{console.error(e.stack);process.exitCode=1;});
|