import assert from 'node:assert/strict'; import test from 'node:test'; import { viewGame, type Action, 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'; import { createTruthBidFixture } from './fixture-truth-bid'; import { truthAllianceNexus } from './fixture-truth-alliance'; import { beforeIxSpiceDraw, orderIxSpiceCards } from './fixture-ix-spice-cards'; /** Genuine public Advanced start, actual turn-two worm and reciprocal alliance. * Only unopened setup/deck/Spice order and scalar lotteries are selected by * the shared fixture; wallets, alliances, auctions and commitments are earned. */ function fixture(phase: 3 | 5 = 3) { const base = createTruthAllianceFixture({advanced:true, consent:'allied'}); const game = advance(base.game, state => state.phase === phase && (phase !== 3 || !!state.auction) && clean(state)); assert.equal(player(game,base.target).ally,base.partner); assert.equal(player(game,base.partner).ally,base.target); assert.deepEqual(inventory(game),base.originalCards); return {...base, game, ally:base.partner, physical:inventory(game)}; } /** A public four-seat Advanced game with the Guild. Its original second-turn * worm opens the actual Nexus; Atreides and Emperor explicitly consent before * the Emperor's native Shipment and Movement turn. */ function guildShipmentFixture() { 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,base.opponent,{type:'alliance',target:base.target}); game=act(game,base.target,{type:'alliance',target:base.opponent}); game=advance(game,state=>state.phase===5&&state.active===base.target&&clean(state)); assert.equal(player(game,base.target).ally,base.opponent); assert.equal(player(game,base.opponent).ally,base.target); assert.ok(!player(game,base.target).shipped); assert.deepEqual(inventory(game),base.originalCards); return {...base,game,ally:base.opponent,physical:inventory(game)}; } function ask(f:{game:Game;asker:string;target:string;ally:string;truths:string[]}, source:Game, minimum:number) { let game=declareTruthBribe(f,source).game; assert.ok(viewGame(game,f.asker).truthPledgeTargets.includes(f.target)); game=act(game,f.asker,{type:'truthAsk',question:{kind:'pledgeAid',target:f.target, turn:game.turn,phase:game.phase,ally:f.ally,minimum}}); return game; } function rejected(game:Game, id:string, action:Action) { const before=structuredClone(game); assert.throws(()=>act(game,id,action));assert.deepEqual(game,before); } function promise(game:Game) { const row=game.pledgePromises?.at(-1);assert.ok(row);return row; } function passResponse(game:Game) { for(let n=0;game.response&&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'}); } assert.equal(game.response,null);return game; } for(const phase of [3,5] as const) void test(`Advanced phase ${phase} pledge Yes preserves native escrow and bot continuation`,()=>{ const f=fixture(phase); let game=act(f.game,f.target,{type:'pledgeAid',amount:1}); const before=structuredClone(game),recipientSpice=player(game,f.ally).spice; game=ask(f,game,4); assert.ok(viewGame(game,f.target).truthPledgeAnswers?.includes('yes')); game=act(game,f.target,{type:'truthAnswer',answer:'yes'}); assert.ok(!promise(game).fulfilled); const guide=viewGame(game,f.target).pledgePromiseGuide;assert.ok(guide); assert.equal(guide.action.type,'pledgeAid');assert.equal(guide.action.amount,4); 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.doesNotThrow(()=>act(game,f.target,action),difficulty); } game=act(game,f.target,guide.action); assert.equal(promise(game).fulfilled,true); assert.equal(game.aid[f.target].recipient,f.ally);assert.equal(game.aid[f.target].amount,4); assert.equal(player(game,f.target).spice,player(before,f.target).spice-3); assert.equal(player(game,f.ally).spice,recipientSpice); assert.deepEqual(inventory(game),f.physical); }); for(const phase of [3,5] as const) void test(`Advanced phase ${phase} pledge No prevents qualifying future updates`,()=>{ const f=fixture(phase);let game=ask(f,f.game,3); assert.ok(viewGame(game,f.target).truthPledgeAnswers?.includes('no')); game=act(game,f.target,{type:'truthAnswer',answer:'no'}); game=act(game,f.target,{type:'pledgeAid',amount:2}); const aid=structuredClone(game.aid[f.target]),spice=player(game,f.target).spice; rejected(game,f.target,{type:'pledgeAid',amount:3}); assert.deepEqual(game.aid[f.target],aid);assert.equal(player(game,f.target).spice,spice); assert.deepEqual(inventory(game),f.physical); }); void test('Advanced pledge excludes an unaffordable Yes without mutating native resources',()=>{ const f=fixture(), game=ask(f,f.game,player(f.game,f.target).spice+1); assert.deepEqual(viewGame(game,f.target).truthPledgeAnswers,['no']); rejected(game,f.target,{type:'truthAnswer',answer:'yes'}); }); void test('Advanced pledge Yes blocks voluntary spending that would make its update impossible',()=>{ const f=fixture();let game=ask(f,f.game,4); game=act(game,f.target,{type:'truthAnswer',answer:'yes'}); const available=player(game,f.target).spice; rejected(game,f.target,{type:'bribe',target:f.asker,amount:available-1}); assert.ok(!promise(game).fulfilled&&!promise(game).released); const guide=viewGame(game,f.target).pledgePromiseGuide;assert.ok(guide); game=act(game,f.target,guide.action);assert.equal(promise(game).fulfilled,true); assert.deepEqual(inventory(game),f.physical); }); void test('Advanced Yes rejects an unaffordable pending Guild shipment without mutating state',()=>{ const f=guildShipmentFixture(),available=player(f.game,f.target).spice; assert.ok(available>=4); let game=ask(f,f.game,available-1); game=act(game,f.target,{type:'truthAnswer',answer:'yes'}); const ship:Action={type:'ship',amount:1,territory:'polar_sink',sector:0,allyPayment:0}; rejected(game,f.target,ship); assert.ok(!promise(game).fulfilled&&!promise(game).released); assert.equal(game.pendingShipment??null,null); assert.deepEqual(inventory(game),f.physical); }); void test('Advanced Yes permits a funded Guild shipment and its original continuation',()=>{ const f=guildShipmentFixture(),available=player(f.game,f.target).spice; assert.ok(available>=4); let game=ask(f,f.game,available-2); game=act(game,f.target,{type:'truthAnswer',answer:'yes'}); const before=structuredClone(game); game=act(game,f.target,{type:'ship',amount:1,territory:'polar_sink',sector:0,allyPayment:0}); assert.equal(game.decision?.kind,'guildShipment');assert.ok(game.pendingShipment); assert.equal(player(game,f.target).spice,player(before,f.target).spice); game=act(game,'guild',{type:'decision',allow:true}); game=passResponse(game); assert.equal(game.pendingShipment??null,null); assert.equal(player(game,f.target).spice,available-2); assert.equal(player(game,f.target).reserves,player(before,f.target).reserves-1); const guide=viewGame(game,f.target).pledgePromiseGuide;assert.ok(guide); game=act(game,f.target,guide.action);assert.equal(promise(game).fulfilled,true); assert.deepEqual(inventory(game),f.physical); }); void test('Advanced No remains available with a locked winning bid and rejects a qualifying update',()=>{ const f=fixture();let game=act(f.game,f.target,{type:'pledgeAid',amount:4}); game=advance(game,state=>state.auction?.active===f.ally && clean(state)); game=act(game,f.ally,{type:'bid',amount:5,allyPayment:4}); assert.equal(game.auction?.bidder,f.ally);assert.equal(game.auction?.allyPayment,4); game=ask(f,game,4); assert.ok(viewGame(game,f.target).truthPledgeAnswers?.includes('no')); game=act(game,f.target,{type:'truthAnswer',answer:'no'}); const bid=structuredClone(game.auction), aid=structuredClone(game.aid[f.target]); rejected(game,f.target,{type:'pledgeAid',amount:4}); rejected(game,f.target,{type:'pledgeAid',amount:5}); assert.deepEqual(game.auction,bid);assert.deepEqual(game.aid[f.target],aid); const owner=player(game,f.target).spice,buyer=player(game,f.ally).spice; game=advance(game,state=>state.decision?.kind==='auctionPayment'); if(game.pendingEmperorAuctionGift)game=act(game,f.target,{type:'emperorAuctionGiftContinue',event:game.pendingEmperorAuctionGift.event}); if(game.decision?.kind==='auctionPayment')game=act(game,f.ally,{type:'decision',karama:false}); game=passResponse(game); assert.equal(game.aid[f.target]?.amount??0,0); assert.equal(player(game,f.ally).spice,buyer-1); assert.equal(player(game,f.target).spice,owner+1,'Advanced sends the ally-funded portion to the bank'); assert.deepEqual(inventory(game),f.physical); });