PlotDirector/PlotLine.Tests/Browser/context-spatial.cjs

31 lines
17 KiB
JavaScript

// Actual compiled Context fragments and canonical renderers; synthetic fixtures, no database writes.
const {chromium}=require('playwright'),assert=require('node:assert/strict'),fs=require('node:fs');
const base=process.env.PD_BROWSER_BASE||'http://127.0.0.1:5238',out=process.env.PD_SPATIAL_EVIDENCE||'/tmp/pd-spatial-evidence';fs.mkdirSync(out,{recursive:true});
(async()=>{const browser=await chromium.launch();let step='setup';try{const context=await browser.newContext({viewport:{width:1440,height:1000}});const errors=[],requests=[];
await context.route('**/native-companion/**',r=>r.fulfill({json:[]}));await context.route('**/SharedFocus/**',r=>r.fulfill({json:{}}));await context.route('**/Writing/Context/Position*',r=>r.fulfill({json:{connected:false}}));await context.route('**/Writing/Context/MyWindows*',r=>r.fulfill({json:[]}));
await context.route('**/Writing/Context/Tabs/*',r=>{const u=new URL(r.request().url());return r.fulfill({contentType:'text/html',body:`<div data-context-tab-fragment data-project-id="7" data-book-id="12" data-chapter-id="${u.searchParams.get('chapterId')}" data-scene-id="${u.searchParams.get('sceneId')}">Existing Context tab</div>`});});
const page=await context.newPage();page.on('pageerror',e=>errors.push(e.message));page.on('request',r=>{if(r.url().includes('/SpatialContext/'))requests.push(r.url());});await page.goto(base+'/Writing/Context?pinned=true');const tab=i=>`[data-context-pane="${i}"] [data-context-tab]`,pane=i=>page.locator(`[data-context-pane="${i}"]`);const shot=async name=>page.screenshot({path:`${out}/${name}.png`});
step='locations';const t=Date.now();await page.selectOption(tab(0),'locations');await pane(0).getByRole('heading',{name:'Bedroom',exact:true}).waitFor();const load=Date.now()-t;assert.equal(await pane(0).locator('.spatial-scene').count(),3);await shot('01-locations-single');
step='adjacent inspection';await pane(0).locator('[data-scene-position="-1"] button').click();await pane(0).getByRole('heading',{name:'Lounge',exact:true}).waitFor();assert.equal(await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.position.sceneId),22);await pane(0).getByRole('button',{name:'Return to scene location'}).click();
step='maps renderer';await page.selectOption(tab(0),'maps');await pane(0).locator('iframe').waitFor();await page.waitForFunction(()=>document.querySelector('iframe')?.contentWindow?.PlotDirectorMapWorkspace?.document.features.length===2);await page.waitForTimeout(150);const frame=page.frames().find(f=>f.url().includes('/SpatialContext/Map'));assert.equal(await frame.locator('.spatial-map-ring').count(),3);await shot('02-maps-single');await frame.locator('[data-feature-id="21111111-2222-4333-8444-555555555555"]').first().click();await pane(0).getByText('No associated location; inspection does not assign this feature to a scene.',{exact:true}).waitFor();assert.match(await pane(0).locator('[data-spatial-advanced]').getAttribute('href'),/featureId=21111111-2222-4333-8444-555555555555/);assert.equal(await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.position.sceneId),22);await pane(0).locator('[data-spatial-resource]').selectOption('2');await pane(0).getByText('No authoritative representation of the inspected location on this resource.',{exact:true}).waitFor();assert.match(await pane(0).locator('[data-spatial-advanced]').getAttribute('href'),/id=2$/);await pane(0).locator('[data-spatial-resource]').selectOption('1');await pane(0).getByText('Represented through containing location:',{exact:false}).waitFor();
step='floor renderer';await page.selectOption(tab(0),'floorplans');await pane(0).locator('[data-block="51"]').waitFor();assert.equal(await pane(0).locator('.spatial-room-badge').count(),3);await shot('03-floorplans-single');
step='split dedup and coordination';await page.selectOption('[data-context-layout]','columns');await page.selectOption(tab(0),'locations');await page.selectOption(tab(1),'maps');await pane(1).locator('iframe').waitFor();await page.waitForTimeout(200);await shot('04-locations-maps');await pane(0).locator('.spatial-path button').filter({hasText:'The old house'}).click();await pane(0).getByRole('heading',{name:'The old house',exact:true}).waitFor();await pane(1).locator('[data-spatial-resolution]').filter({hasText:'Directly associated'}).waitFor();
step='locations floor';await page.selectOption(tab(1),'floorplans');await pane(1).locator('[data-block="51"]').waitFor();await shot('05-locations-floorplans');await pane(1).locator('[data-block="52"]').click();await pane(0).getByRole('heading',{name:'Lounge',exact:true}).waitFor();
step='metadata refresh retains chosen level and viewport';await pane(1).locator('[data-spatial-floor]').selectOption('42');const oldFloorCanvas=await pane(1).locator('[data-spatial-floor-canvas]').elementHandle(),savedTransform=await pane(1).locator('[data-spatial-floor-world]').evaluate(n=>n.style.transform);await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.invalidate({projectId:7,bookId:12,kind:'locations'}));await page.waitForFunction(node=>!node.isConnected,oldFloorCanvas);assert.equal(await pane(1).locator('[data-spatial-floor]').inputValue(),'42');assert.equal(await pane(1).locator('[data-spatial-floor-world]').evaluate(n=>n.style.transform),savedTransform);await pane(1).locator('[data-spatial-floor]').selectOption('41');
step='maps floor';await page.selectOption(tab(0),'maps');await pane(0).locator('iframe').waitFor();await page.waitForTimeout(100);await shot('06-maps-floorplans');await page.selectOption('[data-context-layout]','rows');await shot('07-stacked');
step='same building retains renderer and viewport';await page.evaluate(()=>{window.spatialFrame=document.querySelector('[data-context-pane="0"] iframe');spatialFrame.contentWindow.PlotDirectorMapWorkspace.setView({x:-250,y:-200,w:900,h:600});document.querySelector('[data-context-workspace]').contextController.setPosition({projectId:7,bookId:12,chapterId:10,sceneId:24});});await pane(0).locator('[data-scene-position="0"] button').filter({hasText:'Lounge'}).waitFor();assert.equal(await page.evaluate(()=>window.spatialFrame===document.querySelector('[data-context-pane="0"] iframe')),true);assert.equal(await page.evaluate(()=>window.spatialFrame.contentWindow.PlotDirectorMapWorkspace.view.w),900);await shot('10-repeated-location');await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.setPosition({projectId:7,bookId:12,chapterId:10,sceneId:22}));await pane(0).locator('[data-scene-position="0"] button').filter({hasText:'Bedroom'}).waitFor();
step='paragraph no loading';const before=requests.length;await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.setPosition({projectId:7,bookId:12,chapterId:10,sceneId:22,paragraphId:'a-paragraph'}));await page.waitForTimeout(100);assert.equal(requests.length,before);
step='active edit stays pinned';await page.selectOption('[data-context-layout]','columns');await page.selectOption(tab(0),'locations');await pane(0).getByRole('button',{name:'Edit description'}).click();await pane(0).locator('textarea').fill('Unsaved author draft');await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.setPosition({projectId:7,bookId:12,chapterId:10,sceneId:23}));assert.equal(await pane(0).locator('textarea').inputValue(),'Unsaved author draft');assert.equal(await pane(0).locator('[data-spatial-context]').getAttribute('data-scene-id'),'22');for(const close of await page.locator('[data-toast-host] button').all())await close.click();await pane(0).getByRole('button',{name:'Cancel',exact:true}).click();await pane(0).getByText('This scene has no assigned location.',{exact:false}).waitFor();await shot('15-unassigned');
step='placement cancellation and rejected write';await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.setPosition({projectId:7,bookId:12,chapterId:10,sceneId:22}));await pane(0).getByRole('heading',{name:'Bedroom',exact:true}).waitFor();await page.selectOption(tab(1),'maps');await pane(1).locator('iframe').waitFor();await page.waitForFunction(()=>document.querySelector('[data-context-pane="1"] iframe')?.contentWindow?.PlotDirectorMapWorkspace?.document.features.length===2);await pane(0).getByRole('button',{name:'Place on map',exact:true}).click();await pane(1).locator('[data-spatial-operation]').filter({hasText:'Place existing location: Bedroom'}).waitFor();await shot('13-map-placement');let attempted;await page.route('**/SpatialContext/Mutate',route=>{attempted=route.request().postDataJSON();return route.fulfill({status:409,json:{message:'Synthetic concurrent change; previous geometry retained.'}});});const mapFrame=page.frames().find(f=>f.url().includes('/SpatialContext/Map'));await mapFrame.locator('#mapsCanvas').click({position:{x:100,y:100}});await pane(1).getByText('Synthetic concurrent change; previous geometry retained.').waitFor();assert.equal(attempted.locationID,103);assert.equal(attempted.sceneID,22);assert.equal(attempted.kind,'map');assert.equal(await mapFrame.evaluate(()=>PlotDirectorMapWorkspace.document.features.length),2);await pane(1).getByRole('button',{name:'Cancel',exact:true}).click();
step='native cross-pane location drop';const dragSource=await pane(0).getByRole('heading',{name:'Bedroom',exact:true}).boundingBox(),dropTarget=await mapFrame.locator('#mapsCanvas').boundingBox();await page.mouse.move(dragSource.x+30,dragSource.y+10);await page.mouse.down();await page.mouse.move(dragSource.x+60,dragSource.y+12,{steps:5});await page.mouse.move(dropTarget.x+100,dropTarget.y+100,{steps:25});await page.mouse.up();await pane(1).getByRole('button',{name:'Confirm',exact:true}).waitFor();await shot('13-native-cross-pane-drop');await pane(1).getByRole('button',{name:'Cancel',exact:true}).click();
step='room association reviewed conflict';await page.selectOption(tab(1),'floorplans');await pane(1).locator('[data-block="52"]').waitFor();await pane(1).locator('[data-block="52"]').click();await page.route('**/LocationWorkspace/Picker?*',route=>route.fulfill({json:{locations:[{id:102,name:'The old house',parent:null,type:'4'},{id:103,name:'Bedroom',parent:102,type:'4'},{id:104,name:'Lounge',parent:102,type:'4'}],types:[]}}));await pane(1).getByRole('button',{name:'Associate existing location',exact:true}).click();await page.locator('dialog [data-picker-id="103"]').waitFor();await page.locator('dialog [data-picker-id="103"] .pd-location-picker-name').click();await page.getByRole('button',{name:'Choose',exact:true}).click();await pane(1).getByRole('button',{name:'Confirm',exact:true}).waitFor();await shot('14-room-association');await pane(1).getByRole('button',{name:'Confirm',exact:true}).click();await pane(1).getByText('Synthetic concurrent change; previous geometry retained.').waitFor();assert.equal(attempted.kind,'room');assert.equal(attempted.locationID,103);assert.equal(attempted.geometryID,'52');assert.equal(attempted.replace,true);assert.equal(await pane(1).locator('[data-block="52"]').getAttribute('data-location-id'),'104');await pane(1).getByRole('button',{name:'Cancel',exact:true}).click();
step='native room drop';const roomSource=await pane(0).getByRole('heading',{name:'Lounge',exact:true}).boundingBox(),roomTarget=await pane(1).locator('[data-block="51"]').boundingBox();await page.mouse.move(roomSource.x+25,roomSource.y+10);await page.mouse.down();await page.mouse.move(roomSource.x+55,roomSource.y+12,{steps:5});await page.mouse.move(roomTarget.x+40,roomTarget.y+40,{steps:25});await page.mouse.up();await pane(1).getByRole('button',{name:'Confirm',exact:true}).waitFor();await shot('14-native-room-drop');await pane(1).getByRole('button',{name:'Cancel',exact:true}).click();
step='unmapped location';await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.setPosition({projectId:7,bookId:12,chapterId:10,sceneId:26}));await pane(0).getByRole('heading',{name:'Remote valley',exact:true}).waitFor();await shot('12-unmapped-location');await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.setPosition({projectId:7,bookId:12,chapterId:10,sceneId:22}));await pane(0).getByRole('heading',{name:'Bedroom',exact:true}).waitFor();
step='unavailable assignment remains recorded';await page.route('**/SpatialContext/State?bookId=12&sceneId=28*',async route=>{const r=await route.fetch();const data=await r.json();data.scope.primaryLocationID=999;data.scenes.find(s=>s.position===0).locationID=999;return route.fulfill({json:data});});await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.setPosition({projectId:7,bookId:12,chapterId:10,sceneId:28}));await pane(0).getByText('Assigned location unavailable',{exact:true}).waitFor();assert(await pane(0).getByText('The assigned or inspected location is unavailable.',{exact:false}).count());await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.setPosition({projectId:7,bookId:12,chapterId:10,sceneId:22}));await pane(0).getByRole('heading',{name:'Bedroom',exact:true}).waitFor();
step='themes';if(await pane(0).getByRole('button',{name:'Return to scene location'}).isEnabled())await pane(0).getByRole('button',{name:'Return to scene location'}).click();await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.setPosition({projectId:7,bookId:12,chapterId:10,sceneId:22}));await pane(0).getByRole('heading',{name:'Bedroom',exact:true}).waitFor();for(const theme of ['plotdirector','modern-light','modern-dark','warm-light','warm-dark','rose-pink','baby-blue']){await page.evaluate(t=>{localStorage.setItem('plotdirector.applicationTheme',t);window.dispatchEvent(new StorageEvent('storage',{key:'plotdirector.applicationTheme',newValue:t}));},theme);await page.waitForTimeout(40);await shot('theme-'+theme);assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth>innerWidth+2),false);}
step='narrow';await page.setViewportSize({width:768,height:900});await shot('16-narrow');assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth>innerWidth+2),false);
step='independent Context windows';const second=await context.newPage();await second.goto(base+'/Writing/Context?pinned=true');await second.selectOption('[data-context-pane="0"] [data-context-tab]','locations');await second.getByRole('heading',{name:'Bedroom',exact:true}).waitFor();await page.setViewportSize({width:1440,height:1000});await pane(0).locator('[data-scene-position="-1"] button').click();await pane(0).getByRole('heading',{name:'Lounge',exact:true}).waitFor();assert.equal(await second.getByRole('heading',{name:'Bedroom',exact:true}).count(),1);assert.notEqual(await second.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.windowId),await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.windowId));await second.close();
step='hidden cache invalidation without hidden requests';await page.selectOption('[data-context-layout]','single');await pane(0).locator('[data-scene-position="0"] button').first().click();await pane(0).getByRole('heading',{name:'Bedroom',exact:true}).waitFor();await page.selectOption(tab(0),'overview');await page.route('**/SpatialContext/State?*',async route=>{const r=await route.fetch(),d=await r.json();for(const l of d.locations)l.description='Fresh hidden location data';await route.fulfill({json:d});});const hiddenReads=requests.length;await page.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.invalidate({projectId:7,bookId:12,kind:'locations'}));await page.waitForTimeout(100);assert.equal(requests.length,hiddenReads);await page.selectOption(tab(0),'locations');await pane(0).getByText('Fresh hidden location data',{exact:true}).waitFor();
step='obsolete base response cannot repopulate cache';const race=await context.newPage();let raceReads=0;await race.route('**/SpatialContext/State?*',async route=>{const n=++raceReads,r=await route.fetch(),d=await r.json();for(const l of d.locations)l.description=n===1?'Obsolete asynchronous data':'Latest asynchronous data';if(n===1)await new Promise(resolve=>setTimeout(resolve,300));await route.fulfill({json:d});});await race.goto(base+'/Writing/Context?pinned=true');const firstRead=race.waitForRequest(r=>r.url().includes('/SpatialContext/State'));await race.selectOption('[data-context-pane="0"] [data-context-tab]','locations');await firstRead;await race.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.invalidate({projectId:7,bookId:12,kind:'locations'}));await race.getByText('Latest asynchronous data',{exact:true}).waitFor();await race.waitForTimeout(350);await race.locator('[data-scene-position="-1"] button').first().click();await race.getByRole('heading',{name:'Lounge',exact:true}).waitFor();await race.locator('[data-scene-position="0"] button').first().click();await race.getByRole('heading',{name:'Bedroom',exact:true}).waitFor();await race.getByText('Latest asynchronous data',{exact:true}).waitFor();await race.close();
assert.deepEqual(errors,[]);fs.writeFileSync(`${out}/results.json`,JSON.stringify({passed:true,locations_initial_ms:load,spatial_requests:requests.length,paragraph_spatial_requests:0,errors},null,2));console.log('PASS spatial Context: actual Razor/renderers, coordinated panes, paragraph stability, pinned edits, seven themes');await browser.close();}catch(e){console.error('FAIL',step,e);await browser.close();process.exitCode=1;}})();