window.addEventListener('load', async () => { const wait = async check => { for (let i = 0; i < 200; i++) { if (check()) return; await new Promise(resolve => setTimeout(resolve, 25)); } throw new Error('Immersive City did not finish rendering'); }; const assert = (value, message) => { if (!value) throw new Error(message); }; const click = (element, ctrlKey = false) => { assert(element, 'Missing click target'); element.dispatchEvent(new MouseEvent('click', {bubbles:true, cancelable:true, ctrlKey})); }; const boxesOverlap = (a, b) => a[0] < b[0]+b[2] && b[0] < a[0]+a[2] && a[1] < b[1]+b[3] && b[1] < a[1]+a[3]; const inspectStreet = () => { const plots = [...document.querySelectorAll('[data-estate-box]')]; assert(plots.length <= 6, 'Street exceeded bounded Spot projection'); plots.forEach((plot, i) => { const bounds = plot.dataset.estateBox.split(',').map(Number); for (const other of plots.slice(i+1)) assert(!boxesOverlap(bounds, other.dataset.estateBox.split(',').map(Number)), 'Spot courtyards overlap'); assert(plot.querySelectorAll('.city-map-page-link').length <= 6, 'Street renders unbounded Page buildings'); const more = plot.querySelector('.city-estate-more'); if (more) { const m = more.getBBox(); for (const page of plot.querySelectorAll('.city-map-page-link')) { const p = page.getBBox(); assert(!boxesOverlap([m.x,m.y,m.width,m.height], [p.x,p.y,p.width,p.height]), 'More Pages badge covers a Page'); } } }); return plots.map(plot => plot.dataset.cityUuid); }; try { const fixture = new URLSearchParams(location.search).get('fixture'); if (fixture === 'mobile-light') { await new Promise((resolve, reject) => { const style = document.createElement('link'); style.rel = 'stylesheet'; style.href = '/static/css/theme/index-white.css'; style.onload = resolve; style.onerror = reject; document.head.append(style); }); } const originalURL = location.href, originalHistory = history.length; const data = await (await fetch('/_/api/v1/city/map')).json(); const quartier = data.quartiers[0], street = quartier.streets.find(s => s.name === 'home'); const alpha = street.spots.find(s => s.title === 'alpha'); await wait(() => document.querySelector('.city-quartier-tile')); assert(document.querySelector('#city-map').getBoundingClientRect().height > innerHeight * .8, 'Utilities occupy the map'); assert(document.documentElement.scrollHeight <= innerHeight+1, 'Map has document scrolling'); assert(!document.querySelector('[data-city-command-menu]').open, 'Utility menu is open on entry'); click(document.querySelector('.city-quartier-tile')); await wait(() => document.querySelector('.city-street-tile')); click(document.querySelector(`[data-city-type="Street"][data-city-uuid="${street.uuid}"]`)); await wait(() => document.querySelector('[data-estate-box]')); const seenSpots = new Set(inspectStreet()); assert(!document.querySelector('[data-city-scene-pager]').hidden, 'Large Street has no pagination'); click(document.querySelector('[data-city-scene-next]')); await wait(() => document.querySelector('[data-city-scene-next]').disabled && document.querySelector('[data-estate-box]')); inspectStreet().forEach(id => seenSpots.add(id)); const nonempty = street.spots.filter(s => !window.PagesWikiCityEstates.undefinedSpot(s) || s.pages.length); assert(seenSpots.size === nonempty.length, 'Pagination makes some Spots unreachable'); click(document.querySelector('[data-city-scene-prev]')); await wait(() => document.querySelector(`[data-city-uuid="${alpha.uuid}"] .city-spot-entry`)); click(document.querySelector(`[data-city-uuid="${alpha.uuid}"] .city-spot-entry`)); await wait(() => document.querySelector('.city-estate-detail .city-map-page-link')); assert(location.href === originalURL && history.length === originalHistory, 'Opening a Spot navigated to a document'); assert(document.querySelector('[aria-current="location"]').textContent === alpha.title, 'Breadcrumb omits focused Spot'); assert(document.querySelector('.city-portal-link').getAttribute('href') === '/life/home/alpha', 'Spot portal URL changed'); const pageLinks = [...document.querySelectorAll('.city-map-page-link')]; assert(pageLinks.length === 24, 'Focused Spot must bound Page rendering'); const seenPages = new Map(pageLinks.map(a => [a.dataset.pageUuid,a.getAttribute('href')])); click(pageLinks[0], true); assert(pageLinks[0].classList.contains('city-is-selected') && location.href === originalURL, 'Page building cannot be Ctrl-selected'); click(pageLinks[0], true); click(document.querySelector('[data-city-scene-next]')); await wait(() => document.querySelector('[data-city-scene-next]').disabled && document.querySelectorAll('.city-map-page-link').length < 24); for (const a of document.querySelectorAll('.city-map-page-link')) seenPages.set(a.dataset.pageUuid,a.getAttribute('href')); click(document.querySelector('.city-estate-focused > .city-courtyard')); assert(document.querySelector('[data-city-scene-next]').disabled, 'Empty focused courtyard resets pagination'); const expected = window.PagesWikiCityEstates.surroundingPages(alpha); assert(!seenPages.has(alpha.portal_page_uuid), 'Portal duplicates itself as a child Page'); assert(seenPages.size === expected.length, 'Focused Spot hides Pages permanently'); expected.forEach(p => assert(seenPages.get(p.page_uuid) === p.path, 'Page building points to the wrong URL')); click(document.querySelector('[data-city-menu-open]')); const menu = document.querySelector('[data-city-command-menu]'); assert(menu.open && menu.contains(document.activeElement), 'Menu did not acquire focus'); assert(menu.getBoundingClientRect().right <= innerWidth && menu.getBoundingClientRect().bottom <= innerHeight, 'Menu is outside viewport'); for (const tab of ['directory','planning','settings','guide']) { click(menu.querySelector(`[data-city-menu-tab="${tab}"]`)); assert([...menu.querySelectorAll('[data-city-menu-panel]')].filter(p => !p.hidden).length === 1, 'Utility panels overlap'); assert(!menu.querySelector(`[data-city-menu-panel="${tab}"]`).hidden, 'Wrong utility panel'); } click(menu.querySelector('[data-city-menu-tab="planning"]')); const kind = menu.querySelector('[data-city-create-kind]'); kind.value = 'spot'; kind.dispatchEvent(new Event('change', {bubbles:true})); assert([...menu.querySelectorAll('[data-city-create]')].filter(f => !f.hidden).length === 1 && !menu.querySelector('[data-city-create="spot"]').hidden, 'Creation forms are not compact'); click(menu.querySelector('[data-city-menu-close]')); assert(!menu.open, 'Menu cannot close'); // Click outside the courtyard ascends without adding browser history. click(document.querySelector('#city-map')); await wait(() => document.querySelector('[data-estate-box]')); assert(!document.querySelector('.city-estate-detail'), 'Background click does not ascend to Street'); assert(location.href === originalURL && history.length === originalHistory, 'Map exploration changed URL/history'); // The menu directory is also a map navigator, including locations whose // assigned generated-road hit target is too small to pick visually. click(document.querySelector('[data-city-menu-open]')); click(menu.querySelector('[data-city-menu-tab="directory"]')); click(menu.querySelector(`[data-city-jump="spot"][data-city-id="${alpha.uuid}"]`)); await wait(() => document.querySelectorAll('.city-estate-detail .city-map-page-link').length === 24); assert(!menu.open && location.href === originalURL, 'Directory jump did not return to the map'); if (fixture === 'menu' || fixture === 'mobile-light') window.PagesWikiCityMenu.open('planning'); 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); } });