PlotDirector/PlotLine.Tests/Browser/names-workspace.cjs

30 lines
8.7 KiB
JavaScript

// Compiled main Names Razor and shared production scripts; disposable HTTP data only.
const {chromium}=require('playwright'),assert=require('node:assert/strict'),fs=require('node:fs'),os=require('node:os'),path=require('node:path');
(async()=>{
const installed=process.argv.includes('--installed'),profile=installed?fs.mkdtempSync(path.join(os.tmpdir(),'pd-names-pwa-')):null;
const persistent=installed?await chromium.launchPersistentContext(profile,{headless:false,viewport:{width:1920,height:1080}}):null;
const browser=installed?persistent.browser():await chromium.launch(),cdp=installed?await browser.newBrowserCDPSession():null,manifestId='http://127.0.0.1:5238/Projects';
let page;try{
if(installed){await persistent.pages()[0].goto('http://127.0.0.1:5238/fixture?page=calendar-test');await cdp.send('PWA.install',{manifestId,installUrlOrBundleUrl:'http://127.0.0.1:5238/fixture?page=calendar-test'});await cdp.send('PWA.changeAppUserSettings',{manifestId,displayMode:'standalone'});const opened=persistent.waitForEvent('page');await cdp.send('PWA.launch',{manifestId,url:'http://127.0.0.1:5238/fixture?page=calendar-test'});page=await opened;await page.waitForLoadState();assert(await page.evaluate(()=>matchMedia('(display-mode: standalone)').matches));}else page=await browser.newPage({viewport:{width:1920,height:1080}});
const errors=[],calls={search:0,details:0};page.on('pageerror',e=>errors.push(e.message));
await page.route('**/Names/Results?*',async route=>{calls.search++;const u=new URL(route.request().url());await route.fulfill({response:await page.request.get('http://127.0.0.1:5238/names-workspace-fixture/results?'+u.searchParams)});});
await page.route('**/Names/Details?*',async route=>{calls.details++;const u=new URL(route.request().url());await route.fulfill({response:await page.request.get('http://127.0.0.1:5238/names-workspace-fixture/details?'+u.searchParams)});});
await page.route('**/SharedFocus/**',route=>route.fulfill({json:{success:true}}));
await page.goto('http://127.0.0.1:5238/names-workspace-fixture/page');await page.locator('[data-name-select]').first().waitFor();assert.equal(calls.details,0);
await page.locator('[name=Search]').fill('George');await page.locator('[data-names-search]').evaluate(f=>f.requestSubmit());await page.waitForFunction(()=>document.querySelector('[data-name-select]')?.dataset.nameSelect==='George 1');
await page.locator('[data-name-select]').first().click();await page.locator('[data-name-details-fragment]').waitFor();assert.equal(await page.locator('.names-association').count(),2);assert((await page.locator('.names-details').innerText()).includes('Cat'));assert((await page.locator('.names-details').innerText()).includes('Known name / alias'));assert.equal(await page.locator('.names-association a').first().getAttribute('href'),'/Characters/Details/501');
fs.mkdirSync('/tmp/pd-names-workspace',{recursive:true});const measurements=[];
for(const width of [1280,1920,2560,3840,768,390]){await page.setViewportSize({width,height:1080});const m=await page.evaluate(()=>({width:innerWidth,overflow:document.documentElement.scrollWidth>innerWidth,workspace:document.querySelector('[data-names-workspace]').getBoundingClientRect().width,rowHeight:document.querySelector('.names-result').getBoundingClientRect().height,details:!!document.querySelector('[data-names-details]').getBoundingClientRect().width}));assert(!m.overflow);assert(m.rowHeight<=65);measurements.push(m);if(width===2560||width===390)await page.screenshot({path:`/tmp/pd-names-workspace/${installed?'pwa':'browser'}-${width}.png`});if(width===390){await page.locator('[data-names-back]').click();assert(await page.locator('[data-names-details]').isHidden());await page.locator('[data-name-select]').first().click();await page.locator('[data-name-details-fragment]').waitFor();assert(await page.locator('[data-names-results-host]').isHidden());}}
await page.setViewportSize({width:1920,height:1080});for(const theme of ['plotdirector','modern-light','modern-dark','warm-light','warm-dark','rose-pink','baby-blue']){await page.evaluate(theme=>{document.documentElement.dataset.appTheme=theme;document.documentElement.dataset.bsTheme=theme.endsWith('dark')?'dark':'light';document.documentElement.dataset.theme=theme.endsWith('dark')?'dark':'light';},theme);await page.screenshot({path:`/tmp/pd-names-workspace/${installed?'pwa':'browser'}-${theme}.png`});}
await page.reload();await page.locator('[data-name-details-fragment]').waitFor();assert.equal(await page.locator('[name=Search]').inputValue(),'George');assert.equal(await page.locator('[data-name-select][aria-pressed=true]').count(),1);assert.deepEqual(errors,[]);
assert.equal(await page.locator('.names-association a').first().getAttribute('target'),null);assert.equal(await page.locator('.names-association a').nth(1).getAttribute('target'),null);
await page.locator('[data-names-filters]').evaluate(n=>n.open=true);await page.locator('[name=PageSize]').selectOption('50');await page.locator('[name=BookID]').selectOption('13');await page.locator('[name=LocaleCode]').selectOption('fr-FR');await page.locator('[name=UsedOnly]').selectOption('false');await page.locator('[name=SortOrder]').selectOption('name-desc');
await page.waitForFunction(()=>JSON.parse(document.querySelector('[data-names-applied]').textContent).SortOrder==='name-desc');await page.locator('[data-name-page="2"]').click();await page.waitForFunction(()=>document.querySelector('[data-names-results]').dataset.page==='2');await page.locator('[data-name-select]').first().click();await page.waitForFunction(()=>document.querySelector('[data-name-details-fragment] h2')?.textContent==='George 51');
const scroll=await page.evaluate(()=>{let node=document.querySelector('[data-names-workspace]').parentElement;for(;node&&node!==document.body&&node!==document.documentElement;node=node.parentElement)if(/auto|scroll/.test(getComputedStyle(node).overflowY)&&node.scrollHeight>node.clientHeight){node.scrollTo({top:450,behavior:"instant"});return node.scrollTop}document.scrollingElement.scrollTo({top:450,behavior:"instant"});return document.scrollingElement.scrollTop});assert(scroll>100);
const pages=page.context().pages().length;await page.route('**/Characters/Details/501',r=>r.fulfill({contentType:'text/html',body:'<h1>Canonical Character Details</h1>'}));await page.route('**/Characters/Edit/501',r=>r.fulfill({contentType:'text/html',body:'<h1>Canonical Character Editor</h1>'}));
for(const action of ['View Character','Edit Character']){await Promise.all([page.waitForURL(action==='View Character'?'**/Characters/Details/501':'**/Characters/Edit/501'),page.getByRole('link',{name:action,exact:true}).first().click()]);assert.equal(page.context().pages().length,pages);await page.goBack();await page.waitForFunction(()=>document.querySelector('[data-name-details-fragment] h2')?.textContent==='George 51');assert.equal(await page.locator('[name=SortOrder]').inputValue(),'name-desc');assert.equal(await page.locator('[name=Page]').inputValue(),'2');assert.equal(await page.locator('[name=PageSize]').inputValue(),'50');assert.equal(await page.locator('[name=BookID]').inputValue(),'13');assert.equal(await page.locator('[name=LocaleCode]').inputValue(),'fr-FR');await page.waitForFunction(expected=>{let n=document.querySelector('[data-names-workspace]').parentElement;for(;n&&n!==document.body&&n!==document.documentElement;n=n.parentElement)if(/auto|scroll/.test(getComputedStyle(n).overflowY)&&n.scrollHeight>n.clientHeight)return Math.abs(n.scrollTop-expected)<3;return Math.abs(document.scrollingElement.scrollTop-expected)<3},scroll);}
for(const expanded of [false,true]){await page.locator('[data-names-filters]').evaluate((n,open)=>n.open=open,expanded);await page.locator('[name=Search]').fill('R');await page.locator('[data-names-clear]').click();await page.waitForFunction(()=>{const f=JSON.parse(document.querySelector('[data-names-applied]').textContent);return !f.Search&&f.Page===1});assert.equal(await page.locator('[name=Search]').inputValue(),'');assert.equal(await page.locator('[name=UsedOnly]').inputValue(),'');assert.equal(await page.locator('[name=LocaleCode]').inputValue(),'');assert.equal(await page.locator('[name=PageSize]').inputValue(),'50');assert.equal(await page.locator('[name=BookID]').inputValue(),'13');assert.equal(await page.locator('[name=SortOrder]').inputValue(),'name-desc');}
fs.writeFileSync(`/tmp/pd-names-workspace/${installed?'pwa':'browser'}-measurements.json`,JSON.stringify({measurements,calls},null,2));console.log('PASS Names compact results, deferred canonical details, animal/alias associations, navigation, state, themes, wide/narrow layouts',calls);
}finally{if(installed){await cdp.send('PWA.uninstall',{manifestId}).catch(()=>{});await persistent.close();fs.rmSync(profile,{recursive:true,force:true});}else await browser.close();}
})().catch(e=>{console.error(e);process.exitCode=1;});