import test from 'node:test'; import assert from 'node:assert/strict'; import {renderMissionJournal} from '../control-panel/public/cp-v2/cp-v2-mission-journal.mjs'; import {buildJournal} from '../control-panel/public/cp-v2/cp-v2-mission-journal-model.mjs'; class Element { constructor(tag){this.tagName=tag;this.children=[];this.dataset={};this.attributes={};this.style={};} append(...children){this.children.push(...children);} setAttribute(key,value){this.attributes[key]=value;} } const descendants=node=>[node,...node.children.flatMap(descendants)]; test('journal renders a separate mission-steps route and keeps claim bound to the displayed mission',()=>{ const before=globalThis.document;globalThis.document={createElement:tag=>new Element(tag),querySelector:()=>null,head:new Element('head')}; try{ const mission={id:'fremen-turnin',title:'Deliver supplies',branch:'fremen',state:'completed',progress:1,target:1,canClaim:true,rewardConfigured:true,xp:50,requires:[]}; const journal=buildJournal({branches:[{id:'fremen',title:'Fremen'}],quests:[mission]}); const root=new Element('main'),calls=[]; renderMissionJournal(root,{journal,selected:mission.id,tracked:null},{onSelect:()=>{},onTrack:()=>{},onAct:q=>calls.push(['claim',q.id]),onSteps:q=>calls.push(['steps',q.id])}); const buttons=descendants(root).filter(e=>e.tagName==='button'); buttons.find(e=>e.textContent==='Mission steps').onclick(); buttons.find(e=>e.textContent==='Claim reward').onclick(); assert.deepEqual(calls,[['steps',mission.id],['claim',mission.id]]); assert.equal(buttons.filter(e=>e.textContent==='Track').length,1); }finally{globalThis.document=before;} }); test('busy journal prevents both mission steps and reward actions',()=>{ const before=globalThis.document;globalThis.document={createElement:tag=>new Element(tag),querySelector:()=>null,head:new Element('head')}; try{ const mission={id:'ready',title:'Ready',branch:'a',state:'completed',progress:1,target:1,canClaim:true,rewardConfigured:true,requires:[]}; const journal=buildJournal({quests:[mission]}),root=new Element('main'); renderMissionJournal(root,{journal,selected:mission.id,busy:true},{onSelect:()=>{},onTrack:()=>{},onAct:()=>{},onSteps:()=>{}}); for(const name of ['Mission steps','Claim reward'])assert.equal(descendants(root).find(e=>e.textContent===name).disabled,true); }finally{globalThis.document=before;} }); test('current journal groups categories without hiding unknown branches or changing mission actions',()=>{ const before=globalThis.document;globalThis.document={createElement:tag=>new Element(tag),querySelector:()=>null,head:new Element('head')}; try{ const missions=[{id:'wing',title:'Wings and Armour',branch:'elytra',state:'completed',progress:1,target:1,canClaim:true,rewardConfigured:true,xp:0,requires:[]},{id:'unknown',title:'New story',branch:'new-branch',state:'active',progress:0,target:1,requires:[]}]; const journal=buildJournal({branches:[{id:'elytra',title:'Flight'},{id:'new-branch',title:'New work'}],quests:missions}),root=new Element('main'),calls=[]; renderMissionJournal(root,{journal,selected:'wing'},{onSelect:id=>calls.push(id),onTrack:()=>{},onAct:()=>{},onSteps:()=>{}}); const all=descendants(root),headings=all.filter(e=>e.className==='quest-category-heading'); assert.deepEqual(headings.map(h=>h.children.find(e=>e.tagName==='span').textContent),['Wings across the Realms','Other Work']); const unknown=all.find(e=>e.tagName==='button'&&e.children.some(c=>c.textContent==='New story'));assert.ok(unknown);unknown.onclick();assert.deepEqual(calls,['unknown']); assert.equal(all.filter(e=>e.textContent==='Claim reward').length,1);assert.equal(all.filter(e=>e.className==='quest-category-hero').length,1); }finally{globalThis.document=before;} }); test('one named story arc combines former letter branches once in prerequisite order',()=>{ const before=globalThis.document;globalThis.document={createElement:tag=>new Element(tag),querySelector:()=>null,head:new Element('head')}; try{ const mission=(id,branch,requires=[])=>({id,title:'Mission '+id,branch,requires,state:'active',progress:0,target:1}); const journal=buildJournal({branches:[{id:'e',title:'E'},{id:'f',title:'F'},{id:'g',title:'G'}],quests:[mission('third','g',['second']),mission('first','e'),mission('second','f',['first'])]}),root=new Element('main'),calls=[]; renderMissionJournal(root,{journal,selected:'first',tracked:'second'},{onSelect:id=>calls.push(id),onTrack:()=>{},onAct:()=>{},onSteps:()=>{}}); const all=descendants(root),headings=all.filter(e=>e.tagName==='summary');assert.equal(headings.length,1);assert.equal(headings[0].children.find(e=>e.tagName==='span').textContent,'A Place in the Realm'); const picks=all.filter(e=>e.className==='mj-mission');assert.deepEqual(picks.map(e=>e.children[0].textContent),['Mission first','Mission second','Mission third']); assert.equal(new Set(picks.map(e=>e.children[0].textContent)).size,3);picks[1].onclick();assert.deepEqual(calls,['second']); assert.ok(!all.some(e=>['E','F','G'].includes(e.textContent))); }finally{globalThis.document=before;} });