import assert from 'node:assert/strict'; import test from 'node:test'; import {applyAction, createGame, joinGame, newPlayer, viewGame, type Game} from '../game/engine'; import {botActions} from '../game/bots'; import {DIFFICULTIES} from '../game/bot-profiles'; import {actTruthMovementNativeEarly as act, nextTruthMovementNativeEarlyStep} from './fixture-truth-movement-native-early'; const owner='richese', observer='atreides', choam='choam'; const seat=(game:Game,id:string)=>{const player=game.players.find(row=>row.id===id);assert.ok(player);return player;}; const clean=(game:Game)=>!game.response&&!game.decision&&!game.phaseOpening&&!game.truthtrance&&!game.pendingTreacheryDiscard; function next(game:Game) { return nextTruthMovementNativeEarlyStep(game,{position:'first',method:'onceAround',direction:'clockwise',keepUnbid:false}); } function reach(game:Game,until:(state:Game)=>boolean):Game { for(let count=0;count<2400;count++) { if(until(game))return game; assert.notEqual(game.status,'finished','Original Advanced game must reach its next requested window'); const step=next(game);assert.ok(step,`Missing native action at ${game.turn}/${game.phase}/${game.decision?.kind}`); game=act(game,step.actor,step.action); } throw Error('Advanced native reveal programme did not reach its window'); } /** Real public Advanced setup and paid No-Field shipment before turn two. * Only unopened starting/Spice order and conserved board positions are staged; * the auction-specific branch additionally stages its purse and Karama card. */ function source() { let game=createGame('ADVREVEALECON',newPlayer(owner,'Richese','richese'),true,['choam','ix']); joinGame(game,newPlayer(observer,'Atreides','atreides')); joinGame(game,newPlayer(choam,'CHOAM','choam')); for(const player of game.players)game=act(game,player.id,{type:'ready'}); game=act(game,owner,{type:'start',advancedPreview:true}); const ordinary=game.deck.filter(card=>['projectile','poison','shield','snooper'].includes(card.kind)).slice(0,3); assert.equal(ordinary.length,3); for(const [position,card] of ordinary.entries()) { const from=game.deck.findIndex(candidate=>candidate.id===card.id);assert.ok(from>=position); [game.deck[position],game.deck[from]]=[game.deck[from],game.deck[position]]; } game=reach(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={}; } for(const position of [0,1,2,3]) { const from=game.spiceDeck.findIndex((card,index)=>index>=position&&'territory' in card&&!card.discovery); assert.ok(from>=position); [game.spiceDeck[position],game.spiceDeck[from]]=[game.spiceDeck[from],game.spiceDeck[position]]; } game=reach(game,state=>state.turn===1&&state.phase===5&&state.active===owner&&clean(state)); const token=seat(game,owner).noField!.tokens.find(row=>row.value===3);assert.ok(token); const event=seat(game,owner).noFieldEvent;assert.ok(event); game=act(game,owner,{type:'ship',noField:token.id,event,territory:'polar_sink',sector:0,allyPayment:0}); game=reach(game,clean); assert.equal(seat(game,owner).noField?.deployed?.tokenId,token.id); game=act(game,owner,{type:'endMovement'}); game=reach(game,state=>state.turn===2&&state.phase===0&&!!state.phaseOpening&&!state.response&&!state.decision); const revealEvent=seat(game,owner).noFieldEvent;assert.ok(revealEvent); return {game,token:token.id,event:revealEvent}; } let cached:ReturnType|undefined; const fixture=()=>{const f=cached??=source();return {...f,game:structuredClone(f.game)};}; function reveal(game:Game,token:string,event:string) { assert.equal(viewGame(game,owner).richeseNoField?.canReveal,true); const before=structuredClone(game), next=applyAction(game,owner,{type:'revealNoField',token,event}); assert.equal(seat(next,owner).reserves,seat(before,owner).reserves-3); assert.equal(seat(next,owner).forces['polar_sink:0'],(seat(before,owner).forces['polar_sink:0']??0)+3); assert.equal(seat(next,owner).noField?.deployed,null); assert.notEqual(seat(next,owner).noFieldEvent,event); assert.throws(()=>applyAction(next,owner,{type:'revealNoField',token,event})); return next; } void test('Advanced Ix opening preserves existing passes through a real voluntary No-Field reveal',()=>{ const f=fixture();let game=act(f.game,owner,{type:'ready'}); const opening=structuredClone(game.phaseOpening);assert.ok(opening?.passed.includes(owner)); game=reveal(game,f.token,f.event); assert.deepEqual(game.phaseOpening,opening);assert.equal(game.turn,f.game.turn);assert.equal(game.phase,f.game.phase); game=reach(game,state=>!state.phaseOpening); assert.equal(seat(game,owner).forces['polar_sink:0'],3); }); for(const kind of ['choamMarket','richeseDeclaration','richeseCache','richeseUnbid'] as const) void test( `Advanced ${kind} preserves its original economic decision through a No-Field reveal`,()=>{ const f=fixture(); const before=reach(f.game,state=>state.turn===2&&state.decision?.kind===kind&&!state.response&&!state.phaseOpening); const parent=structuredClone(before.decision),market=structuredClone(before.choamMarket??null), bidding=structuredClone(before.richeseBidding??null),auction=structuredClone(before.auction??null); let game=reveal(before,f.token,f.event); assert.deepEqual(game.decision,parent);assert.deepEqual(game.choamMarket??null,market); assert.deepEqual(game.richeseBidding??null,bidding);assert.deepEqual(game.auction??null,auction); const step=next(game);assert.ok(step);assert.equal(step.actor,parent!.player); if(kind==='richeseDeclaration')for(const difficulty of DIFFICULTIES) { const view=viewGame(game,step.actor);view.players.find(player=>player.id===step.actor)!.bot=difficulty; const choice=botActions(view).find(action=>action.type==='decision');assert.ok(choice,difficulty); assert.doesNotThrow(()=>applyAction(game,step.actor,choice),difficulty); } game=act(game,step.actor,step.action); assert.notDeepEqual(game.decision,parent);assert.equal(seat(game,owner).forces['polar_sink:0'],3); }); void test('Advanced normal auction payment retains its paid lot and buyer decision through reveal',()=>{ const f=fixture();let game=reach(f.game,state=>state.turn===2&&state.phase===3&&!!state.auction&&clean(state)); // Ensure space for the real purchased card and a real Karama payment option. for(const player of game.players)game.deck.push(...player.hand.splice(0)); const counter=game.deck.findIndex(card=>card.effect==='karama');assert.ok(counter>=0); seat(game,observer).hand.push(...game.deck.splice(counter,1));seat(game,observer).spice=6; while(game.auction!.active!==observer)game=act(game,game.auction!.active,{type:'passBid'}); const card=game.auction!.cards[game.auction!.index].id; game=act(game,observer,{type:'bid',amount:3}); for(let n=0;game.auction&&!game.response&&!game.decision&&n<15;n++)game=act(game,game.auction.active,{type:'passBid'}); assert.equal(game.decision?.kind,'auctionPayment'); const parent=structuredClone(game.decision),auction=structuredClone(game.auction),aid=structuredClone(game.aid); game=reveal(game,f.token,f.event); assert.deepEqual(game.decision,parent);assert.deepEqual(game.auction,auction);assert.deepEqual(game.aid,aid); game=act(game,observer,{type:'decision',karama:false}); while(game.response) { const actor=game.players.find(player=>!game.response!.passed.includes(player.id));assert.ok(actor); game=act(game,actor.id,{type:'passResponse'}); } assert.ok(seat(game,observer).hand.some(row=>row.id===card)); });