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} from '../game/bots'; import {DIFFICULTIES} from '../game/bot-profiles'; import {takeKullCard} from './fixture-choam-kull'; import {advanceSemutaNexusTrade, nextSemutaNexusTradeNativeStep} from './fixture-semuta-nexus-trade'; import {beforeIxSpiceDraw, orderIxSpiceCards} from './fixture-ix-spice-cards'; const act=(game:Game,id:string,action:Action)=>applyAction(game,id,action); const seat=(game:Game,id:string)=>{const player=game.players.find(row=>row.id===id);assert.ok(player);return player;}; const quiet=(game:Game)=>!game.phaseOpening&&!game.response&&!game.decision&&!game.truthtrance&&!game.pendingTreacheryDiscard; function inventory(game:Game) { const cards=[...game.deck,...game.discard,...(game.richeseCache??[]),...(game.richeseRemoved??[]), ...(game.auction?.cards.slice(game.auction.index+Number(game.currentAuctionSale?.origin==='normal'))??[]), ...(game.ixAuction?.cards??[]),...game.players.flatMap(player=>player.hand)].map(card=>card.id).sort(); assert.equal(new Set(cards).size,cards.length);return cards; } function finishResponses(game:Game):Game { for(let n=0;game.response&&n<20;n++) { const actor=game.players.find(player=>!game.response!.passed.includes(player.id));assert.ok(actor); game=act(game,actor.id,{type:'passResponse'}); } return game; } function finishDiscardOffers(game:Game):Game { for(let n=0;game.pendingTreacheryDiscard?.reaction?.stage==='offer'&&n<20;n++) { const pending=game.pendingTreacheryDiscard,reaction=pending.reaction; assert.ok(reaction&&reaction.stage==='offer'); const actor=game.players.find(player=>!reaction.passed.includes(player.id));assert.ok(actor); game=act(game,actor.id,{type:'semutaPass',event:pending.batch.event}); } return game; } /** Original Advanced setup, natural second-turn worm and actual mutual alliance. * The unopened Spice order, conserved board positions, hands and purses are staged. */ function fixture(method:'normal'|'onceAround'|'silent', self=false) { let game=createGame('ADVBLACKGIFT',newPlayer('e','Emperor','emperor'),true,['choam','ix']); for(const [id,faction] of [['g','guild'],['c','choam'],['r','richese'],['a','atreides']] as const) joinGame(game,newPlayer(id,faction,faction)); for(const player of game.players)game=act(game,player.id,{type:'ready'}); game=act(game,'e',{type:'start',advancedPreview:true}); game=advanceSemutaNexusTrade(game,state=>state.status==='playing'); for(const player of game.players){player.reserves+=Object.values(player.forces).reduce((sum,n)=>sum+n,0);player.forces={};} game=advanceSemutaNexusTrade(game,beforeIxSpiceDraw);orderIxSpiceCards(game,['land','land']); game=advanceSemutaNexusTrade(game,state=>state.turn===2&&beforeIxSpiceDraw(state)); orderIxSpiceCards(game,['worm','land','land']); game=advanceSemutaNexusTrade(game,state=>!!state.nexus&&!state.spiceWindow&&quiet(state)); const buyer=self?'r':'g'; game=act(game,'e',{type:'alliance',target:buyer});game=act(game,buyer,{type:'alliance',target:'e'}); game=advanceSemutaNexusTrade(game,state=>state.phase===3&&state.phaseOpening?.initialize===true); for(const player of game.players)game.deck.push(...player.hand.splice(0)); const karama=takeKullCard(game,'karama');seat(game,'a').hand.push(karama); const card=game.deck.shift();assert.ok(card);seat(game,'r').hand.push(card); seat(game,buyer).spice=7;seat(game,'e').spice=8; game=advanceSemutaNexusTrade(game,state=>state.decision?.kind==='richeseBlackMarket'); const physical=inventory(game), originalHand=seat(game,'r').hand.map(row=>row.id); game=act(game,'r',{type:'decision',event:game.richeseBidding!.event,card:card.id,method, ...(method==='onceAround'?{direction:'clockwise'}:{})}); game=finishResponses(game); assert.equal(game.richeseAuction?.source,'blackMarket'); game=act(game,'e',{type:'pledgeAid',amount:1}); let bid=false, opening=false; for(let n=0;!game.pendingEmperorAuctionGift&&n<300;n++) { const lot=game.richeseAuction; if(lot&&!lot.outcome&&!game.response&&!game.decision&&lot.method!=='silent'&&lot.active) { const actor=lot.active, winning=actor===buyer&&!bid; const prebid=self&&method==='onceAround'&&actor!==buyer&&!opening; game=act(game,actor,{type:'richeseBid',event:lot.event,amount:winning?3:prebid?1:null, ...(winning?{allyPayment:1}:{})}); if(winning)bid=true; if(prebid)opening=true; } else if(lot&&!lot.outcome&&!game.response&&!game.decision&&lot.method==='silent') { const actor=lot.eligible.find(id=>!Object.hasOwn(lot.sealed,id));assert.ok(actor); const winning=actor===buyer&&!bid; game=act(game,actor,{type:'richeseBid',event:lot.event,amount:winning?3:0,...(winning?{allyPayment:1}:{})}); if(winning)bid=true; } else { const next=nextSemutaNexusTradeNativeStep(game);game=act(game,next.actor,next.action); } } assert.equal(bid,true);assert.ok(game.pendingEmperorAuctionGift,'Emperor receives the original Black Market purchase opportunity'); assert.equal(game.richeseAuction?.outcome?.kind,'sold');assert.equal(game.currentAuctionSale??null,null); assert.equal(viewGame(game,'e').emperorAuctionGift?.recipient,buyer); return {game,buyer,card:card.id,karama:karama.id,physical,originalHand}; } for(const method of ['normal','onceAround','silent'] as const) for(const mode of ['continue','allow','cancel'] as const) void test( `Advanced ${method} Black Market sale retains seller custody and split after Emperor ${mode}`,()=>{ const f=fixture(method),before=structuredClone(f.game),lot=structuredClone(f.game.richeseAuction); const funding=structuredClone(f.game.richeseFunding),aid=structuredClone(f.game.aid); const offer=viewGame(f.game,'e').emperorAuctionGift;assert.ok(offer); for(const difficulty of DIFFICULTIES) { const view=viewGame(f.game,'e');view.players.find(player=>player.id==='e')!.bot=difficulty; const action=botActions(view).find(row=>row.type==='emperorAuctionGiftContinue');assert.ok(action,difficulty); assert.doesNotThrow(()=>act(f.game,'e',action),difficulty); } let game=f.game; if(mode!=='continue') { game=act(game,'e',{type:'emperorGift',amount:2});assert.equal(game.response?.kind,'emperorGift'); if(mode==='cancel')game=finishDiscardOffers(act(game,'a',{type:'card',mode:'cancel',card:f.karama})); else game=finishResponses(game); } assert.deepEqual(game.richeseAuction,lot);assert.deepEqual(game.richeseFunding,funding);assert.deepEqual(game.aid,aid); assert.equal(game.pendingEmperorAuctionGift?.event,offer.event); assert.ok(seat(game,'r').hand.some(row=>row.id===f.card),'Seller retains the physical card before settlement'); assert.equal(seat(game,'e').spice,seat(before,'e').spice-(mode==='allow'?2:0)); assert.equal(seat(game,f.buyer).spice,seat(before,f.buyer).spice+(mode==='allow'?2:0)); game=act(game,'e',{type:'emperorAuctionGiftContinue',event:offer.event}); game=finishResponses(game); assert.ok(seat(game,f.buyer).hand.some(row=>row.id===f.card)); assert.ok(!seat(game,'r').hand.some(row=>row.id===f.card)); assert.equal(seat(game,f.buyer).spice,seat(before,f.buyer).spice+(mode==='allow'?2:0)-2); assert.equal(seat(game,'e').spice,seat(before,'e').spice-(mode==='allow'?2:0)); assert.equal(game.aid.e?.amount??0,0); assert.equal(seat(game,'r').spice,seat(before,'r').spice+2,'Emperor-funded one goes to the bank, buyer-funded two to Richese'); assert.deepEqual(inventory(game),f.physical); }); for(const method of ['normal','onceAround','silent'] as const) void test( `Advanced ${method} Black Market seller retains its own card after an Emperor gift`,()=>{ const f=fixture(method,true),before=structuredClone(f.game); const offer=viewGame(f.game,'e').emperorAuctionGift;assert.ok(offer); let game=act(f.game,'e',{type:'emperorGift',amount:2});game=finishResponses(game); assert.deepEqual(seat(game,'r').hand.map(row=>row.id),f.originalHand); game=act(game,'e',{type:'emperorAuctionGiftContinue',event:offer.event}); game=finishResponses(game); assert.deepEqual(seat(game,'r').hand.map(row=>row.id),f.originalHand,'Paid self-retention does not reacquire or reorder the card'); assert.equal(seat(game,'r').spice,seat(before,'r').spice,'Gift two less buyer-funded two'); assert.equal(seat(game,'e').spice,seat(before,'e').spice,'Gift two less Emperor-funded bank one plus buyer-funded income two'); assert.equal(game.aid.e?.amount??0,0); assert.deepEqual(inventory(game),f.physical); });