PlotDirector/PlotLine.Tests/Browser/context-spatial-dev-setup.cjs

8 lines
5.0 KiB
JavaScript

// Creates only disposable, dedicated-author fixtures through normal authenticated endpoints.
const{chromium}=require('playwright'),fs=require('node:fs'),assert=require('node:assert/strict');const file=process.argv[2];if(!file?.startsWith('/tmp/pd-dev-browser-'))throw Error('Dedicated identity required');const identity=JSON.parse(fs.readFileSync(file));assert.equal(identity.Database,'PlotDirector_Development');assert(identity.Email.startsWith('pwa-browser-'));assert(identity.ProjectID&&identity.BookID);assert(!identity.SpatialFixture);const base='https://dev.plotdirector.com';
(async()=>{const browser=await chromium.launch();try{const page=await browser.newPage({viewport:{width:1440,height:1000}});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 button[type=submit]').click()]);await page.goto(base+`/Locations?projectId=${identity.ProjectID}`);await page.locator('[data-location-records][aria-busy=false]').waitFor();let token=await page.locator('[name=__RequestVerificationToken]').first().inputValue();async function json(route,body){const r=await page.request.post(base+route,{headers:{RequestVerificationToken:token},data:body});if(!r.ok())throw Error(`${route}: ${r.status()} ${(await r.text()).slice(0,200)}`);return r.json();}async function form(route,values){const r=await page.request.post(base+route,{form:{__RequestVerificationToken:token,...Object.fromEntries(Object.entries(values).filter(([k,v])=>v!=null).map(([k,v])=>[k,String(v)]))}});if(!r.ok())throw Error(`${route}: ${r.status()}`);return r;}
const locations=[];for(const [name,parent]of [['Spatial acceptance house',null],['Bedroom',0],['Lounge',0],['Kitchen',0],['Unmapped valley',null]]){const result=await json('/LocationWorkspace/Create',{projectId:identity.ProjectID,name,parentId:parent==null?null:locations[parent]});locations.push(result.id);}const chapters=[];for(const n of [1,2]){await page.goto(base+`/Chapters/Create?bookId=${identity.BookID}`);const defaults=await page.locator('form[action="/Chapters/Save"]').evaluate(f=>Object.fromEntries(new FormData(f)));const r=await form('/Chapters/Save',{...defaults,ChapterID:0,BookID:identity.BookID,ChapterNumber:n,ChapterTitle:`Spatial disposable chapter ${n}`});assert.match(r.url(),/\/Chapters\/Details\/\d+/);chapters.push(Number(new URL(r.url()).pathname.split('/').pop()));}const scenes=[];for(const [index,chapter,title,location]of [[1,chapters[0],'Spatial disposable previous',locations[2]],[1,chapters[1],'Spatial disposable current',locations[1]],[2,chapters[1],'Spatial disposable next',locations[3]]]){await page.goto(base+`/Scenes/Create?chapterId=${chapter}`);const inputs=JSON.parse(fs.readFileSync('/tmp/pd-spatial-lookups.json'));const r=await form('/Scenes/Save',{...inputs,SceneID:0,ChapterID:chapter,SceneNumber:index,SceneTitle:title,PrimaryLocationID:location});assert.match(r.url(),/\/Scenes\/Edit\/\d+/);scenes.push(Number(new URL(r.url()).pathname.split('/').pop()));}
const createdMap=await form('/Maps/Create',{ProjectID:identity.ProjectID,Name:'Spatial disposable town map',Kind:'Region'});const mapId=(await createdMap.json()).id;const mapResponse=await page.request.get(base+`/Maps/State?projectId=${identity.ProjectID}&id=${mapId}`);const document=await mapResponse.json();document.features=[{featureID:crypto.randomUUID(),mapID:mapId,name:'Spatial acceptance house',type:'Custom',geometryClass:'Marker',layer:'Places',x:100,y:80,width:48,height:48,isPlaced:true,isProvisional:false,isLocked:false,showLabel:true,locationID:locations[0],geometryJson:'{}'}];await json('/Maps/Save',document);
const plan=await form('/FloorPlans/Create',{ProjectID:identity.ProjectID,Name:'Spatial disposable house plan',LocationID:locations[0]});assert.match(plan.url(),/\/FloorPlans\/Edit\/\d+/);const planId=Number(new URL(plan.url()).pathname.split('/').pop());await form('/FloorPlans/AddFloor',{FloorPlanID:planId,Name:'Ground floor',SortOrder:0,GridWidth:18,GridHeight:14});await page.goto(base+`/FloorPlans/Edit/${planId}`);const floor=Number(await page.locator('[data-floor-panel]').first().getAttribute('data-floor-panel'));assert(floor);for(const [index,x,y]of [[1,2,2],[2,7,2],[3,2,6]])await form('/FloorPlans/AddBlock',{floorPlanId:planId,FloorPlanFloorID:floor,LocationID:locations[index],X:x,Y:y,WidthCells:5,HeightCells:4,BlockType:'Room'});
identity.SpatialFixture={locations,chapters,scenes,mapId,planId,floor};fs.writeFileSync(file,JSON.stringify(identity,null,2),{mode:0o600});await page.goto(base+`/Writing/Context?projectId=${identity.ProjectID}&bookId=${identity.BookID}&sceneId=${scenes[1]}&pinned=true`);await page.screenshot({path:'/tmp/pd-spatial-evidence/development-before.png'});console.log('PASS dedicated Development spatial fixtures: five locations, two chapters, three scenes, canonical map and floor plan; existing author records untouched.');}finally{await browser.close();}})().catch(e=>{console.error(e);process.exitCode=1;});