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},permissions:['clipboard-read','clipboard-write']});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')?[]:{connected:false,known:false}}));let queries=[];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})});});await c.route('**/Writing/Context/Names/*',async r=>{const u=new URL(r.request().url()),part=u.pathname.split('/').at(-1).toLowerCase();if(part==='search')queries.push(u.searchParams);await r.fulfill({response:await r.fetch({url:base+'/context-names-fixture/'+part+u.search})});});await c.route('**/Writing/Context/EditCharacter?*',async r=>r.fulfill({response:await r.fetch({url:base+'/context-edit-fixture?field=name'})}));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]','names');await p.waitForSelector('[data-context-name-id]'); step='discovery default and complete criteria';assert.equal(await p.locator('[data-context-name-id]').count(),25);assert.equal(await p.locator('[data-name-row]').count(),0);await p.locator('[name=Search]').fill('Uncommon');await p.selectOption('[name=LocaleCode]','fr-FR');await p.selectOption('[name=UsedOnly]','false');await p.click('[data-name-filters] summary');await p.locator('[name=BirthYear]').fill('1850');await p.selectOption('[data-name-gender]','Male:0.9');await p.selectOption('[name=NameType]','Variant');await p.selectOption('[name=MinimumUsage]','0.05');await p.selectOption('[name=UsedScope]','Book');await p.selectOption('[name=BookID]','13');await p.selectOption('[name=PageSize]','100');await p.locator('[data-context-name-search] [type=submit]').click();await p.waitForFunction(()=>document.querySelectorAll('[data-context-name-id]').length===100);for(const [key,value]of Object.entries({Search:'Uncommon',LocaleCode:'fr-FR',BirthYear:'1850',GenderPreference:'Male',GenderThreshold:'0.9',NameType:'Variant',MinimumUsage:'0.05',UsedScope:'Book',BookID:'13',UsedOnly:'false',Page:'1',PageSize:'100'}))assert.equal(queries.at(-1).get(key),value,key); step='profile, copy, related forms and selected usage';const first=p.locator('[data-context-name-id]').first();await first.locator('[data-name-copy]').click();assert.equal(await p.evaluate(()=>navigator.clipboard.readText()),'Uncommon 1');await first.locator('summary').click();assert.match(await first.innerText(),/Known spelling variant/);await first.locator('[data-name-usage]').click();await first.locator('[data-name-usage-fragment]').waitFor();await first.locator('[data-name-identity]').click();await p.waitForSelector('[data-name-identity-fragment]');assert.match(await p.locator('[data-name-identity-fragment]').innerText(),/Known variant/);await p.locator('[data-context-edit="name"]').click();await p.waitForSelector('[data-context-edit-form]');await p.click('[data-context-edit-cancel]'); step='pagination preserves all criteria';await p.click('[data-name-page="2"]');await p.waitForSelector('[data-name-results-fragment][data-page="2"]');assert.equal(queries.at(-1).get('GenderThreshold'),'0.9');assert.equal(await p.locator('[data-context-name-id]').first().getAttribute('data-context-name-id'),'101'); step='independent same-tool panes and book scope';await p.selectOption('[data-context-layout]','columns');await p.selectOption('[data-context-pane="1"] [data-context-tab]','names');await p.locator('[data-context-pane="1"] [name=Search]').fill('Other lookup');assert.equal(await p.locator('[data-context-pane="0"] [name=Search]').inputValue(),'Uncommon');const before=queries.length;await p.evaluate(()=>document.querySelector('[data-context-workspace]').contextController.setPosition({projectId:7,bookId:12,chapterId:11,sceneId:23}));await p.waitForTimeout(150);assert.equal(queries.length,before); step='actual pane widths and substantial result set';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(60);assert(await p.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+2),width+'/'+layout);}}assert.deepEqual(errors,[]);console.log('PASS Names discovery browser: all library criteria, 100 bounded candidates, profiles/related forms/copy, on-demand usage/identity/correction, pagination, book scope, independent panes and desktop/half/stacked/mobile widths (query parity qualified separately against Development).'); }catch(e){console.error('FAILED '+step+': '+e.stack);process.exitCode=1;}finally{await b.close();}})();