web/website/tests/game-guild-open-travel.test.mjs
unimatrixgit 633cb376e9
All checks were successful
DuneRealms Web CI / web (push) Successful in 5m6s
Keep Guild routes selectable before scheduled boarding
2026-12-05 14:07:55 +01:00

202 lines
16 KiB
JavaScript

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 {createGuildService,createGameGuildAPI,GUILD_SCHEMA,SMUGGLER_PASSAGE_SCHEMA,TRAVEL_LADDER_SCHEMA,TRAVEL_BOUNTY_SCHEMA,TRAVEL_BOUNTY_CLAIM_SCHEMA,TRAVEL_OPEN_PASSAGE_SCHEMA} from '../node/game-guild.mjs';
const DAY=86400000,strip=sql=>sql.replace(/,INDEX \w+\([^)]*\)/,'').replace(' ENGINE=InnoDB DEFAULT CHARSET=utf8mb4','');
async function fixture({openTravel=true,treasury=5000000,holding=async()=>false,decree=async()=>false}={}){
const db=new BankFixture();for(const sql of [GUILD_SCHEMA,SMUGGLER_PASSAGE_SCHEMA,TRAVEL_LADDER_SCHEMA,TRAVEL_BOUNTY_SCHEMA,TRAVEL_BOUNTY_CLAIM_SCHEMA,TRAVEL_OPEN_PASSAGE_SCHEMA])await db.query(strip(sql));
const make=(name,faction)=>{const user={id:randomUUID(),username:name,uuid:randomUUID(),faction,version:1,keyId:randomUUID()};return{user,entity:{id:randomUUID(),kind:'person',key:user.id,name}};};
const traveller=make('traveller','atreides'),hunter=make('hunter','harkonnen'),people=[traveller,hunter];
const authority=async u=>({entities:people.filter(p=>!u||p.user.id===u.id).map(p=>({...p.entity,canView:true,canActivate:true,canPay:true}))}),bank=new PostbankService(db,authority);
const accounts={};
for(const p of people){await bank.activate(p.user,{entityId:p.entity.id,packageKey:'pb_flow',termsVersion:BANK_VERSION,accepted:true,requestId:randomUUID()});accounts[p.user.username]=(await db.first("SELECT id FROM pb_accounts WHERE type_code='102' AND system_key IS NULL ORDER BY id DESC LIMIT 1")).id;}
await db.query('UPDATE pb_accounts SET balance_minor=1000000 WHERE id=?',[accounts.traveller]);
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,'active',0)",[treasury]);
await db.query("INSERT INTO pb_accounts(system_key,type_code,display_name,balance_minor,reserved_minor,state,created_at) VALUES('sys_payroll_po','979','P&O issuer',0,0,'active',0)");
let now=1200000;const byUuid=new Map(people.map(p=>[p.user.uuid,p.user]));
const service=createGuildService(db,{authority,openTravel,allowJourney:async()=>true,allowArrakis:decree,holdingOnWorld:holding,resolve:async uuid=>byUuid.get(uuid)||null,clock:()=>now});
const balance=async who=>BigInt((await db.first('SELECT balance_minor FROM pb_accounts WHERE id=?',[accounts[who]])).balance_minor);
const issuerBalance=async()=>BigInt((await db.first("SELECT balance_minor FROM pb_accounts WHERE system_key='sys_payroll_po'")).balance_minor);
const treasuryBalance=async()=>BigInt((await db.first("SELECT balance_minor FROM pb_accounts WHERE system_key='sys_game_treasury'")).balance_minor);
const input=(destination,fareMinor)=>({uuid:traveller.user.uuid,requestId:randomUUID(),keyId:traveller.user.keyId,version:1,fareMinor,sourceWorld:'lobby',destination});
const route=async(world)=>(await service.quote(traveller.user.uuid,'lobby')).routes.find(r=>r.server===world);
return{db,service,traveller:traveller.user,hunter:hunter.user,balance,treasuryBalance,issuerBalance,input,route,setTime:v=>now=v,advance:v=>now+=v,now:()=>now};
}
test('the race world is free and every other world is quoted 150 DM with a bounty of five times that',async()=>{
const f=await fixture();try{
const home=await f.route('caladan'),other=await f.route('giedi_prime');
assert.equal(home.fareMinor,0);assert.equal(home.free,true);assert.equal(home.available,true);
assert.equal(other.fareMinor,15000);assert.equal(other.bountyMinor,75000);assert.equal(other.free,false);assert.equal(other.available,true);assert.equal(other.reduced,false);
}finally{f.db.close();}
});
test('free passage to the race world moves no money, creates no bounty and does not move the fare ladder',async()=>{
const f=await fixture();try{
const before=await f.balance('traveller'),booked=await f.service.board(f.input('caladan',0));assert.equal(booked.fareMinor,0);
const arrived=await f.service.arrive(f.traveller.uuid,'caladan');assert.equal(arrived.passage.state,'arrived');assert.equal(arrived.bounty,undefined);
assert.equal(await f.balance('traveller'),before);assert.equal((await f.route('giedi_prime')).fareMinor,15000);
assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_travel_bounties')).n,0);
}finally{f.db.close();}
});
test('a paid arrival charges the fare, places a bounty of five times the fare and raises the next fare by 10%',async()=>{
const f=await fixture();try{
const before=await f.balance('traveller'),treasuryBefore=await f.treasuryBalance();
await assert.rejects(f.service.board(f.input('giedi_prime',0)),/fare changed/);
await f.service.board(f.input('giedi_prime',15000));assert.equal(await f.balance('traveller'),before);
const arrived=await f.service.arrive(f.traveller.uuid,'giedi_prime');
assert.equal(await f.balance('traveller'),before-15000n);assert.equal(await f.treasuryBalance(),treasuryBefore+15000n);
assert.deepEqual({world:arrived.bounty.world,amountMinor:arrived.bounty.amountMinor,traveller:arrived.bounty.traveller,state:arrived.bounty.state},{world:'giedi_prime',amountMinor:75000,traveller:'traveller',state:'open'});
assert.equal((await f.route('giedi_prime')).fareMinor,16500);assert.equal((await f.route('giedi_prime')).bountyMinor,82500);assert.equal((await f.route('giedi_prime')).trip,2);
assert.equal((await f.service.arrive(f.traveller.uuid,'giedi_prime')).passage,null,'the arrival is reported once, so it is announced once');
}finally{f.db.close();}
});
test('a clan or company on the destination halves both the fare and the bounty',async()=>{
const f=await fixture({holding:async(_u,_uuid,world)=>world==='giedi_prime'});try{
assert.equal((await f.route('giedi_prime')).fareMinor,7500);assert.equal((await f.route('giedi_prime')).reduced,true);assert.equal((await f.route('kaitain')).fareMinor,15000);
await f.service.board(f.input('giedi_prime',7500));const arrived=await f.service.arrive(f.traveller.uuid,'giedi_prime');assert.equal(arrived.bounty.amountMinor,37500);
}finally{f.db.close();}
});
test('the fare ladder resets after thirty days without a paid trip',async()=>{
const f=await fixture();try{
await f.service.board(f.input('giedi_prime',15000));await f.service.arrive(f.traveller.uuid,'giedi_prime');assert.equal((await f.route('giedi_prime')).fareMinor,16500);
f.advance(30*DAY-1);assert.equal((await f.route('giedi_prime')).fareMinor,16500);
f.advance(1);assert.equal((await f.route('giedi_prime')).fareMinor,15000);
}finally{f.db.close();}
});
test('an Emperor free-passage decree makes Arrakis free and sanctioned: no fare and no bounty',async()=>{
const f=await fixture({decree:async()=>true});try{
assert.equal((await f.route('arrakis')).fareMinor,0);const before=await f.balance('traveller');
await f.service.board(f.input('arrakis',0));const arrived=await f.service.arrive(f.traveller.uuid,'arrakis');assert.equal(arrived.bounty,undefined);assert.equal(await f.balance('traveller'),before);
}finally{f.db.close();}
});
test('a hunter claims once from fixed P&O, preserving the Guild fare treasury',async()=>{
const f=await fixture();try{
await f.service.board(f.input('giedi_prime',15000));await f.service.arrive(f.traveller.uuid,'giedi_prime');
const hunterBefore=await f.balance('hunter'),treasuryBefore=await f.treasuryBalance(),issuerBefore=await f.issuerBalance(),requestId=randomUUID();
assert.deepEqual(await f.service.bountyClaim({killer:f.traveller.uuid,victim:f.traveller.uuid,server:'giedi_prime',requestId:randomUUID()}),{ok:true,claimed:false},'you cannot claim your own bounty');
const claim=await f.service.bountyClaim({killer:f.hunter.uuid,victim:f.traveller.uuid,server:'giedi_prime',requestId});
assert.equal(claim.claimed,true);assert.equal(claim.amountMinor,75000);assert.equal(await f.balance('hunter'),hunterBefore+75000n);assert.equal(await f.treasuryBalance(),treasuryBefore);assert.equal(await f.issuerBalance(),issuerBefore-75000n);
assert.equal((await f.service.bountyClaim({killer:f.hunter.uuid,victim:f.traveller.uuid,server:'giedi_prime',requestId})).replayed,true);assert.equal(await f.balance('hunter'),hunterBefore+75000n);
assert.deepEqual(await f.service.bountyClaim({killer:f.hunter.uuid,victim:f.traveller.uuid,server:'giedi_prime',requestId:randomUUID()}),{ok:true,claimed:false});
}finally{f.db.close();}
});
test('P&O uses the established issuer path without topping up or debiting Guild funds',async()=>{
const f=await fixture({treasury:0});try{
await f.service.board(f.input('giedi_prime',15000));await f.service.arrive(f.traveller.uuid,'giedi_prime');
await f.db.query("UPDATE pb_accounts SET balance_minor=0 WHERE system_key='sys_game_treasury'");
const claim=await f.service.bountyClaim({killer:f.hunter.uuid,victim:f.traveller.uuid,server:'giedi_prime',requestId:randomUUID()});
assert.equal(claim.claimed,true);assert.equal(await f.treasuryBalance(),0n);assert.equal(await f.issuerBalance(),-75000n);
const entries=await f.db.query('SELECT amount_minor FROM pb_entries WHERE order_id=(SELECT id FROM pb_orders WHERE memo LIKE ?)', ['Travel bounty%']);
assert.equal(entries.length,2);assert.equal(entries.reduce((n,r)=>n+BigInt(r.amount_minor),0n),0n);
}finally{f.db.close();}
});
test('a kill on another world cannot collect this destination bounty',async()=>{
const f=await fixture();try{
await f.service.board(f.input('giedi_prime',15000));await f.service.arrive(f.traveller.uuid,'giedi_prime');
const before=await f.balance('hunter');
assert.deepEqual(await f.service.bountyClaim({killer:f.hunter.uuid,victim:f.traveller.uuid,server:'kaitain',requestId:randomUUID()}),{ok:true,claimed:false});
assert.equal(await f.balance('hunter'),before);assert.equal((await f.db.first('SELECT state FROM pb_travel_bounties')).state,'open');
}finally{f.db.close();}
});
test('a kill reference cannot be replayed against a different victim or world',async()=>{
const f=await fixture();try{
await f.service.board(f.input('giedi_prime',15000));await f.service.arrive(f.traveller.uuid,'giedi_prime');
const report={killer:f.hunter.uuid,victim:f.traveller.uuid,server:'giedi_prime',requestId:randomUUID()};
await f.service.bountyClaim(report);
await assert.rejects(f.service.bountyClaim({...report,victim:randomUUID()}),/reference was already used/);
await assert.rejects(f.service.bountyClaim({...report,server:'kaitain'}),/reference was already used/);
assert.equal(await f.balance('hunter'),75000n);
}finally{f.db.close();}
});
test('a no-bounty report stays terminal when the victim later buys a passage',async()=>{
const f=await fixture();try{
const report={killer:f.hunter.uuid,victim:f.traveller.uuid,server:'giedi_prime',requestId:randomUUID()};
assert.deepEqual(await f.service.bountyClaim(report),{ok:true,claimed:false});
await f.service.board(f.input('giedi_prime',15000));await f.service.arrive(f.traveller.uuid,'giedi_prime');
assert.deepEqual(await f.service.bountyClaim(report),{ok:true,claimed:false,replayed:true});
assert.equal(await f.balance('hunter'),0n);assert.equal((await f.db.first('SELECT state FROM pb_travel_bounties')).state,'open');
}finally{f.db.close();}
});
test('an inactive P&O issuer rolls back the claim and leaves its bounty open',async()=>{
const f=await fixture();try{
await f.service.board(f.input('giedi_prime',15000));await f.service.arrive(f.traveller.uuid,'giedi_prime');
await f.db.query("UPDATE pb_accounts SET state='blocked' WHERE system_key='sys_payroll_po'");
const report={killer:f.hunter.uuid,victim:f.traveller.uuid,server:'giedi_prime',requestId:randomUUID()};
await assert.rejects(f.service.bountyClaim(report),/P&O issuer is unavailable/);
assert.equal(await f.balance('hunter'),0n);assert.equal((await f.db.first('SELECT state FROM pb_travel_bounties')).state,'open');
assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_travel_bounty_claims')).n,0);
await f.db.query("UPDATE pb_accounts SET state='active' WHERE system_key='sys_payroll_po'");
assert.equal((await f.service.bountyClaim(report)).claimed,true);
}finally{f.db.close();}
});
test('legacy pending passages settle under their original terms without a retroactive travel bounty',async()=>{
const f=await fixture();try{
const input=f.input('giedi_prime',15000);await f.service.board(input);
await f.db.query('DELETE FROM pb_guild_open_passages WHERE id=?',[input.requestId]);
const arrived=await f.service.arrive(f.traveller.uuid,'giedi_prime');
assert.equal(arrived.passage.state,'arrived');assert.equal(arrived.bounty,undefined);
assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_travel_ladder')).n,0);
}finally{f.db.close();}
});
test('paid bookings keep existing Guild Lobby origin authority',async()=>{
const f=await fixture();try{
await assert.rejects(f.service.board({...f.input('giedi_prime',15000),sourceWorld:'kaitain'}),/Guild Lobby/);
assert.equal((await f.service.quote(f.traveller.uuid,'kaitain')).routes.find(r=>r.server==='giedi_prime').available,false);
}finally{f.db.close();}
});
test('the disabled rollout gate acknowledges native death reports without any payout or progress write',async()=>{
const f=await fixture({openTravel:false});try{
const result=await f.service.bountyClaim({killer:f.hunter.uuid,victim:f.traveller.uuid,server:'giedi_prime',requestId:randomUUID()});
assert.deepEqual(result,{ok:true,claimed:false,disabled:true});
assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_travel_bounty_claims')).n,0);
assert.equal(await f.balance('hunter'),0n);assert.equal(await f.issuerBalance(),0n);
}finally{f.db.close();}
});
test('the authenticated bounty endpoint requires the exact canonical server and refuses amount/account overrides',async()=>{
const secret='x'.repeat(64),calls=[];
const api=createGameGuildAPI(null,null,null,{GAME_LINK_SECRET:secret},{service:{openTravel:true,bountyClaim:async input=>{calls.push(input);return{ok:true,claimed:false};}}});
const body={action:'bounty_claim',uuid:randomUUID(),victim:randomUUID(),requestId:randomUUID(),server:'giedi_prime'};
const send=(b,token=secret)=>api(new Request('https://cp.dunerealms.com/internal/game/guild',{method:'POST',headers:{authorization:'Bearer '+token,'content-type':'application/json'},body:JSON.stringify(b)}));
assert.equal((await send(body,'bad')).status,401);
const missing={...body};delete missing.server;assert.equal((await send(missing)).status,400);
for(const extra of [{server:'lobby'},{server:'giedi-prime'},{amountMinor:75000},{accountId:'123'}])assert.equal((await send({...body,...extra})).status,400);
assert.equal((await send(body)).status,200);assert.deepEqual(calls,[{killer:body.uuid,victim:body.victim,requestId:body.requestId,server:'giedi_prime'}]);
});
test('concurrent native reports cannot post the same bounty twice',async()=>{
const f=await fixture();try{
await f.service.board(f.input('giedi_prime',15000));await f.service.arrive(f.traveller.uuid,'giedi_prime');
const report={killer:f.hunter.uuid,victim:f.traveller.uuid,server:'giedi_prime',requestId:randomUUID()};
const results=await Promise.all([f.service.bountyClaim(report),f.service.bountyClaim(report)]);
assert.equal(results.filter(r=>r.replayed).length,1);assert.equal(await f.balance('hunter'),75000n);
assert.equal((await f.db.first("SELECT COUNT(*) n FROM pb_orders WHERE memo LIKE 'Travel bounty%'")).n,1);
}finally{f.db.close();}
});
test('all five worlds can be picked before departure, but boarding stays timed and paid fares still rise',async()=>{
const f=await fixture();try{
const open=(await f.service.quote(f.traveller.uuid,'lobby')).destinations;
assert.equal(open.length,5);
for(let minute=0;minute<60;minute++){
f.setTime(minute*60000);
const quote=await f.service.quote(f.traveller.uuid,'lobby');
assert.deepEqual(quote.destinations,open);
assert(quote.routes.every(route=>route.available));
}
f.setTime(19*60000);
await assert.rejects(f.service.board(f.input('giedi_prime',15000)),/Boarding opens/);
f.setTime(20*60000);
await f.service.board(f.input('giedi_prime',15000));
await f.service.arrive(f.traveller.uuid,'giedi_prime');
f.setTime(25*60000);
assert.equal((await f.route('giedi_prime')).fareMinor,16500);
}finally{f.db.close();}
});