web/website/tests/official-account.test.mjs

28 lines
3.2 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';
import {OFFICIAL_ACCOUNT,organisationSubject} from '../node/official-account.mjs';
import {organisationRuntimeAccess} from '../node/organisation-runtime.mjs';import {DEFAULT_RANKS,NODES} from '../node/permissions.mjs';
import {staffSealFor} from '../node/native-seals.mjs';
test('Official alt has no personal signature seal while the human Owner keeps their own',()=>{
const manifest={official:{role:'OWNER',label:'dunerealms',footer150:'/assets/seals/owner-dunerealms-150.png'},owner:{role:'OWNER',label:'unimatrix999',footer150:'/assets/seals/owner-unimatrix999-150.png'}};
assert.equal(staffSealFor({id:OFFICIAL_ACCOUNT.id,username:'dunerealms',ranks:['org_owner_ceo']},manifest),null);
assert.equal(staffSealFor({id:OFFICIAL_ACCOUNT.ownerId,username:'unimatrix999',ranks:['org_owner_ceo']},manifest),'/assets/seals/personal-unimatrix999-ceo-v1-150.png');
});
test('Owner alias is pinned, requires current verified database link, and falls back after revocation',async()=>{
const queries=[];let enabled=true;const reader={query:async(sql,args)=>{queries.push({sql,args});return enabled?[{user_id:OFFICIAL_ACCOUNT.id}]:[];}};
assert.deepEqual(await organisationSubject(reader,'other-account'),{userId:'other-account',alias:false});assert.equal(queries.length,0);
assert.deepEqual(await organisationSubject(reader,OFFICIAL_ACCOUNT.id,{lock:true}),{userId:OFFICIAL_ACCOUNT.ownerId,alias:true});assert.match(queries[0].sql,/FOR SHARE$/);assert(queries[0].args.includes(OFFICIAL_ACCOUNT.minecraftUuid));assert.match(queries[0].sql,/owner\.password_hash NOT LIKE/);
enabled=false;assert.deepEqual(await organisationSubject(reader,OFFICIAL_ACCOUNT.id),{userId:OFFICIAL_ACCOUNT.id,alias:false});
});
test('Verified alt inherits current Owner function rights and payroll control without copying appointments or honours',async()=>{
const assignment={id:'owner-position',scope_type:'organisation',scope_key:'dunerealms',role_key:'owner_ceo',status:'active',starts_at:1,ends_at:null};let enabled=true;
const db={readSnapshot:async fn=>fn({query:async(sql,args)=>{
if(sql.startsWith('SELECT x.user_id'))return enabled?[{user_id:OFFICIAL_ACCOUNT.id}]:[];
if(sql.includes('org_control_principals'))return args[0]===OFFICIAL_ACCOUNT.ownerId?[{principal_kind:'owner'}]:[{principal_kind:'owner_alias'}];
if(sql.includes('org_assignments'))return args[0]===OFFICIAL_ACCOUNT.ownerId?[assignment]:[];
return [{scope_type:'organisation',scope_key:'dunerealms',role_key:'owner_ceo',permissions_json:JSON.stringify(['web.bank.staff.manage','web.payroll.manage','web.ranks.manage']),version:1}];
}})};
const rights=id=>organisationRuntimeAccess(db,{id},{registry:NODES,basicRank:DEFAULT_RANKS[0],legacyRanks:[],clock:()=>10});
const owner=await rights(OFFICIAL_ACCOUNT.ownerId),alt=await rights(OFFICIAL_ACCOUNT.id);assert.deepEqual(alt.nodes,owner.nodes);assert.equal(alt.organisation.permissionPrincipalId,OFFICIAL_ACCOUNT.ownerId);assert.equal(alt.organisation.officialOwnerAlias,true);assert(!alt.ranks.includes('crysknife'));
enabled=false;const revoked=await rights(OFFICIAL_ACCOUNT.id);assert(!revoked.nodes.includes('web.bank.staff.manage'));assert(!revoked.nodes.includes('web.payroll.manage'));
});