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(`

`); await page.evaluate(()=>{ window.invocations=[];window.handlers={}; window.signalR={HubConnectionBuilder:class{ withUrl(){return this;}withAutomaticReconnect(){return this;} build(){return {on(name,handler){handlers[name]=handler;},onreconnecting(){},onreconnected(){},onclose(){},start:async()=>{},invoke:async(name)=>{invocations.push(name);return name==='WatchCompanionPresence'?{isConnected:false}:{status:'Running'};}};} }}; }); await page.addScriptTag({content:fs.readFileSync(path.join(__dirname,'../../PlotLine/wwwroot/js/word-companion-presence.js'),'utf8')}); const label=page.locator('[data-word-companion-compact-status-label]'),button=page.locator('[data-onboarding-scan-start]'); async function state(leases,connectionState='Connected'){await page.evaluate(({leases,connectionState})=>{leases.known=true;leases.connectionState=connectionState;window.dispatchEvent(new CustomEvent('native-companion-presence',{detail:leases}));},{leases,connectionState});} await state([],'Disconnected');await assert.match(await label.textContent(),/disconnected/i);assert.equal(await button.isDisabled(),true); await state([]);assert.match(await label.textContent(),/authenticated.*Word unavailable/);assert.equal(await button.isDisabled(),true); await state([{wordAvailable:false,wordRunning:true}]);assert.match(await label.textContent(),/Word connected.*open a manuscript/);assert.equal(await button.isDisabled(),true); const manuscript={bookId:0,wordAvailable:true,wordRunning:true,canCapture:true,active:true,documentTitle:'Title ',expires:new Date(Date.now()+45000).toISOString()}; await state([manuscript]);assert.match(await label.textContent(),/not yet associated/);assert.equal(await button.isDisabled(),false,'first-time import must not deadlock on missing association');assert.match(await page.locator('[data-word-companion-connected-copy]').textContent(),/only after your request/);assert.equal(await page.locator('script:not([src])').count(),1,'filename must not inject script markup'); await state([{...manuscript,active:false},{...manuscript,bookId:7,active:true}]);assert.equal(await button.isDisabled(),true,'do not substitute inactive unassociated manuscript for a different active book'); await state([{...manuscript,bookId:5}]);assert.match(await label.textContent(),/ready to import/);assert.equal(await button.isDisabled(),false); await state([{...manuscript,bookId:7}]);assert.match(await label.textContent(),/another book/);assert.equal(await button.isDisabled(),true); await state([{...manuscript,expires:new Date(Date.now()-1000).toISOString()}]);assert.equal(await button.isDisabled(),true,'stale document cannot enable import'); await page.evaluate(()=>handlers.WordCompanionPresenceChanged({isConnected:true,documentOpen:true,currentDocumentName:'Legacy Word'}));assert.equal(await button.isDisabled(),false,'connected Office.js workflow remains available when native manuscript is stale');await page.evaluate(()=>handlers.WordCompanionPresenceChanged({isConnected:false})); await state([{...manuscript,bookId:5}]);assert.equal(await page.evaluate(()=>invocations.includes('StartOnboardingManuscriptScan')),false,'presence updates never upload prose');await button.click();await page.waitForFunction(()=>invocations.includes('StartOnboardingManuscriptScan')); await page.evaluate(()=>{handlers.OnboardingScanStateChanged({status:'NotStarted',percentComplete:0});const checkbox=document.createElement('input');checkbox.type='checkbox';checkbox.dataset.paragraphTracking='';checkbox.checked=true;document.body.append(checkbox);}); assert.equal(await page.evaluate(()=>invocations.includes('StartParagraphManuscriptScan')),false,'opting in alone must not upload'); await state([{...manuscript,bookId:5,canParagraphSync:true}]);await button.click();await page.waitForFunction(()=>invocations.includes('StartParagraphManuscriptScan')); console.log('PASS explicit paragraph opt-in chooses the additive hub method; unchanged legacy callers remain compatible'); console.log('PASS browser wizard states: disconnected, authenticated/no Word, Word/no manuscript, unassociated first import, selected book, wrong book, stale lease and explicit-only transfer'); }finally{await browser.close();}})().catch(e=>{console.error(e);process.exit(1)});