172 lines
10 KiB
TypeScript
172 lines
10 KiB
TypeScript
import assert from 'node:assert/strict';
|
||
import test from 'node:test';
|
||
import {handLimit, viewGame, type Game} from '../game/engine';
|
||
import {botActions} from '../game/bots';
|
||
import {DIFFICULTIES} from '../game/bot-profiles';
|
||
import {actTruthAlliance as act, advanceTruthAlliance as advance, createTruthAllianceFixture,
|
||
truthAllianceClean as clean, truthAllianceInventory as inventory,
|
||
truthAlliancePlayer as player} from './fixture-truth-alliance';
|
||
import {declareTruthBribe} from './fixture-truth-bribe';
|
||
|
||
/** Original Advanced Harkonnen/Atreides/Emperor setup; a real turn-two worm
|
||
* and mutual alliance precede the actual Truthtrance and paid winning bid.
|
||
* The shared fixture selects only unopened deal and Spice order/lotteries. */
|
||
function fixture(answer:'yes'|'no', soleLot=false, winningAid=0) {
|
||
const f=createTruthAllianceFixture({advanced:true,consent:'allied'});
|
||
let game=structuredClone(f.game);
|
||
if(soleLot) {
|
||
// Before Bidding, make only the allied buyer eligible for a dealt lot.
|
||
// All transfers use original physical cards and preserve the inventory.
|
||
for(const id of [f.target,f.asker]) {
|
||
const holder=player(game,id);
|
||
while(holder.hand.length<handLimit(holder)) {const card=game.deck.shift();assert.ok(card);holder.hand.push(card);}
|
||
}
|
||
const buyer=player(game,f.partner);
|
||
while(buyer.hand.length>=handLimit(buyer))game.deck.push(buyer.hand.pop()!);
|
||
}
|
||
game=advance(game,state=>state.phase===3&&!!state.auction&&clean(state));
|
||
if(soleLot)assert.equal(game.auction?.cards.length,1,'Only the buyer had room for a lot');
|
||
if(winningAid)game=act(game,f.target,{type:'pledgeAid',amount:winningAid});
|
||
game=declareTruthBribe(f,game).game;
|
||
game=act(game,f.asker,{type:'truthAsk',question:{kind:'pledgeAid',target:f.target,
|
||
turn:game.turn,phase:3,ally:f.partner,minimum:4}});
|
||
assert.ok(viewGame(game,f.target).truthPledgeAnswers?.includes(answer));
|
||
game=act(game,f.target,{type:'truthAnswer',answer});
|
||
const originalPromise=structuredClone(game.pledgePromises?.at(-1));assert.ok(originalPromise);
|
||
assert.equal(originalPromise.answer,answer==='yes');
|
||
assert.equal(originalPromise.fulfilled??false,false);assert.equal(originalPromise.released??false,false);
|
||
while(game.auction?.active!==f.partner) {
|
||
assert.ok(game.auction&&!game.decision&&!game.response);
|
||
game=act(game,game.auction.active,{type:'passBid'});
|
||
}
|
||
game=act(game,f.partner,{type:'bid',amount:1+winningAid,allyPayment:winningAid});
|
||
for(let n=0;game.auction&&!game.decision&&!game.response&&!game.pendingEmperorAuctionGift&&n<15;n++)
|
||
game=act(game,game.auction.active,{type:'passBid'});
|
||
assert.equal(game.decision?.kind,'auctionPayment');
|
||
const offer=viewGame(game,f.target).emperorAuctionGift;assert.ok(offer);
|
||
assert.equal(offer.recipient,f.partner);
|
||
const publicOffer=viewGame(game,f.asker).emperorAuctionGiftPending;
|
||
assert.deepEqual(publicOffer,{owner:f.target});
|
||
assert.equal(viewGame(game,f.asker).emperorAuctionGift,null);
|
||
return {f,game,offer,originalPromise};
|
||
}
|
||
function settleGift(game:Game):Game {
|
||
for(let n=0;game.response?.kind==='emperorGift'&&n<20;n++) {
|
||
const responder=game.players.find(row=>!game.response!.passed.includes(row.id));assert.ok(responder);
|
||
game=act(game,responder.id,{type:'passResponse'});
|
||
}
|
||
return game;
|
||
}
|
||
|
||
void test('a live Advanced pledge Yes allows exactly the excess spice as an allied auction gift',()=>{
|
||
const {f,game,offer,originalPromise}=fixture('yes');
|
||
assert.equal(offer.maximum,player(game,f.target).spice-4);
|
||
assert.ok(offer.maximum>0);
|
||
const before=structuredClone(game);
|
||
assert.throws(()=>act(game,f.target,{type:'emperorGift',amount:offer.maximum+1}));
|
||
assert.deepEqual(game,before);
|
||
const shared=settleGift(act(game,f.target,{type:'emperorGift',amount:offer.maximum}));
|
||
assert.equal(player(shared,f.target).spice,4);
|
||
assert.deepEqual(shared.pledgePromises?.at(-1),originalPromise);
|
||
const beforeContinue=structuredClone(shared);
|
||
assert.throws(()=>act(shared,f.target,{type:'emperorAuctionGiftContinue',event:offer.event}));
|
||
assert.deepEqual(shared,beforeContinue);
|
||
const guide=viewGame(shared,f.target).pledgePromiseGuide;assert.ok(guide);
|
||
const pledged=act(shared,f.target,guide.action);
|
||
assert.equal(pledged.pledgePromises?.at(-1)?.fulfilled,true);
|
||
assert.deepEqual(pledged.auction,shared.auction);
|
||
const continued=act(pledged,f.target,{type:'emperorAuctionGiftContinue',event:offer.event});
|
||
assert.equal(continued.pendingEmperorAuctionGift??null,null);
|
||
assert.equal(continued.pledgePromises?.at(-1)?.fulfilled,true);
|
||
assert.deepEqual(inventory(continued),f.originalCards);
|
||
});
|
||
|
||
void test('a No-only Advanced pledge answer does not suppress an otherwise legal allied auction gift',()=>{
|
||
const {f,game,offer,originalPromise}=fixture('no');
|
||
assert.equal(offer.maximum,player(game,f.target).spice);
|
||
assert.ok(offer.maximum>0);
|
||
const shared=settleGift(act(game,f.target,{type:'emperorGift',amount:offer.maximum}));
|
||
assert.equal(player(shared,f.target).spice,0);
|
||
assert.deepEqual(shared.pledgePromises?.at(-1),originalPromise);
|
||
const continued=act(shared,f.target,{type:'emperorAuctionGiftContinue',event:offer.event});
|
||
assert.equal(continued.pendingEmperorAuctionGift??null,null);
|
||
assert.deepEqual(continued.pledgePromises?.at(-1),originalPromise);
|
||
assert.deepEqual(inventory(continued),f.originalCards);
|
||
});
|
||
|
||
for(const soleLot of [false,true]) void test(
|
||
`a pending Advanced allied gift permits the native pledge guide${soleLot?' on the sole lot':''}` ,()=>{
|
||
const {f,game,offer}=fixture('yes',soleLot);
|
||
const lot=structuredClone(game.auction), saleCard=lot!.cards[lot!.index].id;
|
||
const guide=viewGame(game,f.target).pledgePromiseGuide;assert.ok(guide);
|
||
assert.deepEqual(guide.action,{type:'pledgeAid',amount:4,emperorAuctionGiftEvent:offer.event});
|
||
const beforeStale=structuredClone(game);
|
||
assert.throws(()=>act(game,f.target,{...guide.action,emperorAuctionGiftEvent:'stale-event'}));
|
||
assert.deepEqual(game,beforeStale);
|
||
assert.equal(viewGame(game,f.asker).pledgePromiseGuide,null);
|
||
for(const difficulty of DIFFICULTIES) {
|
||
const view=viewGame(game,f.target);view.players.find(row=>row.id===f.target)!.bot=difficulty;
|
||
const action=botActions(view)[0];assert.ok(action,difficulty);
|
||
assert.deepEqual(action,guide.action,`${difficulty} fulfills the pledge before Continue`);
|
||
const bot=act(game,f.target,action);
|
||
assert.equal(bot.pledgePromises?.at(-1)?.fulfilled,true);
|
||
assert.deepEqual(bot.auction,lot);
|
||
assert.equal(bot.pendingEmperorAuctionGift?.event,offer.event);
|
||
}
|
||
const pledged=act(game,f.target,guide.action);
|
||
assert.equal(pledged.pledgePromises?.at(-1)?.fulfilled,true);
|
||
assert.equal(pledged.aid[f.target]?.amount,4);
|
||
assert.deepEqual(pledged.auction,lot);
|
||
assert.equal(pledged.auction?.allyPayment,0,'The earlier winning split is not changed');
|
||
assert.equal(pledged.pendingEmperorAuctionGift?.event,offer.event);
|
||
let continued=act(pledged,f.target,{type:'emperorAuctionGiftContinue',event:offer.event});
|
||
assert.equal(continued.pendingEmperorAuctionGift??null,null);
|
||
assert.throws(()=>act(continued,f.target,guide.action),'A stale guide cannot create a later funding update');
|
||
for(let n=0;!player(continued,f.partner).hand.some(card=>card.id===saleCard)&&n<20;n++) {
|
||
if(continued.decision?.kind==='auctionPayment')
|
||
continued=act(continued,f.partner,{type:'decision',karama:false});
|
||
else if(continued.response) {
|
||
const actor=continued.players.find(row=>!continued.response!.passed.includes(row.id));assert.ok(actor);
|
||
continued=act(continued,actor.id,{type:'passResponse'});
|
||
} else throw Error('The original winning purchase lost its payment continuation');
|
||
}
|
||
assert.equal(continued.pledgePromises?.at(-1)?.fulfilled,true);
|
||
assert.ok(player(continued,f.partner).hand.some(card=>card.id===saleCard));
|
||
assert.deepEqual(inventory(continued),f.originalCards);
|
||
});
|
||
|
||
void test('the Advanced pledge guide preserves a winning bid’s existing two-spice allied contribution',()=>{
|
||
const {f,game,offer}=fixture('yes',false,2);
|
||
const lot=structuredClone(game.auction);assert.ok(lot);
|
||
const card=lot.cards[lot.index].id;
|
||
assert.equal(lot.bid,3);assert.equal(lot.allyPayment,2);
|
||
assert.equal(game.aid[f.target]?.amount,2);
|
||
const buyerBefore=player(game,f.partner).spice,emperorBefore=player(game,f.target).spice;
|
||
const guide=viewGame(game,f.target).pledgePromiseGuide;assert.ok(guide);
|
||
assert.deepEqual(guide.action,{type:'pledgeAid',amount:4,emperorAuctionGiftEvent:offer.event});
|
||
let next=act(game,f.target,guide.action);
|
||
assert.equal(next.pledgePromises?.at(-1)?.fulfilled,true);
|
||
assert.equal(next.aid[f.target]?.amount,4);
|
||
assert.deepEqual(next.auction,lot);
|
||
assert.equal(next.pendingEmperorAuctionGift?.event,offer.event);
|
||
next=act(next,f.target,{type:'emperorAuctionGiftContinue',event:offer.event});
|
||
for(let n=0;!player(next,f.partner).hand.some(row=>row.id===card)&&n<20;n++) {
|
||
if(next.decision?.kind==='auctionPayment')next=act(next,f.partner,{type:'decision',karama:false});
|
||
else if(next.response) {
|
||
const actor=next.players.find(row=>!next.response!.passed.includes(row.id));assert.ok(actor);
|
||
next=act(next,actor.id,{type:'passResponse'});
|
||
} else throw Error('The original winning purchase lost its payment continuation');
|
||
}
|
||
assert.ok(player(next,f.partner).hand.some(row=>row.id===card));
|
||
for(let n=0;next.response?.kind==='emperorIncome'&&n<20;n++) {
|
||
const actor=next.players.find(row=>!next.response!.passed.includes(row.id));assert.ok(actor);
|
||
next=act(next,actor.id,{type:'passResponse'});
|
||
}
|
||
assert.equal(player(next,f.partner).spice,buyerBefore-1,'The buyer pays the original one-spice share');
|
||
assert.equal(next.aid[f.target]?.amount,2,'Only the original two-spice winning contribution is consumed');
|
||
assert.equal(player(next,f.target).spice,emperorBefore-2+1,
|
||
'The additional pledge remains escrowed; only the buyer-funded share reaches the Emperor');
|
||
assert.equal(next.pledgePromises?.at(-1)?.fulfilled,true);
|
||
assert.deepEqual(inventory(next),f.originalCards);
|
||
});
|