PlotDirector/PlotLine.Tests/Browser/writer-workspace.cjs

55 lines
5.2 KiB
JavaScript

// Exercise the retained reference-window/presence workflow through the Writer 2 layout.
const {chromium}=require('playwright');
const assert=require('node:assert/strict');
const fs=require('node:fs');
const base='http://127.0.0.1:5238';
(async()=>{const browser=await chromium.launch();try{
for(const pwa of [false,true]){
const context=await browser.newContext({viewport:{width:1600,height:1000}});
if(pwa)await context.addInitScript(()=>Object.defineProperty(navigator,'standalone',{value:true}));
await context.route('**/signalr.min.js',route=>route.fulfill({contentType:'application/javascript',body:fs.readFileSync(require.resolve('@microsoft/signalr/dist/browser/signalr.min.js'),'utf8')}));
const page=await context.newPage();const errors=[];page.on('pageerror',e=>errors.push(e.message));
let negotiations=0,allowConnection=false;
await page.route('**/native-companion/status',r=>r.fulfill({json:[]}));
await page.route('**/native-companion/connection-state',r=>r.fulfill({json:{state:'Disconnected',known:false}}));
await page.route('**/hubs/word-companion-follow/negotiate?*',route=>{negotiations++;return allowConnection?route.continue():route.fulfill({status:503,body:'Transient fixture outage'});});
await page.goto(base+'/fixture?page=writer&layout=full');
// The independent native status can say Disconnected while reference presence is unavailable.
// Fail initial registrations consistently, regardless of subscriber startup order.
await page.waitForFunction(()=>document.querySelector('[data-writing-workspace-context]').textContent==='Unavailable');
const initialNegotiations=negotiations;assert(initialNegotiations>0);
await page.waitForTimeout(250);assert.equal(negotiations,initialNegotiations,'No tight retry loop after initial network failure');
allowConnection=true;
await page.locator('#display-setup > summary').click();
assert.equal(await page.locator('[data-start-writing-workspace]').isVisible(),false,'A disconnected book has no linked-Word controls');
await page.evaluate(()=>{window.__open=window.open;window.open=()=>null;});
await page.locator('[data-open-writing-context]').click();
await page.waitForFunction(()=>document.querySelector('[data-writing-workspace-message]').textContent.includes('blocked'));
assert(negotiations>initialNegotiations,'Manual reference-window command retries connection');
await page.evaluate(()=>window.open=window.__open);
const windows=[];
for(const role of ['context','intent']){
const opened=context.waitForEvent('page');await page.locator(`[data-open-writing-${role}]`).click();const popup=await opened;await popup.waitForLoadState();
await popup.waitForFunction(()=>!!window.PlotDirectorWritingWorkspace);windows.push(popup);
}
await page.waitForFunction(()=>document.querySelector('[data-writing-workspace-context]').textContent==='Open'&&document.querySelector('[data-writing-workspace-intent]').textContent==='Open');
const identities=await Promise.all(context.pages().map(tab=>tab.evaluate(()=>window.PlotDirectorWritingWorkspace.getBrowserWindowId())));
assert.equal(new Set(identities).size,3,'Each window owns a distinct presence identity');
await page.locator('[data-focus-writing-context]').click();await page.locator('[data-focus-writing-intent]').click();assert.equal(context.pages().length,3,'Focus reuses existing windows');
await page.evaluate(()=>document.dispatchEvent(new CustomEvent('plotdirector:story-position-changed',{detail:{projectId:7,bookId:13}})));
assert.equal(await page.locator('[data-writing-workspace-book]').textContent(),'Book 12','Word activity in another book cannot retarget the selected dashboard');
await page.evaluate(()=>Object.defineProperty(window,'getScreenDetails',{value:undefined,configurable:true}));
await page.locator('[data-detect-writing-displays]').click();await page.waitForFunction(()=>!document.querySelector('[data-detect-writing-displays]').disabled);
assert.match(await page.locator('[data-writing-workspace-message]').textContent(),/unavailable/);
await page.evaluate(()=>Object.defineProperty(window,'getScreenDetails',{value:async()=>{throw new DOMException('denied','NotAllowedError');},configurable:true}));
await page.locator('[data-detect-writing-displays]').click();await page.waitForFunction(()=>!document.querySelector('[data-detect-writing-displays]').disabled);
assert.match(await page.locator('[data-writing-workspace-message]').textContent(),/denied/);
for(const popup of windows)await popup.close();
await page.waitForFunction(()=>document.querySelector('[data-writing-workspace-context]').textContent==='Not open');
const response=page.waitForResponse(r=>r.request().isNavigationRequest()&&new URL(r.url()).pathname==='/Writer/WritingPlans');
await page.locator('#ribbon-panel-writer').getByRole('link',{name:'Writing Plan',exact:true}).click();assert.equal((await response).status(),200,'Existing plans remain reachable through the ribbon');
assert.deepEqual(errors,[]);await context.close();
}
console.log('PASS Writer reference windows: initial failure/retry, window identity/reuse, pinned book, denied/unavailable display permissions, plan navigation, browser/PWA.');
}finally{await browser.close();}})().catch(e=>{console.error(e);process.exitCode=1;});