web/website/tests/menu-updates.test.mjs

31 lines
5.2 KiB
JavaScript

import test from'node:test';import assert from'node:assert/strict';import{mkdtempSync,mkdirSync,writeFileSync,readFileSync,rmSync}from'node:fs';import{tmpdir}from'node:os';import path from'node:path';import{pagesForChanges,menuBadgeScript,recordPageUpdates}from'../scripts/menu-updates.mjs';
test('Release badge mapping follows deployed page scopes and ignores tests and unrelated game changes',()=>{assert.deepEqual(pagesForChanges(['website/node/staff-roster.mjs','website/node/staff-roster.mjs']),['https://www.dunerealms.com/staff.html']);assert.deepEqual(pagesForChanges(['website/tests/staff-roster.test.mjs','DuneCore/src/Main.java']),[]);assert.deepEqual(pagesForChanges(['website/node/memorials.mjs','website/control-panel/public/panel.js']),['https://cp.dunerealms.com/','https://www.dunerealms.com/']);assert(pagesForChanges(['website/integrations/merch/catalogue.json']).includes('https://www.dunerealms.com/shop.html'));});
test('Shared badge script reads the one central manifest and changes labels only when needed',()=>{const s=menuBadgeScript();assert(s.includes('https://cp.dunerealms.com/api/menu-badges'));assert(s.includes("cache:'no-store'"));assert(s.includes("if(tag.textContent!==label)"));assert(s.includes("includes(item?.badge)"));assert(s.includes('https://www.dunerealms.com/staff.html'));new Function(s);});
test('Immutable CI artifact without .git builds release badge manifest without crashing',()=>{
const repo=mkdtempSync(path.join(tmpdir(),'dune-gitless-build-'));
try{
const root=path.join(repo,'website');mkdirSync(path.join(root,'web'),{recursive:true});
const initial={items:[{href:'https://cp.dunerealms.com/',introducedIn:'1.0.0.3',badge:'NEW'}]};
const manifest=path.join(root,'web/menu-new-items.json');writeFileSync(manifest,JSON.stringify(initial));
assert.deepEqual(recordPageUpdates({root,version:'1.0.0.4'}),[]);
assert.deepEqual(JSON.parse(readFileSync(manifest,'utf8')),initial);
}finally{rmSync(repo,{recursive:true,force:true});}
});
test('Explicit changed paths still update menu badges without Git metadata',()=>{
const repo=mkdtempSync(path.join(tmpdir(),'dune-manifest-'));
try{
const root=path.join(repo,'website');mkdirSync(path.join(root,'web'),{recursive:true});
const manifest=path.join(root,'web/menu-new-items.json');writeFileSync(manifest,JSON.stringify({items:[]}));
assert.deepEqual(recordPageUpdates({root,version:'1.0.0.4',paths:['website/control-panel/banking.mjs']}),['https://cp.dunerealms.com/']);
const data=JSON.parse(readFileSync(manifest,'utf8'));assert.equal(data.items[0].updatedIn,'1.0.0.4');
}finally{rmSync(repo,{recursive:true,force:true});}
});
test('Release badges are confined to the shared header navigation',()=>{const s=menuBadgeScript();assert(s.includes('header nav[aria-label="Main navigation"] a'));assert(!s.includes('#portals .portal[data-id]'));new Function(s);});
test('New publication clears untouched badges and distinguishes a new page from an update',()=>{const repo=mkdtempSync(path.join(tmpdir(),'dune-badge-expiry-'));try{const root=path.join(repo,'website');mkdirSync(path.join(root,'web'),{recursive:true});const file=path.join(root,'web/menu-new-items.json');writeFileSync(file,JSON.stringify({items:[{href:'https://clans.dunerealms.com/',badge:'NEW'},{href:'https://www.dunerealms.com/staff.html',badge:'NEW',introducedIn:'1.0.0.4'}]}));recordPageUpdates({root,version:'1.0.0.4',paths:['website/node/staff-roster.mjs','website/web/rates-and-charges.html'],newPages:['https://www.dunerealms.com/rates-and-charges.html']});const items=JSON.parse(readFileSync(file)).items;assert.equal(items.find(x=>x.href==='https://clans.dunerealms.com/').badge,undefined);assert.equal(items.find(x=>x.href.endsWith('/staff.html')).badge,'UPDATE');assert.equal(items.find(x=>x.href.endsWith('/rates-and-charges.html')).badge,'NEW');}finally{rmSync(repo,{recursive:true,force:true});}});
test('Bank changes update the bank workspace badge without marking unrelated workspace tiles',()=>{assert.deepEqual(pagesForChanges(['website/node/postbank-payments.mjs']),['https://cp.dunerealms.com/','https://cp.dunerealms.com/#bank']);assert.deepEqual(pagesForChanges(['website/tests/postbank-payments.test.mjs']),[]);});
test('combined release highlights apply only when that release is published and expire on the next ordinary update',()=>{const repo=mkdtempSync(path.join(tmpdir(),'dune-release-highlights-'));try{const root=path.join(repo,'website');mkdirSync(path.join(root,'web'),{recursive:true});const file=path.join(root,'web/menu-new-items.json');writeFileSync(file,JSON.stringify({items:[]}));writeFileSync(path.join(root,'web/release.json'),JSON.stringify({version:'1.0.0.7',menuUpdates:[{href:'https://cp.dunerealms.com/#bank',badge:'NEW'},{href:'https://clans.dunerealms.com/',badge:'UPDATE'}]}));recordPageUpdates({root,version:'1.0.0.7',paths:['website/web/release.json']});let items=JSON.parse(readFileSync(file)).items;assert.equal(items.find(x=>x.href.endsWith('#bank')).badge,'NEW');assert.equal(items.find(x=>x.href==='https://clans.dunerealms.com/').badge,'UPDATE');recordPageUpdates({root,version:'1.0.0.7',paths:['website/node/staff-roster.mjs']});items=JSON.parse(readFileSync(file)).items;assert.equal(items.find(x=>x.href.endsWith('#bank')).badge,undefined);}finally{rmSync(repo,{recursive:true,force:true});}});