forked from unimatrix/web
Add daily emergency rescue and world-scoped home travel
This commit is contained in:
parent
48a40f0c90
commit
43998712f1
1 changed files with 169 additions and 46 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';
|
||||
import {BankFixture} from './postbank-fixture.mjs';import {PostbankService} from '../node/postbank-service.mjs';import {BANK_VERSION} from '../node/postbank-catalogue.mjs';
|
||||
import {createHomeTravelService,createGameHomeAPI,HOME_SCHEMA,HOME_COOLDOWN_MS} from '../node/game-home.mjs';
|
||||
async function fixture(){
|
||||
import {createHomeTravelService,createGameHomeAPI,HOME_SCHEMA,HOME_COOLDOWN_MS,RESCUE_COOLDOWN_MS,RESCUE_FARE_MINOR} from '../node/game-home.mjs';
|
||||
async function fixture(guildFare){
|
||||
const db=new BankFixture();await db.query(HOME_SCHEMA.replace(/,INDEX home_actor\([^)]*\)/,'').replace(' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4',''));
|
||||
const user={id:randomUUID(),username:'traveller',uuid:randomUUID()},entity={kind:'person',key:user?.id,name:'traveller'};
|
||||
// Postbank's deterministic person entity is the lock shared by all backends.
|
||||
|
|
@ -12,7 +12,7 @@ async function fixture(){
|
|||
const source=(await db.first("SELECT id FROM pb_accounts WHERE type_code='102' AND system_key IS NULL")).id;
|
||||
await db.query('UPDATE pb_accounts SET balance_minor=20000 WHERE id=?',[source]);
|
||||
await db.query("INSERT INTO pb_accounts(system_key,type_code,display_name,balance_minor,reserved_minor,state,created_at) VALUES('sys_game_treasury','999','Game treasury',0,0,'active',0)");
|
||||
const service=createHomeTravelService(db,{authority,resolve:async()=>linked?user:null,clock:()=>now});
|
||||
const service=createHomeTravelService(db,{authority,resolve:async()=>linked?user:null,clock:()=>now,guildFare});
|
||||
return{db,user,service,source,input:()=>({action:'reserve',uuid:user.uuid,requestId:randomUUID(),server:'caladan',destinationHash:'a'.repeat(64)}),setTime:v=>now=v,revoke:()=>revoked=true,unlink:()=>linked=false,account:()=>db.first('SELECT balance_minor,reserved_minor FROM pb_accounts WHERE id=?',[source])};
|
||||
}
|
||||
test('home reserves central funds, settles exactly once and shares cooldown across backends',async()=>{
|
||||
|
|
@ -43,3 +43,34 @@ test('private home endpoint rejects account, price, world and cooldown overrides
|
|||
assert.equal((await api(request(b,'bad'))).status,401);assert.equal((await api(request(b))).status,200);
|
||||
for(const patch of [{fareMinor:0},{accountId:'1'},{world:'arrakis'},{cooldown:0},{server:'other'},{action:'expire'}])assert.equal((await api(request({...b,...patch}))).status,400);assert.equal(calls.length,1);
|
||||
});
|
||||
|
||||
test('rescue reserves 1000 DM, settles once and imposes a network-wide 24-hour cooldown',async()=>{
|
||||
const f=await fixture();
|
||||
try{
|
||||
await f.db.query('UPDATE pb_accounts SET balance_minor=200000 WHERE id=?',[f.source]);
|
||||
const q={...f.input(),mode:'rescue'};
|
||||
assert.equal((await f.service.execute(q)).fareMinor,RESCUE_FARE_MINOR);
|
||||
await assert.rejects(f.service.execute({...q,mode:'world',action:'arrive'}),/already used/);
|
||||
const result=await f.service.execute({...q,action:'arrive'});
|
||||
await f.service.execute({...q,action:'arrive'});
|
||||
assert.equal(result.cooldownUntil,1000000+RESCUE_COOLDOWN_MS);
|
||||
assert.deepEqual({...await f.account()},{balance_minor:100000,reserved_minor:0});
|
||||
await assert.rejects(f.service.execute({...f.input(),server:'arrakis',mode:'rescue'}),/cooldown/);
|
||||
assert.equal((await f.service.execute(f.input())).state,'pending');
|
||||
}finally{f.db.close();}
|
||||
});
|
||||
|
||||
test('world home uses the Guild tariff at reservation and retains it even if the tariff changes',async()=>{
|
||||
let quotedFare=13000;
|
||||
const f=await fixture(async()=>quotedFare);
|
||||
try{
|
||||
const q={...f.input(),mode:'world'};
|
||||
assert.equal((await f.service.execute(q)).fareMinor,13000);
|
||||
quotedFare=17000;
|
||||
const result=await f.service.execute({...q,action:'arrive'});
|
||||
assert.equal(result.fareMinor,13000);
|
||||
assert.equal(result.cooldownUntil,1000000+24*60*60*1000);
|
||||
assert.deepEqual({...await f.account()},{balance_minor:7000,reserved_minor:0});
|
||||
await assert.rejects(f.service.execute({...f.input(),mode:'world'}),/cooldown/);
|
||||
}finally{f.db.close();}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff