PlotDirector/PlotLine.Tests/Browser/writer-goal-navigation.cjs

63 lines
5.4 KiB
JavaScript

const {chromium}=require('playwright');
const fs=require('node:fs');
const assert=require('node:assert/strict');
const base='http://127.0.0.1:5238';
(async()=>{const browser=await chromium.launch();try{
for(const pwa of [false,true]){
const context=await browser.newContext({viewport:{width:1500,height:1000}});
if(pwa)await context.addInitScript(()=>Object.defineProperty(navigator,'standalone',{value:true}));
await context.route('**/signalr.min.js',route=>route.fulfill({contentType:'application/javascript',body:fs.readFileSync(require.resolve('@microsoft/signalr/dist/browser/signalr.min.js'))}));
const page=await context.newPage(),errors=[];page.on('pageerror',error=>errors.push(error.message));
const goal=page.locator('[name=GoalSummary]');
const modal=page.locator('#pdApplicationDialog');
const guarded=()=>page.evaluate(()=>{const event=new Event('beforeunload',{cancelable:true});window.dispatchEvent(event);return event.defaultPrevented;});
const leave=async()=>{await page.getByRole('tab',{name:'Project',exact:true}).click();await page.getByRole('link',{name:'Writer',exact:true}).first().click();await modal.waitFor();};
await page.goto(base+'/Writer/Chapter/21');
const original=await goal.inputValue();
const draft=original+' — “Δοκιμή / 日本語”';
await goal.fill(draft);
assert(await guarded(),'Unsaved chapter intent protects browser unload');
if(pwa){
const help=page.locator('[data-pwa-context-controls] a[data-help-open]');
const menu=help.locator('xpath=ancestor::details[@data-compact="true"]').last();
if(await menu.count()&&!(await menu.evaluate(node=>node.open)))await menu.locator(':scope > summary').click();
await help.click();
assert.equal(await modal.count(),0,'In-page Help is not a navigation away from the draft');
assert(await page.locator('[data-help-layout]').evaluate(node=>node.classList.contains('help-open')));
await page.locator('[data-help-close]').click();
assert.equal(await goal.inputValue(),draft);
}
await leave();await modal.getByRole('button',{name:'Keep editing',exact:true}).click();await modal.waitFor({state:'hidden'});
assert.equal(new URL(page.url()).pathname,'/Writer/Chapter/21');assert.equal(await goal.inputValue(),draft);
await page.getByRole('button',{name:'Save chapter goals',exact:true}).click();
await page.waitForFunction(()=>document.querySelector('[data-unsaved-form] [role=status]')?.textContent==='Saved');
assert.equal(await guarded(),false,'Only acknowledged saves clear protection');
const check=await context.newPage();await check.goto(base+'/Writer/Chapter/21');assert.equal(await check.locator('[name=GoalSummary]').inputValue(),draft,'Actual controller/service boundary receives Unicode content');await check.close();
await goal.fill(draft+' discarded');await leave();await modal.getByRole('button',{name:'Discard and leave',exact:true}).click();await page.waitForURL(url=>url.pathname==='/Writer');
await page.goto(base+'/Writer/Chapter/21');assert.equal(await goal.inputValue(),draft,'Discard never writes the draft');
const savedDraft=draft+' saved on navigation';await goal.fill(savedDraft);await leave();await modal.getByRole('button',{name:'Save and leave',exact:true}).click();await page.waitForURL(url=>url.pathname==='/Writer');
await page.goto(base+'/Writer/Chapter/21');assert.equal(await goal.inputValue(),savedDraft);
let reject=true;
await page.route('**/Writer/SaveChapterGoal',route=>reject?(reject=false,route.fulfill({status:503,json:{message:'Synthetic save failure'}})):route.continue());
const retryDraft=savedDraft+' retry';await goal.fill(retryDraft);await leave();await modal.getByRole('button',{name:'Save and leave',exact:true}).click();
await page.waitForFunction(()=>document.querySelector('[data-dialog-error]')?.textContent==='Synthetic save failure');
assert.equal(new URL(page.url()).pathname,'/Writer/Chapter/21');assert.equal(await goal.inputValue(),retryDraft);assert(await guarded());
await modal.getByRole('button',{name:'Save and leave',exact:true}).click();await page.waitForURL(url=>url.pathname==='/Writer');
await page.goto(base+'/Writer/Chapter/21');assert.equal(await goal.inputValue(),retryDraft);
let release;
const held=new Promise(resolve=>release=resolve);let first=true;
await page.route('**/Writer/SaveChapterGoal',async route=>{if(first){first=false;await held;}await route.continue();});
await goal.fill(retryDraft+' older request');
const started=page.waitForRequest('**/Writer/SaveChapterGoal');
await page.getByRole('button',{name:'Save chapter goals',exact:true}).click();await started;
const newer=retryDraft+' newer edit';await goal.fill(newer);release();
await page.waitForFunction(()=>document.querySelector('[data-unsaved-form] [role=status]')?.textContent.includes('newer edits'));
assert(await guarded(),'An older acknowledgement does not clear newer text changes');
await page.getByRole('button',{name:'Save chapter goals',exact:true}).click();
await page.waitForFunction(()=>document.querySelector('[data-unsaved-form] [role=status]')?.textContent==='Saved');
assert.equal(await guarded(),false);
assert.deepEqual(errors,[]);await context.close();
}
console.log('PASS Writer chapter-goal unsaved guard, cancelled/discarded navigation, real antiforgery/controller saves, Unicode persistence, save-and-leave failure/retry and concurrent edits in browser/PWA');
}finally{await browser.close();}})().catch(error=>{console.error(error);process.exitCode=1;});