web/website/tests/imperial-raid-protocol.test.mjs

29 lines
2.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {validateImperialRaidInput,imperialRaidInputHash,imperialRaidEventHash,imperialRaidFee,unpaidRaidResponse,raidEventAcknowledgement} from '../node/imperial-raid-protocol.mjs';
const id=n=>'00000000-0000-4000-8000-'+String(n).padStart(12,'0');
const intent={server:'arrakis',uuid:id(1),requestId:id(2),orderingClanId:id(3),targetClanId:id(4),sourceAccountId:'12'};
test('prepare, status and cancellation bind exactly the same original intent',()=>{
const hashes=['prepare','status','cancel_prepared'].map(action=>imperialRaidInputHash(validateImperialRaidInput({action,...intent})));
assert.equal(new Set(hashes).size,1);
for(const [key,value] of [['sourceAccountId','13'],['targetClanId',id(9)],['uuid',id(8)],['server','kaitain']])assert.notEqual(imperialRaidInputHash({...intent,[key]:value}),hashes[0]);
});
test('reject unknown fields, lobby execution, numeric account IDs and malformed references',()=>{
for(const input of [{action:'prepare',...intent,feeMinor:'1'},{action:'prepare',...intent,server:'lobby'},{action:'prepare',...intent,sourceAccountId:12},{action:'prepare',...intent,requestId:'new-ref'}])assert.throws(()=>validateImperialRaidInput(input));
});
test('accept requires the exact current native safety evidence, not a generic success boolean',()=>{
const input={action:'accept',server:'arrakis',orderId:id(2),eventId:id(5),inputHash:imperialRaidInputHash(intent),preflightNonce:id(6),preflight:{worldUuid:id(7),position:{x:12,y:80,z:30},claimRevision:1,defendersOnline:0,spawnable:true,protected:false,observedAt:1000}};
assert.equal(validateImperialRaidInput(input).preflight.spawnable,true);
assert.notEqual(imperialRaidEventHash(input),imperialRaidEventHash({...input,preflight:{...input.preflight,claimRevision:2}}));
for(const patch of [{protected:true},{spawnable:false},{claimRevision:0},{defendersOnline:-1},{position:{x:NaN,y:80,z:30}},{extra:true}])assert.throws(()=>validateImperialRaidInput({...input,preflight:{...input.preflight,...patch}}));
});
test('retaliation fees use exact minor-unit amounts and stop at the war threshold',()=>{
assert.deepEqual([0,1,2,3].map(imperialRaidFee),['2500000','3000000','3500000','4000000']);assert.throws(()=>imperialRaidFee(4));
});
test('only a bound definitive unpaid response can release a prepared reference',()=>{
const r=unpaidRaidResponse(id(2),imperialRaidInputHash(intent),'cancelled');assert.equal(r.charged,false);assert.equal(r.definitive,true);assert.equal(r.orderId,id(2));
assert.throws(()=>unpaidRaidResponse(id(2),imperialRaidInputHash(intent),'accepted'));assert.throws(()=>unpaidRaidResponse(id(2),'not-bound','cancelled'));
});
test('acknowledgements bind both original order and original event',()=>{
assert.deepEqual(raidEventAcknowledgement(id(2),id(5),'started'),{ok:true,schema:1,orderId:id(2),eventId:id(5),state:'started'});assert.throws(()=>raidEventAcknowledgement(id(2),'generic-ok','started'));
});