calendar
All repositories: gitoria
6.2 KB
import {spawn} from 'node:child_process';import {mkdirSync,writeFileSync} from 'node:fs';import {resolve,join} from 'node:path';import {launchBrowser,sleep} from './cdp.mjs';const app=resolve(process.env.TEST_APP||'.'), scratch=resolve(process.env.MONTH_SCRATCH||'.scratch/month-url'),base='http://localhost:8812';mkdirSync(scratch,{recursive:true});let server,browser,page,log='',passed=0,failed=0;function check(name,v){console.log((v?'ok ':'FAIL ')+name);if(v)passed++;else {failed++;throw Error(name)}}const pages=async()=> (await (await fetch(process.env.HL_CDP_URL+'/json/list')).json()).filter(p=>p.type==='page');try{if(!process.env.MONTH_EXTERNAL)server=spawn(join(app,'bin/hybriel'),['project.hl'],{cwd:app,env:{...process.env,CALENDAR_PORT:'8812',CALENDAR_WATCH:'0',CALENDAR_PUBLIC_URL:base,CALENDAR_STORAGE:join(scratch,'db'),CALENDAR_SESSIONS:join(scratch,'sessions')+'/'},stdio:['ignore','pipe','pipe']});if(server){server.stdout.on('data',d=>log+=d);server.stderr.on('data',d=>log+=d);}for(const end=Date.now()+150000;;){try{if((await fetch(base)).ok)break}catch{}if(Date.now()>end)throw Error(log);await sleep(250)}if(process.env.HL_CDP_URL)writeFileSync(join(scratch,'pages-before.json'),JSON.stringify(await pages(),null,2));browser=await launchBrowser({debugPortRange:[8814,8819]});page=await browser.newPage();await page.send('Network.setExtraHTTPHeaders',{headers:{'Accept-Language':'en'}});const ready=()=>page.waitFor('!!document.querySelector("#month-next") && !!window.__hl?.socket && window.__hl.socket.readyState===1',{timeout:120000});// Neighbour address identifies the displayed month independently of translated names.const title=()=>page.evaluate("document.querySelector('#month-next').getAttribute('href')");const reload=async()=>{page._loaded=false;await page.send('Page.reload');const end=Date.now()+120000;while(!page._loaded){if(Date.now()>end)throw Error('reload load event');await sleep(25)}};const key=async(k,n)=>{await page.send('Input.dispatchKeyEvent',{type:'keyDown',key:k,code:k,windowsVirtualKeyCode:n});await page.send('Input.dispatchKeyEvent',{type:'keyUp',key:k,code:k,windowsVirtualKeyCode:n})};for(const [width,height] of (process.env.HL_CDP_URL?[[390,844],[800,1280],[1280,800],[1280,900]]:[[390,844]])){await page.send('Emulation.setDeviceMetricsOverride',{width,height,deviceScaleFactor:1,mobile:width<600});await page.goto(base,{timeout:120000});await ready();const old=await title();const shot=async(label)=>{const {data}=await page.send('Page.captureScreenshot',{format:'png'});writeFileSync(join(scratch,width+'-'+height+'-'+label+'.png'),Buffer.from(data,'base64'))};await shot('before');await page.click('#month-next');await page.waitFor(`document.querySelector('#month-next').getAttribute('href')!==${JSON.stringify(old)}`);const url=await page.evaluate('location.href'),next=await title();check(width+' next changes address',url!==base+'/' && /[?&]m=\d{4}-\d{2}/.test(url));await reload();await ready();check(width+' reload keeps month',await title()===next);await page.evaluate('history.back()');await page.waitFor(`document.querySelector('#month-next').getAttribute('href')===${JSON.stringify(old)}`);check(width+' Back keeps month',await page.evaluate('location.search')==='');await page.evaluate('history.forward()');await page.waitFor(`document.querySelector('#month-next').getAttribute('href')===${JSON.stringify(next)}`);check(width+' Forward keeps month',true);await page.click('#month-prev');await page.waitFor(`document.querySelector('#month-next').getAttribute('href')===${JSON.stringify(old)}`);check(width+' previous changes address',await page.evaluate('location.href')!==url);await page.focus('#month-prev');await key('Tab',9);check(width+' Tab reaches next',await page.evaluate('document.activeElement.id')==='month-next');await key('Enter',13);await page.waitFor(`document.querySelector('#month-next').getAttribute('href')===${JSON.stringify(next)}`);check(width+' Enter navigates',await page.evaluate('location.href')===url);await shot('after');if(!process.env.HL_CDP_URL){const fresh=await browser.newPage();try{await fresh.goto(url,{timeout:120000});await fresh.waitForSelector('#month-title');check('fresh tab renders addressed month',await fresh.evaluate('document.querySelector("#month-next").getAttribute("href")')===await page.evaluate('document.querySelector("#month-next").getAttribute("href")'))}finally{await fresh.close()}}// A fresh document in the one owned tab exercises a copied address without opening another tab in his browser.await page.goto('about:blank');await page.goto(url,{timeout:120000});await ready();check(width+' fresh document keeps month',await title()===next);const html=await(await fetch(url,{headers:{'Accept-Language':'en'}})).text();writeFileSync(join(scratch,'served.html'),html);const [year,month]=new URL(url).searchParams.get('m').split('-').map(Number);const serverTitle=new Intl.DateTimeFormat('en',{month:'long',timeZone:'UTC'}).format(new Date(Date.UTC(year,month-1,15)))+' '+year;check(width+' document renders month',html.includes('<h2 id="month-title">'+serverTitle+'</h2>'));await page.waitFor('!!navigator.serviceWorker.controller',{timeout:120000});await page.setOffline(true);await reload();await page.waitForSelector('#month-title',{timeout:120000});check(width+' offline reload keeps shell/month',await title()===next);await page.setOffline(false);await page.goto(base+'/?m=bad',{timeout:120000});await ready();check(width+' invalid month uses current',await title()===old);}}catch(e){console.error(e.stack);if(page){console.log('STATE',await page.evaluate('JSON.stringify({url:location.href,title:document.querySelector("#month-title")?.textContent,days:document.querySelector("#month-days")?.textContent})'));console.log(page.dumpConsole())}failed++}finally{if(page)await page.close();if(browser)await browser.close();if(process.env.HL_CDP_URL){writeFileSync(join(scratch,'pages-after.json'),JSON.stringify(await pages(),null,2));writeFileSync(join(scratch,'pages-final.json'),JSON.stringify(await pages(),null,2))}if(server){server.kill('SIGTERM');await new Promise(r=>{if(server.exitCode!==null)return r();server.once('exit',r);setTimeout(r,5000)})}writeFileSync(join(scratch,'server.log'),log)}console.log(`${passed} passed, ${failed} failed`);process.exitCode=failed?1:0;
Branches
Latest commits
- 384d3d23|||PR give calendar months their own addressAtlas
- fff2f537Merge branch 'calendar31-lib' into mainAtlas
- 07cde3dc|||PR keep worker records out of the repo; README vendor is day72Atlas
- a753b0ab|||PR move shared calendar logic to libAtlas
- b32a921dMerge branch 'vendor-day72' into mainAtlas
- 60a0dd19|||PR Hybriel day72 (00c9dbb8) instead of the withdrawn day73Atlas
- 53f5464dMerge branch 'calendar26-day73' into mainAtlas
- 338499ec|||PR Keep reminder settings open during editing and rejected savesAtlas
- ad9e8d59|||PR Use framework page and popover state, Hybriel day73Atlas
- cab224a1Re-vendor calendar to verified Hybriel day73Atlas
- f9b8f918Merge branch 'calendar-vendor-day61' into mainAtlas
- 66341793|||PR Vendor Hybriel day61Atlas
- 36fc596bMerge branch 'vendor-day59' into mainAtlas
- b37cc1da|||PR Vendor Hybriel day59Atlas
- 05665102Merge branch 'vendor-day56' into mainAtlas
- 687296fb|||PR Vendor Hybriel day56Atlas
- a6166d4eMerge branch 'hl-standard-3' into mainAtlas
- 6d0eb3b0|||PR Calendar day add buttons only when signed in (Kunibert minor)Atlas
- add4b499|||PR Make calendar day actions keyboard accessibleAtlas
- 79e49b4dMerge branch 'hl-standard' into mainAtlas