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

19 lines
1.7 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';
import {createGameNativeStateAPI} from '../node/game-native-state.mjs';
import {fixture} from './rebellion-fixture.mjs';
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();}
});