const {chromium}=require('playwright'),fs=require('node:fs'),assert=require('node:assert/strict');
(async()=>{
const browser=await chromium.launch({headless:true});
try{
const page=await browser.newPage({viewport:{width:1280,height:850}});
await page.route('http://assets.test/**',r=>r.fulfill({contentType:'text/html',body:''}));
await page.goto('http://assets.test');
await page.addStyleTag({content:fs.readFileSync('PlotLine/wwwroot/css/context-spatial.css','utf8')+' main {height:750px}'});
for(const name of ['context-spatial','context-assets'])await page.addScriptTag({content:fs.readFileSync(`PlotLine/wwwroot/js/${name}.js`,'utf8')});
const frame={scope:{projectID:7,sceneID:3},assets:[1,2].map(id=>({asset:{storyAssetID:id,assetName:`Parcel ${id}`,kindName:'Object',description:'Saved description'},presence:id===1?'Mentioned':'Present',openingHolder:{value:id===1?'Recipient':'Unknown',sceneID:id===1?1:null,sceneLabel:'Handover'},openingLocation:{value:'Office',sceneID:2,sceneLabel:'Move'},changes:['Mentioned: parcel']}))};
async function mount(data,pane=0){await page.evaluate(({data,pane})=>{
const previous=document.querySelector(`[data-pane="${pane}"]`);if(previous){PlotDirectorContextLifecycles.assets.dispose(previous);previous.remove();}
const node=document.createElement('div');node.dataset.pane=pane;node.className='context-spatial';node.style.height='700px';node.innerHTML='
';
node.querySelector('script').textContent=JSON.stringify(data);document.querySelector('main').append(node);PlotDirectorContextLifecycles.assets.mount(node,{windowId:'window1',paneId:pane});
},{data,pane});}
await mount(frame);
assert.equal(await page.locator('[data-spatial-splitter]').getAttribute('aria-valuenow'),'30');
assert.match(await page.locator('[data-asset-detail]').innerText(),/Recipient/);
assert.deepEqual(await page.locator('[data-asset-detail] a').evaluateAll(nodes=>nodes.map(n=>n.getAttribute('href'))),['/StoryAssets/Details/1','/Scenes/Edit/1','/Scenes/Edit/2']);
await page.getByRole('button',{name:'Parcel 2 ยท Present'}).click();
const next=structuredClone(frame);next.scope.sceneID=4;await mount(next);
assert.equal(await page.locator('[data-asset-detail] h3').first().innerText(),'Parcel 2');
assert.match(await page.locator('[data-asset-detail]').innerText(),/Unknown/);
await page.locator('[data-spatial-splitter]').focus();await page.keyboard.press('ArrowRight');assert.equal(await page.locator('[data-spatial-splitter]').getAttribute('aria-valuenow'),'32');
await page.setViewportSize({width:460,height:850});await page.waitForFunction(()=>document.querySelector('[data-spatial-layout]').dataset.orientation==='vertical');
assert.equal(await page.locator('[data-spatial-splitter]').getAttribute('aria-orientation'),'horizontal');
const boxes=await page.locator('[data-spatial-layout]').evaluate(n=>[...n.children].map(x=>x.getBoundingClientRect().toJSON()));assert(boxes[2].y>boxes[0].y);assert(boxes.every(b=>b.width>0&&b.height>0));
await page.setViewportSize({width:1280,height:850});await page.waitForFunction(()=>document.querySelector('[data-spatial-layout]').dataset.orientation==='horizontal');assert.equal(await page.locator('[data-spatial-splitter]').getAttribute('aria-valuenow'),'32');
await mount(frame,1);assert.equal(await page.locator('[data-pane="1"] [data-spatial-splitter]').getAttribute('aria-valuenow'),'30');assert.equal(await page.locator('[data-pane="1"] [data-asset-detail] h3').first().innerText(),'Parcel 1');
next.assets=[];await mount(next);assert.match(await page.locator('[data-pane="0"] [data-asset-list]').innerText(),/No assets linked/);
console.log('PASS Context Assets source links, selection across scenes, unknown possession, shared splitter keyboard, narrow stacking, pane isolation and empty scene');
}finally{await browser.close();}
})().catch(e=>{console.error(e);process.exitCode=1;});