PlotDirector/PlotLine.Tests/Browser/native-context-launch.cjs

44 lines
3.3 KiB
JavaScript

// Isolated production launch asset: no account, database, native process or manuscript.
const {chromium}=require('playwright');
const fs=require('node:fs'),path=require('node:path'),assert=require('node:assert/strict');
const script=fs.readFileSync(path.join(__dirname,'../../PlotLine/wwwroot/js/native-context-launch.js'),'utf8');
const statusScript=fs.readFileSync(path.join(__dirname,'../../PlotLine/wwwroot/js/native-companion-status.js'),'utf8');
(async()=>{
const browser=await chromium.launch();
try{
for(const outcome of ['open','reused','pending','failure','no-storage','no-titlebar']){
const context=await browser.newContext(),page=await context.newPage();
await page.route('https://fixture.invalid/**',route=>route.fulfill({contentType:'text/html',body:'<p>Context destination</p>'}));
await page.goto('https://fixture.invalid/launch');
await page.setContent('<main data-native-context-launch data-book="12" data-project="3" data-instance="instance" data-session="session"><p data-context-launch-status></p><button data-context-launch-retry hidden>Retry</button></main>');
await page.evaluate(outcome=>{
window.calls=[];window.closedByLaunch=false;window.close=()=>{window.closedByLaunch=true;};
window.PlotDirectorContextEnvironment=outcome==='no-storage'?null:'browser-profile';
const connection={state:'Connected',on:()=>{},onreconnected:()=>{},start:async()=>{},stop:async()=>{},invoke:async(name,...args)=>{
window.calls.push([name,...args]);if(name==='OpenContextWindow'){if(outcome==='failure')throw new Error('offline');return outcome==='no-titlebar'?'open':outcome;}
}};
if(outcome==='no-titlebar'){
window.fetch=async()=>({ok:true,json:async()=>[]});
window.signalR={HubConnectionBuilder:class{withUrl(){return this;}withAutomaticReconnect(){return this;}build(){return connection;}}};
}else window.PlotDirectorPresenceConnection=connection;
},outcome);
if(outcome==='no-titlebar')await page.addScriptTag({content:statusScript});
await page.addScriptTag({content:script});
if(outcome==='open'||outcome==='no-titlebar'){
await page.waitForURL('**/Writing/Context?**');const url=new URL(page.url());
assert.equal(url.searchParams.get('bookId'),'12');assert.equal(url.searchParams.get('sessionId'),'session');
assert.equal(url.searchParams.has('sceneId'),false,'opening cannot force a scene selection');
}else{
await page.waitForFunction(()=>window.closedByLaunch||document.querySelector('[data-context-launch-status]').textContent.length>0);
const state=await page.evaluate(()=>({calls:window.calls,closed:window.closedByLaunch,retry:!document.querySelector('button').hidden,text:document.querySelector('p').textContent}));
assert.equal(state.closed,['reused','pending'].includes(outcome));
assert.equal(state.calls.some(c=>/Publish|Resolve|Navigate/.test(c[0])),false,'opening never publishes or redirects manuscript focus');
assert.equal(state.retry,outcome==='failure');
if(outcome==='no-storage')assert.equal(state.calls.length,0,'missing environment cannot target another device');
if(outcome==='reused')assert.match(state.text,/layout, tools, Hold and position were retained/);
}
console.log('PASS isolated native Context launch: '+outcome);await context.close();
}
}finally{await browser.close();}
})().catch(error=>{console.error(error);process.exitCode=1;});