web/website/tests/game-native-state.test.mjs

25 lines
2.2 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';
import {createGameNativeStateAPI,nativeClanControllerIds} from '../node/game-native-state.mjs';
import {fixture} from './rebellion-fixture.mjs';
test('native build controllers follow the current mother deed without changing the child roster',()=>{
const child={clan:{id:'child'},root:'mother',owner:{id:'child-owner'},heir:{id:'child-heir'}};
assert.deepEqual(nativeClanControllerIds(child,{parentControllerIds:['mother-owner','mother-heir','mother-owner']}),['child-owner','mother-heir','mother-owner']);
assert.deepEqual(nativeClanControllerIds({...child,root:'child'},{parentControllerIds:[]}),['child-heir','child-owner']);
assert.equal(child.heir.id,'child-heir');
});
test('native state uses the existing private game credential, never browser fields or arbitrary commands',async()=>{
const secret='x'.repeat(40);let reads=0;const route=createGameNativeStateAPI({}, {GAME_LINK_SECRET:secret},{read:async()=>{reads++;return {ok:true};}});
const request=(body={action:'read'},auth='Bearer '+secret)=>new Request('https://cp.dunerealms.com/internal/game/native-state',{method:'POST',headers:{authorization:auth,'content-type':'application/json'},body:JSON.stringify(body)});
assert.equal((await route(request({},''))).status,401);assert.equal((await route(request({action:'read',command:'op player'}))).status,400);assert.equal(reads,0);
assert.equal((await route(request())).status,200);assert.equal(reads,1);
});
test('native war projection reuses the declaration guard and deactivates withdrawn or changed deeds',async()=>{
const f=await fixture();try{
const quest=await f.service.declare(f.winner,f.input);
let wars=await f.service.nativeWars();assert.equal(wars.length,1);assert.equal(wars[0].keyId,quest.keyId);assert.equal(wars[0].active,true);
await f.db.query('UPDATE site_clan_deeds SET version=version+1 WHERE clan_id=?',[f.attacker]);
assert.equal((await f.service.nativeWars())[0].active,false);
await f.db.query('UPDATE site_clan_deeds SET version=version-1 WHERE clan_id=?',[f.attacker]);
await f.service.cancel(f.winner,{questKeyId:quest.keyId});wars=await f.service.nativeWars();assert.equal(wars[0].active,false);
}finally{f.db.close();}
});