74 lines
6.7 KiB
JavaScript
74 lines
6.7 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {awardPayload,recipientPage,mountCrysknife} from '../control-panel/public/cp-v2/cp-v2-crysknife.mjs';
|
|
import {validCitation,canAwardCrysknife} from '../node/honours.mjs';
|
|
const id='12345678-1234-4234-8234-123456789abc',candidates=[{id,username:'recipient'}];
|
|
const page={recipients:[{username:'recipient',profile:'https://www.dunerealms.com/people/recipient',avatar:'/api/avatars/roster/recipient',skin:true}],page:1,pages:1,total:1};
|
|
|
|
test('award form payload matches production citation and explicit confirmation requirements',()=>{
|
|
const payload=awardPayload({userId:id,citation:'A lasting contribution.\r\nThank you.',confirmed:true},candidates);
|
|
assert.deepEqual(payload,{action:'award',userId:id,citation:'A lasting contribution.\nThank you.',confirmed:true});
|
|
assert.equal(validCitation(payload.citation),true);
|
|
for(const value of [{confirmed:false},{userId:'unknown'},{citation:''},{citation:'a\n'.repeat(21)},{citation:'a'.repeat(2001)},{citation:'bad\x00citation'}])assert.throws(()=>awardPayload({userId:id,citation:'Dedication',confirmed:true,...value},candidates));
|
|
assert.doesNotThrow(()=>awardPayload({userId:id,citation:'🗡'.repeat(2000),confirmed:true},candidates));
|
|
});
|
|
test('recipient records retain real profiles while refusing outside links and oversized pages',()=>{
|
|
assert.equal(recipientPage(page).recipients[0].avatar,page.recipients[0].avatar);
|
|
assert.equal(recipientPage({...page,recipients:[{...page.recipients[0],avatar:'https://outside.example/avatar'}]}).recipients[0].avatar,'/api/avatars/fallback');
|
|
for(const profile of ['javascript:alert(1)','https://outside.example/people/a','https://www.dunerealms.com/git/','https://user:pw@www.dunerealms.com/people/a'])assert.throws(()=>recipientPage({...page,recipients:[{...page.recipients[0],profile}]}));
|
|
assert.throws(()=>recipientPage({...page,recipients:Array(10).fill(page.recipients[0])}));
|
|
assert.throws(()=>recipientPage({...page,page:0}));
|
|
});
|
|
test('ordinary staff and owners without named custody do not gain Order authority',()=>{
|
|
assert.equal(canAwardCrysknife({username:'other_owner',ranks:['owners']}),false);
|
|
assert.equal(canAwardCrysknife({username:'grmacken'}),true);
|
|
assert.equal(canAwardCrysknife({username:'member',ranks:['crysknife']}),true);
|
|
});
|
|
|
|
// Small DOM fixture exercises actual event handlers and request boundaries without a browser session.
|
|
class Node {
|
|
constructor(tag){this.tagName=tag.toUpperCase();this.children=[];this.dataset={};this.attrs={};this.value='';this.checked=false;this.disabled=false;this.hidden=false;this._text='';}
|
|
set textContent(value){this._text=String(value);this.children=[];}get textContent(){return this._text+this.children.map(n=>n.textContent).join('');}
|
|
append(...nodes){for(const node of nodes){node.parent=this;this.children.push(node);}}prepend(...nodes){for(const node of nodes)node.parent=this;this.children.unshift(...nodes);}
|
|
replaceChildren(...nodes){this._text='';this.children=[];this.append(...nodes);}
|
|
setAttribute(k,v){this.attrs[k]=String(v);}closest(){return null;}focus(){}reportValidity(){return true;}
|
|
querySelector(selector){const match=selector.match(/^\[data-tab-id="(.+)"\]$/);return descendants(this).find(n=>match&&n.dataset.tabId===match[1])||null;}
|
|
remove(){if(this.parent)this.parent.children=this.parent.children.filter(n=>n!==this);}
|
|
reset(){for(const n of descendants(this)){n.checked=false;if(['INPUT','TEXTAREA','SELECT'].includes(n.tagName))n.value='';}}
|
|
}
|
|
const descendants=root=>root.children.flatMap(n=>[n,...descendants(n)]);
|
|
const settle=async()=>{for(let i=0;i<8;i++)await new Promise(resolve=>setImmediate(resolve));};
|
|
async function fixture(run,{authorised=true,writeStatus=200}={}){
|
|
const prior=globalThis.document;globalThis.document={createElement:tag=>new Node(tag)};
|
|
const host=new Node('div'),calls=[];
|
|
const fetcher=async(path,options={})=>{calls.push({path,...options});let data;
|
|
if(path==='/api/auth/session')data={authenticated:true,user:{id:'actor',username:authorised?'unimatrix999':'ordinary_staff',ranks:authorised?['owners']:['staff']}};
|
|
else if(path.includes('access=1'))return Response.json(authorised?{canAward:true}:{message:'Only members of the Order may access this register.'},{status:authorised?200:403});
|
|
else if(path.includes('candidates=1'))data={candidates};else if(path.includes('recipients=1'))data=page;
|
|
else if(path==='/api/account/profile')data={profile:{honours:[]}};
|
|
else if(options.method==='POST')return Response.json(writeStatus===200?{ok:true,message:'Award saved.'}:{message:'Service unavailable.'},{status:writeStatus});
|
|
else throw Error('Unexpected request '+path);
|
|
return Response.json(data);
|
|
};
|
|
const dispose=mountCrysknife(host,{fetcher});
|
|
try{await settle();await run({host,calls,all:()=>descendants(host)});}finally{dispose();globalThis.document=prior;}
|
|
}
|
|
test('opening the real Order page performs reads only and exposes recipient review before any award',()=>fixture(async({all,calls})=>{
|
|
assert.equal(calls.filter(c=>c.method==='POST').length,0);
|
|
assert.ok(all().some(n=>n.tagName==='H5'&&n.textContent==='recipient'));
|
|
const select=all().find(n=>n.tagName==='SELECT'),citation=all().find(n=>n.tagName==='TEXTAREA');select.value=id;citation.value='Service above and beyond.';
|
|
const form=all().find(n=>n.tagName==='FORM');form.onsubmit({preventDefault(){}});
|
|
const award=all().find(n=>n.tagName==='BUTTON'&&n.textContent==='Award the Crysknife');assert.equal(award.disabled,true);
|
|
await award.onclick();assert.equal(calls.filter(c=>c.method==='POST').length,0);
|
|
const check=all().find(n=>n.type==='checkbox');check.checked=true;check.onchange();assert.equal(award.disabled,false);
|
|
await award.onclick();assert.equal(calls.filter(c=>c.method==='POST').length,1);
|
|
assert.deepEqual(JSON.parse(calls.find(c=>c.method==='POST').body),{action:'award',userId:id,citation:'Service above and beyond.',confirmed:true});
|
|
}));
|
|
test('unconfirmed write outcome locks retry until the register is reconciled',()=>fixture(async({all,calls})=>{
|
|
all().find(n=>n.tagName==='SELECT').value=id;all().find(n=>n.tagName==='TEXTAREA').value='Dedicated service.';
|
|
all().find(n=>n.tagName==='FORM').onsubmit({preventDefault(){}});const check=all().find(n=>n.type==='checkbox');check.checked=true;check.onchange();
|
|
const award=all().find(n=>n.tagName==='BUTTON'&&n.textContent==='Award the Crysknife');await award.onclick();assert.equal(award.disabled,true);await award.onclick();assert.equal(calls.filter(c=>c.method==='POST').length,1);
|
|
},{writeStatus:503}));
|
|
test('unauthorised visitors never request private candidates or recipients',()=>fixture(async({calls,all})=>{
|
|
assert.deepEqual(calls.map(c=>c.path),['/api/auth/session','/api/ranks/honour?access=1']);assert.equal(all().find(n=>n.tagName==='FORM').hidden,true);
|
|
},{authorised:false}));
|

Staff