Files
Dune/tests/emperor-auction-gift-advanced-native.test.ts
T
Quintine 28700389dc
Verify and publish checkpoint container / container (push) Successful in 11m23s
Support Advanced expansion auction gifts
2026-11-16 18:28:19 +11:00

95 lines
5.8 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {applyAction, createGame, joinGame, newPlayer, viewGame, type Action, type Game} from '../game/engine';
import {botActions, runBots} from '../game/bots';
import {DIFFICULTIES} from '../game/bot-profiles';
import {advanceSemutaNexusTrade} from './fixture-semuta-nexus-trade';
import {beforeIxSpiceDraw, orderIxSpiceCards} from './fixture-ix-spice-cards';
const act=(g:Game,id:string,action:Action)=>applyAction(g,id,action);
const seat=(g:Game,id:string)=>{const p=g.players.find(row=>row.id===id);assert.ok(p);return p;};
const quiet=(g:Game)=>!g.response&&!g.decision&&!g.phaseOpening&&!g.truthtrance&&!g.pendingTreacheryDiscard;
function inventory(g:Game) {
const cards=[...g.deck,...g.discard,...(g.richeseCache??[]),...(g.richeseRemoved??[]),
...(g.auction?.cards.slice(g.auction.index+Number(g.currentAuctionSale?.origin==='normal'))??[]),
...(g.ixAuction?.cards??[]),...g.players.flatMap(p=>p.hand)].map(card=>card.id).sort();
assert.equal(new Set(cards).size,cards.length);return cards;
}
/** Actual public Advanced selected-deck setup, second-turn worm, mutual
* alliance and native auction. Unopened Spice order, conserved board pieces,
* physical card custody and wallets are selected for this paid example. */
function fixture() {
let g=createGame('ADVANCEDNATIVEGIFT',newPlayer('c','CHOAM','choam'),true,['choam','ix']);
for(const [id,faction] of [['r','richese'],['e','emperor'],['h','harkonnen'],['a','atreides']] as const)
joinGame(g,newPlayer(id,faction,faction));
for(const p of g.players)g=act(g,p.id,{type:'ready'});
g=act(g,'c',{type:'start',advancedPreview:true});
g=advanceSemutaNexusTrade(g,s=>s.status==='playing');
const original=inventory(g);
for(const p of g.players) {p.reserves+=Object.values(p.forces).reduce((sum,n)=>sum+n,0);p.forces={};}
g=advanceSemutaNexusTrade(g,beforeIxSpiceDraw);orderIxSpiceCards(g,['land','land']);
g=advanceSemutaNexusTrade(g,s=>s.turn===2&&beforeIxSpiceDraw(s));orderIxSpiceCards(g,['worm','land','land']);
g=advanceSemutaNexusTrade(g,s=>!!s.nexus&&!s.spiceWindow&&quiet(s));
g=act(g,'e',{type:'alliance',target:'h'});g=act(g,'h',{type:'alliance',target:'e'});
g=advanceSemutaNexusTrade(g,s=>s.phase===3&&s.phaseOpening?.initialize===true);
for(const p of g.players)g.deck.push(...p.hand.splice(0));
const counterIndex=g.deck.findIndex(card=>card.effect==='karama');assert.ok(counterIndex>=0);
const [counter]=g.deck.splice(counterIndex,1);seat(g,'a').hand.push(counter);
seat(g,'e').spice=8;seat(g,'h').spice=6;
g=advanceSemutaNexusTrade(g,s=>s.phase===3&&!!s.auction&&!s.richeseAuction&&quiet(s));
while(g.auction!.active!=='h')g=act(g,g.auction!.active,{type:'passBid'});
g=act(g,'e',{type:'pledgeAid',amount:2});
const card=g.auction!.cards[g.auction!.index].id;
g=act(g,'h',{type:'bid',amount:6,allyPayment:2});
for(let n=0;g.auction&&!g.response&&!g.decision&&n<20;n++)g=act(g,g.auction.active,{type:'passBid'});
assert.equal(g.decision?.kind,'auctionPayment');
const offer=viewGame(g,'e').emperorAuctionGift;assert.ok(offer,'Allied buyer receives an Emperor gift opportunity');
assert.deepEqual(inventory(g),original);
return {g,card,original,counter:counter.id,event:offer.event};
}
function settleInterrupts(g:Game) {
for(let n=0;n<60;n++) {
if(g.pendingKull?.stage==='offer') {g=act(g,g.pendingKull.player,{type:'kullDecision',event:g.pendingKull.event,decline:true});continue;}
if(g.pendingTreacheryDiscard?.reaction?.stage==='offer') {
const pending=g.pendingTreacheryDiscard,reaction=pending.reaction;
if(!reaction || reaction.stage!=='offer') throw Error('Discard offer disappeared');
const p=g.players.find(row=>!reaction.passed.includes(row.id));assert.ok(p);
g=act(g,p.id,{type:'semutaPass',event:pending.batch.event});continue;
}
if(g.response) {const p=g.players.find(row=>!g.response!.passed.includes(row.id));assert.ok(p);g=act(g,p.id,{type:'passResponse'});continue;}
return g;
}
throw Error('Auction response did not settle');
}
for(const cancel of [false,true]) void test(`Advanced native auction allied gift ${cancel?'cancels':'allows'} and pays its original split`,()=>{
const f=fixture(),before=structuredClone(f.g), auction=structuredClone(f.g.auction),aid=structuredClone(f.g.aid);
let g=act(f.g,'e',{type:'emperorGift',amount:2});assert.equal(g.response?.kind,'emperorGift');
if(cancel)g=act(g,'a',{type:'card',mode:'cancel',card:f.counter});
g=settleInterrupts(g);
assert.deepEqual(g.auction,auction);assert.deepEqual(g.aid,aid);
assert.equal(g.pendingEmperorAuctionGift?.event,f.event);
assert.equal(seat(g,'h').spice,seat(before,'h').spice+(cancel?0:2));
assert.equal(seat(g,'e').spice,seat(before,'e').spice-(cancel?0:2));
for(const difficulty of DIFFICULTIES) {
const view=viewGame(g,'e');view.players.find(p=>p.id==='e')!.bot=difficulty;
const action=botActions(view).find(row=>row.type==='emperorAuctionGiftContinue');assert.ok(action,difficulty);
assert.doesNotThrow(()=>act(g,'e',action),difficulty);
}
g=act(g,'e',{type:'emperorAuctionGiftContinue',event:f.event});
if(g.decision?.kind==='auctionPayment')g=act(g,'h',{type:'decision',karama:false});
g=settleInterrupts(g);
assert.ok(seat(g,'h').hand.some(row=>row.id===f.card));
assert.equal(g.aid.e?.amount??0,0);
assert.equal(seat(g,'h').spice,seat(before,'h').spice+(cancel?0:2)-4);
assert.equal(seat(g,'e').spice,seat(before,'e').spice-(cancel?0:2)+4,
'Only the buyer-funded share returns as Emperor income; the Emperor pledge goes to the bank');
assert.deepEqual(inventory(g),f.original);
});
void test('an Advanced native bot Emperor can continue the original auction automatically',()=>{
const {g,card}=fixture();seat(g,'e').bot='Medium';
const done=runBots(g,8);
assert.ok(seat(done,'h').hand.some(row=>row.id===card));
});