PlotDirector/PlotLine.Tests/Browser/reader-word-freshness.cjs

3 lines
2.0 KiB
JavaScript

const{chromium}=require('playwright'),assert=require('node:assert/strict');
(async()=>{const b=await chromium.launch();try{for(const pwa of[false,true]){const c=await b.newContext();if(pwa)await c.addInitScript(()=>Object.defineProperty(navigator,'standalone',{value:true}));const p=await c.newPage();let stamp='2026-11-16T12:00:00Z',checks=0;await p.route('**/manuscript/reader/changes?*',r=>{checks++;const u=new URL(r.request().url());return r.fulfill({json:{revision:u.searchParams.get('knownRevision'),version:u.searchParams.get('knownVersion'),changed:false,lastSuccessfulSyncUtc:stamp,snapshotCapturedUtc:'2026-11-15T12:00:00Z'}})});await p.goto('http://127.0.0.1:5238/manuscript/reader?bookId=12&chapterId=21&focus=true');await p.locator('#reader-timestamps time[datetime="'+stamp+'"]').waitFor();const row=await p.locator('[data-paragraph-id]').first().elementHandle();await row.evaluate(n=>{const r=document.createRange();r.selectNodeContents(n);getSelection().removeAllRanges();getSelection().addRange(r)});const selected=await p.evaluate(()=>getSelection().toString());const before=checks;stamp='2026-11-16T13:00:00Z';await p.evaluate(()=>window.dispatchEvent(new CustomEvent('native-companion-presence',{detail:[{bookId:12,snapshotRevision:99999}]})));await p.locator('#reader-timestamps time[datetime="'+stamp+'"]').waitFor();assert(checks>before);assert(await row.evaluate(n=>n===document.querySelector('[data-paragraph-id]')));assert.equal(await p.evaluate(()=>getSelection().toString()),selected);assert.equal(await p.locator('.manuscript-reader').getAttribute('data-focus'),'true');const next=p.locator('a[rel=next]');if(await next.count())assert(new URL(await next.getAttribute('href'),'http://fixture').searchParams.get('focus')==='true');await c.close();}console.log('PASS Reader accepted-metadata notification updates timestamps without replacing prose, selected text or Focused Reading; Next preserves focus. Browser/simulated PWA.');}finally{await b.close()}})().catch(e=>{console.error(e.stack);process.exitCode=1});