6 lines
7.5 KiB
JavaScript
6 lines
7.5 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';import {companyFixture} from './company-deed-fixture.mjs';import {createCompanyClaims} from '../node/company-claims.mjs';import {CLAIM_PRODUCT_SCHEMA} from '../node/postbank-claim-products.mjs';import {PostbankService} from '../node/postbank-service.mjs';import {entityId} from '../node/postbank-authority.mjs';import {BANK_VERSION} from '../node/postbank-catalogue.mjs';import {PostbankPayments,standardCycle} from '../node/postbank-payments.mjs';
|
|
test('Company Home Block and claim upgrades use the Company tariff, owner funds and one reserved Tax settlement',async()=>{const f=await companyFixture();try{for(const sql of CLAIM_PRODUCT_SCHEMA)await f.db.query(sql);await f.db.query('UPDATE site_game_links SET minecraft_uuid=? WHERE user_id=?',[randomUUID(),f.people.Owner.id]);const id=await f.create(),person={id:entityId('person',f.people.Owner.id),kind:'person',key:f.people.Owner.id,name:'Owner',canView:true,canActivate:true,canPay:true},company={id:entityId('company',id),kind:'company',key:id,name:'Enterprise',canView:true,canActivate:true,canPay:true},authority=async()=>({entities:[person,company]});await new PostbankService(f.db,authority).activate(f.people.Owner,{entityId:person.id,packageKey:'pb_flow',termsVersion:BANK_VERSION,accepted:true,requestId:randomUUID()});const source=await f.db.first("SELECT * FROM pb_accounts WHERE type_code='102'");await f.db.query('UPDATE pb_accounts SET balance_minor=100000 WHERE id=?',[source.id]);const tax=await f.db.query("INSERT INTO pb_accounts(system_key,type_code,display_name,balance_minor,state,created_at) VALUES('sys_tax_treasury','979','Tax',0,'active',1)");const api=createCompanyClaims(f.db,f.db,authority,{}, {authenticate:async()=>f.currentUser(),access:async()=>({nodes:['web.menu.game']})}),request=(path,body)=>new Request('https://cp.dunerealms.com/api/v1/company-claims/'+path+(body?'':'?companyId='+id),body?{method:'POST',headers:{origin:'https://cp.dunerealms.com','content-type':'application/json'},body:JSON.stringify(body)}:{});const home={companyId:id,worldKey:'kaladam',requestId:randomUUID(),confirmed:true};let r=await api.homeBlock(request('home-block',home));assert.equal(r.status,201,await r.clone().text());assert.equal((await api.homeBlock(request('home-block',home))).status,200);assert.equal((await api.homeBlock(request('home-block',{...home,worldKey:'arrakduin',requestId:randomUUID()}))).status,409);const overview=await(await api.overview(request('overview'))).json();assert.equal(overview.claims.tier,'BCO_T0');assert.equal(overview.claims.usedChunks,1);const stored=JSON.parse((await f.db.first("SELECT snapshot_json FROM site_claim_registry WHERE organisation_kind='company' AND organisation_id=?",[id])).snapshot_json);assert.match(Object.keys(stored.estates[0].worldguard.regions)[0],/dr_company_/);const quote=await api.products.quote(f.people.Owner,id),target=quote.targets.find(t=>t.code==='BCO_T3');assert.equal(target.unlockMinor,27000);const payment={companyId:id,sourceAccount:String(source.id),requestId:randomUUID(),expectedRevision:quote.revision,targetTier:target.code,expectedUnlockMinor:target.unlockMinor,tariffVersion:quote.tariffVersion,confirmed:true};const purchased=await api.products.purchase(f.people.Owner,payment);assert.equal(purchased.tier,'BCO_T3');assert.equal((await f.db.first('SELECT reserved_minor FROM pb_accounts WHERE id=?',[source.id])).reserved_minor,27000);assert.equal((await f.db.first('SELECT balance_minor FROM pb_accounts WHERE id=?',[tax.insertId])).balance_minor,0);assert.equal((await api.products.purchase(f.people.Owner,payment)).replayed,true);assert.equal((await new PostbankPayments(f.db,authority).processClaimBatches({cutoff:standardCycle().nextAt})).posted,1);f.actor('Other');assert.equal((await api.overview(request('overview'))).status,403);
|
|
}finally{f.db.close();}});
|
|
|
|
test('Company claim capacity gates Keyed and Royal jumps and switches payer after COM use',async()=>{const f=await companyFixture();try{for(const sql of CLAIM_PRODUCT_SCHEMA)await f.db.query(sql);await f.db.query('UPDATE site_game_links SET minecraft_uuid=? WHERE user_id=?',[randomUUID(),f.people.Owner.id]);const id=await f.create(),person={id:entityId('person',f.people.Owner.id),kind:'person',key:f.people.Owner.id,name:'Owner',canView:true,canActivate:true,canPay:true},company={id:entityId('company',id),kind:'company',key:id,name:'Enterprise',canView:true,canActivate:true,canPay:true};let revoked=false;const authority=async()=>({entities:[person,{...company,canPay:!revoked}]});const bank=new PostbankService(f.db,authority);for(const [entity,packageKey]of [[person,'pb_flow'],[company,'pb_connect']])await bank.activate(f.people.Owner,{entityId:entity.id,packageKey,termsVersion:BANK_VERSION,accepted:true,requestId:randomUUID()});await f.db.query('UPDATE pb_accounts SET balance_minor=1000000');await f.db.query("INSERT INTO pb_accounts(system_key,type_code,display_name,balance_minor,state,created_at) VALUES('sys_tax_treasury','979','Tax',0,'active',1)");const api=createCompanyClaims(f.db,f.db,authority,{}, {authenticate:async()=>f.currentUser(),access:async()=>({nodes:['web.menu.game']})});const home={companyId:id,worldKey:'kaladam',requestId:randomUUID(),confirmed:true},post=body=>new Request('https://cp.dunerealms.com/api/v1/company-claims/home-block',{method:'POST',headers:{origin:'https://cp.dunerealms.com','content-type':'application/json'},body:JSON.stringify(body)});assert.equal((await api.homeBlock(post(home))).status,201);let quote=await api.products.quote(f.people.Owner,id);assert(quote.targets.every(t=>t.stage==='Basic'));const personal=quote.accounts[0];const key=await f.keys.issue({accountId:f.people.Owner.id,keyType:'company-founding',scopeId:'original'});assert.equal((await f.call(id,{action:'upgrade',code:key.code,version:1,requestId:randomUUID(),confirmed:true})).status,200);quote=await api.products.quote(f.people.Owner,id);assert.equal(quote.targets.at(-1).code,'KCO_T4');assert.equal(quote.funding.kind,'company');assert(quote.accounts.every(a=>a.id!==personal.id));const input={companyId:id,sourceAccount:quote.accounts[0].id,requestId:randomUUID(),expectedRevision:quote.revision,targetTier:'KCO_T4',expectedUnlockMinor:quote.targets.at(-1).unlockMinor,tariffVersion:quote.tariffVersion,confirmed:true};await assert.rejects(api.products.purchase(f.people.Owner,{...input,sourceAccount:personal.id}),e=>e.status===403);await assert.rejects(api.products.purchase(f.people.Owner,{...input,targetTier:'RCO_T3'}),e=>e.status===409);revoked=true;await assert.rejects(api.products.purchase(f.people.Owner,input),e=>e.status===403);revoked=false;assert.equal((await api.products.purchase(f.people.Owner,input)).tier,'KCO_T4');await f.db.query("UPDATE site_company_deeds SET charter_kind='royal' WHERE company_id=?",[id]);quote=await api.products.quote(f.people.Owner,id);assert.equal(quote.targets.at(-1).code,'RCO_T3');assert.equal((await api.products.purchase(f.people.Owner,{...input,requestId:randomUUID(),expectedRevision:quote.revision,targetTier:'RCO_T3',expectedUnlockMinor:quote.targets.at(-1).unlockMinor})).tier,'RCO_T3');assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_claim_batches')).n,1);assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_claim_batch_items')).n,2);const raw=JSON.parse((await f.db.first("SELECT snapshot_json FROM site_claim_registry WHERE organisation_kind='company' AND organisation_id=?",[id])).snapshot_json);assert.equal(raw.estates.length,1);assert.equal(raw.usedChunks,1);
|
|
}finally{f.db.close();}});
|

Staff