PlotDirector/PlotLine.Tests/Browser/maps-workspace.cjs

80 lines
8.9 KiB
JavaScript

const {chromium}=require('playwright');
const assert=require('node:assert/strict');
const base=process.env.PD_MAPS_TEST_URL||'http://127.0.0.1:5246';
(async()=>{
const browser=await chromium.launch();
try{
for(const pwa of [false,true]){
const context=await browser.newContext({viewport:{width:1700,height:1100}});
if(pwa)await context.addInitScript(()=>Object.defineProperty(navigator,'standalone',{value:true}));
const page=await context.newPage(),errors=[];page.setDefaultTimeout(10000);page.on('pageerror',e=>errors.push(e.message));
await page.route('**/Maps/Save',r=>{const d=r.request().postDataJSON();d.map.revision++;return r.fulfill({json:d});});
await page.goto(base+'/fixture?page=maps');await page.waitForFunction(()=>!!window.PlotDirectorMapWorkspace);
const canvas=page.locator('#mapsCanvas');
async function sourceClick(id){
const proxy=page.locator(`[data-pwa-context-controls] [data-command-identity="handler:${id}"]`);
if(pwa&&await proxy.count()){
const menu=proxy.locator('xpath=ancestor::details[1]');
if(await menu.count()&&await menu.getAttribute('data-compact')==='true'&&await menu.getAttribute('open')===null)await menu.locator(':scope > summary').click();
await proxy.click();
}else await page.locator('#'+id).click();
}
async function place(type,x,y){await page.locator('#mapsAssetSearch').fill(type);await page.locator(`[data-asset="${type}"]`).click();const b=await canvas.boundingBox();await page.mouse.click(b.x+b.width*x,b.y+b.height*y);}
for(const[type,x,y]of[['Island',.5,.5],['Mountain Range',.55,.32],['Forest',.35,.5],['River',.6,.55],['Town',.35,.65],['Town',.7,.62],['Road',.5,.65]])await place(type,x,y);
assert.equal(await page.locator('.maps-feature').count(),7,'five-minute map workflow');
const f=await page.evaluate(()=>window.PlotDirectorMapWorkspace.document.features);
const towns=f.filter(f=>f.type==='Town'),road=f.find(f=>f.type==='Road');
const island=f.find(f=>f.type==='Island');
await page.evaluate(id=>window.PlotDirectorMapWorkspace.select(id),island.featureID);
await page.locator('[data-field="width"]').fill('480');await page.locator('[data-field="width"]').blur();
assert.equal(await page.evaluate(id=>window.PlotDirectorMapWorkspace.document.features.find(f=>f.featureID===id).width,island.featureID),480);
await page.locator('[data-action="shape"]').click();assert.equal(await page.locator('[data-handle="point"]').count(),8);
const point=await page.locator('[data-handle="point"]').first().boundingBox(),geometryBefore=await page.evaluate(id=>window.PlotDirectorMapWorkspace.document.features.find(f=>f.featureID===id).geometryJson,island.featureID);
await page.mouse.move(point.x+point.width/2,point.y+point.height/2);await page.mouse.down();await page.mouse.move(point.x+35,point.y+10,{steps:4});await page.mouse.up();
assert.notEqual(await page.evaluate(id=>window.PlotDirectorMapWorkspace.document.features.find(f=>f.featureID===id).geometryJson,island.featureID),geometryBefore,'simple shape points are editable');
const canvasBounds=await canvas.boundingBox();
for(const piece of f){await page.evaluate(id=>window.PlotDirectorMapWorkspace.select(id),piece.featureID);const current=await canvas.boundingBox();assert.deepEqual(current,canvasBounds,'selection does not shift the workspace');}
await page.evaluate(id=>window.PlotDirectorMapWorkspace.select(id),road.featureID);
await page.locator('[data-field="startFeatureID"]').selectOption(towns[0].featureID);await page.locator('[data-field="endFeatureID"]').selectOption(towns[1].featureID);
const roadBefore=await page.locator(`[data-feature-id="${road.featureID}"] .map-shape`).getAttribute('d');
await page.evaluate(id=>window.PlotDirectorMapWorkspace.select(id),towns[0].featureID);
await page.locator('[data-feature-id="'+towns[0].featureID+'"]').focus();await page.keyboard.press('ArrowRight');
assert.notEqual(await page.locator(`[data-feature-id="${road.featureID}"] .map-shape`).getAttribute('d'),roadBefore,'roads follow moved places');
await page.locator('[data-action="relationship"]').click();
const dialog=page.locator('dialog[open]');await dialog.locator('[name="targetID"]').selectOption(towns[1].featureID);await dialog.locator('[name="direction"]').selectOption('NW');await dialog.locator('[name="relativeDistance"]').selectOption('Far');await dialog.getByRole('button',{name:'Keep relationship'}).click();
const before=await page.evaluate(()=>JSON.stringify(window.PlotDirectorMapWorkspace.document.features));
await sourceClick('mapsArrange');assert(await page.locator('#mapsArrangeDialog').isVisible());await sourceClick('mapsArrangeCancel');
assert.equal(await page.evaluate(()=>JSON.stringify(window.PlotDirectorMapWorkspace.document.features)),before,'cancel preserves positions');
await sourceClick('mapsArrange');await sourceClick('mapsArrangeApply');assert.notEqual(await page.evaluate(()=>JSON.stringify(window.PlotDirectorMapWorkspace.document.features)),before);
await sourceClick('mapsUndo');assert.equal(await page.evaluate(()=>JSON.stringify(window.PlotDirectorMapWorkspace.document.features)),before,'undo arrangement');
await sourceClick('mapsRedo');
// Existing Location placement retains its canonical ID and removes it from the tray.
await page.locator('[data-location="11"]').click();let box=await canvas.boundingBox();await page.mouse.click(box.x+box.width*.5,box.y+box.height*.8);assert.equal(await page.locator('[data-location="11"]').count(),0);
assert(await page.evaluate(()=>window.PlotDirectorMapWorkspace.document.features.some(f=>f.locationID===11&&!f.createLocation)));
const bounds=await canvas.boundingBox();
await page.evaluate(()=>document.querySelector('button[data-mode="explore"]').click());assert(await page.locator('#mapsLibrary').isHidden());assert(await page.locator('[data-action="remove"]').count()===0);
const find=page.locator('#mapsFind');
if(pwa){await page.locator('[data-pwa-context-controls] input[aria-label="Find on map"]').fill('Harbour');}
else await find.fill('Harbour');
await page.locator('[data-search-feature]').click();assert.equal(await page.locator('.maps-feature.is-selected').count(),1);
await page.evaluate(async()=>{window.PlotDirectorMapWorkspace.setMode('create');await window.PlotDirectorMapWorkspace.save();});
await page.evaluate(()=>document.querySelector('button[data-mode="story"]').click());assert(await page.locator('#mapsStory').isVisible());
await page.evaluate(()=>document.querySelector('button[data-mode="create"]').click());
{await sourceClick('mapsMaximise');assert(await page.locator('#mapsApp').evaluate(el=>el.classList.contains('maps-maximised')));await sourceClick('mapsMaximise');}
// Pan is stable under repeated pointer moves; zoom and selection do not replace the SVG.
const same=await page.evaluate(()=>{window.__mapCanvas=document.getElementById('mapsCanvas');return true;});
await sourceClick('mapsZoomIn');await sourceClick('mapsZoomOut');assert(await page.evaluate(()=>window.__mapCanvas===document.getElementById('mapsCanvas')));
await page.screenshot({path:`/tmp/maps-${pwa?'pwa':'web'}.png`});
const established=await page.evaluate(()=>window.PlotDirectorMapWorkspace.document.features.map(f=>({id:f.featureID,x:f.x,y:f.y})));
await sourceClick('mapsDefine');const definition=page.locator('dialog[open]');await definition.locator('[name=name]').fill('New northern village');await definition.locator('[name=type]').selectOption('Village');await definition.locator('[name=targetID]').selectOption(towns[0].featureID);await definition.locator('[name=direction]').selectOption('NE');await definition.getByRole('button',{name:'Add place',exact:true}).click();
assert(await page.evaluate(previous=>{const d=window.PlotDirectorMapWorkspace.document,f=d.features.at(-1),target=d.features.find(x=>x.featureID===d.relationships.at(-1).targetID);return f.name==='New northern village'&&window.PlotDirectorMaps.suggestedDirection(f,target)==='NE'&&previous.every(p=>{const f=d.features.find(f=>f.featureID===p.id);return f.x===p.x&&f.y===p.y;});},established),'relationship-first placement moves only the newly defined place');
const scale=await page.evaluate(()=>{const w=window.PlotDirectorMapWorkspace;for(let i=0;i<500;i++)w.document.features.push(window.PlotDirectorMaps.makeFeature('Village',(i%25)*110,Math.floor(i/25)*100,crypto.randomUUID()));const start=performance.now();w.fit();return {ms:performance.now()-start,count:document.querySelectorAll('.maps-feature').length};});
assert(scale.count>500&&scale.ms<3000,'substantial maps render practically');
assert.deepEqual(errors,[]);console.log(`PASS Maps ${pwa?'PWA':'web'}: 7-piece author workflow, Location tray, relationships, arrangement preview/cancel/apply/undo, attached roads, Explore search, Story boundary, stable canvas and maximise`);
await context.close();
}
}finally{await browser.close();}
})().catch(e=>{console.error(e);process.exitCode=1;});