web/website/tests/progression-sidebar.test.mjs
2026-11-21 20:22:37 +01:00

44 lines
4.5 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';
import {xpReport} from '../control-panel/public/cp-v2/cp-v2-progression-sidebar.mjs';
import {mentatFromDeeds,mentatFromCharacterChoice,systemPortrait} from '../control-panel/public/cp-v2/cp-v2-mentat-portraits.mjs';
test('XP report uses authoritative curve, real quest states and independent XP values',()=>{
const data={state:'active',profile:{level:2,effectiveXP:50,totalXP:80,reserveXP:30},season:{id:'S01',curveBase:25,levelCap:70},quests:[{id:'x',title:'Not actually started',state:'completed'},{id:'y',keyId:'q',title:'Current quest',state:'active',progress:1,target:3}]};
const r=xpReport(data);assert.equal(r.percent,50);assert.equal(r.nextXP,75);assert.equal(r.quest.id,'y');assert.equal(r.activeCount,1);assert.equal(r.reserveXP,30);
assert.equal(xpReport(null),null);assert.equal(xpReport({state:'season-not-configured'}),null);
assert.equal(xpReport({...data,season:{...data.season,levelCap:null}}).cap,null);
assert.equal(xpReport({...data,profile:{...data.profile,level:70}}).capped,true);
});
test('Mentat report follows current House deeds, never a visitor or an ambiguous House choice',()=>{
const own={id:'atreides',name:'House Aerdrecht',membership:{source:'active-office-deed',keyId:'key'}},visitor={id:'harkonnen',membership:null};
assert.equal(mentatFromDeeds([own,visitor]).username,'thufir_hawat');assert.equal(mentatFromDeeds([visitor]),null);
assert.equal(mentatFromDeeds([own,{...own,id:'ordos'}]),null);assert.equal(systemPortrait('unknown').cell,7);
assert.equal(systemPortrait('jessica_atreides').atlas,systemPortrait('piter_de_vries').atlas);
});
test('ordinary player Mentats use the existing bound House choice without inventing an office deed',()=>{
const data={linked:true,selection:'atreides',choices:[{id:'atreides',name:'House Aerdrecht · Atreides'}]};const mentat=mentatFromCharacterChoice(data);
assert.equal(mentat.username,'thufir_hawat');assert.equal(mentat.house,'House Aerdrecht');assert.equal(mentat.source,'bound-web-character-selection');
assert.equal(mentatFromCharacterChoice({...data,linked:false}),null);assert.equal(mentatFromCharacterChoice({...data,selection:'emperor'}),null);assert.equal(mentatFromCharacterChoice({...data,choices:[]}),null);
});
test('mission branches and directory persist through decisions/history and stay scoped to the selected journal',async()=>{
const {createProgressionSidebar}=await import('../control-panel/public/cp-v2/cp-v2-progression-sidebar.mjs');
class Node{constructor(){this.children=[];this.style={};this.attributes={};this.classList={remove(){}};this.isConnected=true;}append(...nodes){this.children.push(...nodes);}prepend(...nodes){this.children.unshift(...nodes);}insertBefore(node){this.children.push(node);}replaceChildren(...nodes){this.children=nodes;}setAttribute(k,v){this.attributes[k]=v;}remove(){}querySelector(){return null;}}
const before={document:globalThis.document,location:globalThis.location,history:globalThis.history};
const right=new Node(),left=new Node(),banner=new Node(),shell=new Node();right.parentElement=shell;right.querySelector=()=>banner;shell.querySelector=s=>s==='#sidebar'?left:right;
globalThis.document={createElement:()=>new Node(),querySelector:()=>null};globalThis.location={hash:'#quests/quests'};globalThis.history={replaceState(){}};
const host={closest:()=>({parentElement:shell})},requests=[],selected=[];
const data=kind=>({state:'active',branches:[{id:kind,title:kind}],quests:[{branch:kind}],player:{}});
let sidebar;
try{
sidebar=createProgressionSidebar(host,{onBranch:id=>selected.push(id),fetcher:async path=>{requests.push(path);return Response.json(path==='/api/progression/pol'?data('pol'):path==='/api/progression'?data('personal'):{});}});
sidebar.setNavigation('personal','decisions');sidebar.showBranches(true);await new Promise(r=>setImmediate(r));
const directory=left.children.find(n=>n.attributes['aria-label']==='Mission branches');
assert.equal(directory.hidden,false);assert.equal(banner.hidden,false);
sidebar.setNavigation('personal','history');sidebar.showBranches(true);assert.equal(directory.hidden,false);
directory.children[1].children[0].onclick();assert.deepEqual(selected,['personal']);
sidebar.setNavigation('pol','decisions');assert.equal(directory.children[1].children[0].children[1].textContent,'pol');
assert.equal(requests.filter(p=>p==='/api/progression').length,1);
}finally{sidebar?.dispose();for(const [key,value]of Object.entries(before)){if(value===undefined)delete globalThis[key];else globalThis[key]=value;}}
});