PlotDirector/PlotLine.Tests/Browser/context-maps-development.cjs
2026-12-01 12:45:05 +00:00

36 lines
8.4 KiB
JavaScript

// Genuine Development forms, ordinary writer identity, registered disposable project.
const assert=require('node:assert/strict'),{open}=require('./dev-acceptance.cjs');
let session,stage='login';
(async()=>{try{
session=await open();let p=session.page;const project=await session.project();
const a=await session.create('Locations','?projectId='+project,{LocationName:'Harbour'}),b=await session.create('Locations','?projectId='+project,{LocationName:'Hill'});
const book=await session.create('Books','?projectId='+project,{BookTitle:'Maps acceptance',BookNumber:1}),chapter=await session.create('Chapters','?bookId='+book,{ChapterTitle:'Map chapter',ChapterNumber:1});
async function scene(name,place){await session.goto('/Scenes/Create?chapterId='+chapter);await p.locator('#SceneTitle').fill(name);await p.locator('#PrimaryLocationID').selectOption(String(place));await p.getByRole('button',{name:'Create scene',exact:true}).click();await p.waitForURL('**/Scenes/Edit/*');return Number(new URL(p.url()).pathname.split('/').at(-1));}
const first=await scene('At harbour',a),second=await scene('At hill',b);
const character=await session.create('Characters','?projectId='+project,{CharacterName:'Recorded traveller'},async page=>{const custom=page.locator('[name=CustomSex]');if(await custom.count()&&await custom.isEnabled())await custom.fill('Unspecified');});
const unknown=await session.create('Characters','?projectId='+project,{CharacterName:'Unlocated traveller'},async page=>{const custom=page.locator('[name=CustomSex]');if(await custom.count()&&await custom.isEnabled())await custom.fill('Unspecified');});
await session.goto('/Scenes/SceneCharactersEditor?sceneId='+first);await session.post('/Scenes/SaveSceneCharacters',{SceneID:first,'NewCharacter.CharacterID':character,'NewCharacter.LocationID':b});await session.post('/Scenes/SaveSceneCharacters',{SceneID:first,'NewCharacter.CharacterID':unknown});
async function map(name,extra){stage='create '+name;await session.goto('/Maps?projectId='+project);stage='open map dialog '+name;await p.locator('#mapsWelcomeNew').click();await p.locator('#mapsNewForm [name=Name]').fill(name);await p.locator('#mapsNewForm [name=Starter]').selectOption('islands');stage='submit map '+name;await p.locator('#mapsNewForm button[type=submit]').click();stage='load map '+name;await p.waitForFunction(()=>window.PlotDirectorMapWorkspace?.document?.features.length>1&&!PlotDirectorMapWorkspace.busy);const id=await p.evaluate(()=>PlotDirectorMapWorkspace.document.map.mapID);
const ids=await p.evaluate(()=>PlotDirectorMapWorkspace.document.features.slice(0,2).map(f=>f.featureID));
stage='link '+name;for(const [feature,place] of [[ids[0],a],...(extra?[[ids[1],b]]:[])]){await p.locator(`[data-feature-id="${feature}"]`).click();const select=p.locator('[data-field=locationID]');await select.evaluate(e=>{for(let n=e.parentElement;n;n=n.parentElement)if(n.tagName==='DETAILS')n.open=true;});await select.selectOption(String(place));}
await p.locator('#mapsSave').click();await p.waitForFunction(()=>!PlotDirectorMapWorkspace.dirty&&!PlotDirectorMapWorkspace.busy);return {id,feature:ids[0]};}
stage='map forms';const one=await map('Local chart',false),two=await map('Regional chart',true);
const feature=`/Writing/Context?projectId=${project}&bookId=${book}&chapterId=${chapter}&sceneId=${first}&pinned=true`;
const ctx=await p.context().newPage();await ctx.goto('https://dev.plotdirector.com'+feature);await ctx.selectOption('[data-context-pane="0"] [data-context-tab]','maps');
const ready=id=>ctx.waitForFunction(id=>document.querySelector('iframe')?.contentWindow?.PlotDirectorMapWorkspace?.document.map.mapID===id,id);
stage='selection and recorded whereabouts';await ctx.locator('[data-spatial-resource]').selectOption(String(two.id));await ready(two.id);await ctx.locator('[data-spatial-characters]').getByText(/Recorded traveller/).waitFor();assert.match(await ctx.locator('[data-spatial-characters]').textContent(),/Hill/);assert.match(await ctx.locator('[data-spatial-characters]').textContent(),/Unlocated traveller.*Location unknown \/ unset/);assert.equal(await ctx.locator('[data-spatial-place]').count(),0);await ctx.waitForFunction(()=>document.querySelector('iframe').contentDocument.querySelector('[data-spatial-overlay] [aria-label*="Recorded traveller"]'));assert.equal(await ctx.frameLocator('iframe').locator('.spatial-map-background').count()>0,true);
const mutations=[];ctx.on('request',r=>{if(r.method()==='POST')mutations.push(new URL(r.url()).pathname);});
const snapshot=()=>ctx.evaluate(()=>({position:{...document.querySelector('[data-context-workspace]').contextController.position},view:{...document.querySelector('iframe').contentWindow.PlotDirectorMapWorkspace.view}}));
stage='local browsing';const before=await snapshot();await ctx.locator('[data-spatial-zoom=in]').click();await ctx.waitForFunction(w=>document.querySelector('iframe').contentWindow.PlotDirectorMapWorkspace.view.w<w,before.view.w);assert.deepEqual((await snapshot()).position,before.position);
await ctx.frameLocator('iframe').locator('[data-feature-id]').first().click();assert.deepEqual((await snapshot()).position,before.position);
assert.equal(await ctx.frameLocator('iframe').locator('[data-spatial-overlay] [aria-label*="Unlocated"]').count(),0);assert.deepEqual(mutations,[]);
stage='held committed map refresh';await ctx.waitForFunction(()=>document.querySelector('[data-context-workspace]').contextController.connection?.state==='Connected');assert.equal(await ctx.locator('[data-shared-focus-hold]').getAttribute('aria-pressed'),'true');const held=await snapshot();await session.goto(`/Maps?projectId=${project}&id=${two.id}`);await p.locator(`[data-feature-id="${two.feature}"]`).click();const width=p.locator('[data-field=width]').first();const value=Number(await width.inputValue())+23;await width.fill(String(value));await p.locator('#mapsSave').click();await p.waitForFunction(()=>!PlotDirectorMapWorkspace.dirty&&!PlotDirectorMapWorkspace.busy);await ctx.waitForFunction(v=>document.querySelector('iframe').contentWindow.PlotDirectorMapWorkspace.document.features[0].width===v,value);assert.deepEqual(await snapshot(),held);
stage='main editor navigation';await session.goto('/Locations/Details/'+a);await p.waitForFunction(()=>window.PlotDirectorPresenceConnection?.state==='Connected');const contextUrl=ctx.url();await ctx.getByRole('link',{name:'Open in Maps',exact:true}).click();await p.waitForURL(u=>u.pathname==='/Maps'&&u.searchParams.get('id')===String(two.id));assert.equal(ctx.url(),contextUrl);assert.deepEqual(await snapshot(),held);
stage='scene following';await ctx.locator('[data-spatial-resource]').selectOption(String(one.id));await ready(one.id);await ctx.selectOption('[data-context-scene]',String(second));await ready(two.id);assert.equal((await snapshot()).position.sceneId,second);
stage='split and narrow';await ctx.selectOption('[data-context-layout]','columns');await ctx.selectOption('[data-context-pane="1"] [data-context-tab]','maps');await ctx.locator('[data-context-pane="1"] iframe').waitFor();await ctx.setViewportSize({width:460,height:850});assert.equal(await ctx.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+1),true);
await session.close();session=await open();p=session.page;stage='fresh login persistence';await session.goto(`/Maps?projectId=${project}&id=${two.id}`);await p.waitForFunction(v=>window.PlotDirectorMapWorkspace?.document.features[0].width===v,value);await session.goto(feature);await p.selectOption('[data-context-pane="0"] [data-context-tab]','maps');await p.locator('[data-spatial-characters]').getByText(/Recorded traveller/).waitFor();
console.log(JSON.stringify({passed:true,checks:['two linked maps selected locally','recorded character whereabouts only','lower geography emphasis','read-only Context','local zoom and inspection preserve focus','Hold saved map refresh preserves viewport','scene changes select relevant map','split narrow layout','fresh-login persistence'],featureUrl:'https://dev.plotdirector.com'+feature}));
}catch(error){console.error('Maps acceptance failed at '+stage+' ('+error.name+'); no credentials logged.');if(/^(create|open map|submit map|load map|link )/.test(stage))console.error(error.message.split('\n')[0]);process.exitCode=1;}
finally{if(session){try{await session.cleanup();console.log('PASS disposable project cleanup');}catch{console.error('Cleanup incomplete; preserve journal.');process.exitCode=1;}finally{await session.close();}}}
})();