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 {territory} from '../game/board'; import {beforeIxSpiceDraw, orderIxSpiceCards} from './fixture-ix-spice-cards'; import {nextSemutaNexusTradeNativeStep} from './fixture-semuta-nexus-trade'; 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 cards(g:Game) { const ids=[...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(ids).size,ids.length);return ids; } function advance(g:Game,until:(g:Game)=>boolean) { for(let n=0;n<2400;n++) { if(until(g))return g; assert.notEqual(g.status,'finished'); if(g.status==='setup'&&g.setupStage==='forces'&&seat(g,'ecaz').reserves===20) g=act(g,'ecaz',{type:'ecazSetup',placements:{'imperial_basin:9':6}}); else if(g.decision?.kind==='ecazPlacement'&&!g.response&&!g.phaseOpening) g=act(g,'ecaz',{type:'decision',decline:true}); else {const next=nextSemutaNexusTradeNativeStep(g);g=act(g,next.actor,next.action);} } throw Error('Advanced native game did not reach the requested auction'); } /** Public Advanced setup, genuine Sandtrout, then an actual Ecaz Ambassador * alliance and the following native auction. Spice prefix, conserved board * pieces, physical counter custody and wallets are staged. */ function fixture() { let g=createGame('ADVANCED-GIFT-SANDTROUT',newPlayer('ecaz','Ecaz','ecaz'),true,['choam','ix','ecaz']); for(const id of ['emperor','richese','choam','atreides'] as const)joinGame(g,newPlayer(id,id,id)); for(const p of g.players)g=act(g,p.id,{type:'ready'}); g=act(g,'ecaz',{type:'start',advancedPreview:true}); g=advance(g,s=>s.status==='playing');const original=cards(g); for(const p of g.players){p.reserves+=Object.values(p.forces).reduce((sum,n)=>sum+n,0);p.forces={};} g=advance(g,beforeIxSpiceDraw);orderIxSpiceCards(g,['sandtrout','land','land']); g=advance(g,s=>s.phase===4&&!s.response&&!s.phaseOpening&&s.decision?.kind==='ecazPlacement'); assert.equal(g.sandtrout,true); const location=['habbanya_ridge_sietch','sietch_tabr','carthag','arrakeen','tueks_sietch'] .map(id=>({territory:id,sector:territory(id).sectors.find(s=>s!==g.storm)})) .find((row):row is {territory:string;sector:number}=>row.sector!==undefined);assert.ok(location); const token=g.ecazAmbassadors!.tokens.find(row=>row.effect==='ecaz');assert.ok(token); g=act(g,'ecaz',{type:'decision',token:token.id,territory:location.territory}); g=advance(g,s=>s.phase===5&&quiet(s)&&s.active==='emperor'); g=act(g,'emperor',{type:'ship',amount:1,...location}); g=advance(g,s=>!s.response&&s.pendingAmbassador?.stage==='offer'); const event=g.pendingAmbassador!.event; g=act(g,'ecaz',{type:'decision',event,trigger:true,beneficiary:'ecaz',choice:'alliance'}); g=act(g,'emperor',{type:'decision',event,accept:true}); if(g.pendingAmbassador?.stage==='loan')g=act(g,'ecaz',{type:'decision',event,loan:false}); assert.equal(seat(g,'ecaz').ally,'emperor');assert.equal(seat(g,'emperor').ally,'ecaz'); g=advance(g,s=>s.turn===2&&beforeIxSpiceDraw(s));orderIxSpiceCards(g,['land','land']); g=advance(g,s=>s.phase===3&&s.phaseOpening?.initialize===true); assert.equal(g.sandtrout,true);assert.equal(seat(g,'ecaz').ally,'emperor'); for(const p of g.players)g.deck.push(...p.hand.splice(0)); const index=g.deck.findIndex(card=>card.effect==='karama');assert.ok(index>=0); const [counter]=g.deck.splice(index,1);seat(g,'atreides').hand.push(counter); seat(g,'ecaz').spice=7;seat(g,'emperor').spice=8; g=advance(g,s=>s.phase===3&&!!s.auction&&!s.richeseAuction&&quiet(s)); while(g.auction!.active!=='ecaz')g=act(g,g.auction!.active,{type:'passBid'}); const card=g.auction!.cards[g.auction!.index].id; g=act(g,'emperor',{type:'pledgeAid',amount:1}); g=act(g,'ecaz',{type:'bid',amount:3,allyPayment:1}); 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');assert.equal(g.sandtrout,true); const offer=viewGame(g,'emperor').emperorAuctionGift;assert.ok(offer); assert.deepEqual(cards(g),original);return {g,card,counter:counter.id,original,event:offer.event}; } function settle(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 player=g.players.find(p=>!reaction.passed.includes(p.id));assert.ok(player); g=act(g,player.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('Gift response did not settle'); } for(const cancel of [false,true])void test(`Advanced Emperor auction gift with real waiting Sandtrout ${cancel?'cancels':'allows'} and preserves payment`,()=>{ const f=fixture(),before=structuredClone(f.g),auction=structuredClone(f.g.auction),aid=structuredClone(f.g.aid); let g=act(f.g,'emperor',{type:'emperorGift',amount:2});assert.equal(g.response?.kind,'emperorGift'); if(cancel)g=act(g,'atreides',{type:'card',mode:'cancel',card:f.counter}); g=settle(g);assert.equal(g.sandtrout,true); assert.deepEqual(g.auction,auction);assert.deepEqual(g.aid,aid); assert.equal(g.pendingEmperorAuctionGift?.event,f.event); assert.equal(seat(g,'ecaz').spice,seat(before,'ecaz').spice+(cancel?0:2)); assert.equal(seat(g,'emperor').spice,seat(before,'emperor').spice-(cancel?0:2)); g=act(g,'emperor',{type:'emperorAuctionGiftContinue',event:f.event}); if(g.decision?.kind==='auctionPayment')g=act(g,'ecaz',{type:'decision',karama:false}); g=settle(g);assert.ok(seat(g,'ecaz').hand.some(card=>card.id===f.card)); assert.equal(seat(g,'ecaz').spice,seat(before,'ecaz').spice+(cancel?0:2)-2); assert.equal(seat(g,'emperor').spice,seat(before,'emperor').spice-(cancel?0:2)+2); assert.equal(g.aid.emperor?.amount??0,0);assert.equal(g.sandtrout,true);assert.deepEqual(cards(g),f.original); });