8 lines
4.6 KiB
JavaScript
8 lines
4.6 KiB
JavaScript
import test from'node:test';import assert from'node:assert/strict';import vm from'node:vm';import{MAIN_MENU,canonicalMenuHref,normalizeMainMenus,withCanonicalMenu,CANONICAL_MENU_SCRIPT}from'../scripts/main-menu.mjs';
|
|
class Element{constructor(tag){this.tagName=tag.toUpperCase();this.nodeType=1;this.attrs={};this.childNodes=[];this.parentNode=null;this.textContent='';this.classList={contains:k=>(this.attrs.class||'').split(' ').includes(k)};}get children(){return this.childNodes.filter(n=>n.nodeType===1);}get firstChild(){return this.childNodes[0]||null;}get firstElementChild(){return this.children[0]||null;}get nextElementSibling(){const a=this.parentNode?.children||[];return a[a.indexOf(this)+1]||null;}get href(){return new URL(this.attrs.href||'', 'https://www.dunerealms.com').href;}getAttribute(k){return this.attrs[k]??null;}setAttribute(k,v){this.attrs[k]=v;}removeAttribute(k){delete this.attrs[k];}hasAttribute(k){return k in this.attrs;}remove(){if(this.parentNode){let p=this.parentNode;p.childNodes.splice(p.childNodes.indexOf(this),1);this.parentNode=null;}}append(n){n.remove();n.parentNode=this;this.childNodes.push(n);}insertBefore(n,cursor){n.remove();n.parentNode=this;let i=cursor?this.childNodes.indexOf(cursor):-1;if(i<0)this.childNodes.push(n);else this.childNodes.splice(i,0,n);}querySelectorAll(tag){return this.children.flatMap(n=>[...(n.tagName===tag.toUpperCase()?[n]:[]),...n.querySelectorAll(tag)]);}querySelector(tag){return this.querySelectorAll(tag)[0]||null;}}
|
|
function fixture(){const nav=new Element('nav');for(const[name,url]of [['Home','https://dunerealms.com/'],['Boards','https://forums.dunerealms.com/index.php'],['Xchange','https://www.dunerealms.com/exchange.html'],['Manual/wiki','https://www.dunerealms.com/wiki.html'],['Clan center','https://clans.dunerealms.com/'],['Shop','https://www.dunerealms.com/shop.html'],['Old Shop','https://cp.dunerealms.com/#shop']]){const a=new Element('a');a.setAttribute('href',url);const span=new Element('span');span.textContent=name;a.append(span);nav.append(a);}const shop=nav.children[5],badge=new Element('span');badge.setAttribute('class','dune-new-overlay');badge.textContent='UPDATE';shop.append(badge);shop.listener={retained:true};return{nav,shop,badge,doc:{createElement:t=>new Element(t),querySelectorAll:()=>[nav]}};}
|
|
test('Every canonical destination is unique and Shop is second, including missing Kit Exchange and Dunopedia',()=>{assert.equal(MAIN_MENU.length,11);assert.equal(MAIN_MENU[1][0],'Shop');assert.equal(new Set(MAIN_MENU.map(x=>x[1])).size,11);assert(MAIN_MENU.some(x=>x[0]==='Kit Exchange'));assert.equal(MAIN_MENU.at(-1)[0],'Dunopedia');});
|
|
test('Legacy main menu is reordered, completed and deduplicated while preserving live anchor and badge objects',()=>{const f=fixture();assert(normalizeMainMenus(f.doc,'https://support.dunerealms.com/tickets.php')>0);assert.deepEqual(f.nav.children.map(a=>a.href),MAIN_MENU.map(x=>x[1]));assert.equal(f.nav.children[1],f.shop);assert.equal(f.shop.listener.retained,true);assert.equal(f.shop.querySelectorAll('span').includes(f.badge),true);assert.equal(f.badge.textContent,'UPDATE');assert.equal(f.nav.children[7].getAttribute('aria-current'),'page');assert.equal(normalizeMainMenus(f.doc,'https://support.dunerealms.com/tickets.php'),0);});
|
|
test('Normalizer updates an existing active state and fixed icons without rebuilding application controls',()=>{const f=fixture();normalizeMainMenus(f.doc,'https://www.dunerealms.com/staff.html');assert.equal(f.nav.children[3].getAttribute('aria-current'),'page');normalizeMainMenus(f.doc,'https://cp.dunerealms.com/#permissions');assert.equal(f.nav.children[3].getAttribute('aria-current'),null);assert.equal(f.nav.children[8].getAttribute('aria-current'),'page');assert.equal(f.nav.children[1].querySelector('img').getAttribute('src'),'https://www.dunerealms.com/assets/nav-shop-v1.webp');});
|
|
test('Known aliases canonicalise without confusing independent destinations',()=>{assert.equal(canonicalMenuHref('https://cp.dunerealms.com/#shop'),MAIN_MENU[1][1]);assert.equal(canonicalMenuHref('https://www.dunerealms.com/manual.html'),MAIN_MENU.at(-1)[1]);assert.equal(canonicalMenuHref('https://forums.dunerealms.com/member.php?uid=4'),MAIN_MENU[2][1]);assert.notEqual(canonicalMenuHref('https://elsewhere.test/'),MAIN_MENU[0][1]);});
|
|
test('Script publication is idempotent and embedded native shop frames do not acquire a second header',()=>{const twice=withCanonicalMenu(withCanonicalMenu('const existingFeature=true;'));assert.equal((twice.match(/Dune canonical menu begin/g)||[]).length,1);assert(twice.includes('existingFeature=true'));vm.runInNewContext(CANONICAL_MENU_SCRIPT,{window:{top:1,self:2}});});
|

Staff