PlotDirector/PlotLine.Tests/Browser/word-toast-paths.cjs

43 lines
5.3 KiB
JavaScript

// Real Razor Writer and production scripts; isolated loopback fixture, no database/Word.
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{for(const pwa of [false,true]){
const context=await browser.newContext();if(pwa)await context.addInitScript(()=>Object.defineProperty(navigator,'standalone',{value:true}));
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,canAcknowledgeNavigation:true,canChapterSync:true,canParagraphSync:true,revision:2,snapshotRevision:2,mappingCurrent:true,documentTitle:'Synthetic toast manuscript',expires:new Date(Date.now()+3600000).toISOString()};
let packet={leases:[lease],canonical:{manuscriptID:lease.manuscriptId},verifiedPositions:[]},fail=false,posts=0,requestId='44444444-4444-4444-4444-444444444444';
await context.route('**/native-companion/status*',r=>r.fulfill({json:packet}));
await context.route('**/Writer/Work/State?*',r=>r.fulfill({json:{session:null}}));
await context.route('**/WriterWord/Availability?*',r=>r.fulfill({json:{chapter:true,scene:true,paragraph:true}}));
await context.route('**/manuscript/paragraphs/snapshot',r=>{posts++;return r.fulfill({status:fail?409:200,json:fail?{message:'Synthetic request failed'}:{requestId,message:'Synthetic request queued'}})});
await context.route('**/WriterWord/Navigate',r=>r.fulfill({status:202,json:{navigationId:new URLSearchParams(r.request().postData()).get('navigationId')}}));
await context.route('**/WriterWord/Navigation?*',r=>r.fulfill({json:{...Object.fromEntries(new URL(r.request().url()).searchParams),state:'completed'}}));
const page=await context.newPage(),errors=[];page.on('pageerror',e=>errors.push(e.message));
await page.goto(base+'/Writer/Write?projectId=7&bookId=12&objective=true');
await page.waitForFunction(()=>window.PlotDirector?.wordNotifications&&document.querySelector('[data-word-sync]')?.disabled===false);
await page.locator('[data-word-sync]').evaluate(n=>n.closest('details').open=true);
await page.locator('[data-word-check]').click();assert.equal(await page.locator('.pd-toast').count(),0,'check/progress is silent');
const response=page.waitForResponse(r=>r.url().endsWith('/manuscript/paragraphs/snapshot'));await page.locator('[data-word-sync]').click();await response;
await page.waitForFunction(()=>document.querySelector('[data-word-command]').textContent.includes('requested'));
assert.equal(posts,1);assert.equal(await page.locator('.pd-toast').count(),0,'request acknowledgement is not success');
packet={...packet,leases:[{...lease,paragraphTransfer:{requestId,state:'accepted',kind:'chapter'}}]};
await page.evaluate(v=>dispatchEvent(new CustomEvent('writer-word-state',{detail:v})),packet);
await page.locator('.pd-toast--success').filter({hasText:'Word transfer accepted. Chapter verification is reported separately.'}).waitFor();
fail=true;await page.locator('[data-word-sync]').click();await page.locator('.pd-toast--error').filter({hasText:'Synthetic request failed'}).waitFor();
await page.waitForFunction(()=>document.querySelector('[data-word-navigate]').disabled===false);
await page.locator('[data-word-navigate]').evaluate(n=>n.click());
await page.locator('.pd-toast--success').filter({hasText:'Word confirmed navigation.'}).waitFor();
// Exercise the separate native connection dialog's trusted form submission too.
fail=false;requestId='55555555-5555-5555-5555-555555555555';
await context.route('**/manuscript/paragraphs/snapshot',r=>{posts++;packet={...packet,leases:[{...lease,paragraphTransfer:{requestId,state:'accepted',kind:'chapter',message:'Synthetic native check accepted'}}]};return r.fulfill({json:{requestId,message:'Synthetic native check requested'}})});
await page.locator('#native-companion-status').evaluate(n=>n.click()); // fixture deliberately hides the connection launcher
await page.waitForFunction(()=>document.querySelector('[data-word-sync-button]').disabled===false);
await page.locator('[data-word-sync-button]').click();
await page.locator('[data-word-sync-feedback]').filter({hasText:'Synthetic native check accepted'}).waitFor();
assert(posts>=3,'native dialog submits its existing form');
await page.locator('[data-close-word-details]').click();
assert.equal(await page.locator('[data-word-command][aria-live],[data-word-state][role=status],[data-word-ribbon-status][role=status]').count(),0,'no competing routine live regions');
assert.deepEqual(errors,[]);console.log('PASS actual Writer notification paths: silent check/request, exact accepted manual success, request failure, correlated navigation result, trusted native dialog sync; standalone='+pwa);await context.close();
}}finally{await browser.close();}})().catch(e=>{console.error(e.stack);process.exitCode=1});