PlotDirector/PlotLine.Tests/Browser/writing-context-navigation.cjs

37 lines
6.0 KiB
JavaScript

const {chromium}=require('playwright'),assert=require('node:assert/strict'),path=require('node:path');
const base=process.env.PD_BROWSER_BASE||'http://127.0.0.1:5238';
(async()=>{const browser=await chromium.launch();try{
for(const width of [400,760,1600]) for(const theme of ['modern-light','modern-dark','warm-light','warm-dark']){
const page=await browser.newPage({viewport:{width,height:850}});let commands=0,fullPage=false;
await page.route('**/Writing/Context/Navigate',async route=>{commands++;assert.equal(route.request().method(),'POST');await route.fulfill({json:{message:'Navigation requested in Word. Cursor confirmation is still pending.'}});});
await page.route('**/Writing/Context/Scene?*',async route=>{
if(fullPage){await route.fulfill({contentType:'text/html',body:'<!doctype html><html><body><nav>Timeline shell must never appear here</nav></body></html>'});return;}
const id=new URL(route.request().url()).searchParams.get('sceneId');await new Promise(r=>setTimeout(r,80));
const response=await route.fetch({url:base+'/writing-desk-fixture/context?fragment=true&sceneId='+id});await route.fulfill({response});
});
await page.goto(base+'/writing-desk-fixture/context');
await page.evaluate(theme=>{
document.documentElement.dataset.appTheme=theme;document.documentElement.dataset.theme=theme.endsWith('dark')?'dark':'light';
const root=document.querySelector('main');root.dataset.writingContext='';root.dataset.contextUrl='/Writing/Context/Scene';root.dataset.scopeProjectId='7';root.dataset.scopeBookId='12';
const host=document.createElement('section');host.dataset.writingContextContent='';while(root.firstChild)host.append(root.firstChild);root.append(host);
const status=document.createElement('p');status.dataset.writingContextStatusText='';root.prepend(status);const input=document.createElement('input');input.type='hidden';input.name='__RequestVerificationToken';input.value='fixture';root.append(input);window.originalWritingShell=root;window.focusCalls=0;window.focus=()=>focusCalls++;
},theme);
for(const script of ['story-position','writing-follow','writing-context'])await page.addScriptTag({path:path.resolve(__dirname,'../../PlotLine/wwwroot/js/'+script+'.js')});
assert.equal(await page.locator('iframe').count(),0);
await page.click('[data-writing-context-tab=people]');await page.locator('[data-writing-context-tab=people]').press('ArrowRight');assert.ok(await page.locator('[data-writing-context-panel=relationships]').isVisible());await page.locator('[data-writing-context-tab=relationships]').press('ArrowLeft');await page.locator('[data-writing-context-scene-select]').press('End');
await page.waitForFunction(()=>document.querySelector('[data-writing-context-fragment]').dataset.sceneId==='23');await page.waitForFunction(()=>document.querySelector('[data-writing-context-status-text]').textContent.includes('Navigation requested'));
assert.equal(commands,1);assert.ok(await page.locator('[data-writing-context-panel=people]').isVisible());assert.equal(await page.locator('[data-writing-context-fragment]').getAttribute('data-chapter-id'),'11');
assert.ok(await page.evaluate(()=>originalWritingShell===document.querySelector('[data-writing-context]')));
await page.click('[data-writing-scene-step="22"]');await page.waitForFunction(()=>document.querySelector('[data-writing-context-fragment]').dataset.sceneId==='22');await page.waitForTimeout(30);assert.equal(commands,2);
const beat=async id=>page.evaluate(id=>window.dispatchEvent(new CustomEvent('native-companion-presence',{detail:[{instanceId:'instance',sessionId:'document',bookId:12,chapterId:id===23?11:10,sceneId:id,active:true,wordAvailable:true,wordSaved:true,expires:new Date(Date.now()+45000).toISOString()}]})),id);
await page.evaluate(()=>{const spacer=document.createElement('div');spacer.style.height='1400px';document.body.append(spacer);window.scrollTo(0,150);});await page.waitForFunction(()=>Math.abs(scrollY-150)<=2);
await beat(23);await page.waitForFunction(()=>document.querySelector('[data-writing-context-fragment]').dataset.sceneId==='23');assert.equal(commands,2,'Word follow must not command Word back');assert.ok(Math.abs(await page.evaluate(()=>scrollY)-150)<=2,`Word scene changes retain page scroll where space permits: ${width} ${theme}, actual ${await page.evaluate(()=>scrollY)}`);assert.equal(await page.evaluate(()=>focusCalls),0,'cursor following must not focus the browser window');assert.ok(await page.locator('[data-writing-context-panel=people]').isVisible());
await page.evaluate(()=>window.dispatchEvent(new CustomEvent('native-companion-presence',{detail:[]})));await page.click('[data-writing-scene-step="22"]');await page.waitForFunction(()=>document.querySelector('[data-writing-context-fragment]').dataset.sceneId==='22');
await beat(23);await page.waitForFunction(()=>document.querySelector('[data-writing-context-fragment]').dataset.sceneId==='23');assert.ok(await page.locator('[data-writing-context-panel=people]').isVisible(),'reconnection follows actual Word scene without resetting view');
const before=commands;fullPage=true;await page.locator('[data-writing-context-scene-select]').press('Home');await page.waitForFunction(()=>document.querySelector('[data-writing-context-status-text]').textContent==='Context unavailable');assert.equal(await page.locator('nav').filter({hasText:'Timeline shell must never appear here'}).count(),0);assert.equal(await page.locator('[data-writing-context-fragment]').getAttribute('data-scene-id'),'23');assert.equal(commands,before,'invalid fragments cannot trigger Word navigation');
assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth>innerWidth+2),false,`overflow ${width} ${theme}`);
await page.close();
}
console.log('PASS actual Razor fragments, stable shell, cross-chapter Previous/Next and jump, tab persistence, Word follow without feedback, disconnect/reconnect and full-page response rejection at three widths in four themes');
}finally{await browser.close();}})().catch(e=>{console.error(e);process.exit(1);});