PlotDirector/PlotLine.Tests/Browser/maps-dev-smoke.cjs

59 lines
8.6 KiB
JavaScript

/* Explicit Development-only smoke. Requires a dedicated identity from --dev-browser-identity.
It creates a project owned by that identity; clean up with the matching identity cleanup command. */
const {chromium}=require('playwright'),assert=require('node:assert/strict'),fs=require('node:fs');
const identityPath=process.argv[2];
if(!identityPath?.startsWith('/tmp/pd-dev-browser-'))throw Error('A dedicated Development identity file is required.');
const identity=JSON.parse(fs.readFileSync(identityPath,'utf8'));
if(identity.Database!=='PlotDirector_Development'||!identity.Email.startsWith('pwa-browser-'))throw Error('Development fixture identity required.');
const base='https://dev.plotdirector.com';
(async()=>{
const browser=await chromium.launch();
try{
const context=await browser.newContext({viewport:{width:1700,height:1100}}),page=await context.newPage(),errors=[];
page.on('pageerror',e=>errors.push(e.message));
page.on('dialog',dialog=>dialog.accept());
await page.goto(base+'/Account/Login');await page.locator('#Email').fill(identity.Email);await page.locator('#Password').fill(identity.Password);
await Promise.all([page.waitForURL('**/Projects'),page.locator('form[action="/Account/Login"] button[type=submit]').click()]);
await page.goto(base+'/Projects/Create');await page.locator('#ProjectName').fill('Maps Development smoke '+Date.now());await page.getByRole('button',{name:'Save project',exact:true}).click();
await page.waitForURL(/\/Projects(?:\/Details)?\//);const projectId=Number(page.url().match(/\/(\d+)(?:\?|$)/)[1]);
await page.goto(base+'/Maps?projectId='+projectId);await page.locator('#mapsNew').click();
await page.locator('#mapsNewForm [name=Name]').fill('Working world');await page.locator('#mapsNewForm [name=Kind]').selectOption('World');await page.locator('#mapsNewForm [name=Starter]').selectOption('island');
await Promise.all([page.waitForURL(/&id=\d+/),page.locator('#mapsNewForm button[type=submit]').click()]);
await page.waitForFunction(()=>window.PlotDirectorMapWorkspace?.document?.features.length===1);const rootURL=page.url();
async function place(type,x,y){await page.locator('#mapsAssetSearch').fill(type);await page.locator(`[data-asset="${type}"]`).click();const b=await page.locator('#mapsCanvas').boundingBox();await page.mouse.click(b.x+b.width*x,b.y+b.height*y);}
for(const[type,x,y]of[['Mountain Range',.55,.32],['Forest',.35,.48],['River',.6,.52],['Town',.35,.65],['Town',.7,.62],['Road',.5,.65]])await place(type,x,y);
assert.equal(await page.locator('.maps-feature').count(),7);
const features=await page.evaluate(()=>window.PlotDirectorMapWorkspace.document.features),towns=features.filter(f=>f.type==='Town'),road=features.find(f=>f.type==='Road');
await page.evaluate(id=>window.PlotDirectorMapWorkspace.select(id),towns[0].featureID);await page.locator('[data-field=name]').fill('Harbour town');await page.locator('[data-field=name]').blur();
await page.locator('#mapsDetailContent details').filter({has:page.locator('summary:text-is("Story Location")')}).locator('summary').click();await page.locator('[data-field=createLocation]').check();
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);
await page.evaluate(id=>window.PlotDirectorMapWorkspace.select(id),towns[0].featureID);await page.locator('[data-action=relationship]').click();
const relation=page.locator('dialog[open]');await relation.locator('[name=targetID]').selectOption(towns[1].featureID);await relation.locator('[name=direction]').selectOption('NW');await relation.locator('[name=relativeDistance]').selectOption('Far');
await relation.locator('summary').click();await relation.locator('[name=measuredDistance]').fill('126');await relation.locator('[name=unit]').fill('kilometres');await relation.locator('[name=typicalTravelTime]').fill('Two hours by car');await relation.getByRole('button',{name:'Keep relationship'}).click();
await page.locator('#mapsArrange').click();await page.locator('#mapsArrangeApply').click();await page.locator('#mapsSave').click();await page.waitForFunction(()=>document.getElementById('mapsStatus').textContent==='Saved');
const locationID=await page.evaluate(id=>window.PlotDirectorMapWorkspace.document.features.find(f=>f.featureID===id).locationID,towns[0].featureID);assert(locationID>0);
await page.reload();await page.waitForFunction(()=>!!window.PlotDirectorMapWorkspace);assert.equal(await page.locator('.maps-feature').count(),7);
assert(await page.evaluate(({id,locationID})=>{const d=window.PlotDirectorMapWorkspace.document;return d.features.find(f=>f.featureID===id).locationID===locationID&&d.relationships[0].measuredDistance===126&&d.relationships[0].typicalTravelTime==='Two hours by car';},{id:towns[0].featureID,locationID}));
// A second save must keep the same canonical Location ID.
await page.locator('#mapsSave').click();await page.waitForFunction(()=>document.getElementById('mapsStatus').textContent==='Saved');
await page.evaluate(id=>window.PlotDirectorMapWorkspace.select(id),towns[0].featureID);await page.locator('[data-action=child]').click();await page.locator('#mapsNewForm [name=Name]').fill('Harbour detail');
const rootID=new URL(rootURL).searchParams.get('id');await page.locator('#mapsNewForm button[type=submit]').click();await page.waitForURL(url=>url.searchParams.get('id')!==rootID&&url.pathname==='/Maps');await page.waitForFunction(()=>!!window.PlotDirectorMapWorkspace);
assert.equal(await page.locator('#mapsBreadcrumbs a').count(),3,'hierarchy breadcrumbs');const childURL=page.url();
await page.goto(rootURL);await page.waitForFunction(()=>!!window.PlotDirectorMapWorkspace);await page.evaluate(id=>window.PlotDirectorMapWorkspace.select(id),towns[0].featureID);await page.locator('a:text-is("Open detailed map: Harbour detail ↳")').click();await page.waitForURL(childURL);
// Duplicate and delete are exercised through the real server.
await page.locator('.maps-toolbar details summary').click();await page.locator('#mapsDuplicate').click();await page.waitForURL(url=>url.href!==childURL&&url.searchParams.has('id'));
await page.waitForFunction(()=>window.PlotDirectorMapWorkspace?.document?.map.name.endsWith(' copy'));await page.locator('.maps-toolbar details summary').click();await page.locator('#mapsDelete').click();await page.waitForURL(base+'/Maps?projectId='+projectId);
// New canonical Location is available in another map's unplaced tray.
await page.goto(childURL);await page.waitForFunction(()=>!!window.PlotDirectorMapWorkspace);assert(await page.locator(`[data-location="${locationID}"]`).isVisible());
await page.locator(`[data-location="${locationID}"]`).click();let b=await page.locator('#mapsCanvas').boundingBox();await page.mouse.click(b.x+b.width/2,b.y+b.height/2);await page.locator('#mapsSave').click();await page.waitForFunction(()=>document.getElementById('mapsStatus').textContent==='Saved');
assert(await page.evaluate(id=>window.PlotDirectorMapWorkspace.document.features[0].locationID===id,locationID));
// No anti-forgery token means no mutation.
const csrf=await context.request.post(base+'/Maps/Delete',{form:{projectId,id:Number(new URL(childURL).searchParams.get('id')),revision:1}});assert.equal(csrf.status(),400);
await page.goto(rootURL);await page.waitForFunction(()=>!!window.PlotDirectorMapWorkspace);await page.locator('#mapsFit').click();await page.screenshot({path:'/tmp/maps-development-smoke.png'});
const pwa=await browser.newContext({viewport:{width:1700,height:1100},storageState:await context.storageState()});await pwa.addInitScript(()=>Object.defineProperty(navigator,'standalone',{value:true}));const app=await pwa.newPage();app.on('pageerror',e=>errors.push(e.message));await app.goto(rootURL);await app.waitForFunction(()=>!!window.PlotDirectorMapWorkspace);
assert(await app.locator('.maps-toolbar').isHidden());assert(await app.locator('#mapsMaximise').isHidden());await app.locator('[data-pwa-context-controls] input[aria-label="Find on map"]').fill('Harbour');await app.locator('[data-search-feature]').click();assert.equal(await app.locator('.maps-feature.is-selected').count(),1);await app.screenshot({path:'/tmp/maps-development-pwa.png'});
assert.deepEqual(errors,[]);console.log('PASS authenticated Development Maps: create/starter, 7-piece geography, Location creation/reuse, relationships/distance/time, arrangement, save/reload, hierarchy/drill-down, duplication/deletion, CSRF and PWA search. Fixture project ID: '+projectId);
await context.close();await pwa.close();
}finally{await browser.close();}
})().catch(e=>{console.error(e);process.exitCode=1;});