54 lines
5.2 KiB
JavaScript
54 lines
5.2 KiB
JavaScript
const {chromium}=require(process.env.PLAYWRIGHT_MODULE||'playwright');
|
|
const fs=require('node:fs'),assert=require('node:assert/strict');
|
|
const html=fs.readFileSync('target/podcast-player-fixture.html','utf8'),alpine=fs.readFileSync('target/podcast-alpine.min.js','utf8');
|
|
const near=(a,b,message)=>assert.ok(Math.abs(a-b)<1,`${message}: ${a} vs ${b}`);
|
|
(async()=>{
|
|
const browser=await chromium.launch({headless:true,...(process.env.CHROME_PATH?{executablePath:process.env.CHROME_PATH}:{})});
|
|
try{
|
|
for(const width of [320,390,560,820,1440]){
|
|
const page=await browser.newPage({viewport:{width,height:900}}),errors=[];page.on('pageerror',e=>errors.push(e.message));
|
|
await page.route('**/*',route=>{
|
|
const u=new URL(route.request().url());
|
|
if(u.hostname==='search.test'&&u.pathname==='/')return route.fulfill({contentType:'text/html',body:html});
|
|
if(u.hostname==='cdn.jsdelivr.net'&&u.pathname.includes('alpine'))return route.fulfill({contentType:'application/javascript',body:alpine});
|
|
if(u.pathname==='/api/player/me')return route.fulfill({json:{name:'Listener'}});
|
|
if(u.pathname==='/api/player/artists')return route.fulfill({json:{items:[],total:0,has_more:false}});
|
|
if(u.pathname==='/api/player/search')return route.fulfill({json:{artists:[],releases:[],tracks:[],podcasts:[]}});
|
|
return route.fulfill({json:[]});
|
|
});
|
|
await page.goto('http://search.test/');await page.waitForFunction(()=>window.Alpine?.store('user')?.profile?.name==='Listener');
|
|
const input=page.locator('#search-input'),panel=page.locator('.content-topbar');
|
|
const account=page.locator('.mobile-account-chip');
|
|
if(width<=560)await account.waitFor({state:'visible'});
|
|
const original=await input.boundingBox(),panelBefore=await panel.boundingBox(),buttonBefore=await page.locator('.torrent-import-btn').boundingBox();
|
|
await input.focus();
|
|
if(width<=560){
|
|
await page.waitForFunction(()=>{const input=document.querySelector('#search-input').getBoundingClientRect(),panel=document.querySelector('.content-topbar').getBoundingClientRect();return Math.abs(input.width-panel.width)<1;}).catch(async error=>{console.error({width,errors,state:await page.locator('.search-bar').evaluate(el=>({style:el.getAttribute('style'),classes:el.className,focused:document.activeElement.id,input:document.querySelector('#search-input').getBoundingClientRect().toJSON(),panel:el.closest('.content-topbar').getBoundingClientRect().toJSON()}))});throw error;});
|
|
const expanded=await input.boundingBox();near(expanded.x,panelBefore.x,'expanded left edge');near(expanded.width,panelBefore.width,'expanded width');
|
|
assert.ok(expanded.width>original.width+80,'search gains the space of neighboring controls');
|
|
// Account data may arrive after the user has already focused the input.
|
|
// Changing header controls must not shrink the expanded search again.
|
|
await page.evaluate(()=>Alpine.store('user').profile=null);
|
|
await account.waitFor({state:'hidden'});
|
|
await page.waitForFunction(()=>{const input=document.querySelector('#search-input').getBoundingClientRect(),panel=document.querySelector('.content-topbar').getBoundingClientRect();return Math.abs(input.width-panel.width)<1;});
|
|
await page.evaluate(()=>Alpine.store('user').profile={name:'Listener'});
|
|
await account.waitFor({state:'visible'});
|
|
await page.waitForFunction(()=>{const input=document.querySelector('#search-input').getBoundingClientRect(),panel=document.querySelector('.content-topbar').getBoundingClientRect();return Math.abs(input.width-panel.width)<1;});
|
|
const topmost=await page.evaluate(()=>{const panel=document.querySelector('.content-topbar').getBoundingClientRect();return document.elementFromPoint(panel.right-10,panel.top+panel.height/2)?.id;});
|
|
assert.equal(topmost,'search-input','search overlays account and download controls');
|
|
const duration=await page.locator('.search-field').evaluate(el=>parseFloat(getComputedStyle(el).transitionDuration));assert.ok(duration>0&&duration<=0.2);
|
|
if(buttonBefore){const after=await page.locator('.torrent-import-btn').boundingBox();near(after.x,buttonBefore.x,'neighboring controls remain in place');}
|
|
await input.fill('A long search query');await page.locator('.search-clear').click();
|
|
assert.equal(await input.inputValue(),'');assert.equal(await input.evaluate(el=>document.activeElement===el),true,'clear retains input focus');
|
|
await page.screenshot({path:`target/mobile-search-focused-${width}.png`});
|
|
await input.evaluate(el=>el.blur());
|
|
await page.waitForFunction(expected=>Math.abs(document.querySelector('#search-input').getBoundingClientRect().width-expected)<1,original.width);
|
|
near((await input.boundingBox()).x,original.x,'blur restores position');near((await panel.boundingBox()).height,panelBefore.height,'header height remains stable');
|
|
await page.emulateMedia({reducedMotion:'reduce'});assert.equal(await page.locator('.search-field').evaluate(el=>getComputedStyle(el).transitionDuration),'0s');
|
|
}else{near((await input.boundingBox()).width,original.width,'larger screens retain normal search width');}
|
|
assert.deepEqual(errors,[]);await page.close();
|
|
}
|
|
console.log('Mobile search expansion, overlay, clear, blur and reduced-motion checks passed.');
|
|
}finally{await browser.close();}
|
|
})().catch(e=>{console.error(e);process.exitCode=1;});
|