// Actual Context shell, reusable spatial partials and renderers; synthetic read-only fixtures. const {chromium}=require('playwright'),assert=require('node:assert/strict'); const base=process.env.PD_BROWSER_BASE||'http://127.0.0.1:5238'; (async()=>{ const browser=await chromium.launch();let step='setup'; try{ const context=await browser.newContext({viewport:{width:1440,height:1000}}),reads=[],writes=[],errors=[]; await context.route('**/native-companion/**',r=>r.fulfill({json:[]})); await context.route('**/SharedFocus/**',r=>r.fulfill({json:{}})); await context.route('**/Writing/Context/Position*',r=>r.fulfill({json:{connected:false}})); await context.route('**/Writing/Context/MyWindows*',r=>r.fulfill({json:[]})); await context.route('**/Writing/Context/Tabs/*',r=>{ const u=new URL(r.request().url());return r.fulfill({contentType:'text/html',body:`
Saved ${u.pathname.split('/').at(-1)}
`}); }); context.on('request',r=>{const path=new URL(r.url()).pathname;if(r.method()==='POST'&&!path.startsWith('/hubs/')&&path!=='/NarrativeFocus/Shared/Validate')writes.push(path);if(path.startsWith('/SpatialContext/')||path.includes('/Context/Tabs/'))reads.push({page:r.frame().page(),path});}); const page=await context.newPage();page.on('pageerror',e=>errors.push(e.message)); const tab=(p,i,value)=>p.selectOption(`[data-context-pane="${i}"] [data-context-tab]`,value); const stable=()=>page.waitForTimeout(250); const notify=(p,hints)=>p.evaluate(hints=>{for(const hint of hints)document.querySelector('[data-context-workspace]').contextController.invalidate(hint);},hints); const hint=kind=>({projectId:7,bookId:12,kind,entityId:1}); await page.goto(base+'/Writing/Context?pinned=true');await page.locator('[data-context-pane="0"] [data-context-tab-fragment]').waitFor(); await page.selectOption('[data-context-layout]','columns');await tab(page,1,'maps');await page.locator('iframe').waitFor();await stable(); const before=await page.evaluate(()=>({...document.querySelector('[data-context-workspace]').contextController.position})); step='map bursts only refresh map, one state read';reads.length=0; await notify(page,Array.from({length:8},()=>hint('maps')));await stable(); assert.equal(reads.filter(r=>r.path.includes('/Tabs/')).length,0); assert.equal(reads.filter(r=>r.path==='/SpatialContext/State').length,1); step='scope isolation';reads.length=0; await notify(page,[{...hint('maps'),projectId:8},{...hint('maps'),bookId:13}]);await stable();assert.equal(reads.length,0); step='unrelated dates do not refresh spatial pane';reads.length=0; await notify(page,[hint('dates')]);await stable();assert.equal(reads.filter(r=>r.path.startsWith('/SpatialContext/')).length,0);assert.equal(reads.filter(r=>r.path.toLowerCase().includes('/tabs/overview')).length,1); step='hidden panel deferred until shown';await tab(page,1,'characters');await stable();reads.length=0; await notify(page,[hint('maps')]);await stable();assert.equal(reads.length,0); await tab(page,1,'maps');await stable();assert.equal(reads.filter(r=>r.path==='/SpatialContext/State').length,1); step='two visible map panes share state read';await tab(page,0,'maps');await page.locator('[data-context-pane="0"] iframe').waitFor();await stable();reads.length=0; await notify(page,[hint('maps'),hint('maps')]);await stable();assert.equal(reads.filter(r=>r.path==='/SpatialContext/State').length,1); step='separate windows independently refresh while held';const second=await context.newPage();second.on('pageerror',e=>errors.push(e.message));await second.goto(base+'/Writing/Context?pinned=true');await tab(second,0,'locations');await second.getByRole('heading',{name:'Bedroom',exact:true}).waitFor();await stable();reads.length=0; await Promise.all([notify(page,[hint('locations')]),notify(second,[hint('locations')])]);await stable(); assert.equal(reads.filter(r=>r.path==='/SpatialContext/State'&&r.page===page).length,1); assert.equal(reads.filter(r=>r.path==='/SpatialContext/State'&&r.page===second).length,1); assert.deepEqual(await page.evaluate(()=>({...document.querySelector('[data-context-workspace]').contextController.position})),before); assert.deepEqual(writes,[],'passive refresh never posts a manuscript/Word navigation');assert.deepEqual(errors,[]); console.log('PASS saved-data refresh: category targeting, burst coalescing, project/book isolation, deferred hidden panels, shared split reads, independent held windows, no narrative writes.'); }catch(e){console.error('FAIL '+step,e);process.exitCode=1;}finally{await browser.close();} })();