MC/website/tests/imperial-raid-commerce.test.mjs

36 lines
5.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import test from 'node:test';
import assert from 'node:assert/strict';
import {randomUUID} from 'node:crypto';
import {STAFF_OFFICE_ACCOUNT_ID} from '../node/organisation-keys.mjs';
import {raidRecoveryId} from '../node/imperial-raid-commerce.mjs';
import {imperialRaidFixture as fixture} from './imperial-raid-fixture.mjs';
const id=n=>'00000000-0000-4000-8000-'+String(n).padStart(12,'0');
test('payment takes the original calendar lock and refuses a newly booked war before posting or issuing a receipt',async()=>{
const f=await fixture({verifyCampaigns:true});try{
const intent=f.intent(),prepared=(await f.service.execute({action:'prepare',...intent})).order,war=randomUUID(),now=f.clock();
await f.db.query("INSERT INTO jarvis_war_proposals VALUES(?,?,?,'clan',?,'clan','Fixture war','Fixture',?,'scheduled',1,NULL,?,?)",[war,war,id(10),intent.orderingClanId,JSON.stringify([{kind:'clan',id:intent.orderingClanId},{kind:'clan',id:intent.targetClanId}]),now,now]);
await f.db.query('INSERT INTO jarvis_war_slots VALUES(?,?,?,?,?,?,?)',[war,'clan:'+intent.targetClanId,now+3600000,now+7200000,id(10),randomUUID(),now]);
const query=f.db.query.bind(f.db),seen=[];f.db.query=(sql,args)=>{seen.push(sql);return query(sql,args);};
await assert.rejects(f.db.transaction(tx=>f.commerce.charge(tx,{...prepared,actorId:id(10)})),/scheduled war/);
assert.ok(seen.some(sql=>sql.includes('jarvis_war_calendar_guard')&&sql.includes('FOR UPDATE')));
assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_orders')).n,0);assert.equal((await f.db.first('SELECT COUNT(*) n FROM organisation_keys')).n,0);
}finally{f.close();}
});
test('accepted raid uses the existing bank posting and scoped JARVIS key, with no key secret in the native receipt',async()=>{
const f=await fixture();try{const {response:r}=await f.accept();assert.equal(r.state,'accepted');assert.equal((await f.db.first('SELECT balance_minor b FROM pb_accounts WHERE id=1')).b,7500000);assert.equal((await f.db.first('SELECT balance_minor b FROM pb_accounts WHERE id=2')).b,2500000);const key=await f.db.first('SELECT * FROM organisation_keys WHERE id=?',[r.order.receiptKeyId]);assert.equal(key.account_id,STAFF_OFFICE_ACCOUNT_ID);assert.equal(key.scope_id,'state:imperial-raid:'+r.order.id);assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_entries')).n,2);assert.equal(r.order.code,undefined);assert.equal(r.order.code_sealed,undefined);}finally{f.close();}
});
test('war authority, clan debit rights, protected targets and target backend are independent required guards',async()=>{
for(const patch of [{war:false},{debit:false},{protected:true},{homeServer:'caladan'}]){const f=await fixture();try{Object.assign(f.options,patch);const r=await f.service.execute({action:'prepare',...f.intent()});assert.equal(r.state,'rejected');assert.equal(r.charged,false);assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_orders')).n,0);}finally{f.close();}}
});
test('a missing current game link cannot order or cancel another actor’s request',async()=>{
const f=await fixture();try{await assert.rejects(f.service.execute({action:'prepare',...f.intent(),uuid:id(99)}),e=>e.status===403);assert.equal((await f.db.first('SELECT COUNT(*) n FROM organisation_keys')).n,0);}finally{f.close();}
});
test('neutral recovery reverses exactly the original fee once through the original accounts and receipt',async()=>{
const f=await fixture();try{const {response:r}=await f.accept();await f.neutral(r.order);const row=await f.db.first('SELECT * FROM jarvis_imperial_raid_orders WHERE id=?',[r.order.id]);const refund=()=>f.db.transaction(tx=>f.commerce.refund(tx,row));const a=await refund(),b=await refund();assert.deepEqual(a,b);assert.equal(a.refundOrderId,raidRecoveryId(r.order.id));assert.equal((await f.db.first('SELECT balance_minor b FROM pb_accounts WHERE id=1')).b,10000000);assert.equal((await f.db.first('SELECT balance_minor b FROM pb_accounts WHERE id=2')).b,0);assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_orders')).n,2);assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_entries')).n,4);assert.equal((await f.db.first('SELECT COUNT(*) n FROM organisation_keys')).n,2);}finally{f.close();}
});
test('an unavailable treasury cannot mint or falsely mark a recovery paid',async()=>{
const f=await fixture();try{const {response:r}=await f.accept();await f.neutral(r.order);await f.db.query('UPDATE pb_accounts SET balance_minor=1 WHERE id=2');const row=await f.db.first('SELECT * FROM jarvis_imperial_raid_orders WHERE id=?',[r.order.id]);await assert.rejects(f.db.transaction(tx=>f.commerce.refund(tx,row)),e=>e.status===409);assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_orders')).n,1);assert.equal((await f.db.first('SELECT COUNT(*) n FROM organisation_keys')).n,1);assert.equal((await f.db.first('SELECT state FROM jarvis_imperial_raid_jobs')).state,'queued');}finally{f.close();}
});
test('a refund cannot be redirected to a different account or amount',async()=>{
const f=await fixture();try{const {response:r}=await f.accept();await f.neutral(r.order);const row=await f.db.first('SELECT * FROM jarvis_imperial_raid_orders WHERE id=?',[r.order.id]);for(const patch of [{sourceAccountId:'2'},{feeMinor:'5000000'}]){const changed={...row,accepted_json:JSON.stringify({...r.order,...patch})};await assert.rejects(f.db.transaction(tx=>f.commerce.refund(tx,changed)),e=>e.status===409);}assert.equal((await f.db.first('SELECT COUNT(*) n FROM pb_orders')).n,1);}finally{f.close();}
});