PlotDirector/PlotLine.Tests/Browser/locations-workspace-dev.cjs

31 lines
10 KiB
JavaScript

// Explicit Development-only operational smoke. Uses only a dedicated disposable identity/project.
const {chromium}=require('playwright'),assert=require('node:assert/strict'),fs=require('node:fs');
const base='https://dev.plotdirector.com',file=process.argv[2],setup=process.argv.includes('--setup');
if(!file?.startsWith('/tmp/pd-dev-browser-'))throw Error('Private dedicated identity file required.');
const identity=JSON.parse(fs.readFileSync(file,'utf8'));assert.equal(identity.Database,'PlotDirector_Development');assert(identity.Email.startsWith('pwa-browser-')&&identity.Email.endsWith('@example.invalid'));
(async()=>{const browser=await chromium.launch(),context=await browser.newContext({viewport:{width:1440,height:1000}}),page=await context.newPage();await context.addInitScript(user=>localStorage.setItem(`plotdirector.desktop.v1.${user}`,JSON.stringify({dismissedAt:Date.now()})),identity.UserID);try{
await page.goto(base+'/Account/Login');await page.locator('[name=Email]').fill(identity.Email);await page.locator('[name=Password]').fill(identity.Password);await page.locator('form button[type=submit]').click();await page.waitForURL(base+'/Projects');
if(setup){assert(!identity.ProjectID);await page.goto(base+'/Projects/Create');await page.locator('[name=ProjectName]').fill('Locations acceptance · disposable world');await page.getByRole('button',{name:'Save project',exact:true}).click();await page.waitForURL(/\/Projects\/Details\/\d+/);identity.ProjectID=Number(page.url().split('/').pop());await page.goto(base+`/Books/Create?projectId=${identity.ProjectID}`);await page.locator('[name=BookTitle]').fill('Locations acceptance fixture');await page.getByRole('button',{name:'Save book',exact:true}).click();await page.waitForURL(/\/Books\/Details\/\d+/);identity.BookID=Number(page.url().split('/').pop());fs.writeFileSync(file,JSON.stringify(identity,null,2),{mode:0o600});console.log('PASS dedicated fixture project/book created through normal Development forms.');return;}
assert(identity.ProjectID&&identity.BookID);const q=`projectId=${identity.ProjectID}`;
const api=async(action,body)=>page.evaluate(async({action,body})=>{const response=await fetch('/LocationWorkspace/'+action,{method:'POST',headers:{'Content-Type':'application/json','RequestVerificationToken':document.querySelector('[data-location-token] [name=__RequestVerificationToken]').value},body:JSON.stringify(body)});return{status:response.status,body:await response.json().catch(()=>({}))};},{action,body});
const directory=async()=>{const response=await page.request.get(base+`/LocationWorkspace/Directory?${q}`);assert.equal(response.status(),200);return response.json();};
await page.goto(base+`/Locations?${q}`);await page.locator('[data-location-records][aria-busy=false]').waitFor();let data=await directory();assert.equal(data.locations.length,0,'only an empty dedicated project may be populated');
const created=await api('Create',{projectId:identity.ProjectID,name:'Meridian acceptance',parentId:null});assert.equal(created.status,200);const id=created.body.id;await page.reload();const row=()=>page.locator(`[data-location-id="${id}"]`);await row().locator('[data-location-name]').click();
await page.getByRole('button',{name:'Create child location',exact:true}).click();await page.getByRole('textbox',{name:'New location name'}).fill('Observatory acceptance');await page.getByRole('textbox',{name:'New location name'}).press('Enter');await page.locator('[data-location-dialog]').waitFor({state:'hidden'});data=await directory();const child=data.locations.find(r=>r.name==='Observatory acceptance');assert.equal(child.parent,id);
await page.getByRole('button',{name:'Cards',exact:true}).click();await page.locator(`[data-location-id="${child.id}"] [data-select-location]`).check();await page.getByRole('button',{name:'Hierarchy',exact:true}).click();await page.getByRole('button',{name:'Collapse all',exact:true}).click();await page.locator('[data-selection-menu]').click();await page.getByRole('menuitem',{name:'Rename',exact:true}).click();await page.locator(`[data-location-id="${child.id}"]`).getByRole('textbox',{name:'Location name'}).waitFor();await page.keyboard.press('Escape');await page.locator('[data-clear-selection]').click();await page.getByRole('button',{name:'Expand all',exact:true}).click();
await row().locator('[data-location-name]').click();const peer=await context.newPage();await peer.goto(base+`/Locations?${q}`);await peer.locator('[data-location-records][aria-busy=false]').waitFor();
await page.locator('[data-details]').getByRole('combobox',{name:'Classification',exact:true}).selectOption('Fictional');await page.waitForTimeout(250);data=await directory();assert.equal(data.locations.find(r=>r.id===id).classification,'Fictional');
await peer.locator(`[data-location-id="${id}"] [aria-label="Fictional"]`).waitFor({timeout:20000});
await row().focus();await page.keyboard.press('F2');await row().getByRole('textbox',{name:'Location name'}).fill('Meridian renamed');await row().getByRole('textbox',{name:'Location name'}).press('Enter');await row().getByRole('button',{name:'Meridian renamed',exact:true}).waitFor();data=await directory();assert.equal(data.locations.find(r=>r.id===child.id).parent,id);
await row().locator('[data-location-name]').click();await page.getByRole('button',{name:'Edit description',exact:true}).click();await page.getByRole('textbox',{name:'Location description'}).fill('Author-controlled synthetic description.');await page.locator('[data-details]').getByRole('button',{name:'Save',exact:true}).click();await page.waitForTimeout(200);data=await directory();assert.equal(data.locations.find(r=>r.id===id).description,'Author-controlled synthetic description.');
await page.getByRole('button',{name:'Undo',exact:true}).first().click();await page.waitForTimeout(200);data=await directory();assert.equal(data.locations.find(r=>r.id===id).description,'');assert(await page.locator('[data-undo]').isHidden());await page.getByRole('button',{name:'Edit description',exact:true}).click();await page.getByRole('textbox',{name:'Location description'}).fill('Author-controlled synthetic description.');await page.locator('[data-details]').getByRole('button',{name:'Save',exact:true}).click();await page.waitForTimeout(200);data=await directory();
const bulk=await api('Assign',{projectId:identity.ProjectID,attribute:'quickAdd',changes:data.locations.map(r=>({id:r.id,value:'1',version:r.version}))});assert.equal(bulk.status,200);assert.equal(bulk.body.changes.length,2);assert(bulk.body.undoToken);const restored=await api('Undo',{token:bulk.body.undoToken});assert.equal(restored.status,200);data=await directory();assert(data.locations.every(r=>r.quickAdd==='0'));
const current=data.locations.find(r=>r.id===id),bad=await api('Assign',{projectId:identity.ProjectID,attribute:'parent',changes:[{id,value:String(child.id),version:current.version}]});assert.equal(bad.status,409);const unauthorised=await api('Assign',{projectId:2147483647,attribute:'classification',changes:[{id,value:'Real',version:current.version}]});assert([403,404].includes(unauthorised.status));
const out='/tmp/pd-locations-evidence';await page.screenshot({path:out+'/development-workspace.png'});
await page.goto(base+`/Locations/Edit/${id}`);await page.locator('[name=ParentLocationID]').selectOption(String(child.id));await page.getByRole('button',{name:'Save location',exact:true}).click();await page.waitForLoadState();assert.match(await page.locator('.validation-summary-errors').textContent(),/descendant/);data=await directory();assert.equal(data.locations.find(r=>r.id===id).parent,null);assert.equal(data.locations.find(r=>r.id===id).description,'Author-controlled synthetic description.');
const checks=[];for(const route of [`/Characters?${q}`,`/Names?${q}`,`/RelationshipMap?${q}`,`/Maps?${q}`,`/FloorPlans?${q}`,`/StoryAssets?${q}`,`/Locations/Details/${id}`,`/Locations/Edit/${id}`,`/Writer?${q}&bookId=${identity.BookID}`]){const response=await page.goto(base+route);assert.equal(response.status(),200,route);assert(!page.url().includes('/Account/'));checks.push(route.split('?')[0]);}
await page.goto(base+`/Locations/Details/${id}`);await page.locator('[name=ToLocationID]').selectOption(String(child.id));await page.locator('[name=Notes]').fill('Disposable non-hierarchical relationship');await page.getByRole('button',{name:'Add relationship',exact:true}).click();await page.waitForLoadState();assert(await page.getByText('Disposable non-hierarchical relationship',{exact:true}).count());
await page.goto(base+`/Locations?${q}`);await page.locator('[data-location-records][aria-busy=false]').waitFor();await page.getByRole('button',{name:'Expand all',exact:true}).click();await page.locator(`[data-location-id="${child.id}"]`).getByRole('button',{name:'Actions for Observatory acceptance'}).click();await page.getByRole('menuitem',{name:'Merge with another location…',exact:true}).click();await page.getByRole('combobox',{name:'Surviving location'}).selectOption(String(id));await page.locator('[data-location-dialog] input[type=checkbox]').check();const mergeResponse=page.waitForResponse(r=>r.url().endsWith('/LocationWorkspace/Merge'));await page.locator('[data-dialog-save]').click();const mergeResult=await mergeResponse;assert.equal(mergeResult.status(),200,await mergeResult.text());await page.locator('[data-location-dialog]').waitFor({state:'hidden'});data=await directory();assert.equal(data.locations.length,1);assert.equal(data.locations[0].id,id);assert.equal(data.locations[0].description,'Author-controlled synthetic description.');
fs.writeFileSync(out+'/development-smoke.json',JSON.stringify({status:'passed',target:'Development',checks:['real Directory/Create/Assign/Undo/Merge endpoints','bulk assignment and protected Undo','one-step workspace Undo clears after restoration','normal non-hierarchical relationship form','reviewed merge archives source and retains survivor','inline child Enter-save','context menu for a selection hidden in a collapsed branch','classification','inline rename preserves child identifiers','description save','server cycle rejection','traditional full Edit rejects cycles without losing posted data','cross-project mutation rejection','actual second-window live invalidation',...checks]},null,2));console.log('PASS authenticated Development workspace mutations, hierarchy safeguards, cross-window live updates and related route smoke; disposable data only.');
}finally{await browser.close();}})().catch(e=>{console.error(e);process.exitCode=1;});