// Production JS over disposable HTTP shells and real Writer Razor; no manuscript/DB writes. const {chromium}=require('playwright'),assert=require('node:assert/strict'),path=require('node:path'); const base=process.env.PD_BROWSER_FIXTURE_URL||'http://127.0.0.1:5238',assets=path.resolve(__dirname,'../../PlotLine/wwwroot/js'); (async()=>{const browser=await chromium.launch();try{for(const standalone of [false,true]){ const context=await browser.newContext();if(standalone)await context.addInitScript(()=>Object.defineProperty(navigator,'standalone',{value:true})); await context.route('**/lifecycle-*',r=>r.fulfill({contentType:'text/html',body:'
'})); const page=await context.newPage(); const load=async name=>{await page.goto(base+'/lifecycle-'+name);for(const file of ['notifications','word-notifications','word-chapter-status'])await page.addScriptTag({path:assets+'/'+file+'.js'});}; const lease={instanceId:'instance',sessionId:'session',manuscriptId:'manuscript',bookId:12,revision:19,wordAvailable:true,wordSaved:true,chapterIssue:'needs_review'}; const pulse=async value=>page.evaluate(leases=>dispatchEvent(new CustomEvent('writer-word-state',{detail:{bookId:12,leases}})),value); await load('book');await pulse([lease]);assert.equal(await page.locator('.pd-toast--warning').count(),1,'new reading failure warns once'); await page.locator('.pd-toast--warning button').click();await pulse([lease]);assert.equal(await page.locator('.pd-toast').count(),0,'dismissal respected'); await load('writer');await pulse([lease]);assert.equal(await page.locator('.pd-toast').count(),0,'navigation cannot reannounce same incident'); await load('book');await pulse([lease]);assert.equal(await page.locator('.pd-toast').count(),0); await pulse([{...lease,revision:20}]);assert.equal(await page.locator('.pd-toast--warning').count(),1,'new Word revision failure can notify'); await pulse([{...lease,revision:20,paragraphTransfer:{state:'accepted',kind:'chapter'},chapterProgress:{state:'Synchronised'}}]);assert.equal(await page.locator('.pd-toast--warning').count(),1,'sync completion is not resolution of the reading failure'); await pulse([{...lease,revision:20,chapterIssue:''}]);assert.equal(await page.locator('.pd-toast--warning').count(),0,'explicit cleared reading flag resolves warning'); await page.evaluate(()=>{const now=Date.now;Date.now=()=>now()+6000;}); // retain the shared five-second toast deduplication window await pulse([{...lease,revision:20}]);await page.locator('.pd-toast--warning').waitFor(); await pulse([{...lease,chapterIssue:''}]); await pulse([{...lease,bookId:13}]);assert.equal(await page.locator('.pd-toast--warning').count(),0,'unrelated book cannot interrupt current book'); const status=await page.evaluate(lease=>{const packet={canonical:{manuscriptID:lease.manuscriptId},chapterVerifications:[{bookId:12,chapterId:21,instanceId:lease.instanceId,sessionId:lease.sessionId,wordRevision:lease.revision,verification:{manuscriptID:lease.manuscriptId,checkpointStatus:'Synchronised',isCurrent:true,lastAcceptedUtc:'2026-11-15T21:07:38Z',validatedUtc:'2026-11-21T18:47:24Z'}}]};return{verified:PlotDirector.wordChapterStatus(packet,{...lease,chapterIssue:''},12,21),reading:PlotDirector.wordChapterStatus(packet,lease,12,21),other:PlotDirector.wordChapterStatus(packet,lease,12,22),manual:PlotDirector.wordChapterStatus({...packet,chapterVerifications:packet.chapterVerifications.map(e=>({...e,verification:{...e.verification,isCurrent:false}}))},{...lease,chapterProgress:{state:'Synchronised'}},12,21)};},lease); assert.equal(status.verified.label,'Chapter verified');assert(status.verified.text.includes('historical; not a current failure'));assert(status.reading.text.includes('separately reports a chapter-reading failure'));assert.equal(status.other.label,'Verification unavailable');assert.equal(status.manual.label,'Chapter not verified'); // New browser session shares account-local incident receipts only when same device storage is retained. console.log('PASS Word incident lifecycle: new/dismissed/navigation/new-revision/recovery/rearmed/scoped warnings; independent chapter verification and historical acceptance; standalone='+standalone); await context.close(); }}finally{await browser.close();}})().catch(e=>{console.error(e.stack);process.exitCode=1});