MC/website/tests/postbank-estates.test.mjs
2026-11-27 23:43:05 +00:00

43 lines
5.4 KiB
JavaScript

import {createPostbankApi} from '../node/postbank-api.mjs';
import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
import {companyFixture} from './company-deed-fixture.mjs';import {BankFixture} from './postbank-fixture.mjs';
import {PostbankService} from '../node/postbank-service.mjs';import {BANK_VERSION} from '../node/postbank-catalogue.mjs';
import {initializeEntityState,createEntityStateKeys} from '../node/entity-state-keys.mjs';import {PostbankEstates,ESTATE_SCHEMA} from '../node/postbank-estates.mjs';
async function setup(){const f=await companyFixture(),bank=new BankFixture();await bank.query(ESTATE_SCHEMA);await initializeEntityState(f.db);const actor=f.people.Owner,target=f.people.Member,targetUuid=randomUUID();await f.db.query('UPDATE site_game_links SET minecraft_uuid=? WHERE user_id=?',[targetUuid,target.id]);
const entity={id:randomUUID(),kind:'person',key:target.id,name:'Estate test client',canView:true,canActivate:true,canPay:true};
const authority=async a=>({entities:[entity],eligibleStaff:[],readAdmin:a.id===actor.id,manageAdmin:a.id===actor.id});
const service=new PostbankService(bank,authority);await service.activate(target,{entityId:entity.id,packageKey:'pb_flow',termsVersion:BANK_VERSION,accepted:true,requestId:randomUUID()});
await bank.query("UPDATE pb_accounts SET balance_minor=10000 WHERE state='active'");
const keys=createEntityStateKeys(f.db,f.keys,{clock:()=>1000,authorizeEvent:async a=>a.id===actor.id,authorizeRead:async a=>a.id===actor.id,authorizeEvidence:async a=>a.id===actor.id});
const event={eventId:randomUUID(),source:'lore-test',action:'death',targetUuid,targetKind:'player',entityType:'minecraft:player',cause:'story-event',scope:'website-lore',world:'website',occurredAt:900,attributes:{loreDeath:true}};
return {...f,actor,target,bank,keys,event,authority,estates:new PostbankEstates(bank,f.db,keys,authority,{clock:()=>1000}),close(){bank.close();f.db.close();}};}
test('death ESK freezes existing bank accounts and staff Unlock restores them without moving money; both replay safely',async()=>{const f=await setup();try{
const initial=await f.bank.query('SELECT id,state,balance_minor,reserved_minor FROM pb_accounts ORDER BY id');const {keyId}=await f.keys.record(f.actor,f.event);
const freeze={keyId,confirmed:true,reason:'Verified story death'};const result=await f.estates.freeze(f.actor,freeze);assert.equal(result.result.state,'frozen');assert.equal((await f.estates.freeze(f.actor,freeze)).replayed,true);
assert.equal((await f.bank.first("SELECT COUNT(*) n FROM pb_accounts WHERE state='active'")).n,0);
const view=await f.estates.list(f.actor);assert.equal(view.records[0].state,'frozen');
const unlock={keyId,revision:view.records[0].revision,requestId:randomUUID(),reason:'Estate review completed',confirmed:true};assert.equal((await f.estates.unlock(f.actor,unlock)).result.state,'unlocked');assert.equal((await f.estates.unlock(f.actor,unlock)).replayed,true);
assert.deepEqual(await f.bank.query('SELECT id,state,balance_minor,reserved_minor FROM pb_accounts ORDER BY id'),initial);
assert.equal((await f.bank.first('SELECT COUNT(*) n FROM pb_entries')).n,0);assert.equal((await f.keys.get(f.actor,keyId)).action,'death');
const audit=await f.bank.query("SELECT action FROM pb_audit WHERE action IN ('esk_estate_frozen','esk_estate_unlocked')");assert.equal(audit.length,2);
}finally{f.close();}});
test('ordinary deaths, wrong actors, stale revisions and changed replay requests cannot modify an estate',async()=>{const f=await setup();try{
const ordinary=await f.keys.record(f.actor,{...f.event,attributes:{}});await assert.rejects(f.estates.freeze(f.actor,{keyId:ordinary.keyId,confirmed:true,reason:'Ordinary mob death'}),/lore-death/);
const {keyId}=await f.keys.record(f.actor,{...f.event,eventId:randomUUID()});await assert.rejects(f.estates.freeze(f.target,{keyId,confirmed:true,reason:'Not a bank officer'}),e=>e.status===403);
await f.estates.freeze(f.actor,{keyId,confirmed:true,reason:'Verified story death'});
await assert.rejects(f.estates.unlock(f.actor,{keyId,revision:99,requestId:randomUUID(),reason:'Wrong revision provided',confirmed:true}),/changed/);
assert.equal((await f.bank.first('SELECT state FROM pb_esk_estates')).state,'frozen');
await assert.rejects(f.estates.freeze(f.actor,{keyId,confirmed:true,reason:'Changed replay reason'}),/conflict/);
}finally{f.close();}});
test('existing bank API guards ESK forms with identity, origin, confirmed fields and bank authority',async()=>{const f=await setup();try{
let actor=f.actor;const api=createPostbankApi(f.db,f.bank,{}, {authenticate:async()=>actor,authority:f.authority,entityStateKeys:f.keys});
const {keyId}=await f.keys.record(f.actor,f.event);
const call=(path,body,origin='https://cp.dunerealms.com')=>api(new Request('https://cp.dunerealms.com/api/bank/admin/estates'+path,body?{method:'POST',headers:{origin,'Content-Type':'application/json'},body:JSON.stringify(body)}:{}));
assert.equal((await call('')).status,200);
const input={keyId,reason:'Verified story death',confirmed:true};assert.equal((await call('/freeze',input,'https://evil.invalid')).status,403);
assert.equal((await call('/freeze',{...input,accountId:'arbitrary'})).status,400);
actor=f.target;assert.equal((await call('/freeze',input)).status,403);actor=f.actor;
assert.equal((await call('/freeze',input)).status,200);const view=await(await call('')).json();assert.equal(view.records[0].state,'frozen');
}finally{f.close();}});