const {chromium}=require('playwright'),assert=require('node:assert/strict'),fs=require('node:fs'),path=require('node:path');const base=process.env.PD_BROWSER_BASE||'http://127.0.0.1:5238'; (async()=>{const b=await chromium.launch();let step='setup';try{const c=await b.newContext({viewport:{width:1366,height:900}});await c.route('**/cdn.jsdelivr.net/**signalr*',r=>r.fulfill({contentType:'application/javascript',body:fs.readFileSync(path.join(__dirname,'node_modules/@microsoft/signalr/dist/browser/signalr.min.js'))}));for(const url of ['**/native-companion/status','**/native-companion/connection-state','**/Writing/Context/Position?*'])await c.route(url,r=>r.fulfill({json:url.endsWith('status')?[]:{known:false,connected:false}}));await c.route('**/Writing/Context/Tabs/*',async r=>{const u=new URL(r.request().url());await r.fulfill({response:await r.fetch({url:base+'/context-tab-fixture/'+u.pathname.split('/').at(-1)+u.search})});});let graphReads=0,large=false,readOnly=false,editQuery;await c.route('**/Writing/Context/Relationships/Graph?*',async r=>{graphReads++;const u=new URL(r.request().url());await r.fulfill({response:await r.fetch({url:base+'/context-lenses-fixture'+u.search+'&large='+large+'&readonly='+readOnly})});});await c.route('**/Writing/Context/Relationships/Lookup?*',async r=>r.fulfill({response:await r.fetch({url:base+'/context-lenses-lookup-fixture'})}));await c.route('**/Writing/Context/EditRelationship?*',async r=>{editQuery=new URL(r.request().url()).searchParams;await r.fulfill({response:await r.fetch({url:base+'/context-relationship-edit-fixture'})});});for(const [route,fixture] of [['FamilyIndex','index'],['FamilyTree','tree']])await c.route('**/Writing/Context/Relationships/'+route+'?*',async r=>{const u=new URL(r.request().url());graphReads++;await r.fulfill({response:await r.fetch({url:base+'/context-family-'+fixture+'-fixture'+u.search+'&large='+large+'&readonly='+readOnly})});});const p=await c.newPage(),errors=[];p.on('pageerror',e=>errors.push(e.message));await p.goto(base+'/Writing/Context');await p.selectOption('[data-context-tab]','relationships');await p.waitForSelector('[data-graph-node="5"]'); async function selectEdge(id){await p.locator('[data-context-pane="0"] [data-relationship-svg]').scrollIntoViewIfNeeded();const hit=p.locator(`[data-context-pane="0"] [data-graph-connection="${id}"]`);const pt=await hit.evaluate(n=>{const p=n.getPointAtLength(n.getTotalLength()*.35),m=n.getScreenCTM();return {x:m.a*p.x+m.c*p.y+m.e,y:m.b*p.x+m.d*p.y+m.f};});await p.mouse.click(pt.x,pt.y);await p.locator(`[data-context-pane="0"] [data-graph-edge="${id}"].is-selected`).waitFor({state:'attached'});} step='genealogical view and specific connection edit';assert.equal(await p.locator('[data-relationship-lens="family"]').getAttribute('aria-pressed'),'true');assert.equal(await p.locator('[data-graph-node]').count(),9);assert.match(await p.locator('[data-graph-node="6"]').getAttribute('aria-label'),/deceased; author reference/);assert.equal(await p.locator('[data-relationship-details] [data-context-edit="life-status"]').count(),1);assert.match(await p.locator('[data-relationship-caption]').innerText(),/Author-established/);await p.locator('[data-graph-node="5"]').click();await p.locator('[data-relationship-details] details summary').click();await p.locator('[data-inspect-connection="10"]').click();await p.waitForFunction(()=>document.querySelector('[data-relationship-details]').textContent.includes('Biological Parent'));await p.locator('[data-relationship-details] [data-context-relationship-edit]').click();await p.waitForSelector('[data-context-edit-form]');assert.equal(editQuery.get('referenceLens'),'family');assert.equal(editQuery.get('mode'),'initial');await p.click('[data-context-edit-cancel]'); step='pan zoom and unchanged-family notification stability';await p.click('[data-graph-zoom="in"]');await p.locator('[data-relationship-svg]').focus();await p.keyboard.press('ArrowRight');const transform=await p.locator('[data-relationship-viewport]').getAttribute('transform');await p.evaluate(()=>{window.familyLayouts=0;const original=window.PlotDirectorContextRelationshipLayout.family;window.PlotDirectorContextRelationshipLayout.family=(...a)=>{familyLayouts++;return original(...a);};});const before=graphReads;await p.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.invalidate({bookId:12}));await p.waitForFunction(()=>document.querySelector('[data-graph-node="5"]'));await p.waitForTimeout(200);assert(graphReads>before);assert.equal(await p.evaluate(()=>familyLayouts),0);assert.equal(await p.locator('[data-relationship-viewport]').getAttribute('transform'),transform); step='Story network and recorded point';await p.click('[data-relationship-lens="story"]');await p.waitForFunction(()=>document.querySelector('[data-relationship-caption]').textContent.includes('Later developments are excluded'));assert(await p.locator('[data-graph-edge]').count()>30);assert.equal(await p.locator('[data-graph-node] rect.is-physical').count(),2);assert.equal(await p.locator('[data-relationship-details] [data-context-edit="life-status"]').count(),0);assert.doesNotMatch(await p.locator('[data-graph-node="6"]').getAttribute('aria-label'),/deceased/);await selectEdge(1);await p.waitForFunction(()=>document.querySelector('[data-relationship-details]').textContent.includes('Rival'));assert.match(await p.locator('[data-relationship-details]').innerText(),/does not prove a first meeting/);await p.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.setPosition({projectId:7,bookId:12,chapterId:11,sceneId:23}));await p.waitForFunction(()=>document.querySelector('[data-relationship-details]')?.textContent.includes('Later established'));await p.click('[data-relationship-lens="family"]');await p.waitForFunction(()=>document.querySelector('[data-relationship-caption]').textContent.includes('Author-established'));assert.equal(await p.locator('[data-family-key="21"]').getAttribute('aria-pressed'),'true'); step='lookup and recenter';await p.locator('[data-relationship-person]').fill('relative 7');await p.locator('[data-relationship-matches] button').waitFor();await p.locator('[data-relationship-matches] button').click();await p.waitForFunction(()=>document.querySelector('[data-relationship-details]').textContent.includes('Recorded person 7')); step='large family and independent panes';large=true;await p.locator('[data-relationship-svg]').focus();await p.keyboard.press('ArrowRight');await p.keyboard.press('ArrowRight');await p.locator('[data-family-branch="5"]').click();await p.waitForSelector('[data-graph-node="3"]');await p.selectOption('[data-context-layout]','columns');await p.selectOption('[data-context-pane="1"] [data-context-tab]','relationships');await p.locator('[data-context-pane="1"] [data-graph-node="22"]').waitFor();await p.locator('[data-context-pane="1"] [data-relationship-lens="story"]').click();await p.waitForTimeout(120);assert.equal(await p.locator('[data-context-pane="0"] [data-relationship-lens="family"]').getAttribute('aria-pressed'),'true'); step='container widths and keyboard navigation';for(const width of [1366,760,390]){await p.setViewportSize({width,height:900});for(const layout of ['single','columns','rows']){await p.selectOption('[data-context-layout]',layout);await p.waitForTimeout(50);assert(await p.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+2),width+'/'+layout);}}await p.selectOption('[data-context-layout]','single');await p.locator('[data-context-pane="0"] [data-relationship-svg]').focus();await p.keyboard.press('Home');await p.keyboard.press('+'); step='read-only graph removes edit controls';readOnly=true;await p.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.invalidate({bookId:12}));await p.waitForTimeout(350);assert.equal(await p.locator('[data-context-pane="0"] [data-relationship-add]:visible').count(),0);assert.deepEqual(errors,[]);console.log('PASS visual Relationships browser: Family and Story lenses, real SVG hit selection/edit, pan/zoom/keyboard, stable family layout through notifications, scene changes, lookup/recentre, compact family branches, dense Story network, independent panes, full/half/stacked/mobile and read-only controls.'); }catch(e){console.error('FAILED '+step+': '+e.stack);process.exitCode=1;}finally{await b.close();}})();