PlotDirector/PlotLine.Tests/Browser/context-presence-fallback.cjs

7 lines
2.0 KiB
JavaScript

const {chromium}=require('playwright'),assert=require('node:assert/strict'),path=require('node:path');
(async()=>{const browser=await chromium.launch();try{const page=await browser.newPage();await page.route('https://fixture.test/',route=>route.fulfill({contentType:'text/html',body:'<body class="app-layout" data-pd-user-id="1"></body>'}));await page.goto('https://fixture.test/');await page.evaluate(()=>{
window.PlotDirectorContextEnvironment=crypto.randomUUID();window.metrics={builds:0,announced:0,ready:0,invalidations:0,registrations:0};const methods=new Map();window.fixtureConnection={state:'Disconnected',on:(name,fn)=>{if(!methods.has(name))methods.set(name,[]);methods.get(name).push(fn);},onreconnected:()=>{},invoke:async name=>{if(name==='RegisterContextWindow')metrics.registrations++;return [];},start:async()=>{fixtureConnection.state='Connected';},deliver:()=>{for(const fn of methods.get('ContextInvalidated')||[])fn({kind:'characters'});}};
window.signalR={HubConnectionBuilder:class{withUrl(){return this;}withAutomaticReconnect(){return this;}build(){metrics.builds++;return fixtureConnection;}}};
document.addEventListener('pd-presence-connection',e=>{metrics.announced++;e.detail.on('ContextInvalidated',()=>metrics.invalidations++);});document.addEventListener('pd-presence-ready',()=>metrics.ready++);
});await page.addScriptTag({path:path.resolve(__dirname,'../../PlotLine/wwwroot/js/context-main-window.js')});await page.evaluate(()=>document.dispatchEvent(new Event('DOMContentLoaded')));await page.waitForFunction(()=>metrics.ready===1);await page.evaluate(()=>fixtureConnection.deliver());assert.deepEqual(await page.evaluate(()=>metrics),{builds:1,announced:1,ready:1,invalidations:1,registrations:1});await page.close();console.log('PASS shared presence fallback: hidden Word control still announces one shared connection, one ready event and one invalidation subscription without duplicate registration');}finally{await browser.close();}})().catch(e=>{console.error(e);process.exitCode=1;});