const assert = require('node:assert/strict'); const timings=[]; async function dispatch(target, ctrlKey = false) { return target.evaluate((node, ctrlKey) => { const event = new MouseEvent('contextmenu',{bubbles:true,cancelable:true,clientX:120,clientY:260,ctrlKey});node.dispatchEvent(event);return event.defaultPrevented; },ctrlKey); } const menu = page => page.locator('body > .pd-context-popup').first(); async function checkMode(page, standalone) { const main=page.locator('[data-pwa-page-content]'); assert.equal(await dispatch(main),standalone); if(standalone && await menu(page).count()) { assert(await menu(page).isVisible()); const duration=await page.evaluate(()=>performance.getEntriesByName('pd-context-menu-open').at(-1)?.duration); if(Number.isFinite(duration))timings.push(duration); const rect=await menu(page).boundingBox(), viewport=page.viewportSize(); assert(rect.x>=0&&rect.y>=0&&rect.x+rect.width<=viewport.width&&rect.y+rect.height<=viewport.height); await page.keyboard.press('Escape'); } for(const selector of ['[data-pwa-page-content]','main button','main input:not([type=hidden])','main textarea','main select','main a']) { const target=page.locator(selector).first(); if(await target.count())assert.equal(await dispatch(target,true),false,'Ctrl override'); } assert.equal(await page.locator('body > .pd-context-popup').count(),0); } async function checkObjects(page, base, id) { const scope=`projectId=${id.ProjectID}`; // Use only the dedicated owned import; invocation continues through the actual controls. for(const sceneID of id.SceneIDs.slice(0,2)) { await page.goto(base+`/Timeline?${scope}&bookId=${id.BookID}`); const card=page.locator(`.timeline-scene-card[data-scene-id="${sceneID}"]`); assert(await dispatch(card)); await menu(page).getByRole('menuitem',{name:'Inspect scene',exact:true}).click(); await page.waitForURL(url=>[...url.searchParams].some(([key,value])=>key.toLowerCase()==='selectedsceneid'&&value===String(sceneID))); } for(const [route,kind] of [['Characters','character'],['Locations','location'],['StoryAssets','asset'],['PlotLines','plot line'],['PlotThreads','thread']]) { await page.goto(base+`/${route}?${scope}`); const object=page.locator(`[data-pd-context="${kind}"]`).first(); if(!await object.count())continue; const source=object.locator('a[data-command-operation]').first(), href=await source.getAttribute('href'); await dispatch(object); const action=menu(page).getByRole('menuitem',{name:new RegExp(`^(Open|Edit) ${kind}$`)}).first(); await action.click(); await page.waitForURL(new URL(href,base).href); assert.equal((await page.request.get(page.url())).status(),200); } await page.goto(base+`/Books/Details/${id.BookID}`); const chapter=page.locator('[data-pd-context=chapter]').first(); if(await chapter.count()) { const chapterID=await chapter.getAttribute('data-pd-context-id');await dispatch(chapter);await menu(page).getByRole('menuitem',{name:'Edit',exact:true}).click();await page.waitForURL(base+'/Chapters/Edit/'+chapterID); } await page.goto(base+`/Characters?${scope}`);const character=page.locator('[data-pd-context=character]').first(); await dispatch(character);await menu(page).getByRole('menuitem',{name:'Archive',exact:true}).click(); await page.locator('#plotlineConfirmModal.show').waitFor();await page.locator('#plotlineConfirmModal [data-bs-dismiss=modal]').first().click(); await page.locator('#plotlineConfirmModal').waitFor({state:'hidden'}); await page.goto(base+`/PlotLineWorkspace?${scope}&bookId=${id.BookID}&plotLineId=${id.PlotLineID}`); const callout=page.locator('.plot-map-callout[data-pd-context=scene]').first(); if(await callout.count()) { const sceneID=await callout.getAttribute('data-pd-context-id');await dispatch(callout);await menu(page).getByRole('menuitem',{name:'Inspect scene',exact:true}).click(); await page.locator(`.plot-inspector [data-pd-context-id="${sceneID}"]`).waitFor(); await page.locator('[data-inspector-close]').click(); } await page.locator('[data-map-mode=associate]').click(); const tray=page.locator('[data-associate-scene]').first();if(await tray.count()){ await dispatch(tray);await menu(page).getByRole('menuitem',{name:'Select scene for association',exact:true}).click();assert.equal(await tray.getAttribute('aria-pressed'),'true'); } await page.goto(base+`/Projects/Edit/${id.ProjectID}`);const field=page.locator('textarea[name=Description]'); await field.fill('Context verification text');await field.evaluate(node=>{node.focus();node.setSelectionRange(0,7);}); await dispatch(field);await menu(page).getByRole('menuitem',{name:'Copy',exact:true}).click(); assert.equal(await page.evaluate(()=>navigator.clipboard.readText()),'Context'); await dispatch(field);await menu(page).getByRole('menuitem',{name:'Select All',exact:true}).click(); assert.deepEqual(await field.evaluate(node=>[node.selectionStart,node.selectionEnd]),[0,25]); // Local edit remains unsaved. Text APIs are exercised in the isolated suite too. for(const route of [`/Characters/Details/${id.CharacterIDs[0]}`,`/Scenarios/Timeline/${id.ScenarioID}`,`/RelationshipMap?${scope}`,`/Books/Details/${id.BookID}`]){ await page.goto(base+route);await checkMode(page,true); if(route.startsWith('/Characters/Details/')) { await dispatch(page.locator('[data-pd-context=character]').first()); assert.equal(await menu(page).getByRole('menuitem',{name:'Edit character',exact:true}).count(),1); const pages=menu(page).getByRole('menuitem',{name:'Page actions',exact:true}); if(await pages.count()) { await pages.focus();await page.keyboard.press('ArrowRight');assert.equal(await page.locator('body > .pd-context-popup').nth(1).getByRole('menuitem',{name:'Edit character',exact:true}).count(),0); } await page.keyboard.press('Escape'); } } console.log('Live context-menu timing ' + JSON.stringify({samples:timings.length,meanMs:timings.reduce((a,b)=>a+b,0)/timings.length,maxMs:Math.max(...timings)})); console.log('PASS live context targeting: owned scenes A/B, character/location/asset/line/thread original routes, archive cancellation, map inspector and association selection, real clipboard and detail/sandbox/relationship contexts.'); } module.exports={checkMode,checkObjects,dispatch,menu};