// Actual shared Names Library query parity, read-only on an explicitly disposable Development project. const {chromium}=require('playwright'),fs=require('node:fs'),assert=require('node:assert/strict'); (async()=>{assert(process.env.PD_CONTEXT_IDENTITY&&process.env.PD_CONTEXT_SCOPE);const who=JSON.parse(fs.readFileSync(process.env.PD_CONTEXT_IDENTITY)),s=JSON.parse(fs.readFileSync(process.env.PD_CONTEXT_SCOPE));assert.equal(who.Database,'PlotDirector_Development');assert.equal(who.UserID,s.user);assert(who.Email.startsWith('pwa-browser-'));assert(s.libraryTestName&&s.unusedBook);const b=await chromium.launch();let step='login';try{const c=await b.newContext({viewport:{width:1280,height:900}}),main=await c.newPage();await main.goto('https://dev.plotdirector.com/Account/Login');await main.locator('[name=Email]').fill(who.Email);await main.locator('[name=Password]').fill(who.Password);await Promise.all([main.waitForURL(u=>u.pathname!='/Account/Login'),main.locator('button[type=submit]').click()]);const names=await c.newPage(); await main.goto(`https://dev.plotdirector.com/Names?ProjectID=${s.project}&BookID=${s.book}&Search=${encodeURIComponent(s.libraryTestName)}&UsedScope=Book`);const locales=await main.locator('[name=LocaleCode] option').evaluateAll(ns=>ns.filter(n=>n.value).map(n=>({code:n.value,label:n.textContent.trim()})));assert(locales.length>=2);let testLocale=s.libraryLocale;if(!testLocale){const row=main.locator('tbody tr').first(),label=(await row.locator('td').nth(1).locator('.muted').innerText()).split(' ยท usage')[0].trim();testLocale=locales.find(l=>l.label===label)?.code||locales[0].code;} const common={ProjectID:String(s.project),BookID:String(s.book),UsedScope:'Book',LocaleCode:testLocale,BirthYear:s.libraryBirthYear||'',Page:'1',PageSize:'25'}; const cases=[{}, {Search:'zzxy-uncommon-qualification'}, {Search:s.libraryTestName,UsedOnly:'true'}, {Search:s.libraryTestName,BookID:String(s.unusedBook),UsedOnly:'false'}, {Search:s.libraryTestName,UsedScope:'Project',BookID:''}, {Search:'Jo',GenderPreference:'Female',GenderThreshold:'0.7',NameType:'Variant',MinimumUsage:'0.01'}, {LocaleCode:locales[0].code,Page:'2'}, {LocaleCode:locales[1].code,UsedOnly:'false'}, {Search:s.libraryTestName.slice(0,-1)}, {Search:s.libraryTestName,UsedScope:'Library',BookID:''}];let records=[]; for(let i=0;irows.map(r=>({id:Number(r.dataset.nameLibraryId),used:Number(r.dataset.nameUsedCount)})));await names.goto('https://dev.plotdirector.com/Writing/Context/Names/Search?contextBookId='+s.book+'&'+q);const actual=await names.locator('[data-context-name-id]').evaluateAll(rows=>rows.map(r=>({id:Number(r.dataset.contextNameId),used:Number(r.dataset.usedCount)})));assert.deepEqual(actual,expected);assert(actual.length<=25);records.push(actual);} assert(records[0].length>0&&records[6].length>0,'default and pagination searches must exercise actual candidates'); assert(records[2].some(r=>r.used>0),'recorded alias must be used in its character appearance book');assert(records[3].every(r=>r.used===0)&&records[3].length>0,'same alias must be unused in empty second book');assert(records[4].some(r=>r.used>0),'project usage remains recorded'); step='exact canonical/alias usage query';const response=await c.request.get('https://dev.plotdirector.com/Writing/Context/Names/Usage?'+new URLSearchParams({contextBookId:s.book,search:s.libraryTestName,usedScope:'Project'}));assert(response.ok());assert((await response.text()).includes('data-name-identity="'+s.characters[0]+'"')); console.log('PASS Development Names parity: ten equivalent main/Context searches match exact candidate IDs and usage counts; empty/uncommon/near search, aliases, used/unused, book/project scope, gender/type/commonness, multiple locales and pagination; current-book alias used, empty other-book unused; bounded 25 rows.'); }catch(e){console.error('FAILED '+step+': '+e.message);process.exitCode=1;}finally{await b.close();}})();