PlotDirector/PlotLine.Tests/Browser/word-ribbon-verification.cjs

21 lines
4.4 KiB
JavaScript

const {chromium}=require('playwright'),assert=require('node:assert/strict');
const base=process.env.PD_BROWSER_FIXTURE_URL||'http://127.0.0.1:5238';
const hub=`window.signalR={HubConnectionBuilder:class{withUrl(){return this}withAutomaticReconnect(){return this}build(){return{state:'Connected',on:()=>{},onreconnected:()=>{},onreconnecting:()=>{},onclose:()=>{},start:async()=>{},stop:async()=>{},invoke:async()=>({windows:[],isConnected:false})}}}};`;
(async()=>{const browser=await chromium.launch();try{const context=await browser.newContext();await context.route('**/signalr.min.js',r=>r.fulfill({contentType:'application/javascript',body:hub}));
const lease={instanceId:'11111111-1111-1111-1111-111111111111',sessionId:'22222222-2222-2222-2222-222222222222',manuscriptId:'33333333-3333-3333-3333-333333333333',bookId:12,wordAvailable:true,wordSaved:true,canChapterSync:true,canParagraphSync:true,revision:19,chapterIssue:'',expires:new Date(Date.now()+3600000).toISOString()};
let chapterState='Needs review',proof=false,reading=false,progress={state:'Synchronised'},packet;const errors=[];
await context.route('**/native-companion/status*',r=>{const chapter=Number(new URL(r.request().url()).searchParams.get('writerChapterId'))||21;packet={bookId:12,chapterId:chapter,leases:[{...lease,chapterIssue:reading?'needs_review':'',chapterProgress:progress}],canonical:{manuscriptID:lease.manuscriptId,chapterAcceptedUtc:'2026-11-15T21:07:38Z'},verifiedPositions:[],chapterVerifications:[{bookId:12,chapterId:chapter,instanceId:lease.instanceId,sessionId:lease.sessionId,wordRevision:19,verification:{manuscriptID:lease.manuscriptId,checkpointStatus:chapter===21?'Synchronised':chapterState,checkpointMessage:'Synthetic checkpoint review required',lastAcceptedUtc:'2026-11-15T21:07:38Z',validatedUtc:chapter===21||proof?'2026-11-21T18:47:24Z':null,isCurrent:chapter===21||proof}}]};return r.fulfill({json:packet});});
await context.route('**/WriterWord/Availability?*',r=>r.fulfill({json:{chapter:false,scene:false,paragraph:false}}));await context.route('**/Writer/Work/State?*',r=>r.fulfill({json:{session:null}}));
const page=await context.newPage();page.on('pageerror',e=>errors.push(e.message));await page.goto(base+'/Writer/Write?projectId=7&bookId=12&objective=true');
const label=page.locator('[data-word-ribbon-status]');await label.filter({hasText:'Chapter verified'}).waitFor();assert((await label.getAttribute('title')).includes('historical; not a current failure'));
const select=chapter=>page.evaluate(chapter=>dispatchEvent(new CustomEvent('plotdirector:narrative-focus-changed',{detail:{bookID:12,chapterID:chapter}})),chapter);
await select(22);await label.filter({hasText:'Chapter needs review'}).waitFor();
chapterState='Synchronised';await page.evaluate(()=>dispatchEvent(new Event('plotdirector:refresh-word-status')));await label.filter({hasText:'Chapter not verified'}).waitFor();
await page.evaluate(v=>dispatchEvent(new CustomEvent('writer-word-state',{detail:{...v,leases:v.leases.map(l=>({...l,paragraphTransfer:{kind:'chapter',state:'accepted',requestId:'manual-sync'}}))}})),packet);assert.equal(await label.textContent(),'Chapter not verified','manual sync transfer is not current-chapter proof');
proof=true;await page.evaluate(()=>dispatchEvent(new Event('plotdirector:refresh-word-status')));await label.filter({hasText:'Chapter verified'}).waitFor();await select(21);await label.filter({hasText:'Chapter verified'}).waitFor();reading=true;progress=null;await page.evaluate(()=>dispatchEvent(new Event('plotdirector:refresh-word-status')));
await page.locator('#native-companion-status').evaluate(n=>n.click());await page.locator('[data-word-sync-feedback]').filter({hasText:'Companion reports a chapter-reading failure'}).waitFor();
reading=false;await page.evaluate(()=>dispatchEvent(new Event('plotdirector:refresh-word-status')));await page.locator('[data-word-sync-feedback]').filter({hasText:'No chapter-reading failure is currently reported'}).waitFor();assert.equal(await page.locator('.pd-toast--warning').count(),0,'resolved reading warning is removed while verification remains independent');
assert.deepEqual(errors,[]);
console.log('PASS actual Writer ribbon: current selected chapter, historical accepted checkpoint vs live verification, genuine review, no false success from manual-sync acceptance, verified recovery.');await context.close();
}finally{await browser.close();}})().catch(e=>{console.error(e.stack);process.exitCode=1});