59 lines
11 KiB
JavaScript
59 lines
11 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','warm-light']){
|
|
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=[],writes=[];page.on('pageerror',e=>errors.push(e.message));
|
|
page.on('request',r=>{if(r.method()==='POST')writes.push(new URL(r.url()).pathname);});
|
|
let persisted;
|
|
await page.route('**/Maps/Save',r=>{persisted=r.request().postDataJSON();persisted.map.revision++;return r.fulfill({json:persisted});});
|
|
await page.goto(base+'/fixture?page=maps&settlements=true&theme='+theme);await page.waitForFunction(()=>!!window.PlotDirectorMapWorkspace);
|
|
await page.locator('#mapsAssetSearch').fill('Town');await page.locator('[data-asset="Town"]').click();
|
|
const canvas=await page.locator('#mapsCanvas').boundingBox();await page.mouse.click(canvas.x+canvas.width*.5,canvas.y+canvas.height*.5);
|
|
assert(await page.evaluate(()=>window.PlotDirectorMaps.hasBoundary(window.PlotDirectorMapWorkspace.document.features[0])),'new library town defaults to an area');
|
|
const ids=await page.evaluate(theme=>{const W=window.PlotDirectorMapWorkspace,E=window.PlotDirectorMaps,ids={town:crypto.randomUUID(),house:crypto.randomUUID(),outside:crypto.randomUUID(),road:crypto.randomUUID(),forest:crypto.randomUUID()};W.edit(()=>{const preset=theme.includes('dark')?'dark':theme.includes('warm')?'paper':'light';W.document.map.appearanceJson=JSON.stringify({preset});
|
|
const town=E.makeFeature('Town',0,0,ids.town),g=E.geometry(town);delete g.boundary;Object.assign(town,{name:'Rubery',locationID:11,isProvisional:false,geometryJson:JSON.stringify(g)});
|
|
const house=E.makeFeature('House',40,15,ids.house);Object.assign(house,{name:"Maggie's flat",locationID:12,isProvisional:false});
|
|
const outside=E.makeFeature('House',480,40,ids.outside);Object.assign(outside,{name:"Beth's flat",locationID:13,isProvisional:false});
|
|
const road=E.makeFeature('Road',-300,0,ids.road);Object.assign(road,{name:'A38',endFeatureID:ids.town});
|
|
const forest=E.makeFeature('Forest',60,0,ids.forest);forest.name='Lickey Hills';W.document.features=[forest,town,road,house,outside];
|
|
});W.fit();return ids;},theme);
|
|
const feature=id=>page.evaluate(id=>JSON.parse(JSON.stringify(window.PlotDirectorMapWorkspace.document.features.find(f=>f.featureID===id))),id);
|
|
const select=id=>page.evaluate(id=>window.PlotDirectorMapWorkspace.select(id),id);
|
|
const route=()=>page.locator(`[data-feature-id="${ids.road}"] .map-shape`).getAttribute('d');
|
|
async function command(id){await page.evaluate(id=>document.getElementById(id).click(),id);}
|
|
const original=await feature(ids.town),roadBefore=await route();await select(ids.town);assert(await page.locator('[data-action=convert-area]').isVisible());
|
|
await page.locator('[data-action=convert-area]').click();let town=await feature(ids.town);assert.equal(town.geometryClass,'Marker');assert.equal(town.locationID,11);assert.deepEqual([town.x,town.y],[original.x,original.y]);assert.equal(town.featureID,ids.town);assert.equal(await route(),roadBefore);
|
|
assert.equal(await page.locator(`[data-boundary-feature-id="${ids.town}"]`).count(),1);assert.equal(await page.locator('.maps-anchor-crosshair').count(),1);
|
|
await command('mapsUndo');assert(!JSON.parse((await feature(ids.town)).geometryJson).boundary);await command('mapsRedo');assert(JSON.parse((await feature(ids.town)).geometryJson).boundary);await select(ids.town);
|
|
const houseBefore=await feature(ids.house),outline=JSON.parse((await feature(ids.town)).geometryJson).boundary.points;
|
|
async function dragBy(selector,dx,dy){const box=await page.locator(selector).boundingBox();await page.mouse.move(box.x+box.width/2,box.y+box.height/2);await page.mouse.down();await page.mouse.move(box.x+box.width/2+dx,box.y+box.height/2+dy,{steps:5});await page.mouse.up();}
|
|
await page.locator('[data-settlement-mode=boundary]').click();
|
|
const boundaryStart=await page.evaluate(()=>new DOMPoint(100,30).matrixTransform(document.getElementById('mapsCanvas').getScreenCTM()).toJSON());
|
|
await page.mouse.move(boundaryStart.x,boundaryStart.y);await page.mouse.down();await page.mouse.move(boundaryStart.x+22,boundaryStart.y+8,{steps:5});await page.mouse.up();
|
|
const boundaryMoved=await feature(ids.town);assert.deepEqual([boundaryMoved.x,boundaryMoved.y],[original.x,original.y]);assert.notEqual(JSON.parse(boundaryMoved.geometryJson).boundary.offsetX,0,'dragging outline in boundary mode moves only its frame');assert.equal(await route(),roadBefore);assert.deepEqual(await feature(ids.house),houseBefore);
|
|
await page.locator('[data-settlement-mode=boundary]').click();let count=await page.locator('[data-handle=point]').count();assert.equal(count,8);const beforePoint=(await feature(ids.town)).geometryJson;await dragBy('[data-handle=point]:first-child',16,9);assert.notEqual((await feature(ids.town)).geometryJson,beforePoint,'pointer control-point editor changes the boundary');assert.equal(await route(),roadBefore);
|
|
await page.locator('[data-action=bend]').click();assert.equal(await page.locator('[data-handle=point]').count(),9);
|
|
await page.locator('[data-handle=point]').first().focus();await page.keyboard.press('ArrowLeft');assert.equal(await route(),roadBefore);await page.keyboard.press('Delete');assert.equal(await page.locator('[data-handle=point]').count(),8);
|
|
const input=page.locator('[data-area-frame=width]');await input.fill('420');await input.blur();assert.equal(JSON.parse((await feature(ids.town)).geometryJson).boundary.width,420);assert.equal(await route(),roadBefore);assert.deepEqual(await feature(ids.house),houseBefore);
|
|
const controls=JSON.stringify(JSON.parse((await feature(ids.town)).geometryJson).boundary.points);
|
|
for(const value of ['Rugged','Gentle','Natural']){await page.locator('[data-geometry=coast]').selectOption(value);assert.equal(JSON.stringify(JSON.parse((await feature(ids.town)).geometryJson).boundary.points),controls);assert.equal(await route(),roadBefore);}
|
|
await page.locator('[data-area-style=fillOpacity]').fill('0.4');await page.locator('[data-area-style=fillOpacity]').blur();await page.locator('[data-area-style=borderStyle]').selectOption('Dotted');
|
|
assert.equal(await page.locator(`[data-boundary-feature-id="${ids.town}"] path`).getAttribute('fill-opacity'),'0.4');
|
|
await select(ids.house);const containment=page.locator('#mapsDetailContent details').filter({has:page.locator('summary:text-is("Geographical containment")')});await containment.locator('summary').click();assert.match(await containment.innerText(),/Rubery/);assert.match(await containment.innerText(),/Lickey Hills/);
|
|
await select(ids.town);await page.locator('[data-settlement-mode=anchor]').click();const worldBefore=await page.evaluate(id=>window.PlotDirectorMaps.outline(window.PlotDirectorMapWorkspace.document.features.find(f=>f.featureID===id)),ids.town);
|
|
await page.locator('[data-handle=anchor]').focus();await page.keyboard.press('ArrowRight');town=await feature(ids.town);assert.equal(town.x,original.x+5);assert.notEqual(await route(),roadBefore);
|
|
const worldAfter=await page.evaluate(id=>window.PlotDirectorMaps.outline(window.PlotDirectorMapWorkspace.document.features.find(f=>f.featureID===id)),ids.town);assert(worldAfter.every((p,i)=>p.every((v,j)=>Math.abs(v-worldBefore[i][j])<1e-8)),'independent anchor movement leaves outline fixed');await dragBy('[data-handle=anchor]',14,7);const pointerWorld=await page.evaluate(id=>window.PlotDirectorMaps.outline(window.PlotDirectorMapWorkspace.document.features.find(f=>f.featureID===id)),ids.town);assert(pointerWorld.every((p,i)=>p.every((v,j)=>Math.abs(v-worldBefore[i][j])<1e-8)));town=await feature(ids.town);
|
|
await page.locator('[data-settlement-mode=whole]').click();await page.locator(`[data-feature-id="${ids.town}"]`).focus();await page.keyboard.press('ArrowDown');const moved=await feature(ids.town);assert.equal(moved.y,town.y+5);assert.deepEqual(await feature(ids.house),houseBefore,'whole settlement movement never moves contained buildings');
|
|
await select(ids.house);await page.locator(`[data-feature-id="${ids.house}"]`).focus();await page.keyboard.press('Shift+ArrowRight');await page.evaluate(id=>{const W=window.PlotDirectorMapWorkspace;W.edit(()=>{W.document.features.find(f=>f.featureID===id).x=900;});},ids.house);
|
|
const outsideInfo=page.locator('#mapsDetailContent details').filter({has:page.locator('summary:text-is("Geographical containment")')});await outsideInfo.locator('summary').click();assert.match(await outsideInfo.innerText(),/No mapped area/);
|
|
await select(ids.town);const beforeFit=await feature(ids.town),buildingFit=await feature(ids.house),r=await route();await page.locator('[data-action=fit-boundary]').click();assert.equal((await feature(ids.town)).x,beforeFit.x);assert.equal((await feature(ids.town)).y,beforeFit.y);assert.equal(await route(),r);assert.deepEqual(await feature(ids.house),buildingFit);
|
|
await select(ids.house);const fitted=page.locator('#mapsDetailContent details').filter({has:page.locator('summary:text-is("Geographical containment")')});await fitted.locator('summary').click();assert.match(await fitted.innerText(),/Rubery/,'story-associated exterior content included by explicit Fit');
|
|
await select(ids.town);await page.locator('[data-action=duplicate]').click();const copies=await page.evaluate(id=>window.PlotDirectorMapWorkspace.document.features.filter(f=>f.featureID!==id&&f.name==='Rubery copy'),ids.town);assert.equal(copies.length,1);assert.equal(copies[0].locationID,11);assert(JSON.parse(copies[0].geometryJson).boundary);
|
|
await select(ids.town);await page.locator('[data-field=isLocked]').check();assert(await page.locator('[data-action=fit-boundary]').isDisabled());const locked=await feature(ids.town);await page.locator(`[data-feature-id="${ids.town}"]`).focus();await page.keyboard.press('ArrowRight');assert.deepEqual(await feature(ids.town),locked);
|
|
await command('mapsSave');await page.waitForFunction(()=>!window.PlotDirectorMapWorkspace.busy);assert(persisted.features.find(f=>f.featureID===ids.town).geometryJson.includes('boundary'));assert.deepEqual(errors,[]);assert(writes.every(p=>p==='/Maps/Save'),'geometry never mutates story relationships');
|
|
console.log('PASS settlement editor conversion, preserved road/anchor, control points, modes, containment, fit, style, undo/redo, duplication, locks and Save',pwa?'PWA':'browser',theme);
|
|
await context.close();
|
|
}
|
|
}finally{await browser.close();}})().catch(e=>{console.error(e);process.exit(1);});
|