import assert from 'node:assert/strict'; import test from 'node:test'; import {botActions, runBots} from '../game/bots'; import {DIFFICULTIES} from '../game/bot-profiles'; import {type Action, type Game, viewGame} from '../game/engine'; import {createTruthBidFixture} from './fixture-truth-bid'; import {actTruthAlliance as act, advanceTruthAlliance as advance, truthAllianceClean as clean, truthAllianceInventory as inventory, truthAllianceNexus, truthAlliancePlayer as player} from './fixture-truth-alliance'; import {beforeIxSpiceDraw, orderIxSpiceCards} from './fixture-ix-spice-cards'; /** Genuine public Advanced setup, actual second-turn worm, reciprocal consent * and Guild shipment. Only unopened Spice order, personal purses and physical * counter cards are staged; the alliance, payment and original rate window * are reached through native actions. */ function fixture(counters: number) { const base=createTruthBidFixture({advanced:true,window:'waiting',extraGuild:true}); let game=advance(base.game,state=>state.turn===2&&beforeIxSpiceDraw(state)); orderIxSpiceCards(game,['worm','land','land']); game=advance(game,truthAllianceNexus); game=act(game,'guild',{type:'alliance',target:'emperor'}); game=act(game,'emperor',{type:'alliance',target:'guild'}); game=advance(game,state=>state.phase===5&&state.active==='guild'&&clean(state)); assert.equal(player(game,'guild').ally,'emperor'); assert.equal(player(game,'emperor').ally,'guild'); const cards=inventory(game); // Keep all counters in one public responder's actual hand; the no-counter // case genuinely removes every playable physical Karama from all hands. for(const seat of game.players) { const keep=seat.hand.filter(card=>card.effect!=='karama'); game.deck.push(...seat.hand.filter(card=>card.effect==='karama')); seat.hand=keep; } const counterIds:string[]=[]; for(let n=0;ncard.effect==='karama');assert.ok(index>=0); const [card]=game.deck.splice(index,1);counterIds.push(card.id); player(game,'atreides').hand.push(card); } assert.deepEqual(inventory(game),cards); player(game,'guild').spice=2;player(game,'emperor').spice=8; game=act(game,'guild',{type:'ship',territory:'polar_sink',sector:0,amount:2,allyPayment:0}); if(game.decision?.kind==='guildShipment')game=act(game,'guild',{type:'decision',allow:true}); assert.equal(game.response?.kind,'guildRate');assert.equal(game.pendingShipment?.cost,2); return {game,cards,counterIds}; } function passResponse(game:Game):Game { for(let n=0;game.response&&n<20;n++) { const actor=game.players.find(row=>!game.response!.passed.includes(row.id));assert.ok(actor); game=act(game,actor.id,{type:'passResponse'}); } return game; } function reject(game:Game,id:string,action:Action) { const before=structuredClone(game);assert.throws(()=>act(game,id,action));assert.deepEqual(game,before); } function priority(game:Game) { const view=viewGame(game,'emperor'); const opportunity=view.emperorGuildRatePriority;assert.ok(opportunity?.canContinue); assert.equal(opportunity.recipient,'guild');return opportunity; } void test('Advanced Emperor gift preserves original Guild rate and funds the unchanged full-price shipment',()=>{ const f=fixture(1);let game=f.game; const before=structuredClone(game),original=structuredClone(game.response),shipment=structuredClone(game.pendingShipment); assert.deepEqual(viewGame(game,'emperor').emperorGuildRateGift,{recipient:'guild',maximum:8}); assert.equal(viewGame(game,'atreides').emperorGuildRateGift,null); priority(game);reject(game,'atreides',{type:'card',mode:'cancel',card:f.counterIds[0]}); game=act(game,'emperor',{type:'emperorGift',amount:2}); assert.equal(game.response?.kind,'emperorGift'); while(game.response?.kind==='emperorGift') { const actor=game.players.find(row=>!game.response!.passed.includes(row.id));assert.ok(actor); game=act(game,actor.id,{type:'passResponse'}); } assert.deepEqual(game.response,original);assert.deepEqual(game.pendingShipment,shipment); assert.equal(player(game,'emperor').spice,player(before,'emperor').spice-2); assert.equal(player(game,'guild').spice,player(before,'guild').spice+2); game=act(game,'atreides',{type:'card',mode:'cancel',card:f.counterIds[0]}); assert.equal(game.pendingShipment,null);assert.equal(player(game,'guild').spice,0); assert.equal(player(game,'guild').forces['polar_sink:0'],2); assert.deepEqual(inventory(game),f.cards); }); void test('canceling the gift restores its original rate window and unfunded rate cancellation withdraws shipment',()=>{ const f=fixture(2);let game=f.game; const before=structuredClone(game),original=structuredClone(game.response),shipment=structuredClone(game.pendingShipment); priority(game);game=act(game,'emperor',{type:'emperorGift',amount:2}); game=act(game,'atreides',{type:'card',mode:'cancel',card:f.counterIds[0]}); assert.deepEqual(game.response,original);assert.deepEqual(game.pendingShipment,shipment); assert.equal(player(game,'emperor').spice,player(before,'emperor').spice); assert.equal(player(game,'guild').spice,player(before,'guild').spice); game=act(game,'atreides',{type:'card',mode:'cancel',card:f.counterIds[1]}); assert.equal(game.pendingShipment,null);assert.equal(player(game,'guild').spice,2); assert.equal(player(game,'guild').shipped,false);assert.ok(game.guildRateBlocked); assert.deepEqual(inventory(game),f.cards); }); void test('a counter-free rate window waits for the human Emperor and every bot may continue',()=>{ const f=fixture(0), initial=f.game;const opportunity=priority(initial); assert.equal(initial.response?.kind,'guildRate'); const empty=structuredClone(initial);player(empty,'emperor').spice=0; assert.deepEqual(viewGame(empty,'guild').emperorGuildRatePriority,viewGame(initial,'guild').emperorGuildRatePriority); assert.equal(viewGame(empty,'emperor').emperorGuildRateGift,null); for(const difficulty of DIFFICULTIES) { let game=structuredClone(initial);player(game,'guild').bot=difficulty; const waiting=runBots(game,2);assert.deepEqual(waiting.response,game.response); const view=viewGame(game,'emperor');view.players.find(row=>row.id==='emperor')!.bot=difficulty; const action=botActions(view).find(row=>row.type==='emperorGuildRateContinue');assert.ok(action,difficulty); assert.equal(action.event,opportunity.event); const automated=structuredClone(game);player(automated,'emperor').bot=difficulty; const resumed=runBots(automated,1); assert.equal(resumed.pendingShipment??null,null,`${difficulty} bot releases the original rate`); assert.equal(player(resumed,'guild').forces['polar_sink:0'],2); game=act(game,'emperor',action); game=passResponse(game); assert.equal(player(game,'guild').spice,0);assert.equal(player(game,'guild').forces['polar_sink:0'],2); } });