19 lines
5.2 KiB
JavaScript
19 lines
5.2 KiB
JavaScript
const {chromium}=require('playwright');const fs=require('node:fs');const path=require('node:path');const assert=require('node:assert/strict');
|
|
(async()=>{const browser=await chromium.launch();try{
|
|
const page=await browser.newPage();await page.goto('about:blank');await page.setContent('<main id="native-workspace" data-book="3" data-project="1" data-instance="instance" data-session="session" data-mode="context"><p id="native-current-scene"></p><p id="native-presence-status"></p><select id="native-session"></select><input type="checkbox" id="native-follow"><button id="native-return">Return</button><select id="native-manual-scene"><option value="">Choose</option><option value="33" data-chapter="10">Manual scene</option></select><iframe id="native-story-workspace" hidden></iframe></main>');
|
|
await page.evaluate(()=>{window.polls=[];window.setInterval=f=>{polls.push(f);return 1;};window.live=[{instanceId:'instance',sessionId:'session',bookId:3,chapterId:10,sceneId:20,chapterTitle:'Arrival',sceneTitle:'The letter',documentTitle:'Synthetic manuscript',wordAvailable:true,updated:new Date().toISOString(),expires:new Date(Date.now()+45000).toISOString()}];window.requestCount=0;window.fetch=async()=>(requestCount++,{ok:true,json:async()=>live});window.hubHandlers={};window.signalR={HubConnectionBuilder:class{withUrl(){return this;}withAutomaticReconnect(){return this;}build(){return{on:(name,callback)=>hubHandlers[name]=callback,onreconnected:()=>{},start:async()=>{},stop:async()=>{}};}}};const OriginalURL=URL;window.URL=class extends OriginalURL{constructor(url,base){super(url,base==='null'?'https://fixture.invalid':base);}};});
|
|
await page.addScriptTag({content:fs.readFileSync(path.join(__dirname,'../../PlotLine/wwwroot/js/native-companion-workspace.js'),'utf8')});
|
|
await assert.doesNotReject(()=>page.waitForFunction(()=>document.querySelector('#native-presence-status').textContent.includes('connected')));
|
|
await page.check('#native-follow');await page.waitForFunction(()=>document.querySelector('iframe').src.includes('sceneId=20'));assert.ok((await page.locator('iframe').getAttribute('src')).includes('pinned=true'));assert.match(await page.locator('#native-current-scene').textContent(),/Live from Word.*Arrival.*The letter/);
|
|
await page.evaluate(()=>{document.querySelector('#native-follow').checked=false;live[0].sceneId=21;polls[0]();});await page.waitForTimeout(30);assert.ok((await page.locator('iframe').getAttribute('src')).includes('sceneId=20'),'manual mode does not snap back to Word');
|
|
await page.evaluate(()=>{live=[];polls[0]();});await page.waitForFunction(()=>document.querySelector('#native-presence-status').textContent.includes('disconnected'));assert.equal(await page.isChecked('#native-follow'),false);
|
|
await page.evaluate(()=>{live=[{wordAvailable:false,updated:new Date().toISOString(),expires:new Date(Date.now()+45000).toISOString()}];polls[0]();});await page.waitForFunction(()=>document.querySelector('#native-presence-status').textContent.includes('Word unavailable'));
|
|
await page.evaluate(()=>{window.beforeEvents=requestCount;for(let i=0;i<20;i++)hubHandlers.NativePresenceChanged();});await page.waitForTimeout(350);assert.equal(await page.evaluate(()=>requestCount-beforeEvents),1,'SignalR burst coalesces to one authorised refresh');
|
|
await page.selectOption('#native-manual-scene','33');await page.waitForFunction(()=>document.querySelector('iframe').src.includes('sceneId=33'));await page.click('#native-return');assert.ok((await page.locator('iframe').getAttribute('src')).includes('sceneId=33'),'return without Word preserves manual exploration');
|
|
console.log('PASS native PWA exact-session following, pinned scope, manual pause, disconnection and Word-unavailable states');
|
|
const badge=await browser.newPage();await badge.goto('about:blank');await badge.setContent('<div id="native-companion-status"><span id="native-companion-status-text"></span></div>');await badge.evaluate(()=>{window.tick=null;window.setInterval=f=>(tick=f,1);window.live=[];window.fetch=async()=>({ok:true,json:async()=>live});});
|
|
await badge.addScriptTag({content:fs.readFileSync(path.join(__dirname,'../../PlotLine/wwwroot/js/native-companion-status.js'),'utf8')});assert.equal(await badge.locator('#native-companion-status').getAttribute('data-connected'),null);
|
|
await badge.evaluate(()=>{live=[{bookId:3,instanceId:'a',sessionId:'b',wordAvailable:true,documentTitle:'Title <script>no</script>',updated:new Date().toISOString(),expires:new Date(Date.now()+45000).toISOString()}];tick();});await badge.waitForFunction(()=>document.querySelector('#native-companion-status').dataset.connected==='true');assert.equal(await badge.locator('#native-companion-status script').count(),0);
|
|
await badge.evaluate(()=>{live=[];tick();});await badge.waitForFunction(()=>document.querySelector('#native-companion-status').dataset.connected==='false');assert.equal(await badge.locator('#native-companion-status').getAttribute('data-native-authority'),'true');assert.match(await badge.locator('#native-companion-status-text').textContent(),/disconnected/);console.log('PASS native presence badge never infers connection from associations and clears stale links safely');
|
|
}finally{await browser.close();}})().catch(e=>{console.error(e);process.exit(1)});
|