Files
pages/internal/server/testdata/city-keyboard.js
T
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

59 lines
5.4 KiB
JavaScript

window.addEventListener('load', async () => {
const assert = (condition, message) => { if (!condition) throw new Error(message); };
const wait = async condition => { for (let i=0;i<200;i++) { if(condition())return;await new Promise(r=>setTimeout(r,25)); } throw new Error('Keyboard scene did not render'); };
const key = (target, value, options={}) => { const event=new KeyboardEvent('keydown',{key:value,bubbles:true,cancelable:true,...options});target.dispatchEvent(event);return event; };
const click = (target, options={}) => target.dispatchEvent(new MouseEvent('click',{bubbles:true,cancelable:true,...options}));
const center = element => { const r=element.getBoundingClientRect();return {x:r.left+r.width/2,y:r.top+r.height/2}; };
try {
const url=location.href, historyLength=history.length;
const data=await(await fetch('/_/api/v1/city/map')).json();
const quartier=data.quartiers.find(q=>q.name==='life'), street=quartier.streets.find(s=>s.name==='home'), spot=street.spots.find(s=>s.title==='alpha');
await wait(()=>document.querySelectorAll('.city-quartier-tile').length===2);
const q=document.querySelector(`[data-city-uuid="${quartier.uuid}"]`);
q.focus();key(q,'Enter');
await wait(()=>document.querySelector(`[data-city-type="Street"][data-city-uuid="${street.uuid}"]`));
const s=document.querySelector(`[data-city-type="Street"][data-city-uuid="${street.uuid}"]`);
s.focus();key(s,'Enter');
await wait(()=>document.querySelector(`[data-city-uuid="${spot.uuid}"] .city-spot-entry`));
let entry=document.querySelector(`[data-city-uuid="${spot.uuid}"] .city-spot-entry`);
for(const [keys,axis,direction] of [[['ArrowLeft','a','h'],'x',-1],[['ArrowRight','d','l'],'x',1],[['ArrowUp','w','k'],'y',-1],[['ArrowDown','s','j'],'y',1]]){
for(const name of keys){
entry.focus();const origin=center(entry);const event=key(entry,name);
assert(event.defaultPrevented&&document.activeElement!==entry,`${name} did not move focus`);
assert((center(document.activeElement)[axis]-origin[axis])*direction>0,`${name} moved in the wrong direction`);
}
}
for(const options of [{ctrlKey:true},{metaKey:true},{altKey:true},{isComposing:true},{keyCode:229}]){
entry.focus();const event=key(entry,'h',options);assert(!event.defaultPrevented&&document.activeElement===entry,'Map consumed a modifier/composition shortcut');
}
entry.focus();key(entry,'Enter',{ctrlKey:true});
assert(!document.querySelector('.city-estate-detail'),'Ctrl+Enter activated a Spot');
for(const tag of ['input','textarea','div']){
const input=document.createElement(tag);if(tag==='div')input.contentEditable='true';document.body.append(input);input.focus();
const event=key(input,'w');assert(!event.defaultPrevented&&document.activeElement===input,`${tag} typing was hijacked`);input.remove();
}
window.PagesWikiCityMenu.open('settings');
const dialog=document.querySelector('[data-city-command-menu]'), before=document.activeElement;
const modalKey=key(document.body,'h');assert(!modalKey.defaultPrevented&&document.activeElement===before,'Modal key moved map focus');
assert(dialog.open,'Keyboard closed the utility modal');window.PagesWikiCityMenu.close();
click(entry,{ctrlKey:true});assert(entry.closest('.city-node').classList.contains('city-is-selected'),'Ctrl multi-selection stopped working');click(entry,{ctrlKey:true});
// Intercept before the popup's top-window navigation listener. Verify the
// exact activated href without leaving the browser regression fixture.
let captured='';const capture=event=>{const link=event.target.closest?.('.city-map-page-link');if(link){captured=link.getAttribute('href');event.preventDefault();event.stopImmediatePropagation();}};
window.addEventListener('click',capture,true);
const page=entry.closest('.city-node').querySelector('.city-map-page-link');page.focus();key(page,'Enter');
assert(captured.startsWith('/life/home/alpha/'),'Keyboard Page activation lost the semantic URL');window.removeEventListener('click',capture,true);
entry.focus();key(entry,'Enter');await wait(()=>document.querySelector('.city-estate-detail .city-map-page-link'));
assert(location.href===url&&history.length===historyLength,'Spot drill-down added browser history');
key(document.activeElement,'Backspace');await wait(()=>document.querySelector('.city-spot-entry'));
assert(document.activeElement.closest('[data-city-uuid]')?.dataset.cityUuid===spot.uuid,'Parent navigation did not restore Spot focus');
key(document.activeElement,'Backspace');await wait(()=>document.querySelector('.city-street-tile'));
assert(document.activeElement.dataset.cityUuid===street.uuid,'Parent navigation did not restore Street focus');
key(document.activeElement,'Backspace');await wait(()=>document.querySelector('.city-quartier-tile'));
assert(document.activeElement.dataset.cityUuid===quartier.uuid,'Parent navigation did not restore Quartier focus');
assert(key(document.activeElement,'Backspace').defaultPrevented,'Root Backspace was allowed to navigate browser history');
assert(location.href===url&&history.length===historyLength,'Map keyboard navigation changed browser history');
document.body.dataset.vimReady='true';
}catch(error){const box=document.createElement('div');box.id='error';box.textContent=error.stack||error.message;document.body.append(box);}
});