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

29 lines
6.1 KiB
JavaScript

const{chromium}=require('playwright'),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:1150}});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 saved;
const locations=[[11,'Rubery',null],[12,'Frankley',null],[13,'Redditch',null],[14,'Barmouth',null],[15,'The Doweries',11],[16,"Maggie's flat",15],[17,'Reservoir',12],[18,'Forest',11],[19,'Independent place',11],[20,'Independent child',19]].map(([locationID,locationName,parentLocationID])=>({locationID,locationName,parentLocationID}));
const states=[[11,52.391,-2.016],[12,52.412,-2.023],[13,52.306,-1.942],[14,52.72,-4.05],[19,52.4,-2.01]].map(([locationID,Latitude,Longitude])=>({locationID,status:'Resolved',matchJson:JSON.stringify({Latitude,Longitude,Attribution:'Isolated synthetic coordinates; no external provider calls'})}));
await page.route('**/Geography/State?*',r=>{assert.equal(new URL(r.request().url()).searchParams.get('includeHierarchy'),'true');return r.fulfill({json:{states,locations}});});
await page.route('**/Maps/Save',r=>{saved=r.request().postDataJSON();saved.map.revision++;return r.fulfill({json:saved});});
await page.goto(base+'/fixture?page=maps&theme='+theme);await page.waitForFunction(()=>!!window.PlotDirectorMapWorkspace);
await page.evaluate(()=>{const W=window.PlotDirectorMapWorkspace,E=window.PlotDirectorMaps;W.edit(()=>{
const f=(type,id,locationID,x,y)=>Object.assign(E.makeFeature(type,x,y,id),{name:id,locationID,isProvisional:false});
const rubery=f('Town','Rubery',11,0,0),frankley=f('Town','Frankley',12,300,-40),redditch=f('Town','Redditch',13,100,300),barmouth=f('Town','Barmouth',14,-350,-200),house=f('House',"Maggie's flat",16,25,20),forest=f('Forest','Forest',18,-50,-40),lake=f('Lake','Reservoir',17,320,-30),own=f('House','Independent place',19,80,90),child=f('House','Independent child',20,95,100),decor=f('Landmark','Decoration',null,600,450);
const road=f('Road','A38',null,150,0);road.startFeatureID='Rubery';road.endFeatureID='Frankley';const g=E.geometry(road);g.points=[[-.5,0],[-.1,.35],[.2,-.3],[.5,0]];g.drawingOrder=310000;road.geometryJson=JSON.stringify(g);
const segment=f('Trail','Trail',null,200,100);segment.startFeatureID='Frankley';segment.endFeatureID='Redditch';W.document.features=[rubery,frankley,redditch,barmouth,house,forest,lake,own,child,decor,road,segment];});W.fit();});
const read=()=>page.evaluate(()=>JSON.stringify(window.PlotDirectorMapWorkspace.document.features)),before=await read();
const expected=await page.evaluate(({states,locations})=>window.PlotDirectorGeography.position(window.PlotDirectorMapWorkspace.document.features,states,window.PlotDirectorMapWorkspace.view,locations),{states,locations});assert(expected.canApply);
const command=id=>page.evaluate(id=>document.getElementById(id).click(),id);
await command('mapsGeographic');await page.locator('#mapsGeographicDialog').waitFor({state:'visible'});assert.equal(await read(),before);await page.locator('#mapsGeographicList').evaluate(n=>n.open=true);assert.match(await page.locator('#mapsGeographicSummary').innerText(),/Maggie's flat → Follows Rubery via The Doweries/);assert.match(await page.locator('#mapsGeographicSummary').innerText(),/2 roads\/paths/);assert(await page.locator('#mapsGeographicPreview svg').isVisible());
const scene=await page.locator('#mapsScene').innerHTML();await command('mapsGeographicCancel');assert.equal(await read(),before);
await command('mapsGeographic');await page.locator('#mapsGeographicDialog').waitFor({state:'visible'});await command('mapsGeographicApply');assert(!await page.locator('#mapsGeographicDialog').isVisible());assert.deepEqual(JSON.parse(await read()),expected.features,'Apply takes the exact complete proposal');assert.equal(await page.locator('#mapsScene').innerHTML(),scene,'preview and applied renderer have identical roads/boundaries');
const applied=await read();await command('mapsUndo');assert.equal(await read(),before);await command('mapsRedo');assert.equal(await read(),applied);
await command('mapsSave');await page.waitForFunction(()=>document.getElementById('mapsStatus').textContent==='Saved');assert.deepEqual(saved.features,expected.features);
// Reopen a document supplied by the Save contract, before workspace initialization.
await page.route('**/fixture?**',async route=>{const response=await route.fetch();let html=await response.text();html=html.replace(/(<script id="mapsBootstrap"[^>]*>)(.*?)(<\/script>)/s,(_,a,b,c)=>{const boot=JSON.parse(b);boot.document=saved;return a+JSON.stringify(boot)+c;});await route.fulfill({response,body:html});});
await page.reload();await page.waitForFunction(()=>!!window.PlotDirectorMapWorkspace);assert.equal(await read(),applied,'Save/reopen preserves every transformed object');
// Ordinary manual movement retains its established independent-object behaviour.
await page.evaluate(()=>window.PlotDirectorMapWorkspace.select('Rubery'));await page.locator('[data-feature-id=Rubery]').focus();await page.keyboard.press('ArrowRight');const manual=JSON.parse(await read());assert.equal(manual[0].x,expected.features[0].x+5);assert.deepEqual(manual[4],expected.features[4],'manual movement does not start hierarchy inheritance');assert.deepEqual(await page.evaluate(()=>{const W=window.PlotDirectorMapWorkspace;return window.PlotDirectorMaps.pathPoints(W.document.features.find(f=>f.name==='A38'),W.document.features)[0];}),[manual[0].x,manual[0].y],'ordinary manual movement still updates the existing road anchor');
assert.deepEqual(errors,[]);console.log('PASS hierarchy geography '+(pwa?'PWA':'web')+' '+theme+': fresh hierarchy, Barmouth/Rubery/dependents, exact road/boundary preview, Cancel, Apply, atomic Undo/Redo, Save/reopen, independent manual editing.');await context.close();
}}finally{await browser.close();}})().catch(e=>{console.error(e);process.exitCode=1;});