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 {richeseRevealInterruptSupported} from '../game/richese-reveal-interruption'; 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 clean=(g:Game)=>!g.phaseOpening&&!g.response&&!g.decision&&!g.truthtrance&&!g.pendingTreacheryDiscard; function next(g:Game) { if(g.turn===1&&g.phase===5&&g.active==='r'&&clean(g)) { const p=seat(g,'r'); if(!p.shipped) { const token=p.noField!.tokens.find(row=>row.value===3);assert.ok(token&&p.noFieldEvent); return {actor:'r',action:{type:'ship',noField:token.id,event:p.noFieldEvent,territory:'polar_sink',sector:0,allyPayment:0} satisfies Action}; } return {actor:'r',action:{type:'endMovement'} satisfies Action}; } if(g.phase===5&&g.active&&clean(g))return {actor:g.active,action:{type:'endMovement'} satisfies Action}; return nextSemutaNexusTradeNativeStep(g); } function advance(g:Game,until:(state:Game)=>boolean) { for(let i=0;i<2400;i++) { if(until(g))return g; assert.notEqual(g.status,'finished'); const step=next(g);g=act(g,step.actor,step.action); } throw Error('Original native play did not reach the auction'); } function passResponse(g:Game) { for(let i=0;g.response&&i<20;i++) { const p=g.players.find(row=>!g.response!.passed.includes(row.id));assert.ok(p); g=act(g,p.id,{type:'passResponse'}); } assert.equal(g.response,null);return g; } /** Public native Basic setup, actual paid No-Field shipment, natural second-turn * worm and mutual alliance, then original paid lot. Staging moves only the * unopened Spice prefix, initial board counters, hands/deck and wallets. */ function fixture(source:'ordinary'|'onceAround'|'silent') { let g=createGame('REVEALGIFT',newPlayer('e','Emperor','emperor'),false,['choam','ix']); for(const [id,faction] of [['g','guild'],['r','richese'],['a','atreides']] as const) joinGame(g,newPlayer(id,faction,faction)); for(const p of g.players)g=act(g,p.id,{type:'ready'}); g=act(g,'e',{type:'start'}); g=advance(g,s=>s.status==='playing'); 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,['land','land']); g=advance(g,s=>s.turn===2&&beforeIxSpiceDraw(s)); const marker=seat(g,'r').noField?.deployed;assert.ok(marker,'Richese shipped its actual marker on turn one'); orderIxSpiceCards(g,['worm','land','land']); g=advance(g,s=>!!s.nexus&&!s.spiceWindow&&clean(s)); g=act(g,'e',{type:'alliance',target:'g'});g=act(g,'g',{type:'alliance',target:'e'}); g=advance(g,s=>s.phase===3&&s.phaseOpening?.initialize===true); for(const p of g.players)g.deck.push(...p.hand.splice(0)); const counter=g.deck.findIndex(card=>card.effect==='karama');assert.ok(counter>=0); seat(g,'a').hand.push(...g.deck.splice(counter,1)); seat(g,'g').spice=7;seat(g,'e').spice=8; const event=seat(g,'r').noFieldEvent;assert.ok(event); if(source==='ordinary') { g=advance(g,s=>s.phase===3&&!!s.auction&&!s.richeseAuction&&clean(s)); while(g.auction!.active!=='g')g=act(g,g.auction!.active,{type:'passBid'}); const card=g.auction!.cards[g.auction!.index].id; g=act(g,'e',{type:'pledgeAid',amount:1}); g=act(g,'g',{type:'bid',amount:3,allyPayment:1}); for(let n=0;g.auction&&!g.response&&!g.decision&&n<15;n++)g=act(g,g.auction.active,{type:'passBid'}); assert.equal(g.decision?.kind,'auctionPayment');assert.ok(g.pendingEmperorAuctionGift); return {g,card,token:marker.tokenId,event}; } g=advance(g,s=>s.decision?.kind==='richeseCache'); const card=g.richeseCache![0];assert.ok(card); g=act(g,'r',{type:'decision',event:g.richeseBidding!.event,card:card.id,method:source, ...(source==='onceAround'?{direction:'clockwise' as const}:{})}); g=act(g,'e',{type:'pledgeAid',amount:1}); let bid=false; for(let n=0;!g.pendingEmperorAuctionGift&&n<300;n++) { const lot=g.richeseAuction; if(lot&&!lot.outcome&&!g.response&&!g.decision&&lot.method==='onceAround'&&lot.active) { const buyer=lot.active==='g'&&!bid; g=act(g,lot.active,{type:'richeseBid',event:lot.event,amount:buyer?3:null,...(buyer?{allyPayment:1}:{})}); if(buyer)bid=true; } else if(lot&&!lot.outcome&&!g.response&&!g.decision&&lot.method==='silent') { const actor=lot.eligible.find(id=>!Object.hasOwn(lot.sealed,id));assert.ok(actor); const buyer=actor==='g'&&!bid; g=act(g,actor,{type:'richeseBid',event:lot.event,amount:buyer?3:0,...(buyer?{allyPayment:1}:{})}); if(buyer)bid=true; } else {const step=next(g);g=act(g,step.actor,step.action);} } assert.ok(bid);assert.ok(g.pendingEmperorAuctionGift); return {g,card:card.id,token:marker.tokenId,event}; } for(const source of ['ordinary','onceAround','silent'] as const) for(const duringResponse of [false,true]) void test( `Basic Richese reveals during ${source} Emperor auction gift ${duringResponse?'response':'opportunity'}`,()=>{ const f=fixture(source);let g=f.g; const offer=viewGame(g,'e').emperorAuctionGift;assert.ok(offer); if(duringResponse){ g=act(g,'e',{type:'emperorGift',amount:2});assert.equal(g.response?.kind,'emperorGift'); g=act(g,'g',{type:'passResponse'});assert.ok(g.response?.passed.includes('g')); } const before=structuredClone(g), funds=seat(g,'r').reserves, onBoard=seat(g,'r').forces['polar_sink:0']??0; assert.ok(funds>=3);assert.equal(viewGame(g,'r').richeseNoField?.canReveal,true); g=act(g,'r',{type:'revealNoField',token:f.token,event:f.event}); assert.equal(seat(g,'r').reserves,funds-3);assert.equal(seat(g,'r').forces['polar_sink:0'],onBoard+3); assert.deepEqual(g.pendingEmperorAuctionGift,before.pendingEmperorAuctionGift); assert.deepEqual(g.auction,before.auction);assert.deepEqual(g.richeseAuction,before.richeseAuction); assert.deepEqual(g.richeseFunding,before.richeseFunding);assert.deepEqual(g.aid,before.aid); assert.deepEqual(g.response,before.response);assert.deepEqual(g.decision,before.decision); if(duringResponse)g=passResponse(g); for(const difficulty of DIFFICULTIES) { const view=viewGame(g,'e');view.players.find(row=>row.id==='e')!.bot=difficulty; const action=botActions(view).find(row=>row.type==='emperorAuctionGiftContinue');assert.ok(action,difficulty); assert.doesNotThrow(()=>act(g,'e',action),difficulty); } g=act(g,'e',{type:'emperorAuctionGiftContinue',event:offer.event}); if(g.decision?.kind==='auctionPayment')g=act(g,'g',{type:'decision',karama:false}); g=passResponse(g); assert.ok(seat(g,'g').hand.some(card=>card.id===f.card)); assert.equal(g.aid.e?.amount??0,0); }); void test('auction-gift reveal rejects unrelated child windows and mismatched gift responses',()=>{ const f=fixture('ordinary'); const withOpening=structuredClone(f.g);withOpening.phaseOpening={passed:[],initialize:true}; assert.equal(richeseRevealInterruptSupported(withOpening),false); let response=act(f.g,'e',{type:'emperorGift',amount:2}); assert.equal(response.response?.kind,'emperorGift'); assert.equal(richeseRevealInterruptSupported(response),true); response=structuredClone(response);response.response!.emperorAuctionGiftEvent='different-event'; assert.equal(richeseRevealInterruptSupported(response),false); }); void test('a canceled auction gift still resumes after the No-Field reveal',()=>{ const f=fixture('ordinary'); const counter=seat(f.g,'a').hand.find(card=>card.effect==='karama');assert.ok(counter); const original=structuredClone(f.g.auction), owner=seat(f.g,'e').spice, buyer=seat(f.g,'g').spice; let g=act(f.g,'e',{type:'emperorGift',amount:2});assert.equal(g.response?.kind,'emperorGift'); g=act(g,'r',{type:'revealNoField',token:f.token,event:f.event}); g=act(g,'a',{type:'card',mode:'cancel',card:counter.id}); while(g.pendingTreacheryDiscard?.reaction?.stage==='offer') { const pending=g.pendingTreacheryDiscard,reaction=pending.reaction; if(!reaction || reaction.stage!=='offer') throw Error('Discard offer disappeared'); const actor=g.players.find(player=>!reaction.passed.includes(player.id));assert.ok(actor); g=act(g,actor.id,{type:'semutaPass',event:pending.batch.event}); } assert.equal(g.response,null);assert.deepEqual(g.auction,original); assert.equal(seat(g,'e').spice,owner);assert.equal(seat(g,'g').spice,buyer); const offer=viewGame(g,'e').emperorAuctionGift;assert.ok(offer); g=act(g,'e',{type:'emperorAuctionGiftContinue',event:offer.event}); if(g.decision?.kind==='auctionPayment')g=act(g,'g',{type:'decision',karama:false}); g=passResponse(g);assert.ok(seat(g,'g').hand.some(card=>card.id===f.card)); });