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

74 lines
6.5 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:5238';
(async()=>{
const browser=await chromium.launch();
try{
for(const pwa of [false,true])for(const theme of ['modern-light','modern-dark']){
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.on('pageerror',e=>errors.push(e.message));
let saves=0,lookupRequests=0;
await page.route('**/Maps/Save',r=>{saves++;const d=r.request().postDataJSON();d.map.revision++;return r.fulfill({json:d});});
const states=[[11,52.486,-1.89],[12,52.391,-2.016]].map(([id,lat,lon])=>({locationID:id,status:'Resolved',matchJson:JSON.stringify({Latitude:lat,Longitude:lon,Attribution:'Synthetic fixture; no provider calls'})}));
states.push({locationID:13,status:'Needs confirmation'});
await page.route('**/Geography/RequestLocations',r=>{lookupRequests++;const data=new URLSearchParams(r.request().postData());assert.deepEqual(data.getAll('locationIds'),['12']);states[1].jobState='Queued';return r.fulfill({json:{message:'1 place queued'}});});
await page.route('**/Geography/State?*',r=>r.fulfill({json:states}));
await page.goto(base+'/fixture?page=maps&theme='+theme);await page.waitForFunction(()=>!!window.PlotDirectorMapWorkspace);
await page.evaluate(()=>{
const W=window.PlotDirectorMapWorkspace;
W.edit(()=>{
for(const [id,name,lock]of [[11,'Birmingham',false],[12,'Rubery',false],[13,'Newtown',false],[14,'Locked place',true]])W.document.features.push({featureID:crypto.randomUUID(),name,type:'Town',geometryClass:'Marker',layer:'Places',x:id*5,y:id*7,width:48,height:48,rotation:0,zOrder:1,isPlaced:true,isProvisional:false,isLocked:lock,showLabel:true,labelX:0,labelY:0,locationID:id,geometryJson:'{}'});
W.document.features.push({featureID:crypto.randomUUID(),name:'Drawing',type:'Island',geometryClass:'Area',layer:'Land',x:500,y:500,width:200,height:100,rotation:0,zOrder:0,isPlaced:true,isLocked:false,geometryJson:'{}'});
});
});
const read=()=>page.evaluate(()=>JSON.stringify(window.PlotDirectorMapWorkspace.document.features));
const before=await read();
async function command(id){
if(pwa&&id==='mapsGeographic'){
const proxy=page.locator('[data-pwa-context-controls] [data-command-identity="handler:mapsGeographic"]');
assert.equal(await proxy.count(),1,'geographical action is available in the PWA ribbon');
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.evaluate(id=>document.getElementById(id).click(),id);
}
states[1].status='Pending';states[1].jobState='Awaiting hierarchy request';
await command('mapsGeographic');await page.locator('#mapsGeographicDialog').waitFor({state:'visible'});
assert(await page.locator('#mapsGeographicApply').isDisabled(),'one location must not be an applicable arrangement');
assert.match(await page.locator('#mapsGeographicMessage').innerText(),/At least two/);
assert.match(await page.locator('#mapsGeographicSummary').innerText(),/Rubery — Not requested/);
assert(await page.locator('#mapsGeographicPreview svg').isVisible(),'proposal is visible inside the dialog');
assert.equal(await page.locator('#mapsGeographicPreview [data-feature-id]').count(),0,'preview clone is not editable or selectable');
assert.equal(await read(),before);
await command('mapsGeographicRequest');await page.waitForFunction(()=>document.getElementById('mapsGeographicRequest').hidden);
assert.equal(lookupRequests,1);assert.match(await page.locator('#mapsGeographicSummary').innerText(),/Rubery — Queued/);
states[1].status='Resolved';await command('mapsGeographicRefresh');await page.waitForFunction(()=>!document.getElementById('mapsGeographicApply').disabled);
await command('mapsGeographicCancel');assert.equal(await read(),before);
await command('mapsGeographic');await page.locator('#mapsGeographicDialog').waitFor({state:'visible'});
assert.equal(await read(),before,'preview never mutates saved/working document');
assert.match(await page.locator('#mapsGeographicSummary').innerText(),/Newtown — Needs confirmation/);
assert.match(await page.locator('#mapsGeographicSummary').innerText(),/Locked place — Locked/);
await command('mapsGeographicCancel');assert.equal(await read(),before);assert.equal(saves,0);
await command('mapsGeographic');await page.locator('#mapsGeographicDialog').waitFor({state:'visible'});await page.keyboard.press('Escape');
assert.equal(await page.locator('#mapsGeographicDialog').isVisible(),false,'Escape cancels');assert.equal(await read(),before);
await command('mapsGeographic');await page.locator('#mapsGeographicDialog').waitFor({state:'visible'});await command('mapsGeographicApply');
assert.equal(await page.locator('#mapsGeographicDialog').isVisible(),false,'Apply closes its dialog');
const applied=await read();assert.notEqual(applied,before);
const features=JSON.parse(applied),prior=JSON.parse(before);
assert(features[1].x<features[0].x&&features[1].y>features[0].y,'Rubery is southwest of Birmingham');
assert.deepEqual(features.slice(2),prior.slice(2),'ambiguous/locked/drawings preserved');
await command('mapsUndo');assert.equal(await read(),before,'one undo restores operation');
await command('mapsRedo');assert.equal(await read(),applied,'redo reproduces operation');
await command('mapsSave');await page.waitForFunction(()=>!window.PlotDirectorMapWorkspace.busy);assert.equal(saves,1);
const id=features[0].featureID;await page.evaluate(id=>window.PlotDirectorMapWorkspace.select(id),id);
await page.locator(`[data-feature-id="${id}"]`).focus();await page.keyboard.press('ArrowRight');
assert.equal(JSON.parse(await read())[0].x,features[0].x+5,'manual movement remains available');
assert.equal(states[0].matchJson,JSON.stringify({Latitude:52.486,Longitude:-1.89,Attribution:'Synthetic fixture; no provider calls'}),'canvas movement cannot change geographic coordinates');
assert.deepEqual(errors,[]);
console.log('PASS geographic preview/cancel/Escape/apply/undo/redo/save/manual movement/preservation',pwa?'PWA':'browser',theme);
await context.close();
}
}finally{await browser.close();}
})().catch(e=>{console.error(e);process.exit(1);});